Sha256: 0c8dced07f02175969260e0b771323a4b48b4b3de7a5bb286a21a82615be4707

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require 'childprocess'

require_relative '../errors'

module Lino
  module Executors
    class Childprocess
      def execute(command_line, opts = {})
        process = ::ChildProcess.build(*command_line.array)

        set_output_streams(process, opts)
        set_working_directory(process, command_line.working_directory)
        set_environment(process, command_line.environment_variables)
        start_process(process, opts)

        exit_code = process.wait

        return unless exit_code != 0

        raise Lino::Errors::ExecutionError.new(
          command_line.string, exit_code
        )
      end

      def ==(other)
        self.class == other.class
      end

      alias eql? ==

      def hash
        self.class.hash
      end

      private

      def start_process(process, opts)
        process.duplex = true if opts[:stdin]
        process.start
        process.io.stdin.write(opts[:stdin]) if opts[:stdin]
      end

      def set_output_streams(process, opts)
        process.io.inherit!
        process.io.stdout = opts[:stdout] if opts[:stdout]
        process.io.stderr = opts[:stderr] if opts[:stderr]
      end

      def set_working_directory(process, working_directory)
        process.cwd = working_directory
      end

      def set_environment(process, environment_variables)
        environment_variables.each do |environment_variable|
          process.environment[environment_variable.name] =
            environment_variable.value
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lino-3.2.0.pre.6 lib/lino/executors/childprocess.rb