Class: Babushka::PathChecker

Inherits:
Object
  • Object
show all
Extended by:
ShellHelpers
Defined in:
lib/babushka/path_checker.rb

Class Method Summary collapse

Methods included from ShellHelpers

cmd_dir, current_username, log_shell, login_shell, raw_shell, shell, shell!, shell?, shell_cmd, sudo, which

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Class Method Details

.cmd_location_str_for(cmds) ⇒ Object



68
69
70
# File 'lib/babushka/path_checker.rb', line 68

def self.cmd_location_str_for cmds
  "#{cmds.map {|i| "'#{i.name}'" }.to_list(:conj => '&')} run#{'s' if cmds.length == 1} from #{cmd_dir(cmds.first.name)}"
end

.cmds_in_path?(commands) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/babushka/path_checker.rb', line 15

def self.cmds_in_path? commands
  dir_hash = [*commands].group_by {|cmd| cmd_dir(cmd.name) }

  if dir_hash.keys.compact.length > 1
    log_error "These commands run from more than one place."
    log_error dir_hash.values.map {|cmds|
        cmd_location_str_for cmds
      }.to_list(:oxford => true, :conj => 'but').end_with('.')
    unmeetable! unless Prompt.confirm("Multiple installations might indicate a problem. Attempt to meet the dep anyway?", :default => 'n')
  else
    dir_hash[nil].blank?.tap {|result|
      if result
        cmds = dir_hash.values.first
        log cmd_location_str_for(cmds).end_with('.') unless cmds.blank?
      else
        log "#{dir_hash[nil].map {|i| "'#{i}'" }.to_list} #{dir_hash[nil].length == 1 ? 'is' : 'are'} missing."
      end
    }
  end
end

.in_path?(provided_list) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
# File 'lib/babushka/path_checker.rb', line 5

def self.in_path? provided_list
  commands = [provided_list].flatten(1).versions

  cmds_in_path?(commands) and matching_versions?(commands) {|cmd|
    shell("#{cmd.name} --version 2>&1")
  }
end

.match_potential_versions(str, example = '') ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/babushka/path_checker.rb', line 56

def self.match_potential_versions str, example = ''
  (str || '').split(/[^\w\.\-]+/).map {|piece|
    begin
      piece.to_version
    rescue VersionStrError
      nil
    end
  }.compact.reject {|piece|
    piece.to_s['.'].nil? if example.to_s['.']
  }
end

.matching_versions?(commands) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/babushka/path_checker.rb', line 36

def self.matching_versions? commands
  versions = commands.select {|cmd|
    !cmd.version.nil?
  }.inject({}) {|hsh,cmd|
    potential_versions = match_potential_versions(yield(cmd), cmd.version)
    if potential_versions.empty?
      # No potential versions to check against.
    else
      hsh[cmd] = potential_versions.detect {|piece| cmd.matches?(piece) }
      if hsh[cmd] == cmd.version
        log_ok "#{cmd.name} is #{cmd.version}."
      else
        log "#{cmd.name} is #{hsh[cmd] || potential_versions.first}, which is#{"n't" unless hsh[cmd]} #{cmd.version}.", :as => (:ok if hsh[cmd])
      end
    end
    hsh
  }
  versions.values.all?
end