Module: Fourflusher::Executable
- Included in:
- SimControl
- Defined in:
- lib/fourflusher/executable.rb
Overview
Module which provides support for running executables.
In a class it can be used as:
extend Executable
executable :git
This will create two methods ‘git` and `git!` both accept a command but the later will raise on non successful executions. The methods return the output of the command.
Defined Under Namespace
Classes: Indenter
Class Method Summary collapse
-
.capture_command(executable, command, capture: :merge) ⇒ (String, Process::Status)
Runs the given command, capturing the desired output.
-
.execute_command(executable, command, raise_on_failure = true) ⇒ String
Executes the given command displaying it if in verbose mode.
-
.which(program) ⇒ String, Nil
Returns the absolute path to the binary with the given name on the current ‘PATH`, or `nil` if none is found.
Instance Method Summary collapse
-
#executable(name) ⇒ void
Creates the methods for the executable with the given name.
Class Method Details
.capture_command(executable, command, capture: :merge) ⇒ (String, Process::Status)
Runs the given command, capturing the desired output.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/fourflusher/executable.rb', line 145 def self.capture_command(executable, command, capture: :merge) bin = which(executable) fail Fourflusher::Informative, "Unable to locate the executable `#{executable}`" unless bin require 'open3' command = command.map(&:to_s) case capture when :merge then Open3.capture2e(bin, *command) when :both then Open3.capture3(bin, *command) when :out then Open3.capture2(bin, *command) when :err then Open3.capture3(bin, *command).drop(1) when :none then Open3.capture2(bin, *command).last end end |
.execute_command(executable, command, raise_on_failure = true) ⇒ String
Executes the given command displaying it if in verbose mode.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/fourflusher/executable.rb', line 79 def self.execute_command(executable, command, raise_on_failure = true) bin = which(executable) fail Fourflusher::Informative, "Unable to locate the executable `#{executable}`" unless bin command = command.map(&:to_s) full_command = "#{bin} #{command.join(' ')}" if Config.instance.verbose? UI.("$ #{full_command}") stdout = Indenter.new(STDOUT) stderr = Indenter.new(STDERR) else stdout = Indenter.new stderr = Indenter.new end status = popen3(bin, command, stdout, stderr) stdout = stdout.join stderr = stderr.join output = stdout + stderr unless status.success? if raise_on_failure fail Fourflusher::Informative, "#{full_command}\n\n#{output}" else UI.("[!] Failed: #{full_command}".red) end end output end |
.which(program) ⇒ String, Nil
Returns the absolute path to the binary with the given name on the current ‘PATH`, or `nil` if none is found.
119 120 121 122 123 124 125 126 |
# File 'lib/fourflusher/executable.rb', line 119 def self.which(program) program = program.to_s ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| bin = File.(program, path) return bin if File.file?(bin) && File.executable?(bin) end nil end |
Instance Method Details
#executable(name) ⇒ void
This method returns an undefined value.
Creates the methods for the executable with the given name.
52 53 54 55 56 57 58 59 60 |
# File 'lib/fourflusher/executable.rb', line 52 def executable(name) define_method(name) do |*command| Executable.execute_command(name, Array(command).flatten, false) end define_method(name.to_s + '!') do |*command| Executable.execute_command(name, Array(command).flatten, true) end end |