Module: CommandKit::PackageManager

Includes:
Env::Path, OS, OS::Linux, Sudo
Defined in:
lib/command_kit/package_manager.rb

Overview

Allows installing packages using the system's package manager.

Supports the following package managers:

  • Linux
    • Debian / Ubuntu
    • apt
    • RedHat / Fedora
    • dnf
    • yum
    • Arch
    • pacman
    • SUSE / OpenSUSE
    • zypper
  • macOS
    • brew
    • port
  • FreeBSD
    • pkg
  • OpenBSD
    • pkg_add

Examples

unless install_packages("nmap")
  print_error "failed to install nmap"
  exit -1
end

Installing multiple packages

install_packages apt: ["libxml2-dev", ...],
                 dnf: ["libxml2-devel", ...],
                 brew: ["libxml2", ...],
                 ...

Since:

  • 0.2.0

Instance Attribute Summary collapse

Attributes included from OS

#os

Attributes included from Env::Path

#path_dirs

Attributes included from Env

#env

Attributes included from OS::Linux

#linux_distro

Instance Method Summary collapse

Methods included from Sudo

#sudo

Methods included from OS

#bsd?, #freebsd?, #linux?, #macos?, #netbsd?, #openbsd?, #unix?, #windows?

Methods included from OS::ModuleMethods

#included

Methods included from Env::Path

#command_installed?, #find_command

Methods included from OS::Linux

#arch_linux?, #debian_linux?, #fedora_linux?, #redhat_linux?, #suse_linux?

Methods included from OS::Linux::ModuleMethods

#included

Instance Attribute Details

#package_manager:apt, ... (readonly)

The detected package manager.

Since:

  • 0.2.0



57
58
59
# File 'lib/command_kit/package_manager.rb', line 57

def package_manager
  @package_manager
end

Instance Method Details

#initialize(package_manager: nil, **kwargs) ⇒ Object

Initializes the command and determines which open command to use.

Since:

  • 0.2.0



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
# File 'lib/command_kit/package_manager.rb', line 66

def initialize(package_manager: nil, **kwargs)
  super(**kwargs)

  @package_manager = package_manager || begin
    if macos?
      if    command_installed?('brew') then :brew
      elsif command_installed?('port') then :port
      end
    elsif linux?
      if redhat_linux?
        if    command_installed?('dnf') then :dnf
        elsif command_installed?('yum') then :yum
        end
      elsif debian_linux?
        :apt if command_installed?('apt')
      elsif suse_linux?
        :zypper if command_installed?('zypper')
      elsif arch_linux?
        :pacman if command_installed?('pacman')
      end
    elsif freebsd?
      :pkg if command_installed?('pkg')
    elsif openbsd?
      :pkg_add if command_installed?('pkg_add')
    end
  end
end

#install_packages(*packages, yes: false, apt: nil, brew: nil, dnf: nil, pacman: nil, pkg: nil, pkg_add: nil, port: nil, yum: nil, zypper: nil) ⇒ Boolean?

Installs the packages using the system's package manager.

Examples:

Install a package

install_packages "nmap", ...

Install a list of packages per package-manager

install_packages apt: ["libxml2-dev", ...],
                 dnf: ["libxml2-devel", ...],
                 brew: ["libxml2", ...],
                 ...

Since:

  • 0.2.0



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/command_kit/package_manager.rb', line 144

def install_packages(*packages, yes: false,
                                apt: nil,
                                brew: nil,
                                dnf: nil,
                                pacman: nil,
                                pkg: nil,
                                pkg_add: nil,
                                port: nil,
                                yum: nil,
                                zypper: nil)
  specific_package_names = case @package_manager
                           when :apt     then apt
                           when :brew    then brew
                           when :dnf     then dnf
                           when :pacman  then pacman
                           when :pkg     then pkg
                           when :pkg_add then pkg_add
                           when :port    then port
                           when :yum     then yum
                           when :zypper  then zypper
                           end
  packages += Array(specific_package_names)

  case @package_manager
  when :apt
    args = []
    args << '-y' if yes

    sudo('apt','install',*args,*packages)
  when :brew
    system('brew','install',*packages)
  when :dnf, :yum
    args = []
    args << '-y' if yes

    sudo(@package_manager.to_s,'install',*args,*packages)
  when :pacman
    missing_packages = `pacman -T #{Shellwords.shelljoin(packages)}`.split

    if missing_packages.empty?
      return true
    end

    sudo('pacman','-S',*missing_packages)
  when :pkg
    args = []
    args << '-y' if yes

    sudo('pkg','install',*args,*packages)
  when :pkg_add
    sudo('pkg_add',*packages)
  when :port
    sudo('port','install',*packages)
  when :zypper
    sudo('zypper','-n','in','-l',*packages)
  end
end