Class: Reviewer::Command

Inherits:
Object
  • Object
show all
Includes:
Tool::Conversions
Defined in:
lib/reviewer/command.rb,
lib/reviewer/command/string.rb,
lib/reviewer/command/string/env.rb,
lib/reviewer/command/string/flags.rb

Overview

The core funtionality to translate a tool, command type, and verbosity into a runnable command

Defined Under Namespace

Classes: String

Constant Summary collapse

SEED_SUBSTITUTION_VALUE =
'$SEED'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tool::Conversions

Tool

Constructor Details

#initialize(tool, type, context:) ⇒ Command

Creates an instance of the Command class to synthesize a command string using the tool, command type, and verbosity.

Parameters:

  • tool (Tool, Symbol)

    a tool or tool key to use to look up the command and options

  • type (Symbol)

    the desired command type (:install, :prepare, :review, :format)

  • context (Context)

    the shared runtime dependencies (arguments, output, history)



26
27
28
29
30
31
32
# File 'lib/reviewer/command.rb', line 26

def initialize(tool, type, context:)
  @tool = Tool(tool)
  @type = type.to_sym
  @seed = nil
  @arguments = context.arguments
  @history = context.history
end

Instance Attribute Details

#toolObject (readonly)

Returns the value of attribute tool.



12
13
14
# File 'lib/reviewer/command.rb', line 12

def tool
  @tool
end

#typeSymbol

Returns the command type (:install, :prepare, :review, :format).

Returns:

  • (Symbol)

    the command type (:install, :prepare, :review, :format)



17
18
19
# File 'lib/reviewer/command.rb', line 17

def type
  @type
end

Instance Method Details

#raw_stringString

The raw command string before any substitutions. For example, since seeds need to remain consistent from one run to the next, they’re

Returns:

  • (String)

    the command string before seed substitution



65
66
67
# File 'lib/reviewer/command.rb', line 65

def raw_string
  @raw_string ||= String.new(type, tool_settings: tool.settings, files: target_files).to_s # rubocop:disable Lint/RedundantTypeConversion
end

#run_summaryHash?

Returns a summary hash for run display, or nil if this command should be skipped

Returns:

  • (Hash, nil)

    { name:, files: } or nil if skipped



86
87
88
89
90
# File 'lib/reviewer/command.rb', line 86

def run_summary
  return nil if skip?

  { name: tool.name, files: target_files }
end

#seedInteger

Generates a seed that can be re-used across runs so that the results are consistent across related runs for tools that would otherwise change the seed automatically every run. Since not all tools will use the seed, there’s no need to generate it in the initializer. Instead, it’s memoized if it’s used.

Returns:

  • (Integer)

    a random integer to pass to tools that use seeds



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/reviewer/command.rb', line 48

def seed
  @seed ||= if arguments.keywords.failed?
              history.get(tool.key, :last_seed) || Random.rand(100_000)
            else
              Random.rand(100_000)
            end

  # Store the seed for reference
  history.set(tool.key, :last_seed, @seed)

  @seed
end

#skip?Boolean

Determines if this command should be skipped because files were requested but none match

Returns:

  • (Boolean)

    true if files were requested but resolution left none for this tool



79
80
81
# File 'lib/reviewer/command.rb', line 79

def skip?
  tool.skip_files?(requested_files)
end

#stringString Also known as: to_s

The final command string with all of the conditions appled

Returns:

  • (String)

    the final, valid command string to run



37
38
39
# File 'lib/reviewer/command.rb', line 37

def string
  @string ||= seed_substitution? ? seeded_string : raw_string
end

#target_filesArray<String>

Gets the list of files to target for this command, resolved by the tool

Returns:

  • (Array<String>)

    the list of files from arguments, resolved by tool



72
73
74
# File 'lib/reviewer/command.rb', line 72

def target_files
  @target_files ||= tool.resolve_files(requested_files)
end