lib/hobo/helper/shell.rb in hobo-inviqa-0.0.6 vs lib/hobo/helper/shell.rb in hobo-inviqa-0.0.7.pre.rc1

- old
+ new

@@ -18,35 +18,51 @@ shell *args, &block end def shell *args, &block + def chunk_line_iterator stream + begin + until (chunk = stream.readpartial(1024)).nil? do + chunk.each_line do |outer_line| + outer_line.each_line("\r") do |line| + yield line + end + end + end + rescue EOFError + # NOP + end + end + opts = (args.size > 1 && args.last.is_a?(Hash)) ? args.pop : {} opts = { :capture => false, :indent => 0, :realtime => false, - :env => {} + :env => {}, + :ignore_errors => false }.merge! opts Hobo::Logging.logger.debug("helper.shell: Invoking '#{args.join(" ")}' with #{opts.to_s}") indent = " " * opts[:indent] ::Open3.popen3 opts[:env], *args do |stdin, out, err, external| buffer = ::Tempfile.new 'hobo_run_buf' buffer.sync = true threads = [external] + last_buf = "" ## Create a thread to read from each stream { :out => out, :err => err }.each do |key, stream| threads.push(::Thread.new do - until (line = stream.gets).nil? do - line = ::Hobo.ui.color(line.strip, :error) if key == :err + chunk_line_iterator stream do |line| + line = ::Hobo.ui.color(line, :error) if key == :err buffer.write("#{line.strip}\n") Hobo::Logging.logger.debug("helper.shell: #{line.strip}") line = yield line if block - puts indent + line if opts[:realtime] && !line.nil? + print indent + line if opts[:realtime] && !line.nil? end end) end threads.each do |t| @@ -54,10 +70,10 @@ end buffer.fsync buffer.rewind - raise ::Hobo::ExternalCommandError.new(args.join(" "), external.value.exitstatus, buffer) if external.value.exitstatus != 0 + raise ::Hobo::ExternalCommandError.new(args.join(" "), external.value.exitstatus, buffer) if external.value.exitstatus != 0 && !opts[:ignore_errors] return opts[:capture] ? buffer.read.strip : nil end end end \ No newline at end of file