Sha256: 8c29bd05e9f19d55a9ca76642e776463d9cd15dc30f687783bdf3a951fa1467e

Contents?: true

Size: 1.91 KB

Versions: 34

Compression:

Stored size: 1.91 KB

Contents

module Bake

  class ProcessHelper
    @@pid = nil
    @@rd = nil

    def self.run(cmdLineArray, immediateOutput=false, force=true, outpipe=nil, exitCodeArray = [0])
      rd, wr = IO.pipe
      @@rd = rd if force
      duppedCmdLineArray = cmdLineArray.dup
      duppedCmdLineArray << { :err=>wr, :out=>(outpipe ? outpipe : wr) }
      begin
        pid = spawn(*duppedCmdLineArray)
      rescue Exception => e
        return [false, e.message]
      end
      @@pid = pid if force
      wr.close
      output = ""
      begin
        while not rd.eof?
          tmp = rd.read(1)
          if (tmp != nil)
            tmp.encode!('UTF-8',  :invalid => :replace, :undef => :replace, :replace => '')
            tmp.encode!('binary', :invalid => :replace, :undef => :replace, :replace => '')
            output << tmp
            
            print tmp if immediateOutput
          end
        end
      rescue
        # Seems to be a bug in ruby: sometimes there is a bad file descriptor on Windows instead of eof, which causes
        # an exception on read(). However, this happens not before everything is read, so there is no practical difference
        # how to "break" the loop.
        # This problem occurs on Windows command shell and Cygwin.
      end
      
      begin
        rd.close
      rescue
      end 
      pid, status = Process.wait2(pid)
      @@pid = nil
      @@rd = nil
      return [false, output] if status.nil?
      exitCodeArray = [0] if exitCodeArray.empty?
      [(exitCodeArray.include?status.exitstatus), output]
    end
       
    def self.killProcess(force) # do not kill compile processes or implement rd and pid array if really needed
      begin
        @@rd.close
      rescue Exception => e
      end
      begin
        Process.kill("KILL",@@pid)
      rescue Exception => e
      end
      @@rd = nil
      @@pid = nil
    end
    
  end

end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
bake-toolkit-2.20.4 lib/common/process.rb
bake-toolkit-2.20.3 lib/common/process.rb
bake-toolkit-2.20.2 lib/common/process.rb
bake-toolkit-2.20.1 lib/common/process.rb
bake-toolkit-2.19.2 lib/common/process.rb
bake-toolkit-2.19.1 lib/common/process.rb
bake-toolkit-2.19.0 lib/common/process.rb
bake-toolkit-2.18.0 lib/common/process.rb
bake-toolkit-2.17.4 lib/common/process.rb
bake-toolkit-2.17.3 lib/common/process.rb
bake-toolkit-2.17.2 lib/common/process.rb
bake-toolkit-2.17.1 lib/common/process.rb
bake-toolkit-2.16.1 lib/common/process.rb
bake-toolkit-2.15.0 lib/common/process.rb
bake-toolkit-2.14.0 lib/common/process.rb
bake-toolkit-2.13.1 lib/common/process.rb
bake-toolkit-2.13.0 lib/common/process.rb
bake-toolkit-2.12.2 lib/common/process.rb
bake-toolkit-2.12.1 lib/common/process.rb
bake-toolkit-2.12.0 lib/common/process.rb