lib/mattock/command-line.rb in mattock-0.2.13 vs lib/mattock/command-line.rb in mattock-0.3.0
- old
+ new
@@ -30,16 +30,36 @@
def must_succeed!
case exit_code
when 0
return exit_code
else
- fail "Command #{@command.inspect} failed with exit status #{$?.exitstatus}: \n#{streams.inspect}"
+ fail "Command #{@command.inspect} failed with exit status #{exit_code}: \n#{streams.inspect}"
end
end
end
class CommandLine
+ def self.define_chain_op(opname, klass)
+ define_method(opname) do |other|
+ unless CommandLine === other
+ other = CommandLine.new(*[*other])
+ end
+ chain = nil
+ if klass === self
+ chain = self
+ else
+ chain = klass.new
+ chain.add(self)
+ end
+ chain.add(other)
+ end
+ end
+
+ def self.define_op(opname)
+ CommandLine.define_chain_op(opname, self)
+ end
+
def initialize(executable, *options)
@executable = executable
@options = options
@redirections = []
yield self if block_given?
@@ -54,13 +74,17 @@
def name
@name || executable
end
def command
- ([executable] + options + @redirections).join(" ")
+ ([executable] + options_composition + @redirections).join(" ")
end
+ def options_composition
+ options
+ end
+
def redirect_to(stream, path)
@redirections << "#{stream}>#{path}"
end
def redirect_from(path, stream)
@@ -108,10 +132,16 @@
def must_succeed!
run.must_succeed!
end
end
+ module CommandLineDSL
+ def cmd(*args)
+ CommandLine.new(*args)
+ end
+ end
+
class ShellEscaped < CommandLine
def initialize(cmd)
@escaped = cmd
end
@@ -133,29 +163,36 @@
attr_reader :commands
def add(cmd)
yield cmd if block_given?
@commands << cmd
+ self
end
def name
@name || @commands.last.name
end
end
class WrappingChain < CommandChain
+ define_op('-')
+
def command
@commands.map{|cmd| cmd.command}.join(" -- ")
end
end
class PrereqChain < CommandChain
+ define_op('&')
+
def command
@commands.map{|cmd| cmd.command}.join(" && ")
end
end
class PipelineChain < CommandChain
+ define_op('|')
+
def command
@commands.map{|cmd| cmd.command}.join(" | ")
end
end
end