thor 0.14.6 → 0.19.4

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 (84) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/{CHANGELOG.rdoc → CHANGELOG.md} +91 -31
  4. data/CONTRIBUTING.md +15 -0
  5. data/{LICENSE → LICENSE.md} +2 -2
  6. data/README.md +36 -296
  7. data/bin/thor +1 -1
  8. data/lib/thor/actions/create_file.rb +33 -35
  9. data/lib/thor/actions/create_link.rb +7 -5
  10. data/lib/thor/actions/directory.rb +49 -24
  11. data/lib/thor/actions/empty_directory.rb +68 -67
  12. data/lib/thor/actions/file_manipulation.rb +85 -28
  13. data/lib/thor/actions/inject_into_file.rb +26 -32
  14. data/lib/thor/actions.rb +70 -66
  15. data/lib/thor/base.rb +314 -237
  16. data/lib/thor/command.rb +133 -0
  17. data/lib/thor/core_ext/hash_with_indifferent_access.rb +34 -24
  18. data/lib/thor/core_ext/io_binary_read.rb +12 -0
  19. data/lib/thor/core_ext/ordered_hash.rb +94 -65
  20. data/lib/thor/error.rb +10 -8
  21. data/lib/thor/group.rb +74 -66
  22. data/lib/thor/invocation.rb +65 -56
  23. data/lib/thor/line_editor/basic.rb +35 -0
  24. data/lib/thor/line_editor/readline.rb +88 -0
  25. data/lib/thor/line_editor.rb +17 -0
  26. data/lib/thor/parser/argument.rb +32 -29
  27. data/lib/thor/parser/arguments.rb +107 -93
  28. data/lib/thor/parser/option.rb +39 -13
  29. data/lib/thor/parser/options.rb +144 -97
  30. data/lib/thor/parser.rb +4 -4
  31. data/lib/thor/rake_compat.rb +21 -16
  32. data/lib/thor/runner.rb +166 -153
  33. data/lib/thor/shell/basic.rb +268 -122
  34. data/lib/thor/shell/color.rb +84 -43
  35. data/lib/thor/shell/html.rb +71 -66
  36. data/lib/thor/shell.rb +30 -37
  37. data/lib/thor/util.rb +227 -188
  38. data/lib/thor/version.rb +1 -1
  39. data/lib/thor.rb +289 -131
  40. data/thor.gemspec +21 -0
  41. metadata +50 -189
  42. data/Thorfile +0 -24
  43. data/bin/rake2thor +0 -86
  44. data/lib/thor/core_ext/file_binary_read.rb +0 -9
  45. data/lib/thor/task.rb +0 -114
  46. data/spec/actions/create_file_spec.rb +0 -170
  47. data/spec/actions/directory_spec.rb +0 -136
  48. data/spec/actions/empty_directory_spec.rb +0 -98
  49. data/spec/actions/file_manipulation_spec.rb +0 -310
  50. data/spec/actions/inject_into_file_spec.rb +0 -135
  51. data/spec/actions_spec.rb +0 -322
  52. data/spec/base_spec.rb +0 -269
  53. data/spec/core_ext/hash_with_indifferent_access_spec.rb +0 -43
  54. data/spec/core_ext/ordered_hash_spec.rb +0 -115
  55. data/spec/fixtures/application.rb +0 -2
  56. data/spec/fixtures/bundle/execute.rb +0 -6
  57. data/spec/fixtures/bundle/main.thor +0 -1
  58. data/spec/fixtures/doc/%file_name%.rb.tt +0 -1
  59. data/spec/fixtures/doc/README +0 -3
  60. data/spec/fixtures/doc/block_helper.rb +0 -3
  61. data/spec/fixtures/doc/components/.empty_directory +0 -0
  62. data/spec/fixtures/doc/config.rb +0 -1
  63. data/spec/fixtures/group.thor +0 -114
  64. data/spec/fixtures/invoke.thor +0 -112
  65. data/spec/fixtures/path with spaces +0 -0
  66. data/spec/fixtures/script.thor +0 -184
  67. data/spec/fixtures/task.thor +0 -10
  68. data/spec/group_spec.rb +0 -178
  69. data/spec/invocation_spec.rb +0 -100
  70. data/spec/parser/argument_spec.rb +0 -47
  71. data/spec/parser/arguments_spec.rb +0 -64
  72. data/spec/parser/option_spec.rb +0 -202
  73. data/spec/parser/options_spec.rb +0 -319
  74. data/spec/rake_compat_spec.rb +0 -68
  75. data/spec/register_spec.rb +0 -92
  76. data/spec/runner_spec.rb +0 -210
  77. data/spec/shell/basic_spec.rb +0 -223
  78. data/spec/shell/color_spec.rb +0 -41
  79. data/spec/shell/html_spec.rb +0 -27
  80. data/spec/shell_spec.rb +0 -47
  81. data/spec/spec_helper.rb +0 -54
  82. data/spec/task_spec.rb +0 -69
  83. data/spec/thor_spec.rb +0 -334
  84. data/spec/util_spec.rb +0 -163
