runfile 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/runfile/dsl.rb +4 -1
- data/lib/runfile/runner.rb +42 -28
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 558258a98c5a64125802ab9612acd60906bfd069
|
4
|
+
data.tar.gz: d787f718632c0e16df8d176938a30a54e668d5c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01ff19b161a0ab6492e87a5c432b2ca76de79802fa5356fb2103879e4ed67d11494d926f8343260414a3c51b56ae0c665ff7c423ea93377bc93e1519e68d0b84
|
7
|
+
data.tar.gz: 46ed488ad6b113c67223240d751a83409a7edb70950845cd1a34dd27133d5145ab74bf032b7a92da9e122658d51f7e8675d01af2e66a9c914713c22f80ab4445
|
data/lib/runfile/dsl.rb
CHANGED
@@ -3,7 +3,6 @@
|
|
3
3
|
# for handling.
|
4
4
|
|
5
5
|
module Runfile
|
6
|
-
|
7
6
|
# Set the version of your Runfile program
|
8
7
|
def version(ver)
|
9
8
|
Runner.instance.version = ver
|
@@ -44,4 +43,8 @@ module Runfile
|
|
44
43
|
def call(command_string)
|
45
44
|
Runner.instance.cross_call command_string
|
46
45
|
end
|
46
|
+
|
47
|
+
# Also allow to use 'endcommand' instead of 'command' to end
|
48
|
+
# a command namespace definition
|
49
|
+
alias_method :endcommand, :command
|
47
50
|
end
|
data/lib/runfile/runner.rb
CHANGED
@@ -1,23 +1,25 @@
|
|
1
1
|
require 'docopt'
|
2
|
+
require 'pp'
|
2
3
|
|
3
4
|
module Runfile
|
4
5
|
|
5
6
|
# The Runner class is the main workhorse behind Runfile.
|
6
7
|
# It handles all the Runfile DSL commands and executes the Runfile.
|
7
8
|
class Runner
|
8
|
-
attr_accessor :last_usage, :last_help, :version, :summary, :namespace
|
9
|
+
attr_accessor :last_usage, :last_help, :version, :summary, :namespace, :superspace
|
9
10
|
|
10
11
|
@@instance = nil
|
11
12
|
|
12
13
|
# Initialize all variables to sensible defaults.
|
13
14
|
def initialize
|
14
|
-
@last_usage = nil
|
15
|
-
@last_help
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@
|
20
|
-
@
|
15
|
+
@last_usage = nil # dsl: usage
|
16
|
+
@last_help = nil # dsl: help
|
17
|
+
@superspace = nil # used when filename != Runfile
|
18
|
+
@namespace = nil # dsl: #command
|
19
|
+
@actions = {} # dsl: action
|
20
|
+
@options = {} # dsl: option
|
21
|
+
@version = "0.0.0" # dsl: version
|
22
|
+
@summary = false # dsl: summary
|
21
23
|
end
|
22
24
|
|
23
25
|
# Return a singleton Runner instance.
|
@@ -27,9 +29,9 @@ module Runfile
|
|
27
29
|
end
|
28
30
|
|
29
31
|
# Load and execute a Runfile call.
|
30
|
-
def execute(argv)
|
31
|
-
File.exist?
|
32
|
-
load
|
32
|
+
def execute(argv, filename='Runfile')
|
33
|
+
File.exist? filename or handle_no_runfile argv
|
34
|
+
load filename
|
33
35
|
@@instance.run *argv
|
34
36
|
end
|
35
37
|
|
@@ -41,6 +43,10 @@ module Runfile
|
|
41
43
|
name = "#{namespace}_#{name}".to_sym
|
42
44
|
@last_usage = "#{@namespace} #{@last_usage}"
|
43
45
|
end
|
46
|
+
if @superspace
|
47
|
+
name = "#{superspace}_#{name}".to_sym
|
48
|
+
@last_usage = "#{@superspace} #{@last_usage}"
|
49
|
+
end
|
44
50
|
@actions[name] = Action.new(block, @last_usage, @last_help)
|
45
51
|
@last_usage = nil
|
46
52
|
@last_help = nil
|
@@ -55,9 +61,8 @@ module Runfile
|
|
55
61
|
# generate the docopt document on the fly, using all the
|
56
62
|
# information collected so far.
|
57
63
|
def run(*argv)
|
58
|
-
action = find_action argv
|
59
64
|
begin
|
60
|
-
docopt_exec
|
65
|
+
docopt_exec argv
|
61
66
|
rescue Docopt::Exit => e
|
62
67
|
puts e.message
|
63
68
|
end
|
@@ -68,9 +73,8 @@ module Runfile
|
|
68
73
|
# it was typed in the command prompt.
|
69
74
|
def cross_call(command_string)
|
70
75
|
argv = command_string.split /\s(?=(?:[^"]|"[^"]*")*$)/
|
71
|
-
action = find_action argv
|
72
76
|
begin
|
73
|
-
docopt_exec
|
77
|
+
docopt_exec argv
|
74
78
|
rescue Docopt::Exit => e
|
75
79
|
puts "Cross call failed: #{command_string}"
|
76
80
|
abort e.message
|
@@ -89,24 +93,23 @@ module Runfile
|
|
89
93
|
# parsed arguments.
|
90
94
|
# This should always be called in a begin...rescue block and
|
91
95
|
# you should handle the Docopt::Exit exception.
|
92
|
-
def docopt_exec(
|
96
|
+
def docopt_exec(argv)
|
93
97
|
args = Docopt::docopt(docopt, version: @version, argv:argv)
|
98
|
+
action = find_action argv
|
94
99
|
action or abort "Runfile error: Action not found"
|
95
100
|
@actions[action].execute args
|
96
101
|
end
|
97
102
|
|
98
|
-
# Inspect the first
|
99
|
-
# a matching action
|
100
|
-
# We
|
101
|
-
#
|
102
|
-
# (e.g. also allow a global action
|
103
|
+
# Inspect the first three arguments in the argv and look for
|
104
|
+
# a matching action.
|
105
|
+
# We will first look for a_b_c action, then for a_b and
|
106
|
+
# finally for a. This is intended to allow "overloading" of
|
107
|
+
# the command as an action (e.g. also allow a global action
|
108
|
+
# called 'a').
|
103
109
|
def find_action(argv)
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
end
|
108
|
-
if argv.size >= 1
|
109
|
-
action = argv[0].to_sym
|
110
|
+
3.downto(1).each do |n|
|
111
|
+
next unless argv.size >= n
|
112
|
+
action = argv[0..n-1].join('_').to_sym
|
110
113
|
return action if @actions.has_key? action
|
111
114
|
end
|
112
115
|
return false
|
@@ -115,15 +118,26 @@ module Runfile
|
|
115
118
|
# Handle the case when `run` is called without a Runfile
|
116
119
|
# present. We will let the user know they can type `run make`
|
117
120
|
# to create a new sample Runfile.
|
121
|
+
# If the first argument matches the name of a *.runfile name,
|
122
|
+
# we will execute it.
|
118
123
|
def handle_no_runfile(argv)
|
119
124
|
if argv[0] == "make"
|
120
125
|
sample = File.dirname(__FILE__) + "/../../examples/template/Runfile"
|
121
126
|
dest = Dir.pwd + "/Runfile"
|
122
127
|
File.write(dest, File.read(sample))
|
123
128
|
abort "Runfile created."
|
129
|
+
elsif argv[0] and File.exist? "#{argv[0]}.runfile"
|
130
|
+
@superspace = argv[0]
|
131
|
+
execute argv, "#{argv[0]}.runfile"
|
124
132
|
else
|
125
|
-
|
133
|
+
runfiles = Dir['*.runfile']
|
134
|
+
runfiles.empty? and abort "Runfile not found.\nUse 'run make' to create one."
|
135
|
+
runfiles.each do |f|
|
136
|
+
f.slice! '.runfile'
|
137
|
+
puts "Did you mean 'run #{f}'"
|
138
|
+
end
|
126
139
|
end
|
140
|
+
exit
|
127
141
|
end
|
128
142
|
end
|
129
143
|
end
|