Class: Overcommit::Subprocess

Inherits:
Object
  • Object
show all
Defined in:
lib/overcommit/subprocess.rb

Overview

Manages execution of a child process, collecting the exit status and standard out/error output.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.spawn(args, options = {}) ⇒ Result

Spawns a new process using the given array of arguments (the first element is the command).

Options Hash (options):

  • input (String)

    string to pass via standard input stream


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/overcommit/subprocess.rb', line 27

def spawn(args, options = {})
  args = win32_prepare_args(args) if OS.windows?

  process = ChildProcess.build(*args)

  out, err = assign_output_streams(process)

  process.duplex = true if options[:input] # Make stdin available if needed
  process.start
  if options[:input]
    begin
      process.io.stdin.puts(options[:input])
    rescue StandardError # rubocop:disable Lint/HandleExceptions
      # Silently ignore if the standard input stream of the spawned
      # process is closed before we get a chance to write to it. This
      # happens on JRuby a lot.
    ensure
      process.io.stdin.close
    end
  end
  process.wait

  err.rewind
  out.rewind

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

.spawn_detached(args) ⇒ Object

Spawns a new process in the background using the given array of arguments (the first element is the command).


57
58
59
60
61
62
63
64
65
66
# File 'lib/overcommit/subprocess.rb', line 57

def spawn_detached(args)
  args = win32_prepare_args(args) if OS.windows?

  process = ChildProcess.build(*args)
  process.detach = true

  assign_output_streams(process)

  process.start
end