thor 0.9.9 → 0.11.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. data/CHANGELOG.rdoc +29 -4
  2. data/README.rdoc +234 -0
  3. data/Thorfile +57 -0
  4. data/VERSION +1 -0
  5. data/bin/rake2thor +4 -0
  6. data/bin/thor +1 -1
  7. data/lib/thor.rb +216 -119
  8. data/lib/thor/actions.rb +272 -0
  9. data/lib/thor/actions/create_file.rb +102 -0
  10. data/lib/thor/actions/directory.rb +87 -0
  11. data/lib/thor/actions/empty_directory.rb +133 -0
  12. data/lib/thor/actions/file_manipulation.rb +195 -0
  13. data/lib/thor/actions/inject_into_file.rb +78 -0
  14. data/lib/thor/base.rb +510 -0
  15. data/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  16. data/lib/thor/core_ext/ordered_hash.rb +100 -0
  17. data/lib/thor/error.rb +25 -1
  18. data/lib/thor/group.rb +263 -0
  19. data/lib/thor/invocation.rb +178 -0
  20. data/lib/thor/parser.rb +4 -0
  21. data/lib/thor/parser/argument.rb +67 -0
  22. data/lib/thor/parser/arguments.rb +145 -0
  23. data/lib/thor/parser/option.rb +132 -0
  24. data/lib/thor/parser/options.rb +142 -0
  25. data/lib/thor/rake_compat.rb +67 -0
  26. data/lib/thor/runner.rb +232 -242
  27. data/lib/thor/shell.rb +72 -0
  28. data/lib/thor/shell/basic.rb +220 -0
  29. data/lib/thor/shell/color.rb +108 -0
  30. data/lib/thor/task.rb +97 -60
  31. data/lib/thor/util.rb +230 -55
  32. data/spec/actions/create_file_spec.rb +170 -0
  33. data/spec/actions/directory_spec.rb +118 -0
  34. data/spec/actions/empty_directory_spec.rb +91 -0
  35. data/spec/actions/file_manipulation_spec.rb +242 -0
  36. data/spec/actions/inject_into_file_spec.rb +80 -0
  37. data/spec/actions_spec.rb +291 -0
  38. data/spec/base_spec.rb +236 -0
  39. data/spec/core_ext/hash_with_indifferent_access_spec.rb +43 -0
  40. data/spec/core_ext/ordered_hash_spec.rb +115 -0
  41. data/spec/fixtures/bundle/execute.rb +6 -0
  42. data/spec/fixtures/doc/config.rb +1 -0
  43. data/spec/group_spec.rb +177 -0
  44. data/spec/invocation_spec.rb +107 -0
  45. data/spec/parser/argument_spec.rb +47 -0
  46. data/spec/parser/arguments_spec.rb +64 -0
  47. data/spec/parser/option_spec.rb +212 -0
  48. data/spec/parser/options_spec.rb +255 -0
  49. data/spec/rake_compat_spec.rb +64 -0
  50. data/spec/runner_spec.rb +204 -0
  51. data/spec/shell/basic_spec.rb +206 -0
  52. data/spec/shell/color_spec.rb +41 -0
  53. data/spec/shell_spec.rb +25 -0
  54. data/spec/spec_helper.rb +52 -0
  55. data/spec/task_spec.rb +82 -0
  56. data/spec/thor_spec.rb +234 -0
  57. data/spec/util_spec.rb +196 -0
  58. metadata +69 -25
  59. data/README.markdown +0 -76
  60. data/Rakefile +0 -6
  61. data/lib/thor/options.rb +0 -242
  62. data/lib/thor/ordered_hash.rb +0 -64
  63. data/lib/thor/task_hash.rb +0 -22
  64. data/lib/thor/tasks.rb +0 -77
  65. data/lib/thor/tasks/package.rb +0 -18
