Class: Replr::ProcessRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/replr/process_runner.rb

Overview

Executes and manages processes

Instance Method Summary collapse

Instance Method Details

#execute_command(command) ⇒ Object

Executes command using system. Use this when you have no need for a return value



10
11
12
# File 'lib/replr/process_runner.rb', line 10

def execute_command(command)
  system(command)
end

#execute_command_if_not_stderr(new_command, stderr, process_thread) ⇒ Object

Executes new_command if the previous process_thread had a non-zero return. Otherwise prints out stderr of the previous process to STDERR



41
42
43
44
45
46
47
48
# File 'lib/replr/process_runner.rb', line 41

def execute_command_if_not_stderr(new_command, stderr, process_thread)
  outerror = stderr.read.chomp
  if process_thread.value.to_i.zero?
    system(new_command)
  else
    STDERR.puts outerror
  end
end

#execute_filtered_process(command, matching_regex, not_matching_regex, &_block) ⇒ Object

Runs command and only prints those lines that match matching_regex and doesn’t match not_matching_regex. After execution, it passes stderr and the waiting process_thread to a block.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/replr/process_runner.rb', line 23

def execute_filtered_process(command, matching_regex,
                             not_matching_regex, &_block)
  Open3.popen3(command) do |_stdin, stdout, stderr, process_thread|
    stdout.each_line do |outline|
      if outline.match(matching_regex) &&
         !outline.match(not_matching_regex)
        puts outline
      end
    end

    # Yield to block to process next set of commands or handle stderr
    yield(stderr, process_thread)
  end
end

#execute_repl_with_input(command:, inputs:, prompt_line:, expected_output:, debug: false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/replr/process_runner.rb', line 50

def execute_repl_with_input(command:, inputs:, prompt_line:, expected_output:, debug: false)
  outputs = []
  inputs << ''

  PTY.spawn(command) do |read, write, _pid|
    read.expect(prompt_line)
    inputs.each do |cmd|
      begin
        write.puts cmd
        read.flush
        read.expect(/(.*?)\r\n(.*)>/m) do |output|
          puts "DEBUG: OUTPUT: #{output.inspect}" if debug
          outputs << output[2].match(expected_output) if output
        end
      rescue Errno::EIO
        next
      end
    end
  end

  outputs.any? { |output| output.is_a? MatchData }
end

#process_exists?(process) ⇒ Boolean

Checks whether process exists using ‘which`

Returns:

  • (Boolean)


15
16
17
# File 'lib/replr/process_runner.rb', line 15

def process_exists?(process)
  system("which #{process} >/dev/null")
end