Module: Pindo::Executable
- Included in:
- AndroidResHelper, Command::Utils::Icon, GitHandler
- Defined in:
- lib/pindo/base/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.
- .popen3(bin, command, stdout, stderr) ⇒ Object
- .reader(input, output) ⇒ Object
-
.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.
-
.which!(program) ⇒ String
Returns the absolute path to the binary with the given name on the current ‘PATH`, or raises 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.
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/pindo/base/executable.rb', line 134 def self.capture_command(executable, command, capture: :merge) bin = which!(executable) 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.capture3(bin, *command).values_at(0, -1) when :err then Open3.capture3(bin, *command).drop(1) when :none then Open3.capture3(bin, *command).last end end |
.execute_command(executable, command, raise_on_failure = true) ⇒ String
Executes the given command displaying it if in verbose mode.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/pindo/base/executable.rb', line 48 def self.execute_command(executable, command, raise_on_failure = true) bin = which!(executable) command = command.map(&:to_s) full_command = "#{bin} #{command.join(' ')}" # if Config.instance.verbose? # puts "$ #{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 raise Informative, "#{full_command}\n\n#{output}" else puts "[!] Failed: #{full_command}" end end output end |
.popen3(bin, command, stdout, stderr) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/pindo/base/executable.rb', line 150 def self.popen3(bin, command, stdout, stderr) require 'open3' Open3.popen3(bin, *command) do |i, o, e, t| reader(o, stdout) reader(e, stderr) i.close status = t.value o.flush e.flush sleep(0.01) status end end |
.reader(input, output) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/pindo/base/executable.rb', line 167 def self.reader(input, output) Thread.new do buf = '' begin loop do buf << input.readpartial(4096) loop do string, separator, buf = buf.partition(/[\r\n]/) if separator.empty? buf = string break end output << (string << separator) end end rescue EOFError output << (buf << $/) unless buf.empty? end end 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.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/pindo/base/executable.rb', line 89 def self.which(program) program = program.to_s paths = ENV.fetch('PATH') { '' }.split(File::PATH_SEPARATOR) paths.unshift('./') paths.uniq! paths.each do |path| bin = File.(program, path) if File.file?(bin) && File.executable?(bin) return bin end end nil end |
.which!(program) ⇒ String
Returns the absolute path to the binary with the given name on the current ‘PATH`, or raises if none is found.
111 112 113 114 115 |
# File 'lib/pindo/base/executable.rb', line 111 def self.which!(program) which(program).tap do |bin| raise Informative, "Unable to locate the executable `#{program}`" unless bin end end |
Instance Method Details
#executable(name) ⇒ void
This method returns an undefined value.
Creates the methods for the executable with the given name.
21 22 23 24 25 26 27 28 29 |
# File 'lib/pindo/base/executable.rb', line 21 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 |