data/lib/thor/actions.rb CHANGED
@@ -1,12 +1,12 @@
1
- require 'fileutils'
2
- require 'uri'
3
- require 'thor/core_ext/file_binary_read'
4
- require 'thor/actions/create_file'
5
- require 'thor/actions/create_link'
6
- require 'thor/actions/directory'
7
- require 'thor/actions/empty_directory'
8
- require 'thor/actions/file_manipulation'
9
- require 'thor/actions/inject_into_file'
1
+ require "fileutils"
2
+ require "uri"
3
+ require "thor/core_ext/io_binary_read"
4
+ require "thor/actions/create_file"
5
+ require "thor/actions/create_link"
6
+ require "thor/actions/directory"
7
+ require "thor/actions/empty_directory"
8
+ require "thor/actions/file_manipulation"
9
+ require "thor/actions/inject_into_file"
10
10
 
11
11
  class Thor
12
12
  module Actions
@@ -26,9 +26,9 @@ class Thor
26
26
  end
27
27
 
28
28
  # Stores and return the source root for this class
29
- def source_root(path=nil)
29
+ def source_root(path = nil)
30
30
  @_source_root = path if path
31
- @_source_root
31
+ @_source_root ||= nil
32
32
  end
33
33
 
34
34
  # Returns the source paths in the following order:
@@ -39,8 +39,8 @@ class Thor
39
39
  #
40
40
  def source_paths_for_search
41
41
  paths = []
42
- paths += self.source_paths
43
- paths << self.source_root if self.source_root
42
+ paths += source_paths
43
+ paths << source_root if source_root
44
44
  paths += from_superclass(:source_paths, [])
45
45
  paths
46
46
  end
@@ -55,7 +55,7 @@ class Thor
55
55
  :desc => "Run but do not make any changes"
56
56
 
57
57
  class_option :quiet, :type => :boolean, :aliases => "-q", :group => :runtime,
58
- :desc => "Supress status output"
58
+ :desc => "Suppress status output"
59
59
 
60
60
  class_option :skip, :type => :boolean, :aliases => "-s", :group => :runtime,
61
61
  :desc => "Skip files that already exist"
@@ -71,15 +71,15 @@ class Thor
71
71
  #
72
72
  # destination_root<String>:: The root directory needed for some actions.
73
73
  #
74
- def initialize(args=[], options={}, config={})
74
+ def initialize(args = [], options = {}, config = {})
75
75
  self.behavior = case config[:behavior].to_s
76
- when "force", "skip"
77
- _cleanup_options_and_set(options, config[:behavior])
78
- :invoke
79
- when "revoke"
80
- :revoke
81
- else
82
- :invoke
76
+ when "force", "skip"
77
+ _cleanup_options_and_set(options, config[:behavior])
78
+ :invoke
79
+ when "revoke"
80
+ :revoke
81
+ else
82
+ :invoke
83
83
  end
