Class: Reviewer::Runner

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/reviewer/runner.rb,
lib/reviewer/runner/strategies/captured.rb,
lib/reviewer/runner/strategies/passthrough.rb
more...

Overview

Wrapper for executng a command and printing the results

Defined Under Namespace

Modules: Strategies

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool, command_type, strategy = Strategies::Captured, output: Reviewer.output) ⇒ self

Creates a wrapper for running commansd through Reviewer in order to provide a more accessible

API for recording execution time and interpreting the results of a command in a more
generous way so that non-zero exit statuses can still potentiall be passing.

Parameters:

  • tool (Symbol)

    the key for the desired tool to run

  • command_type (Symbol)

    the key for the type of command to run

  • strategy (defaults to: Strategies::Captured)

    Strategies::Captured [Runner::Strategies] how to execute and handle the

    results of the command

  • output: (defaults to: Reviewer.output)

    Reviewer.output [Review::Output] the output formatter for the results

[View source]

29
30
31
32
33
34
# File 'lib/reviewer/runner.rb', line 29

def initialize(tool, command_type, strategy = Strategies::Captured, output: Reviewer.output)
  @command = Command.new(tool, command_type)
  @strategy = strategy
  @shell = Shell.new
  @output = output
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.


13
14
15
# File 'lib/reviewer/runner.rb', line 13

def command
  @command
end

#outputObject (readonly)

Returns the value of attribute output.


13
14
15
# File 'lib/reviewer/runner.rb', line 13

def output
  @output
end

#shellObject (readonly)

Returns the value of attribute shell.


13
14
15
# File 'lib/reviewer/runner.rb', line 13

def shell
  @shell
end

#strategyObject

Returns the value of attribute strategy.


11
12
13
# File 'lib/reviewer/runner.rb', line 11

def strategy
  @strategy
end

Instance Method Details

#execute_strategyvoid

This method returns an undefined value.

Runs the relevant strategy to either capture or pass through command output.

[View source]

78
79
80
81
82
83
84
# File 'lib/reviewer/runner.rb', line 78

def execute_strategy
  # Run the provided strategy
  strategy.new(self).tap do |run_strategy|
    run_strategy.prepare if run_prepare_step?
    run_strategy.run
  end
end

#guidanceGuidance

Uses the result of the runner to determine what, if any, guidance to display to help the user

get back on track in the event of an unsuccessful run.

Returns:

  • (Guidance)

    the relevant guidance based on the result of the runner

[View source]

127
128
129
# File 'lib/reviewer/runner.rb', line 127

def guidance
  @guidance ||= Reviewer::Guidance.new(command: command, result: result, output: output)
end

#identify_toolvoid

This method returns an undefined value.

Prints the tool name and description to the console as a frame of reference

[View source]

67
68
69
70
71
72
73
# File 'lib/reviewer/runner.rb', line 67

def identify_tool
  # If there's an existing result, the runner is being re-run, and identifying the tool would
  # be redundant.
  return if result.exists?

  output.tool_summary(tool)
end

#prepare_commandComman

Creates_an instance of the prepare command for a tool

Returns:

  • (Comman)

    the current tool’s prepare command

[View source]

98
99
100
# File 'lib/reviewer/runner.rb', line 98

def prepare_command
  @prepare_command ||= Command.new(tool, :prepare)
end

#record_timingvoid

This method returns an undefined value.

Saves the last 5 elapsed times for the commands used this run by using the raw command as a

unique key. This enables the ability to compare times across runs while taking into
consideration that different iterations of the command may be running on fewer files. So
comparing a full run to the average time for a partial run wouldn't be helpful. By using the
raw command string, it will always be apples to apples.
[View source]

118
119
120
121
# File 'lib/reviewer/runner.rb', line 118

def record_timing
  tool.record_timing(prepare_command, timer.prep)
  tool.record_timing(command, timer.main)
end

#runObject

[View source]

36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/reviewer/runner.rb', line 36

def run
  # Show which tool is running
  identify_tool

  # Use the provided strategy to run the command
  execute_strategy

  # If it failed, display guidance to help them get back on track
  guidance.show unless success?

  # Return the exit status generated by the tool as interpreted by the Result
  exit_status
end

#run_prepare_step?Boolean

Determines whether a preparation step should be run before the primary command. If/when the

primary command is a `:prepare` command, then it shouldn't run twice. So it skips what would
be a superfluous run of the preparation.

Returns:

  • (Boolean)

    true the primary command is not prepare and the tool needs to be prepare

[View source]

91
92
93
# File 'lib/reviewer/runner.rb', line 91

def run_prepare_step?
  command.type != :prepare && tool.prepare?
end

#success?Boolean

Returns:

  • (Boolean)
[View source]

50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/reviewer/runner.rb', line 50

def success?
  # Some review tools return a range of non-zero exit statuses and almost never return 0.
  # (`yarn audit` is a good example.) Those tools can be configured to accept a non-zero exit
  # status so they aren't constantly considered to be failing over minor issues.
  #
  # But when other command types (prepare, install, format) are run, they either succeed or they
  # fail. With no shades of gray in those cases, anything other than a 0 is a failure.
  if command.type == :review
    exit_status <= tool.max_exit_status
  else
    exit_status.zero?
  end
end

#update_last_prepared_atTime

Updates the ‘last prepared at’ timestamp that Reviewer uses to know if a tool’s preparation

step is stale and needs to be run again.

Returns:

  • (Time)

    the timestamp ‘last_prepared_at` is updated to

[View source]

106
107
108
109
# File 'lib/reviewer/runner.rb', line 106

def update_last_prepared_at
  # Touch the `last_prepared_at` timestamp for the tool so it waits before running again.
  tool.last_prepared_at = Time.now
end