@@ -0,0 +1,75 @@
1
+ class Thor
2
+ module CoreExt #:nodoc:
3
+
4
+ # A hash with indifferent access and magic predicates.
5
+ #
6
+ # hash = Thor::CoreExt::HashWithIndifferentAccess.new 'foo' => 'bar', 'baz' => 'bee', 'force' => true
7
+ #
8
+ # hash[:foo] #=> 'bar'
9
+ # hash['foo'] #=> 'bar'
10
+ # hash.foo? #=> true
11
+ #
12
+ class HashWithIndifferentAccess < ::Hash #:nodoc:
13
+
14
+ def initialize(hash={})
15
+ super()
16
+ hash.each do |key, value|
17
+ self[convert_key(key)] = value
18
+ end
19
+ end
20
+
21
+ def [](key)
22
+ super(convert_key(key))
23
+ end
24
+
25
+ def []=(key, value)
26
+ super(convert_key(key), value)
27
+ end
28
+
29
+ def delete(key)
30
+ super(convert_key(key))
31
+ end
32
+
33
+ def values_at(*indices)
34
+ indices.collect { |key| self[convert_key(key)] }
35
+ end
36
+
37
+ def merge(other)
38
+ dup.merge!(other)
39
+ end
40
+
41
+ def merge!(other)
42
+ other.each do |key, value|
43
+ self[convert_key(key)] = value
44
+ end
45
+ self
46
+ end
47
+
48
+ protected
49
+
50
+ def convert_key(key)
51
+ key.is_a?(Symbol) ? key.to_s : key
52
+ end
53
+
54
+ # Magic predicates. For instance:
55
+ #
56
+ # options.force? # => !!options['force']
57
+ # options.shebang # => "/usr/lib/local/ruby"
58
+ # options.test_framework?(:rspec) # => options[:test_framework] == :rspec
59
+ #
60
+ def method_missing(method, *args, &block)
61
+ method = method.to_s
62
+ if method =~ /^(\w+)\?$/
63
+ if args.empty?
64
+ !!self[$1]
65
+ else
66
+ self[$1] == args.first
67
+ end
68
+ else
69
+ self[method]
70
+ end
71
+ end
72
+
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,100 @@
1
+ class Thor
2
+ module CoreExt #:nodoc:
3
+
4
+ if RUBY_VERSION >= '1.9'
5
+ class OrderedHash < ::Hash
6
+ end
7
+ else
8
+ # This class is based on the Ruby 1.9 ordered hashes.
9
+ #
10
+ # It keeps the semantics and most of the efficiency of normal hashes
11
+ # while also keeping track of the order in which elements were set.
12
+ #
13
+ class OrderedHash #:nodoc:
14
+ include Enumerable
15
+
16
+ Node = Struct.new(:key, :value, :next, :prev)
17
+
18
+ def initialize
19
+ @hash = {}
20
+ end
21
+
22
+ def [](key)
23
+ @hash[key] && @hash[key].value
24
+ end
25
+
26
+ def []=(key, value)
27
+ if node = @hash[key]
28
+ node.value = value
29
+ else
30
+ node = Node.new(key, value)
31
+
32
+ if @first.nil?
33
+ @first = @last = node
34
+ else
35
+ node.prev = @last
36
+ @last.next = node
37
+ @last = node
38
+ end
39
+ end
40
+
41
+ @hash[key] = node
42
+ value
43
+ end
44
+
45
+ def delete(key)
46
+ if node = @hash[key]
47
+ prev_node = node.prev
48
+ next_node = node.next
49
+
50
+ next_node.prev = prev_node if next_node
51
+ prev_node.next = next_node if prev_node
52
+
53
+ @first = next_node if @first == node
54
+ @last = prev_node if @last == node
55
+
56
+ value = node.value
57
+ end
58
+
59
+ @hash.delete(key)
60
+ value
61
+ end
62
+
63
+ def keys
64
+ self.map { |k, v| k }
65
+ end
66
+
67
+ def values
68
+ self.map { |k, v| v }
69
+ end
70
+
71
+ def each
72
+ return unless @first
73
+ yield [@first.key, @first.value]
74
+ node = @first
75
+ yield [node.key, node.value] while node = node.next
76
+ self
77
+ end
78
+
79
+ def merge(other)
80
+ hash = self.class.new
81
+
82
+ self.each do |key, value|
83
+ hash[key] = value
84
+ end
85
+
86
+ other.each do |key, value|
87
+ hash[key] = value
88
+ end
89
+
90
+ hash
91
+ end
92
+
93
+ def empty?
94
+ @hash.empty?
95
+ end
96
+ end
97
+ end
98
+
99
+ end
100
+ end
data/lib/thor/error.rb CHANGED
@@ -1,3 +1,27 @@
1
1
  class Thor
2
- class Error < Exception; end
2
+ # Thor::Error is raised when it's caused by wrong usage of thor classes. Those
3
+ # errors have their backtrace supressed and are nicely shown to the user.
4
+ #
5
+ # Errors that are caused by the developer, like declaring a method which
6
+ # overwrites a thor keyword, it SHOULD NOT raise a Thor::Error. This way, we
7
+ # ensure that developer errors are shown with full backtrace.
8
+ #
9
+ class Error < StandardError
10
+ end
11
+
12
+ # Raised when a task was not found.
13
+ #
14
+ class UndefinedTaskError < Error
15
+ end
16
+
17
+ # Raised when a task was found, but not invoked properly.
18
+ #
19
+ class InvocationError < Error
20
+ end
21
+
22
+ class RequiredArgumentMissingError < InvocationError
23
+ end
24
+
25
+ class MalformattedArgumentError < InvocationError
26
+ end
3
27
  end
