Sha256: 22562e8bdcf329960373cff6e134d97d3d9c7b42d2b3bc3c515955cc65507a4f

Contents?: true

Size: 1.56 KB

Versions: 10

Compression:

Stored size: 1.56 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 if exit_code.zero?

        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].read) if opts[:stdin]
        process.io.stdin.close 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

10 entries across 10 versions & 1 rubygems

Version Path
lino-4.2.0.pre.2 lib/lino/executors/childprocess.rb
lino-4.2.0.pre.1 lib/lino/executors/childprocess.rb
lino-4.1.0 lib/lino/executors/childprocess.rb
lino-4.1.0.pre.2 lib/lino/executors/childprocess.rb
lino-4.1.0.pre.1 lib/lino/executors/childprocess.rb
lino-4.0.0 lib/lino/executors/childprocess.rb
lino-4.0.0.pre.2 lib/lino/executors/childprocess.rb
lino-3.2.0.pre.10 lib/lino/executors/childprocess.rb
lino-3.2.0.pre.9 lib/lino/executors/childprocess.rb
lino-3.2.0.pre.8 lib/lino/executors/childprocess.rb