Sha256: 26cff1b3e5c916153221c982010fe357de5016b7b8bced09e7c61c0fb17e7a88

Contents?: true

Size: 1.1 KB

Versions: 16

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

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

16 entries across 16 versions & 1 rubygems

Version Path
nanoc-4.8.10 lib/nanoc/extra/piper.rb
nanoc-4.8.9 lib/nanoc/extra/piper.rb
nanoc-4.8.8 lib/nanoc/extra/piper.rb
nanoc-4.8.7 lib/nanoc/extra/piper.rb
nanoc-4.8.6 lib/nanoc/extra/piper.rb
nanoc-4.8.5 lib/nanoc/extra/piper.rb
nanoc-4.8.4 lib/nanoc/extra/piper.rb
nanoc-4.8.3 lib/nanoc/extra/piper.rb
nanoc-4.8.2 lib/nanoc/extra/piper.rb
nanoc-4.8.1 lib/nanoc/extra/piper.rb
nanoc-4.8.0 lib/nanoc/extra/piper.rb
nanoc-4.7.14 lib/nanoc/extra/piper.rb
nanoc-4.7.13 lib/nanoc/extra/piper.rb
nanoc-4.7.12 lib/nanoc/extra/piper.rb
nanoc-4.7.11 lib/nanoc/extra/piper.rb
nanoc-4.7.10 lib/nanoc/extra/piper.rb