Class: Reviewer::Shell

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/reviewer/shell.rb,
lib/reviewer/shell/timer.rb,
lib/reviewer/shell/result.rb

Overview

Handles running, timing, and capturing results for a command

Defined Under Namespace

Classes: Result, Timer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream: $stdout) ⇒ Shell

Initializes a Reviewer shell for running and benchmarking commands, and capturing output

Parameters:

  • (defaults to: $stdout)

    the output stream for direct (passthrough) output



22
23
24
25
26
27
# File 'lib/reviewer/shell.rb', line 22

def initialize(stream: $stdout)
  @stream = stream
  @timer = Timer.new
  @result = Result.new
  @captured_results = nil
end

Instance Attribute Details

#captured_resultsObject (readonly)

Returns the value of attribute captured_results.



14
15
16
# File 'lib/reviewer/shell.rb', line 14

def captured_results
  @captured_results
end

#resultObject (readonly)

Returns the value of attribute result.



14
15
16
# File 'lib/reviewer/shell.rb', line 14

def result
  @result
end

#timerObject (readonly)

Returns the value of attribute timer.



14
15
16
# File 'lib/reviewer/shell.rb', line 14

def timer
  @timer
end

Instance Method Details

#capture_main(command) ⇒ Result

Captures and times the main command execution

Parameters:

  • the command to run

Returns:

  • the captured result including stdout, stderr, and exit status



66
# File 'lib/reviewer/shell.rb', line 66

def capture_main(command) = timer.record_main { capture_results(command) }

#capture_prep(command) ⇒ Result

Captures and times the preparation command execution

Parameters:

  • the command to run

Returns:

  • the captured result including stdout, stderr, and exit status



60
# File 'lib/reviewer/shell.rb', line 60

def capture_prep(command) = timer.record_prep { capture_results(command) }

#direct(command) ⇒ Result

Run a command via PTY, streaming output in real-time while capturing it for later use (e.g. failed file extraction). PTY allocates a pseudo-terminal so the child process preserves ANSI colors and interactive behavior.

Parameters:

  • the command to run

Returns:

  • the captured result including stdout and exit status



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/reviewer/shell.rb', line 35

def direct(command)
  command = String(command)
  buffer = +''

  reader, _writer, pid = PTY.spawn(command)
  begin
    reader.each_line do |line|
      @stream.print line
      buffer << line
    end
  rescue Errno::EIO
    # Expected when child process exits before all output is read
  end

  _, status = Process.waitpid2(pid)
  @result = Result.new(buffer, nil, status)
rescue Errno::ENOENT
  @result = Result.new(buffer, nil, nil)
  @result.exit_status = Result::EXIT_STATUS_CODES[:executable_not_found]
end