wijet-thor 0.14.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/CHANGELOG.rdoc +103 -0
  2. data/LICENSE +20 -0
  3. data/README.md +307 -0
  4. data/Thorfile +24 -0
  5. data/bin/rake2thor +86 -0
  6. data/bin/thor +6 -0
  7. data/lib/thor.rb +334 -0
  8. data/lib/thor/actions.rb +314 -0
  9. data/lib/thor/actions/create_file.rb +105 -0
  10. data/lib/thor/actions/create_link.rb +57 -0
  11. data/lib/thor/actions/directory.rb +93 -0
  12. data/lib/thor/actions/empty_directory.rb +134 -0
  13. data/lib/thor/actions/file_manipulation.rb +270 -0
  14. data/lib/thor/actions/inject_into_file.rb +109 -0
  15. data/lib/thor/base.rb +579 -0
  16. data/lib/thor/core_ext/file_binary_read.rb +9 -0
  17. data/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  18. data/lib/thor/core_ext/ordered_hash.rb +100 -0
  19. data/lib/thor/error.rb +30 -0
  20. data/lib/thor/group.rb +273 -0
  21. data/lib/thor/invocation.rb +168 -0
  22. data/lib/thor/parser.rb +4 -0
  23. data/lib/thor/parser/argument.rb +67 -0
  24. data/lib/thor/parser/arguments.rb +161 -0
  25. data/lib/thor/parser/option.rb +120 -0
  26. data/lib/thor/parser/options.rb +173 -0
  27. data/lib/thor/rake_compat.rb +66 -0
  28. data/lib/thor/runner.rb +309 -0
  29. data/lib/thor/shell.rb +88 -0
  30. data/lib/thor/shell/basic.rb +290 -0
  31. data/lib/thor/shell/color.rb +108 -0
  32. data/lib/thor/shell/html.rb +121 -0
  33. data/lib/thor/task.rb +114 -0
  34. data/lib/thor/util.rb +229 -0
  35. data/lib/thor/version.rb +3 -0
  36. data/spec/actions/create_file_spec.rb +170 -0
  37. data/spec/actions/directory_spec.rb +136 -0
  38. data/spec/actions/empty_directory_spec.rb +98 -0
  39. data/spec/actions/file_manipulation_spec.rb +310 -0
  40. data/spec/actions/inject_into_file_spec.rb +135 -0
  41. data/spec/actions_spec.rb +322 -0
  42. data/spec/base_spec.rb +269 -0
  43. data/spec/core_ext/hash_with_indifferent_access_spec.rb +43 -0
  44. data/spec/core_ext/ordered_hash_spec.rb +115 -0
  45. data/spec/fixtures/application.rb +2 -0
  46. data/spec/fixtures/bundle/execute.rb +6 -0
  47. data/spec/fixtures/bundle/main.thor +1 -0
  48. data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
  49. data/spec/fixtures/doc/README +3 -0
  50. data/spec/fixtures/doc/block_helper.rb +3 -0
  51. data/spec/fixtures/doc/components/.empty_directory +0 -0
  52. data/spec/fixtures/doc/config.rb +1 -0
  53. data/spec/fixtures/group.thor +114 -0
  54. data/spec/fixtures/invoke.thor +112 -0
  55. data/spec/fixtures/path with spaces b/data/spec/fixtures/path with → spaces +0 -0
  56. data/spec/fixtures/script.thor +184 -0
  57. data/spec/fixtures/task.thor +10 -0
  58. data/spec/group_spec.rb +178 -0
  59. data/spec/invocation_spec.rb +100 -0
  60. data/spec/parser/argument_spec.rb +47 -0
  61. data/spec/parser/arguments_spec.rb +64 -0
  62. data/spec/parser/option_spec.rb +202 -0
  63. data/spec/parser/options_spec.rb +319 -0
  64. data/spec/rake_compat_spec.rb +68 -0
  65. data/spec/register_spec.rb +104 -0
  66. data/spec/runner_spec.rb +210 -0
  67. data/spec/shell/basic_spec.rb +223 -0
  68. data/spec/shell/color_spec.rb +41 -0
  69. data/spec/shell/html_spec.rb +27 -0
  70. data/spec/shell_spec.rb +47 -0
  71. data/spec/spec_helper.rb +54 -0
  72. data/spec/task_spec.rb +74 -0
  73. data/spec/thor_spec.rb +334 -0
  74. data/spec/util_spec.rb +163 -0
  75. metadata +193 -0
