Class: Reviewer::Command::String

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

Overview

Assembles tool tool_settings into a usable command string for the command type

Defined Under Namespace

Classes: Env, Flags

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_type, tool_settings:, files: []) ⇒ String

Creates a command string builder for a tool

Parameters:

  • command_type (Symbol)

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

  • tool_settings (Tool::Settings)

    the tool’s configuration settings

  • files (Array<String>) (defaults to: [])

    files to include in the command



18
19
20
21
22
# File 'lib/reviewer/command/string.rb', line 18

def initialize(command_type, tool_settings:, files: [])
  @command_type = command_type
  @tool_settings = tool_settings
  @files = Array(files)
end

Instance Attribute Details

#command_typeObject (readonly)

Returns the value of attribute command_type.



10
11
12
# File 'lib/reviewer/command/string.rb', line 10

def command_type
  @command_type
end

#filesObject (readonly)

Returns the value of attribute files.



10
11
12
# File 'lib/reviewer/command/string.rb', line 10

def files
  @files
end

#tool_settingsObject (readonly)

Returns the value of attribute tool_settings.



10
11
12
# File 'lib/reviewer/command/string.rb', line 10

def tool_settings
  @tool_settings
end

Instance Method Details

#bodyString

The base command string from the tool’s configuration. Uses the file-scoped command when files are present and one is configured.

Returns:

  • (String)

    the configured command for the command type



55
56
57
# File 'lib/reviewer/command/string.rb', line 55

def body
  file_scoped_command || tool_settings.commands.fetch(command_type)
end

#env_variablesString

The string of environment variables built from a tool’s configuration settings

Returns:

  • (String)

    the environment variable names and values concatened for the command



49
# File 'lib/reviewer/command/string.rb', line 49

def env_variables = Env.new(tool_settings.env).to_s

#files_stringString?

Builds the files portion of the command string

Returns:

  • (String, nil)

    the formatted files string or nil if not applicable



73
74
75
76
77
78
79
80
# File 'lib/reviewer/command/string.rb', line 73

def files_string
  return nil unless files_applicable?

  file_list = files.join(tool_settings.files_separator)
  flag = tool_settings.files_flag

  flag.empty? ? file_list : "#{flag} #{file_list}"
end

#flagsString

Gets the flags to be used in conjunction with the review command for a tool

1. The `review` commands are the only commands that use flags
2. If no flags are configured, this won't do anything

Returns:

  • (String)

    the concatenated list of flags to pass to the review command



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

def flags
  return nil unless flags?

  Flags.new(tool_settings.flags).to_s
end

#to_aArray<String, nil>

Converts the command to an array of its components

Returns:

  • (Array<String, nil>)

    env vars, body, flags, and files



37
38
39
40
41
42
43
44
# File 'lib/reviewer/command/string.rb', line 37

def to_a
  [
    env_variables,
    body,
    flags,
    files_string
  ].compact
end

#to_sString

Converts the command to a complete string ready for execution

Returns:

  • (String)

    the full command string



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

def to_s
  to_a
    .map(&:strip) # Remove extra spaces on the components
    .join(' ')    # Merge the components
    .strip        # Strip extra spaces from the end result
end