Module: CovLoupe::Scripts::CommandExecution

Included in:
LatestCiStatus, PreReleaseCheck, SetupDocServer, StartDocServer
Defined in:
lib/cov_loupe/scripts/command_execution.rb

Instance Method Summary collapse

Instance Method Details

#abort_with(message) ⇒ Object

Print an error message and exit with status 1.



35
36
37
38
# File 'lib/cov_loupe/scripts/command_execution.rb', line 35

def abort_with(message)
  warn "ERROR: #{message}"
  exit 1
end

#command_exists?(cmd) ⇒ Boolean

Check if a command exists in the system PATH.

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/cov_loupe/scripts/command_execution.rb', line 41

def command_exists?(cmd)
  return true if File.exist?(cmd) && File.executable?(cmd)

  checker = Gem.win_platform? ? 'where' : 'which'
  system(checker, cmd, out: File::NULL, err: File::NULL)
end

#run_command(cmd, print_output: false, fail_on_error: true) ⇒ String

Execute a command and return its stdout.

Parameters:

  • cmd (String, Array<String>)

    The shell command to run.

  • print_output (Boolean) (defaults to: false)

    If true, prints output to stdout/stderr in real-time.

  • fail_on_error (Boolean) (defaults to: true)

    If true, aborts execution if the command fails.

Returns:

  • (String)

    The stdout output of the command (stripped).



15
16
17
18
19
20
21
# File 'lib/cov_loupe/scripts/command_execution.rb', line 15

def run_command(cmd, print_output: false, fail_on_error: true)
  if print_output
    run_streamed(cmd, fail_on_error: fail_on_error)
  else
    run_captured(cmd, fail_on_error: fail_on_error)
  end
end

#run_command_with_status(cmd) ⇒ Array<String, Boolean>

Execute a command and return stdout and success status.

Parameters:

  • cmd (String, Array<String>)

    The shell command to run.

Returns:

  • (Array<String, Boolean>)

    The stdout and success boolean.



27
28
29
30
31
32
# File 'lib/cov_loupe/scripts/command_execution.rb', line 27

def run_command_with_status(cmd)
  stdout, _stderr, status = capture_command(cmd)
  [stdout.strip, status.success?]
rescue Errno::ENOENT
  ["Command not found: #{command_display(cmd)}", false]
end