84
84
 
85
85
  super
@@ -107,15 +107,19 @@ class Thor
107
107
  #
108
108
  def destination_root=(root)
109
109
  @destination_stack ||= []
110
- @destination_stack[0] = File.expand_path(root || '')
110
+ @destination_stack[0] = File.expand_path(root || "")
111
111
  end
112
112
 
113
113
  # Returns the given path relative to the absolute root (ie, root where
114
114
  # the script started).
115
115
  #
116
- def relative_to_original_destination_root(path, remove_dot=true)
117
- path = path.gsub(@destination_stack[0], '.')
118
- remove_dot ? (path[2..-1] || '') : path
116
+ def relative_to_original_destination_root(path, remove_dot = true)
117
+ path = path.dup
118
+ if path.gsub!(@destination_stack[0], ".")
119
+ remove_dot ? (path[2..-1] || "") : path
120
+ else
121
+ path
122
+ end
119
123
  end
120
124
 
121
125
  # Holds source paths in instance so they can be manipulated.
@@ -127,11 +131,14 @@ class Thor
127
131
  # Receives a file or directory and search for it in the source paths.
128
132
  #
129
133
  def find_in_source_paths(file)
134
+ possible_files = [file, file + TEMPLATE_EXTNAME]
130
135
  relative_root = relative_to_original_destination_root(destination_root, false)
131
136
 
132
137
  source_paths.each do |source|
133
- source_file = File.expand_path(file, File.join(source, relative_root))
134
- return source_file if File.exists?(source_file)
138
+ possible_files.each do |f|
139
+ source_file = File.expand_path(f, File.join(source, relative_root))
140
+ return source_file if File.exist?(source_file)
141
+ end
135
142
  end
136
143
 
137
144
  message = "Could not find #{file.inspect} in any of your source paths. "
@@ -140,11 +147,11 @@ class Thor
140
147
  message << "Please invoke #{self.class.name}.source_root(PATH) with the PATH containing your templates. "
141
148
  end
142
149
 
143
- if source_paths.empty?
144
- message << "Currently you have no source paths."
145
- else
146
- message << "Your current source paths are: \n#{source_paths.join("\n")}"
147
- end
150
+ message << if source_paths.empty?
151
+ "Currently you have no source paths."
152
+ else
153
+ "Your current source paths are: \n#{source_paths.join("\n")}"
154
+ end
148
155
 
149
156
  raise Error, message
150
157
  end
@@ -158,7 +165,7 @@ class Thor
158
165
  # dir<String>:: the directory to move to.
159
166
  # config<Hash>:: give :verbose => true to log and use padding.
160
167
  #
161
- def inside(dir='', config={}, &block)
168
+ def inside(dir = "", config = {}, &block)
162
169
  verbose = config.fetch(:verbose, false)
163
170
  pretend = options[:pretend]
164
171
 
@@ -200,18 +207,18 @@ class Thor
200
207
  #
201
208
  # apply "recipes/jquery.rb"
202
209
  #
203
- def apply(path, config={})
210
+ def apply(path, config = {})
204
211
  verbose = config.fetch(:verbose, true)
205
- is_uri = path =~ /^https?\:\/\//
212
+ is_uri = path =~ %r{^https?\://}
206
213
  path = find_in_source_paths(path) unless is_uri
207
214
 
208
215
  say_status :apply, path, verbose
209
216
  shell.padding += 1 if verbose
210
217
 
211
- if is_uri
212
- contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read }
218
+ contents = if is_uri
219
+ open(path, "Accept" => "application/x-thor-template", &:read)
213
220
  else
214
- contents = open(path) {|io| io.read }
221
+ open(path, &:read)
215
222
  end
216
223
 
217
224
  instance_eval(contents, path)
@@ -223,7 +230,7 @@ class Thor
223
230
  # ==== Parameters
224
231
  # command<String>:: the command to be executed.
225
232
  # config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output. Specify :with
