Class: NativePackageInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/native-package-installer/version.rb,
lib/native-package-installer.rb,
lib/native-package-installer/platform.rb,
lib/native-package-installer/platform/suse.rb,
lib/native-package-installer/platform/msys2.rb,
lib/native-package-installer/platform/debian.rb,
lib/native-package-installer/platform/fedora.rb,
lib/native-package-installer/platform/redhat.rb,
lib/native-package-installer/platform/freebsd.rb,
lib/native-package-installer/platform/unknown.rb,
lib/native-package-installer/executable-finder.rb,
lib/native-package-installer/platform/homebrew.rb,
lib/native-package-installer/platform/macports.rb,
lib/native-package-installer/platform/alt-linux.rb,
lib/native-package-installer/platform/pld-linux.rb,
lib/native-package-installer/platform/arch-linux.rb

Overview

Copyright (C) 2017 Ruby-GNOME2 Project Team

This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Defined Under Namespace

Modules: Platform Classes: ExecutableFinder

Constant Summary collapse

VERSION =
"1.0.6"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ NativePackageInstaller

Returns a new instance of NativePackageInstaller.


30
31
32
33
# File 'lib/native-package-installer.rb', line 30

def initialize(spec)
  @spec = spec
  @platform = Platform.detect
end

Class Method Details

.install(spec) ⇒ Object


25
26
27
# File 'lib/native-package-installer.rb', line 25

def install(spec)
  new(spec).install
end

Instance Method Details

#installObject


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/native-package-installer.rb', line 35

def install
  package = @platform.package(@spec)
  return false if package.nil?

  package_name, *options = package
  package_command_line = [package_name, *options].collect do |component|
    Shellwords.escape(component)
  end.join(" ")

  install_command = "#{@platform.install_command} #{package_command_line}"
  if have_priviledge?
    sudo = nil
  else
    sudo = ExecutableFinder.find("sudo")
  end

  installing_message = "installing '#{package_name}' native package... "
  log_message("%s", installing_message)
  failed_to_get_super_user_priviledge = false
  if have_priviledge?
    succeeded = run_command(install_command)
  else
    if sudo
      prompt = "[sudo] password for %u to install <#{package_name}>: "
      sudo_options = "-p #{Shellwords.escape(prompt)}"
      install_command = "#{sudo} #{sudo_options} #{install_command}"
      succeeded = run_command(install_command)
    else
      succeeded = false
      failed_to_get_super_user_priviledge = true
    end
  end

  if failed_to_get_super_user_priviledge
    result_message = "require super user privilege"
  else
    result_message = succeeded ? "succeeded" : "failed"
  end
  show_postpone_message do
    "#{installing_message}#{result_message}\n"
  end
  log_message("#{result_message}\n")

  error_message = nil
  unless succeeded
    if failed_to_get_super_user_priviledge
      error_message = <<-MESSAGE
'#{package_name}' native package is required.
Run the following command as super user to install required native package:
\# #{install_command}
      MESSAGE
    else
      error_message = <<-MESSAGE
Failed to run '#{install_command}'.
      MESSAGE
    end
  end
  if error_message
    log_message("%s", error_message)
    show_message("%s", error_message)
  end

  show_message("--------------------\n\n")

  succeeded
end