wijet-thor 0.14.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +103 -0
- data/LICENSE +20 -0
- data/README.md +307 -0
- data/Thorfile +24 -0
- data/bin/rake2thor +86 -0
- data/bin/thor +6 -0
- data/lib/thor.rb +334 -0
- data/lib/thor/actions.rb +314 -0
- data/lib/thor/actions/create_file.rb +105 -0
- data/lib/thor/actions/create_link.rb +57 -0
- data/lib/thor/actions/directory.rb +93 -0
- data/lib/thor/actions/empty_directory.rb +134 -0
- data/lib/thor/actions/file_manipulation.rb +270 -0
- data/lib/thor/actions/inject_into_file.rb +109 -0
- data/lib/thor/base.rb +579 -0
- data/lib/thor/core_ext/file_binary_read.rb +9 -0
- data/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
- data/lib/thor/core_ext/ordered_hash.rb +100 -0
- data/lib/thor/error.rb +30 -0
- data/lib/thor/group.rb +273 -0
- data/lib/thor/invocation.rb +168 -0
- data/lib/thor/parser.rb +4 -0
- data/lib/thor/parser/argument.rb +67 -0
- data/lib/thor/parser/arguments.rb +161 -0
- data/lib/thor/parser/option.rb +120 -0
- data/lib/thor/parser/options.rb +173 -0
- data/lib/thor/rake_compat.rb +66 -0
- data/lib/thor/runner.rb +309 -0
- data/lib/thor/shell.rb +88 -0
- data/lib/thor/shell/basic.rb +290 -0
- data/lib/thor/shell/color.rb +108 -0
- data/lib/thor/shell/html.rb +121 -0
- data/lib/thor/task.rb +114 -0
- data/lib/thor/util.rb +229 -0
- data/lib/thor/version.rb +3 -0
- data/spec/actions/create_file_spec.rb +170 -0
- data/spec/actions/directory_spec.rb +136 -0
- data/spec/actions/empty_directory_spec.rb +98 -0
- data/spec/actions/file_manipulation_spec.rb +310 -0
- data/spec/actions/inject_into_file_spec.rb +135 -0
- data/spec/actions_spec.rb +322 -0
- data/spec/base_spec.rb +269 -0
- data/spec/core_ext/hash_with_indifferent_access_spec.rb +43 -0
- data/spec/core_ext/ordered_hash_spec.rb +115 -0
- data/spec/fixtures/application.rb +2 -0
- data/spec/fixtures/bundle/execute.rb +6 -0
- data/spec/fixtures/bundle/main.thor +1 -0
- data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
- data/spec/fixtures/doc/README +3 -0
- data/spec/fixtures/doc/block_helper.rb +3 -0
- data/spec/fixtures/doc/components/.empty_directory +0 -0
- data/spec/fixtures/doc/config.rb +1 -0
- data/spec/fixtures/group.thor +114 -0
- data/spec/fixtures/invoke.thor +112 -0
- data/spec/fixtures/path with spaces b/data/spec/fixtures/path with → spaces +0 -0
- data/spec/fixtures/script.thor +184 -0
- data/spec/fixtures/task.thor +10 -0
- data/spec/group_spec.rb +178 -0
- data/spec/invocation_spec.rb +100 -0
- data/spec/parser/argument_spec.rb +47 -0
- data/spec/parser/arguments_spec.rb +64 -0
- data/spec/parser/option_spec.rb +202 -0
- data/spec/parser/options_spec.rb +319 -0
- data/spec/rake_compat_spec.rb +68 -0
- data/spec/register_spec.rb +104 -0
- data/spec/runner_spec.rb +210 -0
- data/spec/shell/basic_spec.rb +223 -0
- data/spec/shell/color_spec.rb +41 -0
- data/spec/shell/html_spec.rb +27 -0
- data/spec/shell_spec.rb +47 -0
- data/spec/spec_helper.rb +54 -0
- data/spec/task_spec.rb +74 -0
- data/spec/thor_spec.rb +334 -0
- data/spec/util_spec.rb +163 -0
- metadata +193 -0
data/bin/rake2thor
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'ruby2ruby'
|
4
|
+
require 'parse_tree'
|
5
|
+
if Ruby2Ruby::VERSION >= "1.2.0"
|
6
|
+
require 'parse_tree_extensions'
|
7
|
+
end
|
8
|
+
require 'rake'
|
9
|
+
|
10
|
+
input = ARGV[0] || 'Rakefile'
|
11
|
+
output = ARGV[1] || 'Thorfile'
|
12
|
+
|
13
|
+
$requires = []
|
14
|
+
|
15
|
+
module Kernel
|
16
|
+
def require_with_record(file)
|
17
|
+
$requires << file if caller[1] =~ /rake2thor:/
|
18
|
+
require_without_record file
|
19
|
+
end
|
20
|
+
alias_method :require_without_record, :require
|
21
|
+
alias_method :require, :require_with_record
|
22
|
+
end
|
23
|
+
|
24
|
+
load input
|
25
|
+
|
26
|
+
@private_methods = []
|
27
|
+
|
28
|
+
def file_task_name(name)
|
29
|
+
"compile_" + name.gsub('/', '_slash_').gsub('.', '_dot_').gsub(/\W/, '_')
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_for_task(task)
|
33
|
+
file_task = task.is_a?(Rake::FileTask)
|
34
|
+
comment = task.instance_variable_get('@comment')
|
35
|
+
prereqs = task.instance_variable_get('@prerequisites').select(&Rake::Task.method(:task_defined?))
|
36
|
+
actions = task.instance_variable_get('@actions')
|
37
|
+
name = task.name.gsub(/^([^:]+:)+/, '')
|
38
|
+
name = file_task_name(name) if file_task
|
39
|
+
meth = ''
|
40
|
+
|
41
|
+
meth << "desc #{name.inspect}, #{comment.inspect}\n" if comment
|
42
|
+
meth << "def #{name}\n"
|
43
|
+
|
44
|
+
meth << prereqs.map do |pre|
|
45
|
+
pre = pre.to_s
|
46
|
+
pre = file_task_name(pre) if Rake::Task[pre].is_a?(Rake::FileTask)
|
47
|
+
' ' + pre
|
48
|
+
end.join("\n")
|
49
|
+
|
50
|
+
meth << "\n\n" unless prereqs.empty? || actions.empty?
|
51
|
+
|
52
|
+
meth << actions.map do |act|
|
53
|
+
act = act.to_ruby
|
54
|
+
unless act.gsub!(/^proc \{ \|(\w+)\|\n/,
|
55
|
+
" \\1 = Struct.new(:name).new(#{name.inspect}) # A crude mock Rake::Task object\n")
|
56
|
+
act.gsub!(/^proc \{\n/, '')
|
57
|
+
end
|
58
|
+
act.gsub(/\n\}$/, '')
|
59
|
+
end.join("\n")
|
60
|
+
|
61
|
+
meth << "\nend"
|
62
|
+
|
63
|
+
if file_task
|
64
|
+
@private_methods << meth
|
65
|
+
return
|
66
|
+
end
|
67
|
+
|
68
|
+
meth
|
69
|
+
end
|
70
|
+
|
71
|
+
body = Rake::Task.tasks.map(&method(:method_for_task)).compact.map { |meth| meth.gsub(/^/, ' ') }.join("\n\n")
|
72
|
+
|
73
|
+
unless @private_methods.empty?
|
74
|
+
body << "\n\n private\n\n"
|
75
|
+
body << @private_methods.map { |meth| meth.gsub(/^/, ' ') }.join("\n\n")
|
76
|
+
end
|
77
|
+
|
78
|
+
requires = $requires.map { |r| "require #{r.inspect}" }.join("\n")
|
79
|
+
|
80
|
+
File.open(output, 'w') { |f| f.write(<<END.lstrip) }
|
81
|
+
#{requires}
|
82
|
+
|
83
|
+
class Default < Thor
|
84
|
+
#{body}
|
85
|
+
end
|
86
|
+
END
|
data/bin/thor
ADDED
data/lib/thor.rb
ADDED
@@ -0,0 +1,334 @@
|
|
1
|
+
require 'thor/base'
|
2
|
+
|
3
|
+
class Thor
|
4
|
+
class << self
|
5
|
+
# Sets the default task when thor is executed without an explicit task to be called.
|
6
|
+
#
|
7
|
+
# ==== Parameters
|
8
|
+
# meth<Symbol>:: name of the defaut task
|
9
|
+
#
|
10
|
+
def default_task(meth=nil)
|
11
|
+
case meth
|
12
|
+
when :none
|
13
|
+
@default_task = 'help'
|
14
|
+
when nil
|
15
|
+
@default_task ||= from_superclass(:default_task, 'help')
|
16
|
+
else
|
17
|
+
@default_task = meth.to_s
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Registers another Thor subclass as a command.
|
22
|
+
#
|
23
|
+
# ==== Parameters
|
24
|
+
# klass<Class>:: Thor subclass to register
|
25
|
+
# command<String>:: Subcommand name to use
|
26
|
+
# usage<String>:: Short usage for the subcommand
|
27
|
+
# description<String>:: Description for the subcommand
|
28
|
+
def register(klass, subcommand_name, usage, description, options={})
|
29
|
+
if klass <= Thor::Group
|
30
|
+
desc usage, description, options
|
31
|
+
define_method(subcommand_name) { invoke klass }
|
32
|
+
else
|
33
|
+
desc usage, description, options
|
34
|
+
subcommand subcommand_name, klass
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Defines the usage and the description of the next task.
|
39
|
+
#
|
40
|
+
# ==== Parameters
|
41
|
+
# usage<String>
|
42
|
+
# description<String>
|
43
|
+
# options<String>
|
44
|
+
#
|
45
|
+
def desc(usage, description, options={})
|
46
|
+
if options[:for]
|
47
|
+
task = find_and_refresh_task(options[:for])
|
48
|
+
task.usage = usage if usage
|
49
|
+
task.description = description if description
|
50
|
+
else
|
51
|
+
@usage, @desc, @hide = usage, description, options[:hide] || false
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Defines the long description of the next task.
|
56
|
+
#
|
57
|
+
# ==== Parameters
|
58
|
+
# long description<String>
|
59
|
+
#
|
60
|
+
def long_desc(long_description, options={})
|
61
|
+
if options[:for]
|
62
|
+
task = find_and_refresh_task(options[:for])
|
63
|
+
task.long_description = long_description if long_description
|
64
|
+
else
|
65
|
+
@long_desc = long_description
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Maps an input to a task. If you define:
|
70
|
+
#
|
71
|
+
# map "-T" => "list"
|
72
|
+
#
|
73
|
+
# Running:
|
74
|
+
#
|
75
|
+
# thor -T
|
76
|
+
#
|
77
|
+
# Will invoke the list task.
|
78
|
+
#
|
79
|
+
# ==== Parameters
|
80
|
+
# Hash[String|Array => Symbol]:: Maps the string or the strings in the array to the given task.
|
81
|
+
#
|
82
|
+
def map(mappings=nil)
|
83
|
+
@map ||= from_superclass(:map, {})
|
84
|
+
|
85
|
+
if mappings
|
86
|
+
mappings.each do |key, value|
|
87
|
+
if key.respond_to?(:each)
|
88
|
+
key.each {|subkey| @map[subkey] = value}
|
89
|
+
else
|
90
|
+
@map[key] = value
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
@map
|
96
|
+
end
|
97
|
+
|
98
|
+
# Declares the options for the next task to be declared.
|
99
|
+
#
|
100
|
+
# ==== Parameters
|
101
|
+
# Hash[Symbol => Object]:: The hash key is the name of the option and the value
|
102
|
+
# is the type of the option. Can be :string, :array, :hash, :boolean, :numeric
|
103
|
+
# or :required (string). If you give a value, the type of the value is used.
|
104
|
+
#
|
105
|
+
def method_options(options=nil)
|
106
|
+
@method_options ||= {}
|
107
|
+
build_options(options, @method_options) if options
|
108
|
+
@method_options
|
109
|
+
end
|
110
|
+
|
111
|
+
# Adds an option to the set of method options. If :for is given as option,
|
112
|
+
# it allows you to change the options from a previous defined task.
|
113
|
+
#
|
114
|
+
# def previous_task
|
115
|
+
# # magic
|
116
|
+
# end
|
117
|
+
#
|
118
|
+
# method_option :foo => :bar, :for => :previous_task
|
119
|
+
#
|
120
|
+
# def next_task
|
121
|
+
# # magic
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# ==== Parameters
|
125
|
+
# name<Symbol>:: The name of the argument.
|
126
|
+
# options<Hash>:: Described below.
|
127
|
+
#
|
128
|
+
# ==== Options
|
129
|
+
# :desc - Description for the argument.
|
130
|
+
# :required - If the argument is required or not.
|
131
|
+
# :default - Default value for this argument. It cannot be required and have default values.
|
132
|
+
# :aliases - Aliases for this option.
|
133
|
+
# :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
|
134
|
+
# :banner - String to show on usage notes.
|
135
|
+
#
|
136
|
+
def method_option(name, options={})
|
137
|
+
scope = if options[:for]
|
138
|
+
find_and_refresh_task(options[:for]).options
|
139
|
+
else
|
140
|
+
method_options
|
141
|
+
end
|
142
|
+
|
143
|
+
build_option(name, options, scope)
|
144
|
+
end
|
145
|
+
|
146
|
+
# Prints help information for the given task.
|
147
|
+
#
|
148
|
+
# ==== Parameters
|
149
|
+
# shell<Thor::Shell>
|
150
|
+
# task_name<String>
|
151
|
+
#
|
152
|
+
def task_help(shell, task_name)
|
153
|
+
meth = normalize_task_name(task_name)
|
154
|
+
task = all_tasks[meth]
|
155
|
+
handle_no_task_error(meth) unless task
|
156
|
+
|
157
|
+
shell.say "Usage:"
|
158
|
+
shell.say " #{banner(task)}"
|
159
|
+
shell.say
|
160
|
+
class_options_help(shell, nil => task.options.map { |_, o| o })
|
161
|
+
if task.long_description
|
162
|
+
shell.say "Description:"
|
163
|
+
shell.print_wrapped(task.long_description, :ident => 2)
|
164
|
+
else
|
165
|
+
shell.say task.description
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Prints help information for this class.
|
170
|
+
#
|
171
|
+
# ==== Parameters
|
172
|
+
# shell<Thor::Shell>
|
173
|
+
#
|
174
|
+
def help(shell, subcommand = false)
|
175
|
+
list = printable_tasks(true, subcommand)
|
176
|
+
Thor::Util.thor_classes_in(self).each do |klass|
|
177
|
+
list += klass.printable_tasks(false)
|
178
|
+
end
|
179
|
+
list.sort!{ |a,b| a[0] <=> b[0] }
|
180
|
+
|
181
|
+
shell.say "Tasks:"
|
182
|
+
shell.print_table(list, :ident => 2, :truncate => true)
|
183
|
+
shell.say
|
184
|
+
class_options_help(shell)
|
185
|
+
end
|
186
|
+
|
187
|
+
# Returns tasks ready to be printed.
|
188
|
+
def printable_tasks(all = true, subcommand = false)
|
189
|
+
(all ? all_tasks : tasks).map do |_, task|
|
190
|
+
next if task.hidden?
|
191
|
+
item = []
|
192
|
+
item << banner(task, false, subcommand)
|
193
|
+
item << (task.description ? "# #{task.description.gsub(/\s+/m,' ')}" : "")
|
194
|
+
item
|
195
|
+
end.compact
|
196
|
+
end
|
197
|
+
|
198
|
+
def subcommands
|
199
|
+
@subcommands ||= from_superclass(:subcommands, [])
|
200
|
+
end
|
201
|
+
|
202
|
+
def subcommand(subcommand, subcommand_class)
|
203
|
+
self.subcommands << subcommand.to_s
|
204
|
+
subcommand_class.subcommand_help subcommand
|
205
|
+
define_method(subcommand) { |*args| invoke subcommand_class, args }
|
206
|
+
end
|
207
|
+
|
208
|
+
# Extend check unknown options to accept a hash of conditions.
|
209
|
+
#
|
210
|
+
# === Parameters
|
211
|
+
# options<Hash>: A hash containing :only and/or :except keys
|
212
|
+
def check_unknown_options!(options={})
|
213
|
+
@check_unknown_options ||= Hash.new
|
214
|
+
options.each do |key, value|
|
215
|
+
if value
|
216
|
+
@check_unknown_options[key] = Array(value)
|
217
|
+
else
|
218
|
+
@check_unknown_options.delete(key)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
@check_unknown_options
|
222
|
+
end
|
223
|
+
|
224
|
+
# Overwrite check_unknown_options? to take subcommands and options into account.
|
225
|
+
def check_unknown_options?(config) #:nodoc:
|
226
|
+
options = check_unknown_options
|
227
|
+
return false unless options
|
228
|
+
|
229
|
+
task = config[:current_task]
|
230
|
+
return true unless task
|
231
|
+
|
232
|
+
name = task.name
|
233
|
+
|
234
|
+
if subcommands.include?(name)
|
235
|
+
false
|
236
|
+
elsif options[:except]
|
237
|
+
!options[:except].include?(name.to_sym)
|
238
|
+
elsif options[:only]
|
239
|
+
options[:only].include?(name.to_sym)
|
240
|
+
else
|
241
|
+
true
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
protected
|
246
|
+
|
247
|
+
# The method responsible for dispatching given the args.
|
248
|
+
def dispatch(meth, given_args, given_opts, config) #:nodoc:
|
249
|
+
meth ||= retrieve_task_name(given_args)
|
250
|
+
task = all_tasks[normalize_task_name(meth)]
|
251
|
+
|
252
|
+
if task
|
253
|
+
args, opts = Thor::Options.split(given_args)
|
254
|
+
else
|
255
|
+
args, opts = given_args, nil
|
256
|
+
task = Thor::DynamicTask.new(meth)
|
257
|
+
end
|
258
|
+
|
259
|
+
opts = given_opts || opts || []
|
260
|
+
config.merge!(:current_task => task, :task_options => task.options)
|
261
|
+
|
262
|
+
trailing = args[Range.new(arguments.size, -1)]
|
263
|
+
new(args, opts, config).invoke_task(task, trailing || [])
|
264
|
+
end
|
265
|
+
|
266
|
+
# The banner for this class. You can customize it if you are invoking the
|
267
|
+
# thor class by another ways which is not the Thor::Runner. It receives
|
268
|
+
# the task that is going to be invoked and a boolean which indicates if
|
269
|
+
# the namespace should be displayed as arguments.
|
270
|
+
#
|
271
|
+
def banner(task, namespace = nil, subcommand = false)
|
272
|
+
"#{basename} #{task.formatted_usage(self, $thor_runner, subcommand)}"
|
273
|
+
end
|
274
|
+
|
275
|
+
def baseclass #:nodoc:
|
276
|
+
Thor
|
277
|
+
end
|
278
|
+
|
279
|
+
def create_task(meth) #:nodoc:
|
280
|
+
if @usage && @desc
|
281
|
+
base_class = @hide ? Thor::HiddenTask : Thor::Task
|
282
|
+
tasks[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options)
|
283
|
+
@usage, @desc, @long_desc, @method_options, @hide = nil
|
284
|
+
true
|
285
|
+
elsif self.all_tasks[meth] || meth == "method_missing"
|
286
|
+
true
|
287
|
+
else
|
288
|
+
puts "[WARNING] Attempted to create task #{meth.inspect} without usage or description. " <<
|
289
|
+
"Call desc if you want this method to be available as task or declare it inside a " <<
|
290
|
+
"no_tasks{} block. Invoked from #{caller[1].inspect}."
|
291
|
+
false
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
def initialize_added #:nodoc:
|
296
|
+
class_options.merge!(method_options)
|
297
|
+
@method_options = nil
|
298
|
+
end
|
299
|
+
|
300
|
+
# Retrieve the task name from given args.
|
301
|
+
def retrieve_task_name(args) #:nodoc:
|
302
|
+
meth = args.first.to_s unless args.empty?
|
303
|
+
|
304
|
+
if meth && (map[meth] || meth !~ /^\-/)
|
305
|
+
args.shift
|
306
|
+
else
|
307
|
+
nil
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
# Receives a task name (can be nil), and try to get a map from it.
|
312
|
+
# If a map can't be found use the sent name or the default task.
|
313
|
+
def normalize_task_name(meth) #:nodoc:
|
314
|
+
meth = map[meth.to_s] || meth || default_task
|
315
|
+
meth.to_s.gsub('-','_') # treat foo-bar > foo_bar
|
316
|
+
end
|
317
|
+
|
318
|
+
def subcommand_help(cmd)
|
319
|
+
desc "help [COMMAND]", "Describe subcommands or one specific subcommand"
|
320
|
+
class_eval <<-RUBY
|
321
|
+
def help(task = nil, subcommand = true); super; end
|
322
|
+
RUBY
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
include Thor::Base
|
327
|
+
|
328
|
+
map HELP_MAPPINGS => :help
|
329
|
+
|
330
|
+
desc "help [TASK]", "Describe available tasks or one specific task"
|
331
|
+
def help(task = nil, subcommand = false)
|
332
|
+
task ? self.class.task_help(shell, task) : self.class.help(shell, subcommand)
|
333
|
+
end
|
334
|
+
end
|
data/lib/thor/actions.rb
ADDED
@@ -0,0 +1,314 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'uri'
|
3
|
+
require 'thor/core_ext/file_binary_read'
|
4
|
+
require 'thor/actions/create_file'
|
5
|
+
require 'thor/actions/create_link'
|
6
|
+
require 'thor/actions/directory'
|
7
|
+
require 'thor/actions/empty_directory'
|
8
|
+
require 'thor/actions/file_manipulation'
|
9
|
+
require 'thor/actions/inject_into_file'
|
10
|
+
|
11
|
+
class Thor
|
12
|
+
module Actions
|
13
|
+
attr_accessor :behavior
|
14
|
+
|
15
|
+
def self.included(base) #:nodoc:
|
16
|
+
base.extend ClassMethods
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
# Hold source paths for one Thor instance. source_paths_for_search is the
|
21
|
+
# method responsible to gather source_paths from this current class,
|
22
|
+
# inherited paths and the source root.
|
23
|
+
#
|
24
|
+
def source_paths
|
25
|
+
@_source_paths ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
# Stores and return the source root for this class
|
29
|
+
def source_root(path=nil)
|
30
|
+
@_source_root = path if path
|
31
|
+
@_source_root
|
32
|
+
end
|
33
|
+
|
34
|
+
# Returns the source paths in the following order:
|
35
|
+
#
|
36
|
+
# 1) This class source paths
|
37
|
+
# 2) Source root
|
38
|
+
# 3) Parents source paths
|
39
|
+
#
|
40
|
+
def source_paths_for_search
|
41
|
+
paths = []
|
42
|
+
paths += self.source_paths
|
43
|
+
paths << self.source_root if self.source_root
|
44
|
+
paths += from_superclass(:source_paths, [])
|
45
|
+
paths
|
46
|
+
end
|
47
|
+
|
48
|
+
# Add runtime options that help actions execution.
|
49
|
+
#
|
50
|
+
def add_runtime_options!
|
51
|
+
class_option :force, :type => :boolean, :aliases => "-f", :group => :runtime,
|
52
|
+
:desc => "Overwrite files that already exist"
|
53
|
+
|
54
|
+
class_option :pretend, :type => :boolean, :aliases => "-p", :group => :runtime,
|
55
|
+
:desc => "Run but do not make any changes"
|
56
|
+
|
57
|
+
class_option :quiet, :type => :boolean, :aliases => "-q", :group => :runtime,
|
58
|
+
:desc => "Supress status output"
|
59
|
+
|
60
|
+
class_option :skip, :type => :boolean, :aliases => "-s", :group => :runtime,
|
61
|
+
:desc => "Skip files that already exist"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Extends initializer to add more configuration options.
|
66
|
+
#
|
67
|
+
# ==== Configuration
|
68
|
+
# behavior<Symbol>:: The actions default behavior. Can be :invoke or :revoke.
|
69
|
+
# It also accepts :force, :skip and :pretend to set the behavior
|
70
|
+
# and the respective option.
|
71
|
+
#
|
72
|
+
# destination_root<String>:: The root directory needed for some actions.
|
73
|
+
#
|
74
|
+
def initialize(args=[], options={}, config={})
|
75
|
+
self.behavior = case config[:behavior].to_s
|
76
|
+
when "force", "skip"
|
77
|
+
_cleanup_options_and_set(options, config[:behavior])
|
78
|
+
:invoke
|
79
|
+
when "revoke"
|
80
|
+
:revoke
|
81
|
+
else
|
82
|
+
:invoke
|
83
|
+
end
|
84
|
+
|
85
|
+
super
|
86
|
+
self.destination_root = config[:destination_root]
|
87
|
+
end
|
88
|
+
|
89
|
+
# Wraps an action object and call it accordingly to the thor class behavior.
|
90
|
+
#
|
91
|
+
def action(instance) #:nodoc:
|
92
|
+
if behavior == :revoke
|
93
|
+
instance.revoke!
|
94
|
+
else
|
95
|
+
instance.invoke!
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Returns the root for this thor class (also aliased as destination root).
|
100
|
+
#
|
101
|
+
def destination_root
|
102
|
+
@destination_stack.last
|
103
|
+
end
|
104
|
+
|
105
|
+
# Sets the root for this thor class. Relatives path are added to the
|
106
|
+
# directory where the script was invoked and expanded.
|
107
|
+
#
|
108
|
+
def destination_root=(root)
|
109
|
+
@destination_stack ||= []
|
110
|
+
@destination_stack[0] = File.expand_path(root || '')
|
111
|
+
end
|
112
|
+
|
113
|
+
# Returns the given path relative to the absolute root (ie, root where
|
114
|
+
# the script started).
|
115
|
+
#
|
116
|
+
def relative_to_original_destination_root(path, remove_dot=true)
|
117
|
+
path = path.gsub(@destination_stack[0], '.')
|
118
|
+
remove_dot ? (path[2..-1] || '') : path
|
119
|
+
end
|
120
|
+
|
121
|
+
# Holds source paths in instance so they can be manipulated.
|
122
|
+
#
|
123
|
+
def source_paths
|
124
|
+
@source_paths ||= self.class.source_paths_for_search
|
125
|
+
end
|
126
|
+
|
127
|
+
# Receives a file or directory and search for it in the source paths.
|
128
|
+
#
|
129
|
+
def find_in_source_paths(file)
|
130
|
+
relative_root = relative_to_original_destination_root(destination_root, false)
|
131
|
+
|
132
|
+
source_paths.each do |source|
|
133
|
+
source_file = File.expand_path(file, File.join(source, relative_root))
|
134
|
+
return source_file if File.exists?(source_file)
|
135
|
+
end
|
136
|
+
|
137
|
+
message = "Could not find #{file.inspect} in any of your source paths. "
|
138
|
+
|
139
|
+
unless self.class.source_root
|
140
|
+
message << "Please invoke #{self.class.name}.source_root(PATH) with the PATH containing your templates. "
|
141
|
+
end
|
142
|
+
|
143
|
+
if source_paths.empty?
|
144
|
+
message << "Currently you have no source paths."
|
145
|
+
else
|
146
|
+
message << "Your current source paths are: \n#{source_paths.join("\n")}"
|
147
|
+
end
|
148
|
+
|
149
|
+
raise Error, message
|
150
|
+
end
|
151
|
+
|
152
|
+
# Do something in the root or on a provided subfolder. If a relative path
|
153
|
+
# is given it's referenced from the current root. The full path is yielded
|
154
|
+
# to the block you provide. The path is set back to the previous path when
|
155
|
+
# the method exits.
|
156
|
+
#
|
157
|
+
# ==== Parameters
|
158
|
+
# dir<String>:: the directory to move to.
|
159
|
+
# config<Hash>:: give :verbose => true to log and use padding.
|
160
|
+
#
|
161
|
+
def inside(dir='', config={}, &block)
|
162
|
+
verbose = config.fetch(:verbose, false)
|
163
|
+
pretend = options[:pretend]
|
164
|
+
|
165
|
+
say_status :inside, dir, verbose
|
166
|
+
shell.padding += 1 if verbose
|
167
|
+
@destination_stack.push File.expand_path(dir, destination_root)
|
168
|
+
|
169
|
+
# If the directory doesnt exist and we're not pretending
|
170
|
+
if !File.exist?(destination_root) && !pretend
|
171
|
+
FileUtils.mkdir_p(destination_root)
|
172
|
+
end
|
173
|
+
|
174
|
+
if pretend
|
175
|
+
# In pretend mode, just yield down to the block
|
176
|
+
block.arity == 1 ? yield(destination_root) : yield
|
177
|
+
else
|
178
|
+
FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
|
179
|
+
end
|
180
|
+
|
181
|
+
@destination_stack.pop
|
182
|
+
shell.padding -= 1 if verbose
|
183
|
+
end
|
184
|
+
|
185
|
+
# Goes to the root and execute the given block.
|
186
|
+
#
|
187
|
+
def in_root
|
188
|
+
inside(@destination_stack.first) { yield }
|
189
|
+
end
|
190
|
+
|
191
|
+
# Loads an external file and execute it in the instance binding.
|
192
|
+
#
|
193
|
+
# ==== Parameters
|
194
|
+
# path<String>:: The path to the file to execute. Can be a web address or
|
195
|
+
# a relative path from the source root.
|
196
|
+
#
|
197
|
+
# ==== Examples
|
198
|
+
#
|
199
|
+
# apply "http://gist.github.com/103208"
|
200
|
+
#
|
201
|
+
# apply "recipes/jquery.rb"
|
202
|
+
#
|
203
|
+
def apply(path, config={})
|
204
|
+
verbose = config.fetch(:verbose, true)
|
205
|
+
is_uri = path =~ /^https?\:\/\//
|
206
|
+
path = find_in_source_paths(path) unless is_uri
|
207
|
+
|
208
|
+
say_status :apply, path, verbose
|
209
|
+
shell.padding += 1 if verbose
|
210
|
+
|
211
|
+
if is_uri
|
212
|
+
contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read }
|
213
|
+
else
|
214
|
+
contents = open(path) {|io| io.read }
|
215
|
+
end
|
216
|
+
|
217
|
+
instance_eval(contents, path)
|
218
|
+
shell.padding -= 1 if verbose
|
219
|
+
end
|
220
|
+
|
221
|
+
# Executes a command returning the contents of the command.
|
222
|
+
#
|
223
|
+
# ==== Parameters
|
224
|
+
# command<String>:: the command to be executed.
|
225
|
+
# config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output. Specify :with
|
226
|
+
# to append an executable to command executation.
|
227
|
+
#
|
228
|
+
# ==== Example
|
229
|
+
#
|
230
|
+
# inside('vendor') do
|
231
|
+
# run('ln -s ~/edge rails')
|
232
|
+
# end
|
233
|
+
#
|
234
|
+
def run(command, config={})
|
235
|
+
return unless behavior == :invoke
|
236
|
+
|
237
|
+
destination = relative_to_original_destination_root(destination_root, false)
|
238
|
+
desc = "#{command} from #{destination.inspect}"
|
239
|
+
|
240
|
+
if config[:with]
|
241
|
+
desc = "#{File.basename(config[:with].to_s)} #{desc}"
|
242
|
+
command = "#{config[:with]} #{command}"
|
243
|
+
end
|
244
|
+
|
245
|
+
say_status :run, desc, config.fetch(:verbose, true)
|
246
|
+
|
247
|
+
unless options[:pretend]
|
248
|
+
config[:capture] ? `#{command}` : system("#{command}")
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
# Executes a ruby script (taking into account WIN32 platform quirks).
|
253
|
+
#
|
254
|
+
# ==== Parameters
|
255
|
+
# command<String>:: the command to be executed.
|
256
|
+
# config<Hash>:: give :verbose => false to not log the status.
|
257
|
+
#
|
258
|
+
def run_ruby_script(command, config={})
|
259
|
+
return unless behavior == :invoke
|
260
|
+
run command, config.merge(:with => Thor::Util.ruby_command)
|
261
|
+
end
|
262
|
+
|
263
|
+
# Run a thor command. A hash of options can be given and it's converted to
|
264
|
+
# switches.
|
265
|
+
#
|
266
|
+
# ==== Parameters
|
267
|
+
# task<String>:: the task to be invoked
|
268
|
+
# args<Array>:: arguments to the task
|
269
|
+
# config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output.
|
270
|
+
# Other options are given as parameter to Thor.
|
271
|
+
#
|
272
|
+
#
|
273
|
+
# ==== Examples
|
274
|
+
#
|
275
|
+
# thor :install, "http://gist.github.com/103208"
|
276
|
+
# #=> thor install http://gist.github.com/103208
|
277
|
+
#
|
278
|
+
# thor :list, :all => true, :substring => 'rails'
|
279
|
+
# #=> thor list --all --substring=rails
|
280
|
+
#
|
281
|
+
def thor(task, *args)
|
282
|
+
config = args.last.is_a?(Hash) ? args.pop : {}
|
283
|
+
verbose = config.key?(:verbose) ? config.delete(:verbose) : true
|
284
|
+
pretend = config.key?(:pretend) ? config.delete(:pretend) : false
|
285
|
+
capture = config.key?(:capture) ? config.delete(:capture) : false
|
286
|
+
|
287
|
+
args.unshift task
|
288
|
+
args.push Thor::Options.to_switches(config)
|
289
|
+
command = args.join(' ').strip
|
290
|
+
|
291
|
+
run command, :with => :thor, :verbose => verbose, :pretend => pretend, :capture => capture
|
292
|
+
end
|
293
|
+
|
294
|
+
protected
|
295
|
+
|
296
|
+
# Allow current root to be shared between invocations.
|
297
|
+
#
|
298
|
+
def _shared_configuration #:nodoc:
|
299
|
+
super.merge!(:destination_root => self.destination_root)
|
300
|
+
end
|
301
|
+
|
302
|
+
def _cleanup_options_and_set(options, key) #:nodoc:
|
303
|
+
case options
|
304
|
+
when Array
|
305
|
+
%w(--force -f --skip -s).each { |i| options.delete(i) }
|
306
|
+
options << "--#{key}"
|
307
|
+
when Hash
|
308
|
+
[:force, :skip, "force", "skip"].each { |i| options.delete(i) }
|
309
|
+
options.merge!(key => true)
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
end
|
314
|
+
end
|