@@ -0,0 +1,109 @@
1
+ require 'thor/actions/empty_directory'
2
+
3
+ class Thor
4
+ module Actions
5
+
6
+ # Injects the given content into a file. Different from gsub_file, this
7
+ # method is reversible.
8
+ #
9
+ # ==== Parameters
10
+ # destination<String>:: Relative path to the destination root
11
+ # data<String>:: Data to add to the file. Can be given as a block.
12
+ # config<Hash>:: give :verbose => false to not log the status and the flag
13
+ # for injection (:after or :before) or :force => true for
14
+ # insert two or more times the same content.
15
+ #
16
+ # ==== Examples
17
+ #
18
+ # insert_into_file "config/environment.rb", "config.gem :thor", :after => "Rails::Initializer.run do |config|\n"
19
+ #
20
+ # insert_into_file "config/environment.rb", :after => "Rails::Initializer.run do |config|\n" do
21
+ # gems = ask "Which gems would you like to add?"
22
+ # gems.split(" ").map{ |gem| " config.gem :#{gem}" }.join("\n")
23
+ # end
24
+ #
25
+ def insert_into_file(destination, *args, &block)
26
+ if block_given?
27
+ data, config = block, args.shift
28
+ else
29
+ data, config = args.shift, args.shift
30
+ end
31
+ action InjectIntoFile.new(self, destination, data, config)
32
+ end
33
+ alias_method :inject_into_file, :insert_into_file
34
+
35
+ class InjectIntoFile < EmptyDirectory #:nodoc:
36
+ attr_reader :replacement, :flag, :behavior
37
+
38
+ def initialize(base, destination, data, config)
39
+ super(base, destination, { :verbose => true }.merge(config))
40
+
41
+ @behavior, @flag = if @config.key?(:after)
42
+ [:after, @config.delete(:after)]
43
+ else
44
+ [:before, @config.delete(:before)]
45
+ end
46
+
47
+ @replacement = data.is_a?(Proc) ? data.call : data
48
+ @flag = Regexp.escape(@flag) unless @flag.is_a?(Regexp)
49
+ end
50
+
51
+ def invoke!
52
+ say_status :invoke
53
+
54
+ content = if @behavior == :after
55
+ '\0' + replacement
56
+ else
57
+ replacement + '\0'
58
+ end
59
+
60
+ replace!(/#{flag}/, content, config[:force])
61
+ end
62
+
63
+ def revoke!
64
+ say_status :revoke
65
+
66
+ regexp = if @behavior == :after
67
+ content = '\1\2'
68
+ /(#{flag})(.*)(#{Regexp.escape(replacement)})/m
69
+ else
70
+ content = '\2\3'
71
+ /(#{Regexp.escape(replacement)})(.*)(#{flag})/m
72
+ end
73
+
74
+ replace!(regexp, content, true)
75
+ end
76
+
77
+ protected
78
+
79
+ def say_status(behavior)
80
+ status = if behavior == :invoke
81
+ if flag == /\A/
82
+ :prepend
83
+ elsif flag == /\z/
84
+ :append
85
+ else
86
+ :insert
87
+ end
88
+ else
89
+ :subtract
90
+ end
91
+
92
+ super(status, config[:verbose])
93
+ end
94
+
95
+ # Adds the content to the file.
96
+ #
97
+ def replace!(regexp, string, force)
98
+ unless base.options[:pretend]
99
+ content = File.binread(destination)
100
+ if force || !content.include?(replacement)
101
+ content.gsub!(regexp, string)
102
+ File.open(destination, 'wb') { |file| file.write(content) }
103
+ end
104
+ end
105
+ end
106
+
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,579 @@
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
+ autoload :Actions, 'thor/actions'
12
+ autoload :RakeCompat, 'thor/rake_compat'
13
+
14
+ # Shortcuts for help.
15
+ HELP_MAPPINGS = %w(-h -? --help -D)
16
+
17
+ # Thor methods that should not be overwritten by the user.
18
+ THOR_RESERVED_WORDS = %w(invoke shell options behavior root destination_root relative_root
19
+ action add_file create_file in_root inside run run_ruby_script)
20
+
21
+ module Base
22
+ attr_accessor :options
23
+
24
+ # It receives arguments in an Array and two hashes, one for options and
25
+ # other for configuration.
26
+ #
27
+ # Notice that it does not check if all required arguments were supplied.
28
+ # It should be done by the parser.
29
+ #
30
+ # ==== Parameters
31
+ # args<Array[Object]>:: An array of objects. The objects are applied to their
32
+ # respective accessors declared with <tt>argument</tt>.
33
+ #
34
+ # options<Hash>:: An options hash that will be available as self.options.
35
+ # The hash given is converted to a hash with indifferent
36
+ # access, magic predicates (options.skip?) and then frozen.
37
+ #
38
+ # config<Hash>:: Configuration for this Thor class.
39
+ #
40
+ def initialize(args=[], options={}, config={})
41
+ args = Thor::Arguments.parse(self.class.arguments, args)
42
+ args.each { |key, value| send("#{key}=", value) }
43
+
44
+ parse_options = self.class.class_options
45
+
46
+ if options.is_a?(Array)
47
+ task_options = config.delete(:task_options) # hook for start
48
+ parse_options = parse_options.merge(task_options) if task_options
49
+ array_options, hash_options = options, {}
50
+ else
51
+ array_options, hash_options = [], options
52
+ end
53
+
54
+ opts = Thor::Options.new(parse_options, hash_options)
55
+ self.options = opts.parse(array_options)
56
+ opts.check_unknown! if self.class.check_unknown_options?(config)
57
+ end
58
+
59
+ class << self
60
+ def included(base) #:nodoc:
61
+ base.send :extend, ClassMethods
62
+ base.send :include, Invocation
63
+ base.send :include, Shell
64
+ end
65
+
66
+ # Returns the classes that inherits from Thor or Thor::Group.
67
+ #
68
+ # ==== Returns
69
+ # Array[Class]
70
+ #
71
+ def subclasses
72
+ @subclasses ||= []
73
+ end
74
+
75
+ # Returns the files where the subclasses are kept.
76
+ #
77
+ # ==== Returns
78
+ # Hash[path<String> => Class]
79
+ #
80
+ def subclass_files
81
+ @subclass_files ||= Hash.new{ |h,k| h[k] = [] }
82
+ end
83
+
84
+ # Whenever a class inherits from Thor or Thor::Group, we should track the
85
+ # class and the file on Thor::Base. This is the method responsable for it.
86
+ #
87
+ def register_klass_file(klass) #:nodoc:
88
+ file = caller[1].match(/(.*):\d+/)[1]
89
+ Thor::Base.subclasses << klass unless Thor::Base.subclasses.include?(klass)
90
+
91
+ file_subclasses = Thor::Base.subclass_files[File.expand_path(file)]
92
+ file_subclasses << klass unless file_subclasses.include?(klass)
93
+ end
94
+ end
95
+
96
+ module ClassMethods
97
+ attr_accessor :debugging
98
+
99
+ def attr_reader(*) #:nodoc:
100
+ no_tasks { super }
101
+ end
102
+
103
+ def attr_writer(*) #:nodoc:
104
+ no_tasks { super }
105
+ end
106
+
107
+ def attr_accessor(*) #:nodoc:
108
+ no_tasks { super }
109
+ end
110
+
111
+ # If you want to raise an error for unknown options, call check_unknown_options!
112
+ # This is disabled by default to allow dynamic invocations.
113
+ def check_unknown_options!
114
+ @check_unknown_options = true
115
+ end
116
+
117
+ def check_unknown_options #:nodoc:
118
+ @check_unknown_options ||= from_superclass(:check_unknown_options, false)
119
+ end
120
+
121
+ def check_unknown_options?(config) #:nodoc:
122
+ !!check_unknown_options
123
+ end
124
+
125
+ # Adds an argument to the class and creates an attr_accessor for it.
126
+ #
127
+ # Arguments are different from options in several aspects. The first one
128
+ # is how they are parsed from the command line, arguments are retrieved
129
+ # from position:
130
+ #
131
+ # thor task NAME
132
+ #
133
+ # Instead of:
134
+ #
135
+ # thor task --name=NAME
136
+ #
137
+ # Besides, arguments are used inside your code as an accessor (self.argument),
138
+ # while options are all kept in a hash (self.options).
139
+ #
140
+ # Finally, arguments cannot have type :default or :boolean but can be
141
+ # optional (supplying :optional => :true or :required => false), although
142
+ # you cannot have a required argument after a non-required argument. If you
143
+ # try it, an error is raised.
144
+ #
145
+ # ==== Parameters
146
+ # name<Symbol>:: The name of the argument.
147
+ # options<Hash>:: Described below.
148
+ #
149
+ # ==== Options
150
+ # :desc - Description for the argument.
151
+ # :required - If the argument is required or not.
152
+ # :optional - If the argument is optional or not.
153
+ # :type - The type of the argument, can be :string, :hash, :array, :numeric.
154
+ # :default - Default value for this argument. It cannot be required and have default values.
155
+ # :banner - String to show on usage notes.
156
+ #
157
+ # ==== Errors
158
+ # ArgumentError:: Raised if you supply a required argument after a non required one.
159
+ #
160
+ def argument(name, options={})
161
+ is_thor_reserved_word?(name, :argument)
162
+ no_tasks { attr_accessor name }
163
+
164
+ required = if options.key?(:optional)
165
+ !options[:optional]
166
+ elsif options.key?(:required)
167
+ options[:required]
168
+ else
169
+ options[:default].nil?
170
+ end
171
+
172
+ remove_argument name
173
+
174
+ arguments.each do |argument|
175
+ next if argument.required?
176
+ raise ArgumentError, "You cannot have #{name.to_s.inspect} as required argument after " <<
177
+ "the non-required argument #{argument.human_name.inspect}."
178
+ end if required
179
+
180
+ arguments << Thor::Argument.new(name, options[:desc], required, options[:type],
181
+ options[:default], options[:banner])
182
+ end
183
+
184
+ # Returns this class arguments, looking up in the ancestors chain.
185
+ #
186
+ # ==== Returns
187
+ # Array[Thor::Argument]
188
+ #
189
+ def arguments
190
+ @arguments ||= from_superclass(:arguments, [])
191
+ end
192
+
193
+ # Adds a bunch of options to the set of class options.
194
+ #
195
+ # class_options :foo => false, :bar => :required, :baz => :string
196
+ #
197
+ # If you prefer more detailed declaration, check class_option.
198
+ #
199
+ # ==== Parameters
200
+ # Hash[Symbol => Object]
201
+ #
202
+ def class_options(options=nil)
203
+ @class_options ||= from_superclass(:class_options, {})
204
+ build_options(options, @class_options) if options
205
+ @class_options
206
+ end
207
+
208
+ # Adds an option to the set of class options
209
+ #
210
+ # ==== Parameters
211
+ # name<Symbol>:: The name of the argument.
212
+ # options<Hash>:: Described below.
213
+ #
214
+ # ==== Options
215
+ # :desc - Description for the argument.
216
+ # :required - If the argument is required or not.
217
+ # :default - Default value for this argument.
218
+ # :group - The group for this options. Use by class options to output options in different levels.
219
+ # :aliases - Aliases for this option.
220
+ # :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
221
+ # :banner - String to show on usage notes.
222
+ #
223
+ def class_option(name, options={})
224
+ build_option(name, options, class_options)
225
+ end
226
+
227
+ # Removes a previous defined argument. If :undefine is given, undefine
228
+ # accessors as well.
229
+ #
230
+ # ==== Paremeters
231
+ # names<Array>:: Arguments to be removed
232
+ #
233
+ # ==== Examples
234
+ #
235
+ # remove_argument :foo
236
+ # remove_argument :foo, :bar, :baz, :undefine => true
237
+ #
238
+ def remove_argument(*names)
239
+ options = names.last.is_a?(Hash) ? names.pop : {}
240
+
241
+ names.each do |name|
242
+ arguments.delete_if { |a| a.name == name.to_s }
243
+ undef_method name, "#{name}=" if options[:undefine]
244
+ end
245
+ end
246
+
247
+ # Removes a previous defined class option.
248
+ #
249
+ # ==== Paremeters
250
+ # names<Array>:: Class options to be removed
251
+ #
252
+ # ==== Examples
253
+ #
254
+ # remove_class_option :foo
255
+ # remove_class_option :foo, :bar, :baz
256
+ #
257
+ def remove_class_option(*names)
258
+ names.each do |name|
259
+ class_options.delete(name)
260
+ end
261
+ end
262
+
263
+ # Defines the group. This is used when thor list is invoked so you can specify
264
+ # that only tasks from a pre-defined group will be shown. Defaults to standard.
265
+ #
266
+ # ==== Parameters
267
+ # name<String|Symbol>
268
+ #
269
+ def group(name=nil)
270
+ case name
271
+ when nil
272
+ @group ||= from_superclass(:group, 'standard')
273
+ else
274
+ @group = name.to_s
275
+ end
276
+ end
277
+
278
+ # Returns the tasks for this Thor class.
279
+ #
280
+ # ==== Returns
281
+ # OrderedHash:: An ordered hash with tasks names as keys and Thor::Task
282
+ # objects as values.
283
+ #
284
+ def tasks
285
+ @tasks ||= Thor::CoreExt::OrderedHash.new
286
+ end
287
+
288
+ # Returns the tasks for this Thor class and all subclasses.
289
+ #
290
+ # ==== Returns
291
+ # OrderedHash:: An ordered hash with tasks names as keys and Thor::Task
292
+ # objects as values.
293
+ #
294
+ def all_tasks
295
+ @all_tasks ||= from_superclass(:all_tasks, Thor::CoreExt::OrderedHash.new)
296
+ @all_tasks.merge(tasks)
297
+ end
298
+
299
+ # Removes a given task from this Thor class. This is usually done if you
300
+ # are inheriting from another class and don't want it to be available
301
+ # anymore.
302
+ #
303
+ # By default it only remove the mapping to the task. But you can supply
304
+ # :undefine => true to undefine the method from the class as well.
305
+ #
306
+ # ==== Parameters
307
+ # name<Symbol|String>:: The name of the task to be removed
308
+ # options<Hash>:: You can give :undefine => true if you want tasks the method
309
+ # to be undefined from the class as well.
310
+ #
311
+ def remove_task(*names)
312
+ options = names.last.is_a?(Hash) ? names.pop : {}
313
+
314
+ names.each do |name|
315
+ tasks.delete(name.to_s)
316
+ all_tasks.delete(name.to_s)
317
+ undef_method name if options[:undefine]
318
+ end
319
+ end
320
+
321
+ # All methods defined inside the given block are not added as tasks.
322
+ #
323
+ # So you can do:
324
+ #
325
+ # class MyScript < Thor
326
+ # no_tasks do
327
+ # def this_is_not_a_task
328
+ # end
329
+ # end
330
+ # end
331
+ #
332
+ # You can also add the method and remove it from the task list:
333
+ #
334
+ # class MyScript < Thor
335
+ # def this_is_not_a_task
336
+ # end
337
+ # remove_task :this_is_not_a_task
338
+ # end
339
+ #
340
+ def no_tasks
341
+ @no_tasks = true
342
+ yield
343
+ ensure
344
+ @no_tasks = false
345
+ end
346
+
347
+ # Sets the namespace for the Thor or Thor::Group class. By default the
348
+ # namespace is retrieved from the class name. If your Thor class is named
349
+ # Scripts::MyScript, the help method, for example, will be called as:
350
+ #
351
+ # thor scripts:my_script -h
352
+ #
353
+ # If you change the namespace:
354
+ #
355
+ # namespace :my_scripts
356
+ #
357
+ # You change how your tasks are invoked:
358
+ #
359
+ # thor my_scripts -h
360
+ #
361
+ # Finally, if you change your namespace to default:
362
+ #
363
+ # namespace :default
364
+ #
365
+ # Your tasks can be invoked with a shortcut. Instead of:
366
+ #
367
+ # thor :my_task
368
+ #
369
+ def namespace(name=nil)
370
+ case name
371
+ when nil
372
+ @namespace ||= Thor::Util.namespace_from_thor_class(self)
373
+ else
374
+ @namespace = name.to_s
375
+ end
376
+ end
377
+
378
+ # Parses the task and options from the given args, instantiate the class
379
+ # and invoke the task. This method is used when the arguments must be parsed
380
+ # from an array. If you are inside Ruby and want to use a Thor class, you
381
+ # can simply initialize it:
382
+ #
383
+ # script = MyScript.new(args, options, config)
384
+ # script.invoke(:task, first_arg, second_arg, third_arg)
385
+ #
386
+ def start(given_args=ARGV, config={})
387
+ self.debugging = given_args.delete("--debug")
388
+ config[:shell] ||= Thor::Base.shell.new
389
+ dispatch(nil, given_args.dup, nil, config)
390
+ rescue Thor::Error => e
391
+ debugging ? (raise e) : config[:shell].error(e.message)
392
+ exit(1) if exit_on_failure?
393
+ end
394
+
395
+ # Allows to use private methods from parent in child classes as tasks.
396
+ #
397
+ # ==== Paremeters
398
+ # names<Array>:: Method names to be used as tasks
399
+ #
400
+ # ==== Examples
401
+ #
402
+ # public_task :foo
403
+ # public_task :foo, :bar, :baz
404
+ #
405
+ def public_task(*names)
406
+ names.each do |name|
407
+ class_eval "def #{name}(*); super end"
408
+ end
409
+ end
410
+
411
+ def handle_no_task_error(task) #:nodoc:
412
+ if $thor_runner
413
+ raise UndefinedTaskError, "Could not find task #{task.inspect} in #{namespace.inspect} namespace."
414
+ else
415
+ raise UndefinedTaskError, "Could not find task #{task.inspect}."
416
+ end
417
+ end
418
+
419
+ def handle_argument_error(task, error) #:nodoc:
420
+ raise InvocationError, "#{task.name.inspect} was called incorrectly. Call as #{self.banner(task).inspect}."
421
+ end
422
+
423
+ protected
424
+
425
+ # Prints the class options per group. If an option does not belong to
426
+ # any group, it's printed as Class option.
427
+ #
428
+ def class_options_help(shell, groups={}) #:nodoc:
429
+ # Group options by group
430
+ class_options.each do |_, value|
431
+ groups[value.group] ||= []
432
+ groups[value.group] << value
433
+ end
434
+
435
+ # Deal with default group
436
+ global_options = groups.delete(nil) || []
437
+ print_options(shell, global_options)
438
+
439
+ # Print all others
440
+ groups.each do |group_name, options|
441
+ print_options(shell, options, group_name)
442
+ end
443
+ end
444
+
445
+ # Receives a set of options and print them.
446
+ def print_options(shell, options, group_name=nil)
447
+ return if options.empty?
448
+
449
+ list = []
450
+ padding = options.collect{ |o| o.aliases.size }.max.to_i * 4
451
+
452
+ options.each do |option|
453
+ item = [ option.usage(padding) ]
454
+ item.push(option.description ? "# #{option.description}" : "")
455
+
456
+ list << item
457
+ list << [ "", "# Default: #{option.default}" ] if option.show_default?
458
+ end
459
+
460
+ shell.say(group_name ? "#{group_name} options:" : "Options:")
461
+ shell.print_table(list, :ident => 2)
462
+ shell.say ""
463
+ end
464
+
465
+ # Raises an error if the word given is a Thor reserved word.
466
+ def is_thor_reserved_word?(word, type) #:nodoc:
467
+ return false unless THOR_RESERVED_WORDS.include?(word.to_s)
468
+ raise "#{word.inspect} is a Thor reserved word and cannot be defined as #{type}"
469
+ end
470
+
471
+ # Build an option and adds it to the given scope.
472
+ #
473
+ # ==== Parameters
474
+ # name<Symbol>:: The name of the argument.
475
+ # options<Hash>:: Described in both class_option and method_option.
476
+ def build_option(name, options, scope) #:nodoc:
477
+ scope[name] = Thor::Option.new(name, options[:desc], options[:required],
478
+ options[:type], options[:default], options[:banner],
479
+ options[:lazy_default], options[:group], options[:aliases])
480
+ end
481
+
482
+ # Receives a hash of options, parse them and add to the scope. This is a
483
+ # fast way to set a bunch of options:
484
+ #
485
+ # build_options :foo => true, :bar => :required, :baz => :string
486
+ #
487
+ # ==== Parameters
488
+ # Hash[Symbol => Object]
489
+ def build_options(options, scope) #:nodoc:
490
+ options.each do |key, value|
491
+ scope[key] = Thor::Option.parse(key, value)
492
+ end
493
+ end
494
+
495
+ # Finds a task with the given name. If the task belongs to the current
496
+ # class, just return it, otherwise dup it and add the fresh copy to the
497
+ # current task hash.
498
+ def find_and_refresh_task(name) #:nodoc:
499
+ task = if task = tasks[name.to_s]
500
+ task
501
+ elsif task = all_tasks[name.to_s]
502
+ tasks[name.to_s] = task.clone
503
+ else
504
+ raise ArgumentError, "You supplied :for => #{name.inspect}, but the task #{name.inspect} could not be found."
505
+ end
506
+ end
507
+
508
+ # Everytime someone inherits from a Thor class, register the klass
509
+ # and file into baseclass.
510
+ def inherited(klass)
511
+ Thor::Base.register_klass_file(klass)
512
+ end
513
+
514
+ # Fire this callback whenever a method is added. Added methods are
515
+ # tracked as tasks by invoking the create_task method.
516
+ def method_added(meth)
517
+ meth = meth.to_s
518
+
519
+ if meth == "initialize"
520
+ initialize_added
521
+ return
522
+ end
523
+
524
+ # Return if it's not a public instance method
525
+ return unless public_instance_methods.include?(meth) ||
526
+ public_instance_methods.include?(meth.to_sym)
527
+
528
+ return if @no_tasks || !create_task(meth)
529
+
530
+ is_thor_reserved_word?(meth, :task)
531
+ Thor::Base.register_klass_file(self)
532
+ end
533
+
534
+ # Retrieves a value from superclass. If it reaches the baseclass,
535
+ # returns default.
536
+ def from_superclass(method, default=nil)
537
+ if self == baseclass || !superclass.respond_to?(method, true)
538
+ default
539
+ else
540
+ value = superclass.send(method)
541
+ value.dup if value
542
+ end
543
+ end
544
+
545
+ # A flag that makes the process exit with status 1 if any error happens.
546
+ def exit_on_failure?
547
+ false
548
+ end
549
+
550
+ #
551
+ # The basename of the program invoking the thor class.
552
+ #
553
+ def basename
554
+ File.basename($0).split(' ').first
555
+ end
556
+
557
+ # SIGNATURE: Sets the baseclass. This is where the superclass lookup
558
+ # finishes.
559
+ def baseclass #:nodoc:
560
+ end
561
+
562
+ # SIGNATURE: Creates a new task if valid_task? is true. This method is
563
+ # called when a new method is added to the class.
564
+ def create_task(meth) #:nodoc:
565
+ end
566
+
567
+ # SIGNATURE: Defines behavior when the initialize method is added to the
568
+ # class.
569
+ def initialize_added #:nodoc:
570
+ end
571
+
572
+ # SIGNATURE: The hook invoked by start.
573
+ def dispatch(task, given_args, given_opts, config) #:nodoc:
574
+ raise NotImplementedError
575
+ end
576
+
577
+ end
578
+ end
579
+ end