Sha256: 37fb34252918df658ccb91807b3e214eb276243c34d01be891c437501611a327

Contents?: true

Size: 1.18 KB

Versions: 16

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Jive
  module Popen
    Result = Struct.new(:command, :stdout, :stderr, :status, :duration)

    def self.popen(command, path = nil, env = {}, &block)
      result = popen_with_detail(command, path, env, &block)

      ["#{result.stdout}#{result.stderr}", result.status&.exitstatus]
    end

    def self.popen_with_detail(command, path = Dir.pwd, env = {})
      FileUtils.mkdir_p(path) unless File.directory?(path)

      captured_stdout = ""
      captured_stderr = ""
      exit_status = nil
      start = Time.now

      Open3.popen3(env.merge("PWD" => path), *Array(command),
                   { chdir: path }) do |stdin, stdout, stderr, wait_thr|
                     out_reader = Thread.new { stdout.read }
                     err_reader = Thread.new { stderr.read }

                     yield(stdin) if block_given?

                     stdin.close
                     captured_stdout = out_reader.value
                     captured_stderr = err_reader.value
                     exit_status = wait_thr.value
                   end
      Result.new(command, captured_stdout, captured_stderr, exit_status,
                 Time.now - start)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
jive-0.7.0 lib/jive/popen.rb
jive-0.6.0 lib/jive/popen.rb
jive-0.5.0 lib/jive/popen.rb
jive-0.4.4 lib/jive/popen.rb
jive-0.4.3 lib/jive/popen.rb
jive-0.4.2 lib/jive/popen.rb
jive-0.4.1 lib/jive/popen.rb
jive-0.4.0 lib/jive/popen.rb
jive-0.3.3 lib/jive/popen.rb
jive-0.3.2 lib/jive/popen.rb
jive-0.3.1 lib/jive/popen.rb
jive-0.3.0 lib/jive/popen.rb
jive-0.2.3 lib/jive/popen.rb
jive-0.2.2 lib/jive/popen.rb
jive-0.2.1 lib/jive/popen.rb
jive-0.2.0 lib/jive/popen.rb