Sha256: 9784128f7dfe6bfa5b2178e2f8ee7791bd7697990e6b8c098033932d6feb4268

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

require 'childprocess'
require 'tempfile'

module Overcommit
  # Manages execution of a child process, collecting the exit status and
  # standard out/error output.
  class Subprocess
    # Encapsulates the result of a process.
    Result = Struct.new(:status, :stdout, :stderr) do
      def success?
        status == 0
      end
    end

    class << self
      # Spawns a new process using the given array of arguments (the first
      # element is the command).
      def spawn(args)
        process = ChildProcess.build(*args)

        out, err = assign_output_streams(process)

        process.start
        process.wait

        err.rewind
        out.rewind

        Result.new(process.exit_code, out.read, err.read)
      end

      private

      # @param process [ChildProcess]
      # @return [Array<IO>]
      def assign_output_streams(process)
        %w[out err].map do |stream_name|
          ::Tempfile.new(stream_name).tap do |stream|
            stream.sync = true
            process.io.send("std#{stream_name}=", stream)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
overcommit-0.23.0 lib/overcommit/subprocess.rb
overcommit-0.22.0 lib/overcommit/subprocess.rb
jawshooah-overcommit-0.22.0 lib/overcommit/subprocess.rb
overcommit-0.21.0 lib/overcommit/subprocess.rb
overcommit-0.20.0 lib/overcommit/subprocess.rb