class Antex::Command

This class encapsulates the execution of a command line that needs some source files to produce some target files.

Existence checks are performed on files:

Details of the execution itself are given by the attributes {#stdout}, {#stderr} and {#status}.

Attributes

status[R]

@return [Process::Status, nil] the status returned by the command line

stderr[R]

@return [String, nil] the stderr returned by the command line

stdout[R]

@return [String, nil] the stdout returned by the command line

Public Class Methods

new(name:, sources:, targets:, command_line:) click to toggle source

Initializes the command.

@note A command with no targets will always skip

since it needs to produce nothing!

@param name [String] name of the command (just for error reporting) @param sources [Array] list of source files @param targets [Array] list of target files @param command_line [String] command line that will be executed

# File lib/antex/command.rb, line 39
def initialize(name:, sources:, targets:, command_line:)
  @name = name
  @sources = sources
  @targets = targets
  @command_line = command_line
end

Public Instance Methods

run!() click to toggle source

Executes the command.

@raise [MissingSourceFiles] when source files are missing @raise [ExecutionFailed] when command execution fails @raise [MissingTargetFiles] when command does not create target files

# File lib/antex/command.rb, line 51
def run!
  return if all_exist? @targets
  check_source_files!
  @stdout, @stderr, @status = Open3.capture3 @command_line
  check_status!
  check_target_files!
end

Private Instance Methods

all_exist?(files) click to toggle source
# File lib/antex/command.rb, line 61
def all_exist?(files)
  files.all?(&File.method(:exist?))
end
check_source_files!() click to toggle source
# File lib/antex/command.rb, line 69
    def check_source_files!
      raise MissingSourceFiles, <<~MISSING_SOURCE unless all_exist? @sources
        Required source files #{missing @sources} for command #{@name} are missing.
      MISSING_SOURCE
    end
check_status!() click to toggle source
# File lib/antex/command.rb, line 75
    def check_status!
      raise ExecutionFailed, <<~EXECUTION_FAILED unless @status.success?
        Command #{@name} failed.
        Command line: #{@command_line}
        Status: #{@status}
        Stdout:
        #{@stdout}
        Stderr:
        #{@stderr}
      EXECUTION_FAILED
    end
check_target_files!() click to toggle source
# File lib/antex/command.rb, line 87
    def check_target_files!
      raise MissingTargetFiles, <<~MISSING_TARGET unless all_exist? @targets
        Expected target files #{missing @targets} were not produced by command #{@name}.
      MISSING_TARGET
    end
missing(files) click to toggle source
# File lib/antex/command.rb, line 65
def missing(files)
  files.reject(&File.method(:exist?))
end