angry_mob 0.1.0

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