Sha256: d4741b6585c9493b3893c70168f8dae3c1ee99e4445cb88c08896d4d8e5e4344

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 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 read_stdout(keep_ansi)
      wait_for_io do
        @process.io.stdout.flush
        content = filter_ansi(open(@out.path).read, keep_ansi)
      end
    end

    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

    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.5.0 lib/aruba/process.rb