Sha256: 55fd5d0d5ec6621a6f03a47b16191c4e4a98a320e7a4f3d87a2a05aa7d647a77

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

class Rubish::Pipe < Rubish::UnixExecutable
  attr_reader :cmds

  class << self
    def build(workspace=nil,&block)
      workspace ||= Rubish::Context.current.workspace
      cmds = []
      workspace.command_factory_hook = Proc.new { |cmd| cmds << cmd; cmd}
      workspace.eval &block
      self.new(cmds)
    end
  end
  
  
  def initialize(cmds)
    # dun wanna handle special case for now
    raise "pipe length less than 2" if cmds.length < 2
    raise "should build pipe only from Rubish::Command instances" unless cmds.all? { |c| c.is_a?(Rubish::Command)}
    @cmds = cmds
  end

  def exec_with(pipe_in,pipe_out,pipe_err)
    @cmds.each do |cmd|
      cmd.normalize_args!
      if cmd.i || cmd.o || cmd.err
        raise "It's weird to redirect stdioe for command in a pipeline. Don't."
      end
    end
    # pipes == [i0,o1,i1,o2,i2...in,o0]
    # i0 == $stdin
    # o0 == $stdout
    pipe = nil # [r, w]
    pids = []
    @cmds.each_index do |index|
      tail = index == (@cmds.length - 1) # tail
      head = index == 0 # head
      if head
        i = pipe_in
        pipe = IO.pipe
        o = pipe[1] # w
      elsif tail
        i = pipe[0]
        o = pipe_out
      else # middle
        i = pipe[0] # r
        pipe = IO.pipe
        o = pipe[1]
      end

      cmd = @cmds[index]
      if child = fork # children
        #parent
        pids << child
        # it's important to close the pipes held
        # by spawning parent, otherwise the pipes
        # would not close after a program ends.
        i.close unless head
        o.close unless tail
      else
        # Rubish.set_stdioe((cmd.i || i),(cmd.o || o),(cmd.err || pipe_err))
        cmd.system_exec(i,o,pipe_err)
      end
    end
    return pids
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubish-0.1.0 lib/rubish/pipe.rb
rubish-0.0.1 lib/rubish/pipe.rb