Sha256: b8797790b02c6b0296a698a09efa1c2b878865c88df9af598612f8a39b4c53e4

Contents?: true

Size: 1.08 KB

Versions: 27

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

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

27 entries across 27 versions & 1 rubygems

Version Path
nanoc-4.11.2 lib/nanoc/extra/piper.rb
nanoc-4.11.1 lib/nanoc/extra/piper.rb
nanoc-4.11.0 lib/nanoc/extra/piper.rb
nanoc-4.10.4 lib/nanoc/extra/piper.rb
nanoc-4.10.3 lib/nanoc/extra/piper.rb
nanoc-4.10.2 lib/nanoc/extra/piper.rb
nanoc-4.10.1 lib/nanoc/extra/piper.rb
nanoc-4.10.0 lib/nanoc/extra/piper.rb
nanoc-4.9.9 lib/nanoc/extra/piper.rb
nanoc-4.9.8 lib/nanoc/extra/piper.rb
nanoc-4.9.7 lib/nanoc/extra/piper.rb
nanoc-4.9.6 lib/nanoc/extra/piper.rb
nanoc-4.9.5 lib/nanoc/extra/piper.rb
nanoc-4.9.4 lib/nanoc/extra/piper.rb
nanoc-4.9.3 lib/nanoc/extra/piper.rb
nanoc-4.9.2 lib/nanoc/extra/piper.rb
nanoc-4.9.1 lib/nanoc/extra/piper.rb
nanoc-4.9.0 lib/nanoc/extra/piper.rb
nanoc-4.8.19 lib/nanoc/extra/piper.rb
nanoc-4.8.18 lib/nanoc/extra/piper.rb