bundler 0.8.1 → 0.9.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bundler might be problematic. Click here for more details.

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