data/lib/thor/group.rb ADDED
@@ -0,0 +1,263 @@
1
+ # Thor has a special class called Thor::Group. The main difference to Thor class
2
+ # is that it invokes all tasks at once. It also include some methods that allows
3
+ # invocations to be done at the class method, which are not available to Thor
4
+ # tasks.
5
+ #
6
+ class Thor::Group
7
+ class << self
8
+ # The descrition for this Thor::Group. If none is provided, but a source root
9
+ # exists, tries to find the USAGE one folder above it, otherwise searches
10
+ # in the superclass.
11
+ #
12
+ # ==== Parameters
13
+ # description<String>:: The description for this Thor::Group.
14
+ #
15
+ def desc(description=nil)
16
+ case description
17
+ when nil
18
+ @desc ||= from_superclass(:desc, nil)
19
+ else
20
+ @desc = description
21
+ end
22
+ end
23
+
24
+ # Start works differently in Thor::Group, it simply invokes all tasks
25
+ # inside the class.
26
+ #
27
+ def start(given_args=ARGV, config={})
28
+ super do
29
+ if Thor::HELP_MAPPINGS.include?(given_args.first)
30
+ help(config[:shell])
31
+ return
32
+ end
33
+
34
+ args, opts = Thor::Options.split(given_args)
35
+ new(args, opts, config).invoke
36
+ end
37
+ end
38
+
39
+ # Prints help information.
40
+ #
41
+ # ==== Options
42
+ # short:: When true, shows only usage.
43
+ #
44
+ def help(shell, options={})
45
+ if options[:short]
46
+ shell.say banner
47
+ else
48
+ shell.say "Usage:"
49
+ shell.say " #{banner}"
50
+ shell.say
51
+ class_options_help(shell)
52
+ shell.say self.desc if self.desc
53
+ end
54
+ end
55
+
56
+ # Stores invocations for this class merging with superclass values.
57
+ #
58
+ def invocations #:nodoc:
59
+ @invocations ||= from_superclass(:invocations, {})
60
+ end
61
+
62
+ # Stores invocation blocks used on invoke_from_option.
63
+ #
64
+ def invocation_blocks #:nodoc:
65
+ @invocation_blocks ||= from_superclass(:invocation_blocks, {})
66
+ end
67
+
68
+ # Invoke the given namespace or class given. It adds an instance
69
+ # method that will invoke the klass and task. You can give a block to
70
+ # configure how it will be invoked.
71
+ #
72
+ # The namespace/class given will have its options showed on the help
73
+ # usage. Check invoke_from_option for more information.
74
+ #
75
+ def invoke(*names, &block)
76
+ options = names.last.is_a?(Hash) ? names.pop : {}
77
+ verbose = options.fetch(:verbose, :white)
78
+
79
+ names.each do |name|
80
+ invocations[name] = false
81
+ invocation_blocks[name] = block if block_given?
82
+
83
+ class_eval <<-METHOD, __FILE__, __LINE__
84
+ def _invoke_#{name.to_s.gsub(/\W/, '_')}
85
+ klass, task = self.class.prepare_for_invocation(nil, #{name.inspect})
86
+
87
+ if klass
88
+ say_status :invoke, #{name.inspect}, #{verbose.inspect}
89
+ block = self.class.invocation_blocks[#{name.inspect}]
90
+ _invoke_for_class_method klass, task, &block
91
+ else
92
+ say_status :error, %(#{name.inspect} [not found]), :red
93
+ end
94
+ end
95
+ METHOD
96
+ end
97
+ end
98
+
99
+ # Invoke a thor class based on the value supplied by the user to the
100
+ # given option named "name". A class option must be created before this
101
+ # method is invoked for each name given.
102
+ #
103
+ # ==== Examples
104
+ #
105
+ # class GemGenerator < Thor::Group
106
+ # class_option :test_framework, :type => :string
107
+ # invoke_from_option :test_framework
108
+ # end
109
+ #
110
+ # ==== Boolean options
111
+ #
112
+ # In some cases, you want to invoke a thor class if some option is true or
113
+ # false. This is automatically handled by invoke_from_option. Then the
114
+ # option name is used to invoke the generator.
115
+ #
116
+ # ==== Preparing for invocation
117
+ #
118
+ # In some cases you want to customize how a specified hook is going to be
119
+ # invoked. You can do that by overwriting the class method
120
+ # prepare_for_invocation. The class method must necessarily return a klass
121
+ # and an optional task.
122
+ #
123
+ # ==== Custom invocations
124
+ #
125
+ # You can also supply a block to customize how the option is giong to be
126
+ # invoked. The block receives two parameters, an instance of the current
127
+ # class and the klass to be invoked.
128
+ #
129
+ def invoke_from_option(*names, &block)
130
+ options = names.last.is_a?(Hash) ? names.pop : {}
131
+ verbose = options.fetch(:verbose, :white)
132
+
133
+ names.each do |name|
134
+ unless class_options.key?(name)
135
+ raise ArgumentError, "You have to define the option #{name.inspect} " <<
136
+ "before setting invoke_from_option."
137
+ end
138
+
139
+ invocations[name] = true
140
+ invocation_blocks[name] = block if block_given?
141
+
142
+ class_eval <<-METHOD, __FILE__, __LINE__
143
+ def _invoke_from_option_#{name.to_s.gsub(/\W/, '_')}
144
+ return unless options[#{name.inspect}]
145
+
146
+ value = options[#{name.inspect}]
147
+ value = #{name.inspect} if TrueClass === value
148
+ klass, task = self.class.prepare_for_invocation(#{name.inspect}, value)
149
+
150
+ if klass
151
+ say_status :invoke, value, #{verbose.inspect}
152
+ block = self.class.invocation_blocks[#{name.inspect}]
153
+ _invoke_for_class_method klass, task, &block
154
+ else
155
+ say_status :error, %(\#{value} [not found]), :red
156
+ end
157
+ end
158
+ METHOD
159
+ end
160
+ end
161
+
162
+ # Remove a previously added invocation.
163
+ #
164
+ # ==== Examples
165
+ #
166
+ # remove_invocation :test_framework
167
+ #
168
+ def remove_invocation(*names)
169
+ names.each do |name|
170
+ remove_task(name)
171
+ remove_class_option(name)
172
+ invocations.delete(name)
173
+ invocation_blocks.delete(name)
174
+ end
175
+ end
176
+
177
+ # Overwrite class options help to allow invoked generators options to be
178
+ # shown recursively when invoking a generator.
179
+ #
180
+ def class_options_help(shell, ungrouped_name=nil, extra_group=nil) #:nodoc:
181
+ group_options = {}
182
+
183
+ get_options_from_invocations(group_options, class_options) do |klass|
184
+ klass.send(:get_options_from_invocations, group_options, class_options)
185
+ end
186
+
187
+ group_options.merge!(extra_group) if extra_group
188
+ super(shell, ungrouped_name, group_options)
189
+ end
190
+
191
+ # Get invocations array and merge options from invocations. Those
192
+ # options are added to group_options hash. Options that already exists
193
+ # in base_options are not added twice.
194
+ #
195
+ def get_options_from_invocations(group_options, base_options) #:nodoc:
196
+ invocations.each do |name, from_option|
197
+ value = if from_option
198
+ option = class_options[name]
199
+ option.type == :boolean ? name : option.default
200
+ else
201
+ name
202
+ end
203
+ next unless value
204
+
205
+ klass, task = prepare_for_invocation(name, value)
206
+ next unless klass && klass.respond_to?(:class_options)
207
+
208
+ value = value.to_s
209
+ human_name = value.respond_to?(:classify) ? value.classify : value
210
+
211
+ group_options[human_name] ||= []
212
+ group_options[human_name] += klass.class_options.values.select do |option|
213
+ base_options[option.name.to_sym].nil? && option.group.nil? &&
214
+ !group_options.values.flatten.any? { |i| i.name == option.name }
215
+ end
216
+
217
+ yield klass if block_given?
218
+ end
219
+ end
220
+
221
+ protected
222
+
223
+ # The banner for this class. You can customize it if you are invoking the
224
+ # thor class by another ways which is not the Thor::Runner.
225
+ #
226
+ def banner
227
+ "#{self.namespace} #{self.arguments.map {|a| a.usage }.join(' ')}"
228
+ end
229
+
230
+ def baseclass #:nodoc:
231
+ Thor::Group
232
+ end
233
+
234
+ def create_task(meth) #:nodoc:
235
+ tasks[meth.to_s] = Thor::Task.new(meth, nil, nil, nil)
236
+ true
237
+ end
238
+ end
239
+
240
+ include Thor::Base
241
+
242
+ protected
243
+
244
+ # Shortcut to invoke with padding and block handling. Use internally by
245
+ # invoke and invoke_from_option class methods.
246
+ #
247
+ def _invoke_for_class_method(klass, task=nil, *args, &block) #:nodoc:
248
+ shell.padding += 1
249
+
250
+ result = if block_given?
251
+ if block.arity == 2
252
+ block.call(self, klass)
253
+ else
254
+ block.call(self, klass, task)
255
+ end
256
+ else
257
+ invoke klass, task, *args
258
+ end
259
+
260
+ shell.padding -= 1
261
+ result
262
+ end
263
+ end