Class: Runfile::Runner
- Inherits:
-
Object
- Object
- Runfile::Runner
- Includes:
- SettingsMixin, Singleton
- Defined in:
- lib/runfile/runner.rb
Overview
The Runner class is the main workhorse behind Runfile. It handles all the Runfile DSL commands and executes the Runfile with the help of two more specialized classes:
-
DocoptHelper - for deeper docopt related actions
-
RunfileHelper - for Runfile creation and system wide search
Instance Attribute Summary collapse
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#env_vars ⇒ Object
Returns the value of attribute env_vars.
-
#examples ⇒ Object
Returns the value of attribute examples.
-
#last_help ⇒ Object
Returns the value of attribute last_help.
-
#last_usage ⇒ Object
Returns the value of attribute last_usage.
-
#name ⇒ Object
Returns the value of attribute name.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#summary ⇒ Object
Returns the value of attribute summary.
-
#superspace ⇒ Object
Returns the value of attribute superspace.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#add_action(name, altname = nil, &block) ⇒ Object
Add an action to the @actions array, and use the last known usage and help messages sent by the DSL.
-
#add_env_var(name, text, scope = nil) ⇒ Object
Add env_var command.
-
#add_example(command) ⇒ Object
Add example command.
-
#add_option(flag, text, scope = nil) ⇒ Object
Add an option flag and its help text.
-
#add_param(name, text, scope = nil) ⇒ Object
Add a patameter and its help text.
-
#cross_call(command_string) ⇒ Object
Invoke action from another action.
-
#execute(argv, filename = 'Runfile') ⇒ Object
Load and execute a Runfile call.
-
#initialize ⇒ Runner
constructor
Initialize all variables to sensible defaults.
-
#run(*argv) ⇒ Object
Run the command.
Methods included from SettingsMixin
Constructor Details
#initialize ⇒ Runner
Initialize all variables to sensible defaults.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/runfile/runner.rb', line 20 def initialize @superspace = nil # used when filename != Runfile @last_usage = nil # dsl: usage @last_help = nil # dsl: help @namespace = nil # dsl: command @actions = {} # dsl: action @options = {} # dsl: option @params = {} # dsl: param @examples = [] # dsl: example @env_vars = {} # dsl: env_var @name = "Runfile" # dsl: name @version = false # dsl: version @summary = false # dsl: summary end |
Instance Attribute Details
#actions ⇒ Object
Returns the value of attribute actions.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def actions @actions end |
#env_vars ⇒ Object
Returns the value of attribute env_vars.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def env_vars @env_vars end |
#examples ⇒ Object
Returns the value of attribute examples.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def examples @examples end |
#last_help ⇒ Object
Returns the value of attribute last_help.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def last_help @last_help end |
#last_usage ⇒ Object
Returns the value of attribute last_usage.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def last_usage @last_usage end |
#name ⇒ Object
Returns the value of attribute name.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def name @name end |
#namespace ⇒ Object
Returns the value of attribute namespace.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def namespace @namespace end |
#options ⇒ Object
Returns the value of attribute options.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def @options end |
#params ⇒ Object
Returns the value of attribute params.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def params @params end |
#summary ⇒ Object
Returns the value of attribute summary.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def summary @summary end |
#superspace ⇒ Object
Returns the value of attribute superspace.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def superspace @superspace end |
#version ⇒ Object
Returns the value of attribute version.
15 16 17 |
# File 'lib/runfile/runner.rb', line 15 def version @version end |
Instance Method Details
#add_action(name, altname = nil, &block) ⇒ Object
Add an action to the @actions array, and use the last known usage and help messages sent by the DSL.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/runfile/runner.rb', line 52 def add_action(name, altname = nil, &block) if @last_usage.nil? @last_usage = altname ? "(#{name}|#{altname})" : name end [@namespace, @superspace].each do |prefix| prefix or next name = "#{prefix}_#{name}" @last_usage = "#{prefix} #{last_usage}" unless @last_usage == false end name = name.to_sym @actions[name] = Action.new(block, @last_usage, @last_help) @last_usage = nil @last_help = nil if altname @last_usage = false add_action(altname, nil, &block) end end |
#add_env_var(name, text, scope = nil) ⇒ Object
Add env_var command.
86 87 88 89 90 |
# File 'lib/runfile/runner.rb', line 86 def add_env_var(name, text, scope = nil) scope ||= 'Environment Variables' @env_vars[scope] ||= {} @env_vars[scope][name] = text end |
#add_example(command) ⇒ Object
Add example command.
93 94 95 |
# File 'lib/runfile/runner.rb', line 93 def add_example(command) @examples << (@namespace ? "#{@namespace} #{command}" : command) end |
#add_option(flag, text, scope = nil) ⇒ Object
Add an option flag and its help text.
72 73 74 75 76 |
# File 'lib/runfile/runner.rb', line 72 def add_option(flag, text, scope = nil) scope ||= 'Options' @options[scope] ||= {} @options[scope][flag] = text end |
#add_param(name, text, scope = nil) ⇒ Object
Add a patameter and its help text.
79 80 81 82 83 |
# File 'lib/runfile/runner.rb', line 79 def add_param(name, text, scope = nil) scope ||= 'Parameters' @params[scope] ||= {} @params[scope][name] = text end |
#cross_call(command_string) ⇒ Object
Invoke action from another action. Used by the DSL’s #execute function. Expects to get a single string that looks as if it was typed in the command prompt.
112 113 114 115 116 117 118 119 120 |
# File 'lib/runfile/runner.rb', line 112 def cross_call(command_string) argv = command_string.split(/\s(?=(?:[^"]|"[^"]*")*$)/) begin docopt_exec argv rescue Docopt::Exit => ex puts "Cross call failed: #{command_string}" abort ex. end end |
#execute(argv, filename = 'Runfile') ⇒ Object
Load and execute a Runfile call.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/runfile/runner.rb', line 36 def execute(argv, filename='Runfile') @ignore_settings = !filename argv = argv filename and File.file?(filename) or handle_no_runfile argv begin load settings.helper if settings.helper load filename rescue => ex abort "Runfile error:\n#{ex.}\n#{ex.backtrace[0]}" end run(*argv) end |
#run(*argv) ⇒ Object
Run the command. This is a wrapper around docopt. It will generate the docopt document on the fly, using all the information collected so far.
100 101 102 103 104 105 106 107 |
# File 'lib/runfile/runner.rb', line 100 def run(*argv) begin docopt_exec argv rescue Docopt::Exit => ex puts ex. exit 2 end end |