Module: Vimpack::Utils::Process

Includes:
Io
Included in:
Commands::Command, Git
Defined in:
lib/vimpack/utils/process.rb

Defined Under Namespace

Classes: Results

Instance Method Summary collapse

Methods included from Io

#die!, #exit_with_error!, #say, #scream

Instance Method Details

#run_process!(cmd) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/vimpack/utils/process.rb', line 14

def run_process!(cmd)
  child = ::ChildProcess.build(cmd)
  child.io.stdout = ::Tempfile.new('child-out')
  child.io.stderr = ::Tempfile.new('child-err')
  child.start
  child
  Results.new(child)
end

#run_process_and_wait!(cmd, dir = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/vimpack/utils/process.rb', line 36

def run_process_and_wait!(cmd, dir=nil)
  child = nil
  within_dir(dir) do
    child = run_process!(cmd)
  end
  child = wait_for_child(child)
end

#run_process_or_die!(cmd, dir = nil, err_msg = nil) ⇒ Object



44
45
46
47
48
49
# File 'lib/vimpack/utils/process.rb', line 44

def run_process_or_die!(cmd, dir=nil, err_msg=nil)
  child = run_process_and_wait!(cmd, dir)
  child.message << err_msg if err_msg
  exit_with_error!("child process died:\n#{child.message}") unless child.process.exit_code == 0
  child
end

#wait_for_child(child, timeout = 240) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vimpack/utils/process.rb', line 23

def wait_for_child(child, timeout=240)
  child.process.poll_for_exit(timeout.to_f)
  child.process.stop unless child.process.exited?
  child.process.io.stdout.close
  child.process.io.stderr.close
  child.process.io.stdout.open
  child.process.io.stderr.open
  child.message = child.process.io.stdout.read
  child.message << " "
  child.message << child.process.io.stderr.read
  child
end

#within_dir(dir = nil, &block) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/vimpack/utils/process.rb', line 51

def within_dir(dir=nil, &block)
  orig_path = Dir.pwd
  dir = dir.nil? ? orig_path : dir.to_s
  ::Dir.chdir(dir)
  block.call
  ::Dir.chdir(orig_path)
end