Sha256: 80136559cf1b48286254bc19fc7761b1d119bdbb3f3a31276d2976430fdc9a46

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

module Sprout #:nodoc:
  class ProcessRunnerError < StandardError # :nodoc:
  end
  
  class ProcessRunner #:nodoc:
    attr_reader :pid,
                :r,
                :w,
                :e
    
    def initialize(*command)
      @command = command
      begin
        @alive = true
        usr = User.new()
        if(usr.is_a?(WinUser) && !usr.is_a?(CygwinUser))
          require_gem 'win32/open3', '0.2.5'
          Open3.popen3(*@command) do |w, r, e, pid|
          	@w = w
          	@r = r
          	@e = e
          	@pid = pid
          end
        else
          require 'open4'
          @pid, @w, @r, @e = open4.popen4(*@command)
        end
      rescue Errno::ENOENT => e
        @alive = false
        part = command[0].split(' ').shift
        raise ProcessRunnerError.new("The expected executable was not found for command [#{part}], please check your system path and/or sprout definition")
      end
    end
    
    def alive?
      @alive = update_status
    end
    
    def update_status
      pid_int = Integer("#{ @pid }")
      begin
        Process::kill 0, pid_int
        true
      rescue Errno::ESRCH
        false
      end
    end
    
    def readpartial(count)
      @r.readpartial(count)
    end
    
    def readlines
      @r.readlines
    end
    
    def flush
      @w.flush
    end
    
    def getc
      @r.getc
    end
    
    def print(msg)
      @w.print msg
    end
    
    def puts(msg)
      @w.puts(msg)
    end
    
    def read
      return @r.read
    end
    
    def read_err
      return @e.read
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sprout-0.7.191-mswin32 lib/sprout/process_runner.rb
sprout-0.7.191-darwin lib/sprout/process_runner.rb
sprout-0.7.191-x86-linux lib/sprout/process_runner.rb
sprout-0.7.191 lib/sprout/process_runner.rb