Class: Babushka::PkgHelper

Inherits:
Object show all
Extended by:
LogHelpers, ShellHelpers, VersionOf::Helpers
Defined in:
lib/babushka/pkg_helper.rb

Direct Known Subclasses

AptHelper, BaseHelper, BinPkgSrcHelper, BinPortsHelper, BrewHelper, GemHelper, MacportsHelper, NpmHelper, PacmanHelper, PipHelper, SrcHelper, YumHelper

Class Method Summary (collapse)

Methods included from VersionOf::Helpers

VersionOf

Methods included from ShellHelpers

cmd_dir, failable_shell, log_shell, login_shell, raw_shell, shell, shell!, shell?, sudo, which

Methods included from LogHelpers

debug, log, log_block, log_error, log_ok, log_verbose, log_warn

Class Method Details

+ (Object) _install!(pkgs, opts)



30
31
32
# File 'lib/babushka/pkg_helper.rb', line 30

def _install! pkgs, opts
  log_shell "Installing #{pkgs.to_list} via #{manager_key}", "#{pkg_cmd} -y install #{pkgs.join(' ')} #{opts}", :sudo => should_sudo?
end

+ (Object) all_manager_keys



10
11
12
# File 'lib/babushka/pkg_helper.rb', line 10

def all_manager_keys
  [:apt, :brew, :macports, :yum, :binpkgsrc, :binports]
end

+ (Object) bin_path



36
37
38
# File 'lib/babushka/pkg_helper.rb', line 36

def bin_path
  prefix / 'bin'
end

+ (Boolean) cmd_in_path?(cmd_name)

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/babushka/pkg_helper.rb', line 42

def cmd_in_path? cmd_name
  if (_cmd_dir = cmd_dir(cmd_name)).nil?
    log_error "The '#{cmd_name}' command is not available. You probably need to add #{bin_path} to your PATH."
  else
    _cmd_dir.starts_with?(prefix)
  end
end

+ (Object) cmdline_spec_for(pkg)



76
77
78
# File 'lib/babushka/pkg_helper.rb', line 76

def cmdline_spec_for pkg
  "#{pkg.name}#{" --version '#{pkg.version}'" unless pkg.version.nil?}"
end

+ (Boolean) has?(pkg, opts = {})

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
# File 'lib/babushka/pkg_helper.rb', line 18

def has? pkg, opts = {}
  pkg = VersionOf(pkg)
  _has?(pkg).tap {|matching_version|
    matching_pkg = VersionOf(pkg.name, (matching_version if matching_version.is_a?(VersionStr)))
    unless opts[:log] == false
      log "system #{matching_version ? "has" : "doesn't have"} #{matching_pkg} #{pkg_type}", :as => (:ok if matching_version)
    end
  }
end

+ (Object) install!(pkgs, opts = nil)



27
28
29
# File 'lib/babushka/pkg_helper.rb', line 27

def install! pkgs, opts = nil
  _install! [*pkgs].map {|pkg| VersionOf(pkg) }, opts
end

+ (Object) manager_dep



14
15
16
# File 'lib/babushka/pkg_helper.rb', line 14

def manager_dep
  manager_key.to_s
end

+ (Object) pkg_binary



8
# File 'lib/babushka/pkg_helper.rb', line 8

def pkg_binary; pkg_cmd end

+ (Object) pkg_update_command



72
73
74
# File 'lib/babushka/pkg_helper.rb', line 72

def pkg_update_command
  "#{pkg_cmd} update"
end

+ (Object) pkg_update_timeout



69
70
71
# File 'lib/babushka/pkg_helper.rb', line 69

def pkg_update_timeout
  nil # not required by default
end

+ (Object) prefix



33
34
35
# File 'lib/babushka/pkg_helper.rb', line 33

def prefix
  cmd_dir(pkg_binary).p.dir
end

+ (Boolean) present?

Returns:

  • (Boolean)


39
40
41
# File 'lib/babushka/pkg_helper.rb', line 39

def present?
  which pkg_binary
end

+ (Boolean) should_sudo?

Returns:

  • (Boolean)


49
50
51
# File 'lib/babushka/pkg_helper.rb', line 49

def should_sudo?
  !File.writable?(bin_path)
end

+ (Object) update_pkg_lists(message = "Updating #{manager_dep.capitalize} package lists")



66
67
68
# File 'lib/babushka/pkg_helper.rb', line 66

def update_pkg_lists message = "Updating #{manager_dep.capitalize} package lists"
  log_shell message, pkg_update_command, :sudo => should_sudo?
end

+ (Object) update_pkg_lists_if_required



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/babushka/pkg_helper.rb', line 53

def update_pkg_lists_if_required
  if pkg_update_timeout.nil?
    true # not required
  else
    list_age = Time.now - pkg_list_dir.mtime
    if list_age > pkg_update_timeout
      update_pkg_lists "#{manager_dep.capitalize} package lists are #{list_age.round.xsecs} old. Updating"
    else
      debug "#{manager_dep.capitalize} package lists are #{list_age.round.xsecs} old (up to date)."
      true # up to date
    end
  end
end