226
- # to append an executable to command executation.
233
+ # to append an executable to command execution.
227
234
  #
228
235
  # ==== Example
229
236
  #
@@ -231,7 +238,7 @@ class Thor
231
238
  # run('ln -s ~/edge rails')
232
239
  # end
233
240
  #
234
- def run(command, config={})
241
+ def run(command, config = {})
235
242
  return unless behavior == :invoke
236
243
 
237
244
  destination = relative_to_original_destination_root(destination_root, false)
@@ -244,9 +251,7 @@ class Thor
244
251
 
245
252
  say_status :run, desc, config.fetch(:verbose, true)
246
253
 
247
- unless options[:pretend]
248
- config[:capture] ? `#{command}` : system("#{command}")
249
- end
254
+ !options[:pretend] && config[:capture] ? `#{command}` : system(command.to_s)
250
255
  end
251
256
 
252
257
  # Executes a ruby script (taking into account WIN32 platform quirks).
@@ -255,7 +260,7 @@ class Thor
255
260
  # command<String>:: the command to be executed.
256
261
  # config<Hash>:: give :verbose => false to not log the status.
257
262
  #
258
- def run_ruby_script(command, config={})
263
+ def run_ruby_script(command, config = {})
259
264
  return unless behavior == :invoke
260
265
  run command, config.merge(:with => Thor::Util.ruby_command)
261
266
  end
@@ -264,8 +269,8 @@ class Thor
264
269
  # switches.
265
270
  #
266
271
  # ==== Parameters
267
- # task<String>:: the task to be invoked
268
- # args<Array>:: arguments to the task
272
+ # command<String>:: the command to be invoked
273
+ # args<Array>:: arguments to the command
269
274
  # config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output.
270
275
  # Other options are given as parameter to Thor.
271
276
  #
@@ -278,37 +283,36 @@ class Thor
278
283
  # thor :list, :all => true, :substring => 'rails'
279
284
  # #=> thor list --all --substring=rails
280
285
  #
281
- def thor(task, *args)
286
+ def thor(command, *args)
282
287
  config = args.last.is_a?(Hash) ? args.pop : {}
283
288
  verbose = config.key?(:verbose) ? config.delete(:verbose) : true
284
289
  pretend = config.key?(:pretend) ? config.delete(:pretend) : false
285
290
  capture = config.key?(:capture) ? config.delete(:capture) : false
286
291
 
287
- args.unshift task
292
+ args.unshift(command)
288
293
  args.push Thor::Options.to_switches(config)
289
- command = args.join(' ').strip
294
+ command = args.join(" ").strip
290
295
 
291
296
  run command, :with => :thor, :verbose => verbose, :pretend => pretend, :capture => capture
292
297
  end
293
298
 
294
- protected
299
+ protected
295
300
 
296
- # Allow current root to be shared between invocations.
297
- #
298
- def _shared_configuration #:nodoc:
299
- super.merge!(:destination_root => self.destination_root)
300
- end
301
+ # Allow current root to be shared between invocations.
302
+ #
303
+ def _shared_configuration #:nodoc:
304
+ super.merge!(:destination_root => destination_root)
305
+ end
301
306
 
302
- def _cleanup_options_and_set(options, key) #:nodoc:
303
- case options
304
- when Array
305
- %w(--force -f --skip -s).each { |i| options.delete(i) }
306
- options << "--#{key}"
307
- when Hash
308
- [:force, :skip, "force", "skip"].each { |i| options.delete(i) }
309
- options.merge!(key => true)
310
- end
307
+ def _cleanup_options_and_set(options, key) #:nodoc:
308
+ case options
309
+ when Array
310
+ %w(--force -f --skip -s).each { |i| options.delete(i) }
311
+ options << "--#{key}"
312
+ when Hash
313
+ [:force, :skip, "force", "skip"].each { |i| options.delete(i) }
314
+ options.merge!(key => true)
311
315
  end
312
-
316
+ end
313
317
  end
314
318
  end