Class: Runfile::Runner

Inherits:
Object
  • Object
show all
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:

  1. DocoptHelper - for deeper docopt related actions

  2. RunfileHelper - for Runfile creation and system wide search

Constant Summary collapse

@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Initialize all variables to sensible defaults.



18
19
20
21
22
23
24
25
26
27
28
29
# 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
  @examples = []        # dsl: example
  @name     = "Runfile" # dsl: name
  @version  = false     # dsl: version
  @summary  = false     # dsl: summary
end

Instance Attribute Details

#last_helpObject

Returns the value of attribute last_help.



12
13
14
# File 'lib/runfile/runner.rb', line 12

def last_help
  @last_help
end

#last_usageObject

Returns the value of attribute last_usage.



12
13
14
# File 'lib/runfile/runner.rb', line 12

def last_usage
  @last_usage
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/runfile/runner.rb', line 12

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



12
13
14
# File 'lib/runfile/runner.rb', line 12

def namespace
  @namespace
end

#summaryObject

Returns the value of attribute summary.



12
13
14
# File 'lib/runfile/runner.rb', line 12

def summary
  @summary
end

#superspaceObject

Returns the value of attribute superspace.



12
13
14
# File 'lib/runfile/runner.rb', line 12

def superspace
  @superspace
end

#versionObject

Returns the value of attribute version.



12
13
14
# File 'lib/runfile/runner.rb', line 12

def version
  @version
end

Class Method Details

.instanceObject

Return a singleton Runner instance.



32
33
34
35
# File 'lib/runfile/runner.rb', line 32

def self.instance
  @@instance = self.new if @@instance.nil?
  @@instance
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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/runfile/runner.rb', line 50

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_example(command) ⇒ Object

Add example command.



77
78
79
# File 'lib/runfile/runner.rb', line 77

def add_example(command)
  @examples << command
end

#add_option(flag, text, scope = nil) ⇒ Object

Add an option flag and its help text.



70
71
72
73
74
# File 'lib/runfile/runner.rb', line 70

def add_option(flag, text, scope=nil)
  scope or scope = 'Options'
  @options[scope] ||= {}
  @options[scope][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.



95
96
97
98
99
100
101
102
103
# File 'lib/runfile/runner.rb', line 95

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.message
  end
end

#execute(argv, filename = 'Runfile') ⇒ Object

Load and execute a Runfile call.



38
39
40
41
42
43
44
45
46
# File 'lib/runfile/runner.rb', line 38

def execute(argv, filename='Runfile')
  filename and File.file?(filename) or handle_no_runfile argv
  begin
    load filename
  rescue => ex
    abort "Runfile error:\n#{ex.message}\n#{ex.backtrace[0]}"
  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.



84
85
86
87
88
89
90
# File 'lib/runfile/runner.rb', line 84

def run(*argv)
  begin
    docopt_exec argv
  rescue Docopt::Exit => ex
    puts ex.message
  end
end