Skip to content
Snippets Groups Projects
popen.rb 829 B
Newer Older
  • Learn to ignore specific revisions
  • require 'fileutils'
    
        def popen(cmd, path = nil)
    
          unless cmd.is_a?(Array)
            raise "System commands must be given as an array of strings"
          end
    
    
          path ||= Dir.pwd
    
          options = { chdir: path }
    
          unless File.directory?(path)
    
    Dale Hamel's avatar
    Dale Hamel committed
            FileUtils.mkdir_p(path)
    
          Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
    
            # We are not using stdin so we should close it, in case the command we
            # are running waits for input.
            stdin.close
    
            @cmd_output << stdout.read
            @cmd_output << stderr.read
    
            @cmd_status = wait_thr.value.exitstatus
    
          [@cmd_output, @cmd_status]