Class: Wip::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/wip/command_runner.rb

Overview

Executes a built command, pumping its I/O and returning the exit status.

Instance Method Summary collapse

Constructor Details

#initialize(stdin: $stdin, stdout: $stdout, stderr: $stderr, interpreter: ErrorInterpreter.new, debug: !ENV['WIP_DEBUG'].to_s.empty?)) ⇒ CommandRunner

Returns a new instance of CommandRunner.



8
9
10
11
12
13
14
15
# File 'lib/wip/command_runner.rb', line 8

def initialize(stdin: $stdin, stdout: $stdout, stderr: $stderr, interpreter: ErrorInterpreter.new,
               debug: !ENV['WIP_DEBUG'].to_s.empty?)
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
  @interpreter = interpreter
  @debug = debug
end

Instance Method Details

#run(command, env: {}, interactive: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wip/command_runner.rb', line 17

def run(command, env: {}, interactive: false)
  @stderr.puts "+ #{CommandDisplay.for_debug(command)}" if @debug
  return run_attached(command, env) if interactive

  captured = +''
  status = nil
  Open3.popen3(env, *command) do |input, output, error, wait|
    input.close
    threads = [pump(output, @stdout, captured), pump(error, @stderr, captured)]
    threads.each(&:join)
    status = wait.value
  end
  hint = @interpreter.interpret(captured)
  @stderr.puts("\n#{hint}") if !status.success? && hint
  status.exitstatus
rescue Errno::ENOENT => e
  @stderr.puts e.message
  127
end