wycats-thor 0.9.8 → 0.10.26

Sign up to get free protection for your applications and to get access to all the features.
data/lib/thor/base.rb ADDED
@@ -0,0 +1,520 @@
1
+ require 'thor/core_ext/hash_with_indifferent_access'
2
+ require 'thor/core_ext/ordered_hash'
3
+ require 'thor/error'
4
+ require 'thor/shell'
5
+ require 'thor/invocation'
6
+ require 'thor/parser'
7
+ require 'thor/task'
8
+ require 'thor/util'
9
+
10
+ class Thor
11
+ HELP_MAPPINGS = %w(-h -? --help -D)
12
+ THOR_RESERVED_WORDS = %w(invoke shell options behavior root destination_root relative_root source_root)
13
+
14
+ module Base
15
+ attr_accessor :options
16
+
17
+ # It receives arguments in an Array and two hashes, one for options and
18
+ # other for configuration.
19
+ #
20
+ # Notice that it does not check if all required arguments were supplied.
21
+ # It should be done by the parser.
22
+ #
23
+ # ==== Parameters
24
+ # args<Array[Object]>:: An array of objects. The objects are applied to their
25
+ # respective accessors declared with <tt>argument</tt>.
26
+ #
27
+ # options<Hash>:: An options hash that will be available as self.options.
28
+ # The hash given is converted to a hash with indifferent
29
+ # access, magic predicates (options.skip?) and then frozen.
30
+ #
31
+ # config<Hash>:: Configuration for this Thor class.
32
+ #
33
+ def initialize(args=[], options={}, config={})
34
+ Thor::Arguments.parse(self.class.arguments, args).each do |key, value|
35
+ send("#{key}=", value)
36
+ end
37
+
38
+ parse_options = self.class.class_options
39
+
40
+ options = if options.is_a?(Array)
41
+ task_options = config.delete(:task_options) # hook for start
42
+ parse_options = parse_options.merge(task_options) if task_options
43
+ Thor::Options.parse(parse_options, options)
44
+ else
45
+ Thor::Options.parse(parse_options, []).merge(options)
46
+ end
47
+
48
+ self.options = Thor::CoreExt::HashWithIndifferentAccess.new(options).freeze
49
+ end
50
+
51
+ class << self
52
+ def included(base) #:nodoc:
53
+ base.send :extend, ClassMethods
54
+ base.send :include, Invocation
55
+ base.send :include, Shell
56
+ end
57
+
58
+ # Returns the classes that inherits from Thor or Thor::Group.
59
+ #
60
+ # ==== Returns
61
+ # Array[Class]
62
+ #
63
+ def subclasses
64
+ @subclasses ||= []
65
+ end
66
+
67
+ # Returns the files where the subclasses are kept.
68
+ #
69
+ # ==== Returns
70
+ # Hash[path<String> => Class]
71
+ #
72
+ def subclass_files
73
+ @subclass_files ||= Hash.new{ |h,k| h[k] = [] }
74
+ end
75
+
76
+ # Whenever a class inherits from Thor or Thor::Group, we should track the
77
+ # class and the file on Thor::Base. This is the method responsable for it.
78
+ # Also invoke the source_root if the klass respond to it. This is needed
79
+ # to ensure that the source_root does not change after FileUtils#cd is
80
+ # called.
81
+ #
82
+ def register_klass_file(klass) #:nodoc:
83
+ file = caller[1].match(/(.*):\d+/)[1]
84
+
85
+ klass.source_root if klass.respond_to?(:source_root)
86
+ Thor::Base.subclasses << klass unless Thor::Base.subclasses.include?(klass)
87
+
88
+ file_subclasses = Thor::Base.subclass_files[File.expand_path(file)]
89
+ file_subclasses << klass unless file_subclasses.include?(klass)
90
+ end
91
+ end
92
+
93
+ module ClassMethods
94
+ # Adds an argument to the class and creates an attr_accessor for it.
95
+ #
96
+ # Arguments are different from options in several aspects. The first one
97
+ # is how they are parsed from the command line, arguments are retrieved
98
+ # from position:
99
+ #
100
+ # thor task NAME
101
+ #
102
+ # Instead of:
103
+ #
104
+ # thor task --name=NAME
105
+ #
106
+ # Besides, arguments are used inside your code as an accessor (self.argument),
107
+ # while options are all kept in a hash (self.options).
108
+ #
109
+ # Finally, arguments cannot have type :default or :boolean but can be
110
+ # optional (supplying :optional => :true or :required => false), although
111
+ # you cannot have a required argument after a non-required argument. If you
112
+ # try it, an error is raised.
113
+ #
114
+ # ==== Parameters
115
+ # name<Symbol>:: The name of the argument.
116
+ # options<Hash>:: Described below.
117
+ #
118
+ # ==== Options
119
+ # :desc - Description for the argument.
120
+ # :required - If the argument is required or not.
121
+ # :optional - If the argument is optional or not.
122
+ # :type - The type of the argument, can be :string, :hash, :array, :numeric.
123
+ # :default - Default value for this argument. It cannot be required and have default values.
124
+ # :banner - String to show on usage notes.
125
+ #
126
+ # ==== Errors
127
+ # ArgumentError:: Raised if you supply a required argument after a non required one.
128
+ #
129
+ def argument(name, options={})
130
+ is_thor_reserved_word?(name, :argument)
131
+ no_tasks { attr_accessor name }
132
+
133
+ required = if options.key?(:optional)
134
+ !options[:optional]
135
+ elsif options.key?(:required)
136
+ options[:required]
137
+ else
138
+ options[:default].nil?
139
+ end
140
+
141
+ remove_argument name
142
+
143
+ arguments.each do |argument|
144
+ next if argument.required?
145
+ raise ArgumentError, "You cannot have #{name.to_s.inspect} as required argument after " <<
146
+ "the non-required argument #{argument.human_name.inspect}."
147
+ end if required
148
+
149
+ arguments << Thor::Argument.new(name, options[:desc], required, options[:type],
150
+ options[:default], options[:banner])
151
+ end
152
+
153
+ # Returns this class arguments, looking up in the ancestors chain.
154
+ #
155
+ # ==== Returns
156
+ # Array[Thor::Argument]
157
+ #
158
+ def arguments
159
+ @arguments ||= from_superclass(:arguments, [])
160
+ end
161
+
162
+ # Adds a bunch of options to the set of class options.
163
+ #
164
+ # class_options :foo => false, :bar => :required, :baz => :string
165
+ #
166
+ # If you prefer more detailed declaration, check class_option.
167
+ #
168
+ # ==== Parameters
169
+ # Hash[Symbol => Object]
170
+ #
171
+ def class_options(options=nil)
172
+ @class_options ||= from_superclass(:class_options, {})
173
+ build_options(options, @class_options) if options
174
+ @class_options
175
+ end
176
+
177
+ # Adds an option to the set of class options
178
+ #
179
+ # ==== Parameters
180
+ # name<Symbol>:: The name of the argument.
181
+ # options<Hash>:: Described below.
182
+ #
183
+ # ==== Options
184
+ # :desc - Description for the argument.
185
+ # :required - If the argument is required or not.
186
+ # :default - Default value for this argument.
187
+ # :group - The group for this options. Use by class options to output options in different levels.
188
+ # :aliases - Aliases for this option.
189
+ # :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
190
+ # :banner - String to show on usage notes.
191
+ #
192
+ def class_option(name, options)
193
+ build_option(name, options, class_options)
194
+ end
195
+
196
+ # Removes a previous defined argument. If :undefine is given, undefine
197
+ # accessors as well.
198
+ #
199
+ # ==== Paremeters
200
+ # names<Array>:: Arguments to be removed
201
+ #
202
+ # ==== Examples
203
+ #
204
+ # remove_argument :foo
205
+ # remove_argument :foo, :bar, :baz, :undefine => true
206
+ #
207
+ def remove_argument(*names)
208
+ options = names.last.is_a?(Hash) ? names.pop : {}
209
+
210
+ names.each do |name|
211
+ arguments.delete_if { |a| a.name == name.to_s }
212
+ undef_method name, "#{name}=" if options[:undefine]
213
+ end
214
+ end
215
+
216
+ # Removes a previous defined class option.
217
+ #
218
+ # ==== Paremeters
219
+ # names<Array>:: Class options to be removed
220
+ #
221
+ # ==== Examples
222
+ #
223
+ # remove_class_option :foo
224
+ # remove_class_option :foo, :bar, :baz
225
+ #
226
+ def remove_class_option(*names)
227
+ names.each do |name|
228
+ class_options.delete(name)
229
+ end
230
+ end
231
+
232
+ # Defines the group. This is used when thor list is invoked so you can specify
233
+ # that only tasks from a pre-defined group will be shown. Defaults to standard.
234
+ #
235
+ # ==== Parameters
236
+ # name<String|Symbol>
237
+ #
238
+ def group(name=nil)
239
+ case name
240
+ when nil
241
+ @group ||= from_superclass(:group, 'standard')
242
+ else
243
+ @group = name.to_s
244
+ end
245
+ end
246
+
247
+ # Returns the tasks for this Thor class.
248
+ #
249
+ # ==== Returns
250
+ # OrderedHash:: An ordered hash with this class tasks.
251
+ #
252
+ def tasks
253
+ @tasks ||= Thor::CoreExt::OrderedHash.new
254
+ end
255
+
256
+ # Returns the tasks for this Thor class and all subclasses.
257
+ #
258
+ # ==== Returns
259
+ # OrderedHash
260
+ #
261
+ def all_tasks
262
+ @all_tasks ||= from_superclass(:all_tasks, Thor::CoreExt::OrderedHash.new)
263
+ @all_tasks.merge(tasks)
264
+ end
265
+
266
+ # Removes a given task from this Thor class. This is usually done if you
267
+ # are inheriting from another class and don't want it to be available
268
+ # anymore.
269
+ #
270
+ # By default it only remove the mapping to the task. But you can supply
271
+ # :undefine => true to undefine the method from the class as well.
272
+ #
273
+ # ==== Parameters
274
+ # name<Symbol|String>:: The name of the task to be removed
275
+ # options<Hash>:: You can give :undefine => true if you want tasks the method
276
+ # to be undefined from the class as well.
277
+ #
278
+ def remove_task(*names)
279
+ options = names.last.is_a?(Hash) ? names.pop : {}
280
+
281
+ names.each do |name|
282
+ tasks.delete(name.to_s)
283
+ all_tasks.delete(name.to_s)
284
+ undef_method name if options[:undefine]
285
+ end
286
+ end
287
+
288
+ # All methods defined inside the given block are not added as tasks.
289
+ #
290
+ # So you can do:
291
+ #
292
+ # class MyScript < Thor
293
+ # no_tasks do
294
+ # def this_is_not_a_task
295
+ # end
296
+ # end
297
+ # end
298
+ #
299
+ # You can also add the method and remove it from the task list:
300
+ #
301
+ # class MyScript < Thor
302
+ # def this_is_not_a_task
303
+ # end
304
+ # remove_task :this_is_not_a_task
305
+ # end
306
+ #
307
+ def no_tasks
308
+ @no_tasks = true
309
+ yield
310
+ @no_tasks = false
311
+ end
312
+
313
+ # Sets the namespace for the Thor or Thor::Group class. By default the
314
+ # namespace is retrieved from the class name. If your Thor class is named
315
+ # Scripts::MyScript, the help method, for example, will be called as:
316
+ #
317
+ # thor scripts:my_script -h
318
+ #
319
+ # If you change the namespace:
320
+ #
321
+ # namespace :my_scripts
322
+ #
323
+ # You change how your tasks are invoked:
324
+ #
325
+ # thor my_scripts -h
326
+ #
327
+ # Finally, if you change your namespace to default:
328
+ #
329
+ # namespace :default
330
+ #
331
+ # Your tasks can be invoked with a shortcut. Instead of:
332
+ #
333
+ # thor :my_task
334
+ #
335
+ def namespace(name=nil)
336
+ case name
337
+ when nil
338
+ @namespace ||= Thor::Util.constant_to_namespace(self, false)
339
+ else
340
+ @namespace = name.to_s
341
+ end
342
+ end
343
+
344
+ # Default way to start generators from the command line.
345
+ #
346
+ def start(given_args=ARGV, config={}) #:nodoc:
347
+ config[:shell] ||= Thor::Base.shell.new
348
+ yield
349
+ rescue Thor::Error => e
350
+ if given_args.include?("--debug")
351
+ raise e
352
+ else
353
+ config[:shell].error e.message
354
+ end
355
+ end
356
+
357
+ protected
358
+
359
+ # Prints the class options per group. If an option does not belong to
360
+ # any group, it uses the ungrouped name value. This method provide to
361
+ # hooks to add extra options, one of them if the third argument called
362
+ # extra_group that should be a Thor::CoreExt::OrderedHash in the format
363
+ # :group => Array[Options].
364
+ #
365
+ # The second is by returning a lamda used to print values. The lambda
366
+ # requires two options: the group name and the array of options.
367
+ #
368
+ def class_options_help(shell, ungrouped_name=nil, extra_group=nil) #:nodoc:
369
+ unless self.class_options.empty?
370
+ groups = {}
371
+
372
+ class_options.each do |_, value|
373
+ groups[value.group] ||= []
374
+ groups[value.group] << value
375
+ end
376
+
377
+ printer = proc do |group_name, options|
378
+ list = []
379
+ padding = options.collect{ |o| o.aliases.size }.max.to_i * 4
380
+
381
+ options.each do |option|
382
+ list << [ option.usage(padding), option.description || "" ]
383
+ list << [ "", "Default: #{option.default}" ] if option.show_default?
384
+ end
385
+
386
+ unless list.empty?
387
+ if group_name
388
+ shell.say "#{group_name} options:"
389
+ else
390
+ shell.say "Options:"
391
+ end
392
+
393
+ shell.print_table(list, :emphasize_last => true, :ident => 2)
394
+ shell.say ""
395
+ end
396
+ end
397
+
398
+ # Deal with default group
399
+ global_options = groups.delete(nil) || []
400
+ printer.call(ungrouped_name, global_options) if global_options
401
+
402
+ # Print all others
403
+ groups = extra_group.merge(groups) if extra_group
404
+ groups.each(&printer)
405
+ printer
406
+ end
407
+ end
408
+
409
+ # Raises an error if the word given is a Thor reserved word.
410
+ #
411
+ def is_thor_reserved_word?(word, type)
412
+ return false unless THOR_RESERVED_WORDS.include?(word.to_s)
413
+ raise "'#{word}' is a Thor reserved word and cannot be defined as #{type}"
414
+ end
415
+
416
+ # Build an option and adds it to the given scope.
417
+ #
418
+ # ==== Parameters
419
+ # name<Symbol>:: The name of the argument.
420
+ # options<Hash>:: Described in both class_option and method_option.
421
+ #
422
+ def build_option(name, options, scope)
423
+ scope[name] = Thor::Option.new(name, options[:desc], options[:required],
424
+ options[:type], options[:default], options[:banner],
425
+ options[:group], options[:aliases])
426
+ end
427
+
428
+ # Receives a hash of options, parse them and add to the scope. This is a
429
+ # fast way to set a bunch of options:
430
+ #
431
+ # build_options :foo => true, :bar => :required, :baz => :string
432
+ #
433
+ # ==== Parameters
434
+ # Hash[Symbol => Object]
435
+ #
436
+ def build_options(options, scope)
437
+ options.each do |key, value|
438
+ scope[key] = Thor::Option.parse(key, value)
439
+ end
440
+ end
441
+
442
+ # Finds a task with the given name. If the task belongs to the current
443
+ # class, just return it, otherwise dup it and add the fresh copy to the
444
+ # current task hash.
445
+ #
446
+ def find_and_refresh_task(name)
447
+ task = if task = tasks[name.to_s]
448
+ task
449
+ elsif task = all_tasks[name.to_s]
450
+ tasks[name.to_s] = task.clone
451
+ else
452
+ raise ArgumentError, "You supplied :for => #{name.inspect}, but the task #{name.inspect} could not be found."
453
+ end
454
+ end
455
+
456
+ # Everytime someone inherits from a Thor class, register the klass
457
+ # and file into baseclass.
458
+ #
459
+ def inherited(klass)
460
+ Thor::Base.register_klass_file(klass)
461
+ end
462
+
463
+ # Fire this callback whenever a method is added. Added methods are
464
+ # tracked as tasks if the requirements set by valid_task? are valid.
465
+ #
466
+ def method_added(meth)
467
+ meth = meth.to_s
468
+
469
+ if meth == "initialize"
470
+ initialize_added
471
+ return
472
+ end
473
+
474
+ # Return if it's not a public instance method
475
+ return unless public_instance_methods.include?(meth) ||
476
+ public_instance_methods.include?(meth.to_sym)
477
+
478
+ # Return if @no_tasks is enabled or it's not a valid task
479
+ return if @no_tasks || !valid_task?(meth)
480
+
481
+ is_thor_reserved_word?(meth, :task)
482
+ Thor::Base.register_klass_file(self)
483
+ create_task(meth)
484
+ end
485
+
486
+ # Retrieves a value from superclass. If it reaches the baseclass,
487
+ # returns nil.
488
+ #
489
+ def from_superclass(method, default=nil)
490
+ if self == baseclass || !superclass.respond_to?(method, true)
491
+ default
492
+ else
493
+ value = superclass.send(method)
494
+ value.dup if value
495
+ end
496
+ end
497
+
498
+ # SIGNATURE: Sets the baseclass. This is where the superclass lookup
499
+ # finishes.
500
+ def baseclass #:nodoc:
501
+ end
502
+
503
+ # SIGNATURE: Defines if a given method is a valid_task?. This method is
504
+ # called before a new method is added to the class.
505
+ def valid_task?(meth) #:nodoc:
506
+ true # unless otherwise given
507
+ end
508
+
509
+ # SIGNATURE: Creates a new task if valid_task? is true. This method is
510
+ # called when a new method is added to the class.
511
+ def create_task(meth) #:nodoc:
512
+ end
513
+
514
+ # SIGNATURE: Defines behavior when the initialize method is added to the
515
+ # class.
516
+ def initialize_added #:nodoc:
517
+ end
518
+ end
519
+ end
520
+ end