Class: ScriptFinder::Finder
- Inherits:
-
Object
- Object
- ScriptFinder::Finder
- Defined in:
- lib/script_finder/script_finder.rb
Instance Method Summary collapse
- #bin_dir ⇒ Object
- #bin_dir=(dir) ⇒ Object
- #command ⇒ Object
- #execute_command ⇒ Object
- #find_bin_dir(starting_dir = '.', last_dir = nil) ⇒ Object
- #find_command_in_dir(dir) ⇒ Object
-
#initialize(command, bin_dir = nil) ⇒ Finder
constructor
A new instance of Finder.
- #unique_prefixes(possibles, prefixes = {}) ⇒ Object
Constructor Details
#initialize(command, bin_dir = nil) ⇒ Finder
Returns a new instance of Finder.
12 13 14 15 |
# File 'lib/script_finder/script_finder.rb', line 12 def initialize(command, bin_dir = nil) @command = command self.bin_dir = bin_dir end |
Instance Method Details
#bin_dir ⇒ Object
21 22 23 |
# File 'lib/script_finder/script_finder.rb', line 21 def bin_dir @bin_dir ||= DEFAULT_BIN_DIR end |
#bin_dir=(dir) ⇒ Object
17 18 19 |
# File 'lib/script_finder/script_finder.rb', line 17 def bin_dir=(dir) @bin_dir = dir end |
#command ⇒ Object
25 26 27 |
# File 'lib/script_finder/script_finder.rb', line 25 def command @command end |
#execute_command ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/script_finder/script_finder.rb', line 29 def execute_command dir = find_bin_dir if dir cmd = find_command_in_dir(dir) if cmd.nil? cmd_not_found elsif cmd.is_a?(Array) too_many_cmds_found(cmd) else command.shift cmd_string = "#{cmd} #{commands_to_command_string(command)}".strip puts "--> calling '#{cmd_string}'" exec cmd_string end else bin_dir_not_found end end |
#find_bin_dir(starting_dir = '.', last_dir = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/script_finder/script_finder.rb', line 49 def find_bin_dir(starting_dir = '.', last_dir = nil) starting_dir = File.(starting_dir) Dir.chdir(starting_dir) do if starting_dir == last_dir nil elsif bin_dir_exists_in_pwd? File.join(starting_dir, bin_dir) else find_bin_dir('..', starting_dir) end end end |
#find_command_in_dir(dir) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/script_finder/script_finder.rb', line 63 def find_command_in_dir(dir) cmd = command.first Dir.chdir(dir) do if cmd and File.executable?(cmd) possibles = [cmd] else possibles = Dir.glob("#{cmd}*").select {|f| File.executable?(f)} end if possibles.size == 0 nil elsif possibles.size == 1 File.(possibles.first) else possibles end end end |
#unique_prefixes(possibles, prefixes = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/script_finder/script_finder.rb', line 82 def unique_prefixes(possibles, prefixes = {}) if prefixes.values.uniq.size < possibles.size possibles.each do |cmd| prefixes[cmd] ||= "" if prefix_is_not_unique?(cmd, prefixes[cmd], possibles) prefixes[cmd] += cmd[prefixes[cmd].size,1] end end unique_prefixes(possibles, prefixes) else prefixes end end |