Module: Bringit::Popen

Extended by:
Popen
Included in:
Popen, Repository
Defined in:
lib/bringit/popen.rb

Class Method Summary collapse

Class Method Details

.popen(cmd, path = nil, vars = {}) ⇒ Object



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
# File 'lib/bringit/popen.rb', line 8

def self.popen(cmd, path = nil, vars = {})
  unless cmd.is_a?(Array)
    raise 'System commands must be given as an array of strings'
  end

  path ||= Dir.pwd
  vars = vars.dup
  vars['PWD'] = path
  options = {chdir: path}

  FileUtils.mkdir_p(path) unless File.directory?(path)

  cmd_output = ''
  cmd_status = 0
  Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
    yield(stdin) if block_given?
    stdin.close

    cmd_output += stdout.read
    cmd_output += stderr.read
    cmd_status = wait_thr.value.exitstatus
  end

  [cmd_output, cmd_status]
end