Method: Coque::Pipeline#stitch

Defined in:
lib/coque/pipeline.rb

#stitchObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/coque/pipeline.rb', line 32

def stitch
  # Set head in
  if commands.first.stdin.nil?
    start_r, start_w = IO.pipe
    start_w.close
    commands.first.stdin = start_r
  end

  # Connect intermediate in/outs
  commands.each_cons(2) do |left, right|
    read, write = IO.pipe
    left.stdout = write
    right.stdin = read
  end

  # Set tail out
  if self.stdout
    commands.last.stdout = stdout
    stdout
  elsif commands.last.stdout
    commands.last.stdout
  else
    next_r, next_w = IO.pipe
    commands.last.stdout = next_w
    next_r
  end
end