lib/ember_cli/runner.rb in ember-cli-rails-0.7.2 vs lib/ember_cli/runner.rb in ember-cli-rails-0.7.3
- old
+ new
@@ -1,36 +1,49 @@
require "open3"
module EmberCli
class Runner
- def initialize(env: {}, out:, err:, options: {})
+ def initialize(out:, err:, env: {}, options: {})
@env = env
- @out = out
- @err = err
+ @output_streams = Array(out)
+ @error_streams = Array(err)
@options = options
+ @threads = []
end
def run(command)
- output, status = Open3.capture2e(@env, command, @options)
+ Open3.popen3(env, command, options) do |stdin, stdout, stderr, process|
+ stdin.close
- @out.write(output)
+ threads << redirect_stream_in_thread(stdout, write_to: output_streams)
+ threads << redirect_stream_in_thread(stderr, write_to: error_streams)
- [output, status]
+ threads.each(&:join)
+ process.value
+ end
end
def run!(command)
- output, status = run(command)
+ run(command).tap do |status|
+ unless status.success?
+ exit status.exitstatus
+ end
+ end
+ end
- unless status.success?
- @err.write <<-MSG.strip_heredoc
- ERROR: Failed command: `#{command}`
- OUTPUT:
- #{output}
- MSG
+ protected
- exit status.exitstatus
- end
+ attr_reader :env, :error_streams, :options, :output_streams, :threads
- true
+ private
+
+ def redirect_stream_in_thread(stream, write_to:)
+ Thread.new do
+ Thread.current.abort_on_exception = true
+
+ while line = stream.gets
+ write_to.each { |redirection_stream| redirection_stream.puts(line) }
+ end
+ end
end
end
end