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.

Constant Summary collapse

@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Runner

Initialize all variables to sensible defaults.



14
15
16
17
18
19
20
21
22
23
# File 'lib/runfile/runner.rb', line 14

def initialize
	@last_usage = nil  # dsl: usage
	@last_help  = nil  # dsl: help
	@superspace = nil  # used when filename != Runfile
	@namespace  = nil  # dsl: #command
	@actions = {}      # dsl: action
	@options = {}      # dsl: option
	@version = "0.0.0" # dsl: version
	@summary = false   # dsl: summary
end

Instance Attribute Details

#last_help ⇒ Object

Returns the value of attribute last_help.



9
10
11
# File 'lib/runfile/runner.rb', line 9

def last_help
  @last_help
end

#last_usage ⇒ Object

Returns the value of attribute last_usage.



9
10
11
# File 'lib/runfile/runner.rb', line 9

def last_usage
  @last_usage
end

#namespace ⇒ Object

Returns the value of attribute namespace.



9
10
11
# File 'lib/runfile/runner.rb', line 9

def namespace
  @namespace
end

#summary ⇒ Object

Returns the value of attribute summary.



9
10
11
# File 'lib/runfile/runner.rb', line 9

def summary
  @summary
end

#superspace ⇒ Object

Returns the value of attribute superspace.



9
10
11
# File 'lib/runfile/runner.rb', line 9

def superspace
  @superspace
end

#version ⇒ Object

Returns the value of attribute version.



9
10
11
# File 'lib/runfile/runner.rb', line 9

def version
  @version
end

Class Method Details

.instance ⇒ Object

Return a singleton Runner instance.



26
27
28
29
# File 'lib/runfile/runner.rb', line 26

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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/runfile/runner.rb', line 44

def add_action(name, &block)
	@last_usage = name if @last_usage.nil?
	if @namespace 
		name = "#{namespace}_#{name}".to_sym
		@last_usage = "#{@namespace} #{@last_usage}"
	end
	if @superspace 
		name = "#{superspace}_#{name}".to_sym
		@last_usage = "#{@superspace} #{@last_usage}"
	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.



60
61
62
# File 'lib/runfile/runner.rb', line 60

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.



78
79
80
81
82
83
84
85
86
# File 'lib/runfile/runner.rb', line 78

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

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

Load and execute a Runfile call.



32
33
34
35
36
37
38
39
40
# File 'lib/runfile/runner.rb', line 32

def execute(argv, filename='Runfile')
	File.file? filename or handle_no_runfile argv
	begin
		load filename
	rescue
		abort "Failed loading file '#{filename}'.\nIs it a valid Runfile?"
	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.



67
68
69
70
71
72
73
# File 'lib/runfile/runner.rb', line 67

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