Sha256: 769a6084aaefd4a5e14383858a38b6805d2abde53a00909e3670dfdb2cc085d5
Contents?: true
Size: 1.59 KB
Versions: 1
Compression:
Stored size: 1.59 KB
Contents
require 'childprocess' require 'tempfile' require 'shellwords' module Aruba class Process include Shellwords 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 def run!(&block) @process.start yield self if block_given? end def stdin wait_for_io do @process.io.stdin.sync = true @process.io.stdin end end def output(keep_ansi) stdout(keep_ansi) + stderr(keep_ansi) end def stdout(keep_ansi) wait_for_io do @out.rewind filter_ansi(@out.read, keep_ansi) end end def stderr(keep_ansi) wait_for_io do @err.rewind filter_ansi(@err.read, keep_ansi) end end def stop(keep_ansi) if @process stdout(keep_ansi) && stderr(keep_ansi) # flush output @process.poll_for_exit(@exit_timeout) @process.exit_code end end 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 def wait_for_io(&block) sleep @io_wait if @process.alive? yield end def filter_ansi(string, keep_ansi) keep_ansi ? string : string.gsub(/\e\[\d+(?>(;\d+)*)m/, '') end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
aruba-0.4.10 | lib/aruba/process.rb |