Class: Excavator::Runner
- Inherits:
-
Object
- Object
- Excavator::Runner
- Defined in:
- lib/excavator/runner.rb
Overview
Public: Runner is the entry point into an Excavator script library. When running an Excavator script from the command line, a Runner object is created.
Runner duties:
- Load all Excavator commands.
- Provides the object that Excavator::DSL manipulates.
- Finds the command from the command line arguments and executes.
- Creates a useful help message to list all commands defined in the library.
Instance Attribute Summary collapse
-
#command_paths ⇒ Object
Public: An Array of filesystem paths where Excavator commands are found.
-
#current_namespace ⇒ Object
readonly
Public: A pointer to the current namespace being worked on.
Instance Method Summary collapse
-
#clear_last_command! ⇒ Object
Public: Clear the last command being worked on.
-
#cwd ⇒ Object
Public: Return the current working directory for the script.
-
#display_help ⇒ Object
Internal: Display the list of all command names and their descriptions.
-
#find_command(cmd) ⇒ Object
Public: Find a command given it's full name.
-
#in_namespace(name) ⇒ Object
Public: A helper to move the current_namespace pointer to new namespace, yield, and then return the pointer back to the previous namespace.
-
#initialize ⇒ Runner
constructor
A new instance of Runner.
-
#last_command ⇒ Object
Public: The last command being worked on.
-
#namespace ⇒ Object
Internal: The root namespace for all other commands and namespaces.
-
#run(args = ARGV) ⇒ Object
Public: The entry point into all commands.
Constructor Details
Instance Attribute Details
#command_paths ⇒ Object
Public: An Array of filesystem paths where Excavator commands are found. Defaults to the current directory's "command/" directory.
43 44 45 |
# File 'lib/excavator/runner.rb', line 43 def command_paths @command_paths = Excavator.command_paths || [cwd.join("commands")] end |
#current_namespace ⇒ Object (readonly)
Public: A pointer to the current namespace being worked on.
24 25 26 |
# File 'lib/excavator/runner.rb', line 24 def current_namespace @current_namespace end |
Instance Method Details
#clear_last_command! ⇒ Object
Public: Clear the last command being worked on. This is designed for use with Excavator::DSL methods.
122 123 124 |
# File 'lib/excavator/runner.rb', line 122 def clear_last_command! @last_command = nil end |
#cwd ⇒ Object
Public: Return the current working directory for the script.
32 33 34 |
# File 'lib/excavator/runner.rb', line 32 def cwd Excavator.cwd end |
#display_help ⇒ Object
Internal: Display the list of all command names and their descriptions.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/excavator/runner.rb', line 127 def display_help table_view = Excavator::TableView.new do |t| t.title "#{File.basename($0)} commands:\n" t.header "Command" t.header "Description" t.divider "\t" namespace.commands_and_descriptions.sort.each do |command| t.record *command end end puts table_view end |
#find_command(cmd) ⇒ Object
Public: Find a command given it's full name.
cmd - A String/Symbol full name of the Command to look up. (e.g., "server:test:integration")
Returns a Command.
102 103 104 105 106 107 108 109 110 |
# File 'lib/excavator/runner.rb', line 102 def find_command(cmd) *namespaces, command_name = cmd.to_s.split(':').collect {|c| c.to_sym } cur_namespace = current_namespace namespaces.each do |n| cur_namespace = cur_namespace.namespace(n) end cur_namespace.command(command_name) end |
#in_namespace(name) ⇒ Object
Public: A helper to move the current_namespace pointer to new namespace, yield, and then return the pointer back to the previous namespace. If the namespace argument already exists (e.g., created by another file), then this points to the previously created namespace.
name - A String/Symbol of the namespace to move the current_namespace pointer to.
Examples
runner = Runner.new
runner.in_namespace("test") do
runner.current_namespace
# => <Namespace "test">
end
Returns nothing.
64 65 66 67 68 69 70 71 72 |
# File 'lib/excavator/runner.rb', line 64 def in_namespace(name) ns = @current_namespace.namespace(name) || Excavator.namespace_class.new(name) @current_namespace << ns @current_namespace = ns yield @current_namespace = ns.parent end |
#last_command ⇒ Object
Public: The last command being worked on. This is designed for use with Excavator::DSL methods.
114 115 116 117 118 |
# File 'lib/excavator/runner.rb', line 114 def last_command @last_command ||= Excavator.command_class.new( self, :namespace => current_namespace ) end |
#namespace ⇒ Object
Internal: The root namespace for all other commands and namespaces.
143 144 145 |
# File 'lib/excavator/runner.rb', line 143 def namespace @namespace end |
#run(args = ARGV) ⇒ Object
Public: The entry point into all commands. This will load all commands from any external Excavator files, find the command from the command line, and execute the command.
This method is never called directly. See Excavator.run.
Returns value returned from the Command.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/excavator/runner.rb', line 81 def run(args = ARGV) args.flatten! name = args.delete_at(0) load_commands if (name.nil? && args.size == 0) || display_help?(name) display_help return end command = find_command name command.execute *args end |