Sha256: 0b7404d83058ca07911e85a138ec2b288b3998e37931bdbdb6aa55a1922d0648

Contents?: true

Size: 1.72 KB

Versions: 12

Compression:

Stored size: 1.72 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))
          gem 'win32-open3', '0.2.5'
          require 'win32/open3'
          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 kill
      Process.kill(9, pid)
    end
    
    def close
      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 close_write
      @w.close_write
    end
    
    def read
      return @r.read
    end
    
    def read_err
      return @e.read
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
sprout-0.7.206-mswin32 lib/sprout/process_runner.rb
sprout-0.7.205-mswin32 lib/sprout/process_runner.rb
sprout-0.7.204-mswin32 lib/sprout/process_runner.rb
sprout-0.7.204-darwin lib/sprout/process_runner.rb
sprout-0.7.205-x86-linux lib/sprout/process_runner.rb
sprout-0.7.204 lib/sprout/process_runner.rb
sprout-0.7.204-x86-linux lib/sprout/process_runner.rb
sprout-0.7.206-darwin lib/sprout/process_runner.rb
sprout-0.7.205-darwin lib/sprout/process_runner.rb
sprout-0.7.206-x86-linux lib/sprout/process_runner.rb
sprout-0.7.205 lib/sprout/process_runner.rb
sprout-0.7.206 lib/sprout/process_runner.rb