Sha256: ea406a7081b9777c9895e073a728a5f3d81eaed298654bd52b0085a3a2449687

Contents?: true

Size: 1.09 KB

Versions: 16

Compression:

Stored size: 1.09 KB

Contents

require "open3"

module EmberCli
  class Runner
    def initialize(out:, err:, env: {}, options: {})
      @env = env
      @output_streams = Array(out)
      @error_streams = Array(err)
      @options = options
      @threads = []
    end

    def run(command)
      Open3.popen3(env, command, options) do |stdin, stdout, stderr, process|
        stdin.close

        threads << redirect_stream_in_thread(stdout, write_to: output_streams)
        threads << redirect_stream_in_thread(stderr, write_to: error_streams)

        threads.each(&:join)
        process.value
      end
    end

    def run!(command)
      run(command).tap do |status|
        unless status.success?
          exit status.exitstatus
        end
      end
    end

    protected

    attr_reader :env, :error_streams, :options, :output_streams, :threads

    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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ember-cli-rails-0.12.2 lib/ember_cli/runner.rb
ember-cli-rails-0.12.1 lib/ember_cli/runner.rb
ember-cli-rails-0.12.0 lib/ember_cli/runner.rb
ember-cli-rails-0.11.0 lib/ember_cli/runner.rb
ember-cli-rails-0.10.0 lib/ember_cli/runner.rb
ember-cli-rails-0.9.0 lib/ember_cli/runner.rb
ember-cli-rails-0.8.7 lib/ember_cli/runner.rb
ember-cli-rails-0.8.6 lib/ember_cli/runner.rb
ember-cli-rails-0.8.5 lib/ember_cli/runner.rb
ember-cli-rails-0.8.4 lib/ember_cli/runner.rb
ember-cli-rails-0.8.3 lib/ember_cli/runner.rb
ember-cli-rails-0.8.2 lib/ember_cli/runner.rb
ember-cli-rails-0.8.1 lib/ember_cli/runner.rb
ember-cli-rails-0.8.0 lib/ember_cli/runner.rb
ember-cli-rails-0.7.4 lib/ember_cli/runner.rb
ember-cli-rails-0.7.3 lib/ember_cli/runner.rb