class Aruba::Process

Public Class Methods

new(cmd, exit_timeout, io_wait) click to toggle source
# File lib/aruba/process.rb, line 9
def initialize(cmd, exit_timeout, io_wait)
  @exit_timeout = exit_timeout
  @io_wait = io_wait

  @out = Tempfile.new("aruba-out")
  @err = Tempfile.new("aruba-err")
  @process = ChildProcess.build(*shellwords(cmd))
  @process.io.stdout = @out
  @process.io.stderr = @err
  @process.duplex = true
end

Public Instance Methods

output(keep_ansi) click to toggle source
# File lib/aruba/process.rb, line 33
def output(keep_ansi)
  stdout(keep_ansi) + stderr(keep_ansi)
end
run!() { |self| ... } click to toggle source
# File lib/aruba/process.rb, line 21
def run!(&block)
  @process.start
  yield self if block_given?
end
stderr(keep_ansi) click to toggle source
# File lib/aruba/process.rb, line 44
def stderr(keep_ansi)
  wait_for_io do
    @err.rewind
    filter_ansi(@err.read, keep_ansi)
  end
end
stdin() click to toggle source
# File lib/aruba/process.rb, line 26
def stdin
  wait_for_io do
    @process.io.stdin.sync = true
    @process.io.stdin
  end
end
stdout(keep_ansi) click to toggle source
# File lib/aruba/process.rb, line 37
def stdout(keep_ansi)
  wait_for_io do
    @out.rewind
    filter_ansi(@out.read, keep_ansi)
  end
end
stop(reader, keep_ansi) click to toggle source
# File lib/aruba/process.rb, line 51
def stop(reader, keep_ansi)
  return unless @process
  unless @process.exited?
    reader.stdout stdout(keep_ansi)
    reader.stderr stderr(keep_ansi)
    @process.poll_for_exit(@exit_timeout)
  end
  @process.exit_code
end
terminate(keep_ansi) click to toggle source
# File lib/aruba/process.rb, line 61
def terminate(keep_ansi)
  if @process
    stdout(keep_ansi) && stderr(keep_ansi) # flush output
    @process.stop
    stdout(keep_ansi) && stderr(keep_ansi) # flush output
  end
end

Private Instance Methods

filter_ansi(string, keep_ansi) click to toggle source
# File lib/aruba/process.rb, line 76
def filter_ansi(string, keep_ansi)
  keep_ansi ? string : string.gsub(/\e\[\d+(?>(;\d+)*)m/, '')
end
wait_for_io() { || ... } click to toggle source
# File lib/aruba/process.rb, line 71
def wait_for_io(&block)
  sleep @io_wait if @process.alive?
  yield
end