Sha256: a16418a6d12b7790c008b8da0cbc1df7691a528f3a6a9eb8006aa00512b93c50

Contents?: true

Size: 1.82 KB

Versions: 36

Compression:

Stored size: 1.82 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

36 entries across 36 versions & 1 rubygems

Version Path
bake-toolkit-2.24.4 lib/common/process.rb
bake-toolkit-2.24.3 lib/common/process.rb
bake-toolkit-2.24.2 lib/common/process.rb
bake-toolkit-2.24.1 lib/common/process.rb
bake-toolkit-2.24.0 lib/common/process.rb
bake-toolkit-2.23.12 lib/common/process.rb
bake-toolkit-2.23.8 lib/common/process.rb
bake-toolkit-2.23.7 lib/common/process.rb
bake-toolkit-2.23.6 lib/common/process.rb
bake-toolkit-2.23.5 lib/common/process.rb
bake-toolkit-2.23.4 lib/common/process.rb
bake-toolkit-2.23.3 lib/common/process.rb
bake-toolkit-2.23.2 lib/common/process.rb
bake-toolkit-2.23.1 lib/common/process.rb
bake-toolkit-2.22.0 lib/common/process.rb
bake-toolkit-2.21.0 lib/common/process.rb