lib/mattock/command-line.rb in mattock-0.5.3 vs lib/mattock/command-line.rb in mattock-0.7.0
- old
+ new
@@ -1,38 +1,43 @@
require 'mattock/command-line/command-run-result'
module Mattock
class CommandLine
- def self.define_chain_op(opname, klass)
- define_method(opname) do |other|
- unless CommandLine === other
- other = CommandLine.new(*[*other])
+ class << self
+ def 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
- 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)
+ def define_op(opname)
+ CommandLine.define_chain_op(opname, self)
+ end
+
+ attr_accessor :output_stream
end
def initialize(executable, *options)
+ @output_stream = self.class.output_stream || $stderr
@executable = executable
@options = options
@redirections = []
@env = {}
yield self if block_given?
end
- attr_accessor :name, :executable, :options, :env
+ attr_accessor :name, :executable, :options, :env, :output_stream
attr_reader :redirections
alias_method :command_environment, :env
def set_env(name, value)
@@ -85,12 +90,12 @@
def redirect_stdin(path)
redirect_from(path, 0)
end
def replace_us
- puts "Ceding execution to: "
- puts string_format
+ output_steeam.puts "Ceding execution to: "
+ output_stream.puts string_format
Process.exec(command_environment, command)
end
def spawn_process
host_stdout, cmd_stdout = IO.pipe
@@ -142,17 +147,21 @@
def complete(pid, out, err)
kill_process(pid)
collect_result(pid, out, err)
end
+ def report(message, newline=true)
+ output_stream.print(message + (newline ? "\n" : ""))
+ end
+
def run
- print string_format + " "
+ report string_format + " ", false
result = execute
- puts "=> #{result.exit_code}"
- puts result.format_streams if verbose
+ report "=> #{result.exit_code}"
+ report result.format_streams if verbose
return result
ensure
- puts if verbose
+ report "" if verbose
end
def succeeds?
run.succeeded?
end