Class: Wip::CommandRunner
- Inherits:
-
Object
- Object
- Wip::CommandRunner
- 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
-
#initialize(stdin: $stdin, stdout: $stdout, stderr: $stderr, interpreter: ErrorInterpreter.new, debug: !ENV['WIP_DEBUG'].to_s.empty?)) ⇒ CommandRunner
constructor
A new instance of CommandRunner.
- #run(command, env: {}, interactive: false, chdir: nil) ⇒ Object
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.
10 11 12 13 14 15 16 17 |
# File 'lib/wip/command_runner.rb', line 10 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, chdir: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/wip/command_runner.rb', line 19 def run(command, env: {}, interactive: false, chdir: nil) @stderr.puts "+ #{CommandDisplay.for_debug(command)}" if @debug return run_interactive(command, env, chdir) if interactive opts = chdir ? { chdir: chdir } : {} captured = +'' status = nil Open3.popen3(env, *command, opts) do |input, output, error, wait| input.close threads = [pump(output, @stdout, captured), pump(error, @stderr, captured)] threads.each(&:join) status = wait.value end report_hint(captured) unless status.success? exitstatus(status) rescue Errno::ENOENT => e @stderr.puts e. 127 rescue Interrupt @stderr.puts "\nwip: interrupted" 130 end |