Sha256: d67d9d50f84ed13e953c90ef481a4d94bd08b2956158840f33559a3b0701f24d

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Mina
  class Runner
    class Pretty
      include Mina::Helpers::Output

      attr_reader :script

      def initialize(script)
        @script = Shellwords.shellsplit(script)
        @coathooks = 0
      end

      def run # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
        exit_status = nil

        Open3.popen3(*script) do |_stdin, stdout, stderr, wait_thr|
          pid = wait_thr.pid

          trap('INT') { handle_sigint(pid) }

          stdout_thread = Thread.new do
            while (line = stdout.gets)
              print_line(line)
            end
          end

          stderr_thread = Thread.new do
            while (line = stderr.gets)
              print_stderr(line)
            end
          end

          stdout_thread.join
          stderr_thread.join

          exit_status = wait_thr.value
        end

        exit_status.success?
      end

      private

      def handle_sigint(pid)
        puts ''
        if @coathooks > 1
          print_status 'Mina: SIGINT received again. Force quitting...'
          Process.kill 'KILL', pid
        else
          print_status 'Mina: SIGINT received.'
          Process.kill 'TERM', pid
        end
        @coathooks += 1
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mina-1.2.5 lib/mina/runner/pretty.rb