Class: Nanoc::Extra::Piper

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/extra/piper.rb

Defined Under Namespace

Classes: Error

Instance Method Summary (collapse)

Constructor Details

- (Piper) initialize(params = {})

Returns a new instance of Piper

Parameters:

  • [IO] (Hash)

    a customizable set of options



20
21
22
23
# File 'lib/nanoc/extra/piper.rb', line 20

def initialize(params = {})
  @stdout = params.fetch(:stdout, $stdout)
  @stderr = params.fetch(:stderr, $stderr)
end

Instance Method Details

- (Object) run(cmd, input)

Parameters:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nanoc/extra/piper.rb', line 28

def run(cmd, input)
  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
    stdout_thread = Thread.new { @stdout << stdout.read }
    stderr_thread = Thread.new { @stderr << stderr.read }

    if input
      stdin << input
    end
    stdin.close

    stdout_thread.join
    stderr_thread.join

    exit_status = wait_thr.value
    unless exit_status.success?
      raise Error.new(cmd, exit_status.to_i)
    end
  end
end