Class: Sprout::ProcessRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/sprout/process_runner.rb

Overview

The ProcessRunner is a cross-platform wrapper for executing external executable processes.

As it turns out, Ruby handle differences very well, and other process libraries (like win32-open3 and open4.popen4), do make the experience more consistent on a given platform, but they don't hide the differences introduced by the continuing beligerence of Windows or *nix (depending on which side of the fence you're on).

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ProcessRunner) initialize

A new instance of ProcessRunner



30
31
32
33
# File 'lib/sprout/process_runner.rb', line 30

def initialize
  super
  @ruby_version = RUBY_VERSION
end

Instance Attribute Details

- (Object) e (readonly)

Error IO (readable)



28
29
30
# File 'lib/sprout/process_runner.rb', line 28

def e
  @e
end

- (Object) pid (readonly)

Returns the value of attribute pid



15
16
17
# File 'lib/sprout/process_runner.rb', line 15

def pid
  @pid
end

- (Object) r (readonly)

Read IO (readable)



20
21
22
# File 'lib/sprout/process_runner.rb', line 20

def r
  @r
end

- (Object) ruby_version (readonly)

Returns the value of attribute ruby_version



16
17
18
# File 'lib/sprout/process_runner.rb', line 16

def ruby_version
  @ruby_version
end

- (Object) w (readonly)

Write IO (writeable)



24
25
26
# File 'lib/sprout/process_runner.rb', line 24

def w
  @w
end

Instance Method Details

- (Boolean) alive?

Returns:

  • (Boolean)


56
57
58
# File 'lib/sprout/process_runner.rb', line 56

def alive?
  @alive = update_status
end

- (Object) close



64
65
66
# File 'lib/sprout/process_runner.rb', line 64

def close
  update_status
end

- (Object) close_write



102
103
104
# File 'lib/sprout/process_runner.rb', line 102

def close_write
  @w.close_write
end

- (Object) execute_open4(*command)

Execute the provided command using the open4.popen4 library. This is generally only used by Cygwin and *nix variants (including OS X).



38
39
40
41
42
43
44
45
# File 'lib/sprout/process_runner.rb', line 38

def execute_open4 *command
  execute_with_block *command do
    # Not sure about the join - with the 1.0 push, we're
    # sending in 2 args - the exe path, and options as a string.
    # This was new and was causing problems...
    @pid, @w, @r, @e = open4_popen4_block *command.join(' ')
  end
end

- (Object) execute_win32(*command)

Execute the provided command using the win32-open3 library. This is generally used even by 64-bit Windows installations.



50
51
52
53
54
# File 'lib/sprout/process_runner.rb', line 50

def execute_win32(*command)
  execute_with_block *command do
    @pid, @w, @r, @e = io_popen_block *command.join(' ')
  end
end

- (Object) flush



86
87
88
# File 'lib/sprout/process_runner.rb', line 86

def flush
  @w.flush
end

- (Object) getc



90
91
92
# File 'lib/sprout/process_runner.rb', line 90

def getc
  @r.getc
end

- (Object) kill



60
61
62
# File 'lib/sprout/process_runner.rb', line 60

def kill
  Process.kill(9, pid)
end


94
95
96
# File 'lib/sprout/process_runner.rb', line 94

def print(msg)
  @w.print msg
end

- (Object) puts(msg)



98
99
100
# File 'lib/sprout/process_runner.rb', line 98

def puts(msg)
  @w.puts(msg)
end

- (Object) read



106
107
108
# File 'lib/sprout/process_runner.rb', line 106

def read
  return @r.read
end

- (Object) read_err



110
111
112
# File 'lib/sprout/process_runner.rb', line 110

def read_err
  return @e.read
end

- (Object) readlines



82
83
84
# File 'lib/sprout/process_runner.rb', line 82

def readlines
  @r.readlines
end

- (Object) readpartial(count)



78
79
80
# File 'lib/sprout/process_runner.rb', line 78

def readpartial(count)
  @r.readpartial(count)
end

- (Object) update_status



68
69
70
71
72
73
74
75
76
# File 'lib/sprout/process_runner.rb', line 68

def update_status
  pid_int = Integer("#{ @pid }")
  begin
    Process::kill 0, pid_int
    true
  rescue Errno::ESRCH
    false
  end
end