Class: Cuken::Api::Aruba::Process

Inherits:
Object
  • Object
show all
Includes:
Shellwords
Defined in:
lib/cuken/api/aruba/process.rb

Instance Method Summary collapse

Constructor Details

#initialize(cmd, exit_timeout, io_wait) ⇒ Process

Returns a new instance of Process.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cuken/api/aruba/process.rb', line 15

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

Instance Method Details

#outputObject



38
39
40
# File 'lib/cuken/api/aruba/process.rb', line 38

def output
  stdout + stderr
end

#remove_ansi_codes(str) ⇒ Object



11
12
13
# File 'lib/cuken/api/aruba/process.rb', line 11

def remove_ansi_codes(str)
  str.gsub(/\e\[(\d+)m/, '')
end

#run! {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



27
28
29
30
# File 'lib/cuken/api/aruba/process.rb', line 27

def run!(&block)
  @process.start
  yield self if block_given?
end

#stderrObject



49
50
51
52
53
54
# File 'lib/cuken/api/aruba/process.rb', line 49

def stderr
  wait_for_io do
    @err.rewind
    remove_ansi_codes(@err.read)
  end
end

#stdinObject



32
33
34
35
36
# File 'lib/cuken/api/aruba/process.rb', line 32

def stdin
  wait_for_io do
    @process.io.stdin
  end
end

#stdoutObject



42
43
44
45
46
47
# File 'lib/cuken/api/aruba/process.rb', line 42

def stdout
  wait_for_io do
    @out.rewind
    remove_ansi_codes(@out.read)
  end
end

#stopObject



56
57
58
59
60
61
62
# File 'lib/cuken/api/aruba/process.rb', line 56

def stop
  if @process
    stdout && stderr # flush output
    @process.poll_for_exit(@exit_timeout)
    @process.exit_code
  end
end