Sha256: 4c1937390a148c4cba3a4b363676f1bdc1ce05f07bde86973eb721ba20b17c7d

Contents?: true

Size: 1.07 KB

Versions: 54

Compression:

Stored size: 1.07 KB

Contents

require 'open3'

module Nanoc::Extra
  # @api private
  class Piper
    class Error < ::Nanoc::Int::Errors::Generic
      def initialize(command, exit_code)
        @command   = command
        @exit_code = exit_code
      end

      def message
        "command exited with a nonzero status code #{@exit_code} (command: #{@command.join(' ')})"
      end
    end

    # @param [IO] stdout
    # @param [IO] stderr
    def initialize(stdout: $stdout, stderr: $stderr)
      @stdout = stdout
      @stderr = stderr
    end

    # @param [Array<String>] cmd
    #
    # @param [String, nil] input
    def run(cmd, input)
      Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
        stdout_thread = Thread.new { @stdout << stdout.read }
        stderr_thread = Thread.new { @stderr << stderr.read }

        if input
          stdin << input
        end
        stdin.close

        stdout_thread.join
        stderr_thread.join

        exit_status = wait_thr.value
        unless exit_status.success?
          raise Error.new(cmd, exit_status.to_i)
        end
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
nanoc-4.7.9 lib/nanoc/extra/piper.rb
nanoc-4.7.8 lib/nanoc/extra/piper.rb
nanoc-4.7.7 lib/nanoc/extra/piper.rb
nanoc-4.7.6 lib/nanoc/extra/piper.rb
nanoc-4.7.5 lib/nanoc/extra/piper.rb
nanoc-4.7.4 lib/nanoc/extra/piper.rb
nanoc-4.7.3 lib/nanoc/extra/piper.rb
nanoc-4.7.2 lib/nanoc/extra/piper.rb
nanoc-4.7.1 lib/nanoc/extra/piper.rb
nanoc-4.7.0 lib/nanoc/extra/piper.rb
nanoc-4.6.4 lib/nanoc/extra/piper.rb
nanoc-4.6.3 lib/nanoc/extra/piper.rb
nanoc-4.6.2 lib/nanoc/extra/piper.rb
nanoc-4.6.1 lib/nanoc/extra/piper.rb
nanoc-4.6.0 lib/nanoc/extra/piper.rb
nanoc-4.5.4 lib/nanoc/extra/piper.rb
nanoc-4.5.3 lib/nanoc/extra/piper.rb
nanoc-4.5.2 lib/nanoc/extra/piper.rb
nanoc-4.5.1 lib/nanoc/extra/piper.rb
nanoc-4.5.0 lib/nanoc/extra/piper.rb