Class: Bake::ProcessHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/common/process.rb

Constant Summary collapse

@@pid =
nil
@@rd =
nil

Class Method Summary collapse

Class Method Details

.killProcess(force) ⇒ Object

do not kill compile processes or implement rd and pid array if really needed



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/common/process.rb', line 50

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

.run(cmdLineArray, immediateOutput = false, force = true, outpipe = nil, exitCodeArray = [0]) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/common/process.rb', line 7

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