forked from ruby/rake
/
config_task.rb
255 lines (232 loc) · 6.85 KB
/
config_task.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
require 'rake/task.rb'
require 'optparse'
module Rake
# #########################################################################
# = ConfigTask
#
# Config tasks allow the creation of tasks that can recieve simple
# configurations using command line options. The intention is to allow
# command-like tasks to be defined and used in a natural way (ie using a 'name
# --flag arg' syntax). For clarity ConfigTasks are declared using 'tasc' and
# referred to as 'tascs'.
#
# Tasc configs are declared using an options string and accessed on the tasc
# itself. Numeric types are cast to appropriate values.
#
# require 'rake'
#
# desc "welcome a thing"
# tasc :welcome, :thing, %{
# -m,--message [hello] : A welcome message
# -n [1] : Number of times to repeat
# } do |config, args|
# config.n.times do
# puts "#{config.message} #{args.thing}"
# end
# end
#
# Then from the command line, invoke after '--':
#
# % rake -- welcome world
# hello world
# % rake -- welcome --message goodnight -n 3 moon
# goodnight moon
# goodnight moon
# goodnight moon
# % rake -- welcome --help
# Usage: rake -- welcome [options] object
# -m, --message [hello] A welcome message
# -n [1] Number of times to repeat
# -h, --help Display this help message.
#
# Unlike typical tasks which only run once, tascs are reset after each run, so
# that they can be invoked multiple times:
#
# % rake -- welcome world -- welcome moon -m goodnight
# hello world
# goodnight moon
#
# Tascs may participate in dependency workflows, although it gets a little
# peculiar when other tasks depend upon the tasc. Below is an explanation.
# TL;DR; -- tascs may have dependencies, but other tasks/tascs should not depend
# upon a tasc.
#
# == Dependency Discussion
#
# Normally tasks are designed to be unaware of context (for lack of a better
# word). Once you introduce arguments/configs, then suddenly it matters when the
# arguments/configs 'get to' a task. For example:
#
# require 'rake'
#
# task(:a) { puts 'a' }
# task(:b => :a) { puts 'b' }
#
# tasc(:x, [:letter, 'x']) {|t| puts t.letter }
# tasc(:y, [:letter, 'y'], :needs => :x) {|t| puts t.letter }
#
# There is no order issue for the tasks, for which there is no context and
# therefore they can align into a one-true execution order regardless of
# declaration order.
#
# % rake a b
# a
# b
# % rake b a
# a
# b
#
# A problem arises, however with tascs that do have a context. Now it matters
# what order things get declared in. For example:
#
# % rake -- x --letter a -- y --letter b
# a
# a
# b
# % rake -- y --letter b -- x --letter a
# x
# b
# a
#
# You can see that declaration order matters for tascs in a way it does not for
# tasks. The problem is not caused directly by the decision to make tascs run
# multiple times; it's caused by the context which gets interwoven to all
# tasks/tascs via dependencies. For example, pretend tascs only executed once...
# which arguments/configurations should win in this case?
#
# % rake -- welcome world -- welcome -m goodnight
# # hello world ?
# # goodnight ?
# # goodnight world ?
#
# All of this can be avoided by only using tascs as end-points for dependency
# workflows and never as prerequisites. This is fine:
#
# require 'rake'
#
# task(:a) { print 'a' }
# task(:b => :a) { print 'b' }
# tasc(:x, [:letter, 'x'], :needs => [:b, :a]) {|t| puts t.letter }
#
# Now:
#
# % rake -- x --letter c
# abc
# % rake a b -- x --letter c
# abc
# % rake b a -- x --letter c
# abc
#
class ConfigTask < Task
def parser
@parser ||= OptionParser.new do |opts|
opts.on_tail("-h", "--help", "Display this help message.") do
puts opts
exit
end
end
end
def invoke(*args)
parser.parse!(args)
super(*args)
end
def invoke_with_call_chain(*args)
super
reenable
end
def reenable
@config = nil
super
end
def config
@configs ||= default_config.dup
end
def default_config
@default_config ||= {}
end
def [](key)
config[key.to_sym]
end
def method_missing(sym, *args, &block)
sym = sym.to_sym
config.has_key?(sym) ? config[sym] : super
end
def set_arg_names(args)
while options = parse_options(args.last)
set_options(options)
args.pop
end
@arg_names = args.map { |a| a.to_sym }
parser.banner = "Usage: rake -- #{name} [options] #{@arg_names.join(' ')}"
@arg_names
end
def parse_options(obj)
case obj
when Array then [obj]
when String then parse_options_string(obj)
else nil
end
end
def parse_options_string(string)
string = string.strip
return nil unless string[0] == ?-
string.split(/\s*\n\s*/).collect do |str|
flags, desc = str.split(':', 2)
flags = flags.split(',').collect! {|arg| arg.strip }
key = guess_key(flags)
default = flags.last =~ /\s+\[(.*)\]/ ? guess_default($1) : guess_bool_default(flags)
[key, default] + flags + [desc.to_s.strip]
end
end
def guess_key(flags)
keys = flags.collect do |flag|
case flag.split(' ').first
when /\A-([^-])\z/ then $1
when /\A--\[no-\](.*)\z/ then $1
when /\A--(.*)\z/ then $1
else nil
end
end
keys.compact.sort_by {|key| key.length }.last
end
def guess_default(str)
case str
when /\A(\d+)\z/ then str.to_i
when /\A(\d+\.\d+)\z/ then str.to_f
else str
end
end
def guess_bool_default(flags)
flags.any? {|flag| flag =~ /\A--\[no-\]/ ? true : false }
end
def set_options(options)
options.each do |(key, default, *option)|
default = false if default.nil?
option = guess_option(key, default) if option.empty?
default_config[key.to_sym] = default
parser.on(*option) do |value|
config[key.to_sym] = parse_config_value(default, value)
end
end
end
def guess_option(key, default)
n = key.to_s.length
case default
when false
n == 1 ? ["-#{key}"] : ["--#{key}"]
when true
["--[no-]#{key}"]
else
n == 1 ? ["-#{key} [#{key.to_s.upcase}]"] : ["--#{key} [#{key.to_s.upcase}]"]
end
end
def parse_config_value(default, value)
case default
when String then value.to_s
when Integer then value.to_i
when Float then value.to_f
else value
end
end
end
end