Class: Runfile::Runner
- Inherits:
-
Object
- Object
- Runfile::Runner
- 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
Constant Summary collapse
- @@instance =
nil
Instance Attribute Summary collapse
-
#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.
-
#summary ⇒ Object
Returns the value of attribute summary.
-
#superspace ⇒ Object
Returns the value of attribute superspace.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
-
.instance ⇒ Object
Return a singleton Runner instance.
Instance Method Summary collapse
-
#add_action(name, &block) ⇒ Object
Add an action to the @actions array, and use the last known usage and help messages sent by the DSL.
-
#add_option(flag, text) ⇒ Object
Add an option flag 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.
Constructor Details
#initialize ⇒ Runner
Initialize all variables to sensible defaults.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/runfile/runner.rb', line 18 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 @name = "Runfile" # dsl: name @version = "0.0.0" # dsl: version @summary = false # dsl: summary end |
Instance Attribute Details
#last_help ⇒ Object
Returns the value of attribute last_help.
12 13 14 |
# File 'lib/runfile/runner.rb', line 12 def last_help @last_help end |
#last_usage ⇒ Object
Returns the value of attribute last_usage.
12 13 14 |
# File 'lib/runfile/runner.rb', line 12 def last_usage @last_usage end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/runfile/runner.rb', line 12 def name @name end |
#namespace ⇒ Object
Returns the value of attribute namespace.
12 13 14 |
# File 'lib/runfile/runner.rb', line 12 def namespace @namespace end |
#summary ⇒ Object
Returns the value of attribute summary.
12 13 14 |
# File 'lib/runfile/runner.rb', line 12 def summary @summary end |
#superspace ⇒ Object
Returns the value of attribute superspace.
12 13 14 |
# File 'lib/runfile/runner.rb', line 12 def superspace @superspace end |
#version ⇒ Object
Returns the value of attribute version.
12 13 14 |
# File 'lib/runfile/runner.rb', line 12 def version @version end |
Class Method Details
.instance ⇒ Object
Return a singleton Runner instance.
31 32 33 34 |
# File 'lib/runfile/runner.rb', line 31 def self.instance @@instance = self.new if @@instance.nil? @@instance end |
Instance Method Details
#add_action(name, &block) ⇒ Object
Add an action to the @actions array, and use the last known usage and help messages sent by the DSL.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/runfile/runner.rb', line 49 def add_action(name, &block) @last_usage = name if @last_usage.nil? if @namespace name = "#{namespace}_#{name}".to_sym @last_usage = "#{@namespace} #{@last_usage}" unless @last_usage == false end if @superspace name = "#{superspace}_#{name}".to_sym @last_usage = "#{@superspace} #{@last_usage}" unless @last_usage == false end @actions[name] = Action.new(block, @last_usage, @last_help) @last_usage = nil @last_help = nil end |
#add_option(flag, text) ⇒ Object
Add an option flag and its help text.
65 66 67 |
# File 'lib/runfile/runner.rb', line 65 def add_option(flag, text) @options[flag] = text end |
#cross_call(command_string) ⇒ Object
Invoke action from another action. Used by the DSL’s #call function. Expects to get a single string that looks as if it was typed in the command prompt.
83 84 85 86 87 88 89 90 91 |
# File 'lib/runfile/runner.rb', line 83 def cross_call(command_string) argv = command_string.split /\s(?=(?:[^"]|"[^"]*")*$)/ begin docopt_exec argv rescue Docopt::Exit => e puts "Cross call failed: #{command_string}" abort e. end end |
#execute(argv, filename = 'Runfile') ⇒ Object
Load and execute a Runfile call.
37 38 39 40 41 42 43 44 45 |
# File 'lib/runfile/runner.rb', line 37 def execute(argv, filename='Runfile') File.file?(filename) or handle_no_runfile argv begin load filename rescue => e abort "Runfile error:\n#{e.}" end @@instance.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.
72 73 74 75 76 77 78 |
# File 'lib/runfile/runner.rb', line 72 def run(*argv) begin docopt_exec argv rescue Docopt::Exit => e puts e. end end |