Module: HMap::Executable
- Defined in:
- lib/hmap/executable.rb
Defined Under Namespace
Classes: Indenter
Class Method Summary collapse
-
.capture_command(executable, command, capture: :merge, env: {}, **kwargs) ⇒ (String, Process::Status)
Runs the given command, capturing the desired output.
-
.capture_command!(executable, command, **kwargs) ⇒ (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, ornilif 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, env: {}, **kwargs) ⇒ (String, Process::Status)
Runs the given command, capturing the desired output.
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/hmap/executable.rb', line 127 def self.capture_command(executable, command, capture: :merge, env: {}, **kwargs) bin = which!(executable) require 'open3' command = command.map(&:to_s) case capture when :merge then Open3.capture2e(env, [bin, bin], *command, **kwargs) when :both then Open3.capture3(env, [bin, bin], *command, **kwargs) when :out then Open3.capture3(env, [bin, bin], *command, **kwargs).values_at(0, -1) when :err then Open3.capture3(env, [bin, bin], *command, **kwargs).drop(1) when :none then Open3.capture3(env, [bin, bin], *command, **kwargs).last end end |
.capture_command!(executable, command, **kwargs) ⇒ (String, Process::Status)
Runs the given command, capturing the desired output.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/hmap/executable.rb', line 145 def self.capture_command!(executable, command, **kwargs) capture_command(executable, command, **kwargs).tap do |result| result = Array(result) status = result.last unless status.success? output = result[0..-2].join raise Informative, "#{executable} #{command.join(' ')}\n\n#{output}".strip end end end |
.execute_command(executable, command, raise_on_failure = true) ⇒ String
Executes the given command displaying it if in verbose mode.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/hmap/executable.rb', line 37 def self.execute_command(executable, command, raise_on_failure = true) bin = which!(executable) command = command.map(&:to_s) if File.basename(bin) == 'tar.exe' # Tar on Windows needs --force-local command.push('--force-local') end full_command = "#{bin} #{command.join(' ')}" if Pod::Config.instance.verbose? p("$ #{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 p("[!] Failed: #{full_command}".red) end end stdout end |
.popen3(bin, command, stdout, stderr) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/hmap/executable.rb', line 156 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
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/hmap/executable.rb', line 173 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, IOError 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.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/hmap/executable.rb', line 80 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) bin += '.exe' if Gem.win_platform? return bin if File.file?(bin) && File.executable?(bin) 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.
101 102 103 104 105 |
# File 'lib/hmap/executable.rb', line 101 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.
10 11 12 13 14 15 16 17 18 |
# File 'lib/hmap/executable.rb', line 10 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 |