Class: Brut::CLI::Commands::CompoundCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/brut/cli/commands/compound_command.rb

Overview

A command that exists only to call other commands in order. This is useful if you want a convienience command to call two or more other commands. By default, each command must succeed for the others to be called.

Direct Known Subclasses

Apps::BuildAssets::All, Apps::DB::Rebuild

Instance Attribute Summary

Attributes inherited from BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from BaseCommand

#accepts, #args_description, #argv, #bootstrap?, #commands, #default_rack_env, #delegate_to_command, #description, #detailed_description, #env, #env_vars, #name, #options, #opts, #print, #puts, #run, #stdin, #system!, #theme

Constructor Details

#initialize(commands = nil) ⇒ CompoundCommand

Create the compound command with the given list of commands. Note that these commands will be executed with this commands ‘Brut::CLI::Commands::ExecutionContext`, so these commands should all be able to work with whatever command line arguments and `argv` would be provided.

Parameters:

  • The (Array<Brut::CLI::Commands::BaseCommand>|nil)

    list of commands to run when this command is executed. If nil, it is assumed you have overriden ‘commands` to provide the list dynamically.



12
13
14
# File 'lib/brut/cli/commands/compound_command.rb', line 12

def initialize(commands=nil)
  @commands = commands || []
end

Instance Method Details

#execute(execution_context) ⇒ Object

Overrides the parent class to call each command in order. Note that if you subclass this class, **‘run` is not called**. If you want to perform custom logic, you must override this method, but take care to call methods on the passed `execution_context`. Methods like `puts`, `system!`, and `options` **will not work** here since they assume an ivar named `@execution_context` has been set.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/brut/cli/commands/compound_command.rb', line 20

def execute(execution_context)
  sub_commands(execution_context).each do |command|
    execute_result = Brut::CLI::ExecuteResult.new do
      delegate_to_command(command,execution_context)
    end
    if execute_result.failed?
      return execute_result.actual_result
    end
  end
  0
end

#sub_commands(execution_context) ⇒ Array<Brut::CLI::Commands::BaseCommand>

Protocol for which commands to run. You can override this to dynamically set which commands are run based on the parsed command line. Note that you must call methods like ‘puts` or `system!` **on the passed `execution_context`**. Do not call them directly.

Parameters:

  • execution_context (Brut::CLI::ExecutionContext)

    the execution context for this command. You can use this to examine e.g. CLI options to determine which commands to return.

Returns:



42
# File 'lib/brut/cli/commands/compound_command.rb', line 42

def sub_commands(execution_context) = @commands