Module: Executioner::ClassMethods

Defined in:
lib/executioner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_executable(executable, advance_from = nil) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/executioner.rb', line 88

def find_executable(executable, advance_from = nil)
  search_paths = Executioner::SEARCH_PATHS
  search_paths = search_paths[(search_paths.index(advance_from) + 1)..-1] if advance_from
  
  if executable_in_path = search_paths.find { |path| File.exist? File.join(path, executable) }
    File.join executable_in_path, executable
  end
end

Instance Method Details

#executable(executable, options = {}) ⇒ Object



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
# File 'lib/executioner.rb', line 58

def executable(executable, options={})
  options[:switch_stdout_and_stderr] ||= false
  options[:use_queue]                ||= false
  
  executable = executable.to_s if executable.is_a? Symbol
  use_queue = options.delete(:use_queue)
  
  if selection_proc = options.delete(:select_if)
    advance_from = nil
    while executable_path = find_executable(executable, advance_from)
      break if selection_proc.call(executable_path)
      advance_from = File.dirname(executable_path)
    end
  else
    executable_path = options[:path] || find_executable(executable)
  end
  
  if executable_path
    if use_queue
      body = "queue(\"#{executable_path} \#{args}\")"
    else
      body = "execute(\"#{executable_path} \#{args}\", #{options.inspect}.merge(options))"
    end
  else
    body = "raise Executioner::ExecutableNotFoundError, \"Unable to find the executable '#{executable}' in: #{Executioner::SEARCH_PATHS.join(', ')}\""
  end
  
  class_eval "def #{executable.gsub(/-/, '_')}(args, options = {}); #{body}; end", __FILE__, __LINE__
end