thor 0.16.0 → 1.2.1

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 (93) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +15 -0
  3. data/README.md +23 -6
  4. data/bin/thor +1 -1
  5. data/lib/thor/actions/create_file.rb +34 -35
  6. data/lib/thor/actions/create_link.rb +9 -5
  7. data/lib/thor/actions/directory.rb +33 -23
  8. data/lib/thor/actions/empty_directory.rb +75 -85
  9. data/lib/thor/actions/file_manipulation.rb +103 -36
  10. data/lib/thor/actions/inject_into_file.rb +46 -36
  11. data/lib/thor/actions.rb +90 -68
  12. data/lib/thor/base.rb +302 -244
  13. data/lib/thor/command.rb +142 -0
  14. data/lib/thor/core_ext/hash_with_indifferent_access.rb +52 -24
  15. data/lib/thor/error.rb +90 -10
  16. data/lib/thor/group.rb +70 -74
  17. data/lib/thor/invocation.rb +63 -55
  18. data/lib/thor/line_editor/basic.rb +37 -0
  19. data/lib/thor/line_editor/readline.rb +88 -0
  20. data/lib/thor/line_editor.rb +17 -0
  21. data/lib/thor/nested_context.rb +29 -0
  22. data/lib/thor/parser/argument.rb +24 -28
  23. data/lib/thor/parser/arguments.rb +110 -102
  24. data/lib/thor/parser/option.rb +53 -15
  25. data/lib/thor/parser/options.rb +174 -97
  26. data/lib/thor/parser.rb +4 -4
  27. data/lib/thor/rake_compat.rb +12 -11
  28. data/lib/thor/runner.rb +159 -155
  29. data/lib/thor/shell/basic.rb +216 -93
  30. data/lib/thor/shell/color.rb +53 -40
  31. data/lib/thor/shell/html.rb +61 -58
  32. data/lib/thor/shell.rb +29 -36
  33. data/lib/thor/util.rb +231 -213
  34. data/lib/thor/version.rb +1 -1
  35. data/lib/thor.rb +303 -166
  36. data/thor.gemspec +27 -24
  37. metadata +36 -226
  38. data/.gitignore +0 -44
  39. data/.rspec +0 -2
  40. data/.travis.yml +0 -7
  41. data/CHANGELOG.rdoc +0 -134
  42. data/Gemfile +0 -15
  43. data/Thorfile +0 -30
  44. data/bin/rake2thor +0 -86
  45. data/lib/thor/core_ext/dir_escape.rb +0 -0
  46. data/lib/thor/core_ext/file_binary_read.rb +0 -9
  47. data/lib/thor/core_ext/ordered_hash.rb +0 -100
  48. data/lib/thor/task.rb +0 -132
  49. data/spec/actions/create_file_spec.rb +0 -170
  50. data/spec/actions/create_link_spec.rb +0 -81
  51. data/spec/actions/directory_spec.rb +0 -149
  52. data/spec/actions/empty_directory_spec.rb +0 -130
  53. data/spec/actions/file_manipulation_spec.rb +0 -370
  54. data/spec/actions/inject_into_file_spec.rb +0 -135
  55. data/spec/actions_spec.rb +0 -331
  56. data/spec/base_spec.rb +0 -279
  57. data/spec/core_ext/hash_with_indifferent_access_spec.rb +0 -43
  58. data/spec/core_ext/ordered_hash_spec.rb +0 -115
  59. data/spec/exit_condition_spec.rb +0 -19
  60. data/spec/fixtures/application.rb +0 -2
  61. data/spec/fixtures/app{1}/README +0 -3
  62. data/spec/fixtures/bundle/execute.rb +0 -6
  63. data/spec/fixtures/bundle/main.thor +0 -1
  64. data/spec/fixtures/doc/%file_name%.rb.tt +0 -1
  65. data/spec/fixtures/doc/COMMENTER +0 -10
  66. data/spec/fixtures/doc/README +0 -3
  67. data/spec/fixtures/doc/block_helper.rb +0 -3
  68. data/spec/fixtures/doc/components/.empty_directory +0 -0
  69. data/spec/fixtures/doc/config.rb +0 -1
  70. data/spec/fixtures/doc/config.yaml.tt +0 -1
  71. data/spec/fixtures/enum.thor +0 -10
  72. data/spec/fixtures/group.thor +0 -114
  73. data/spec/fixtures/invoke.thor +0 -112
  74. data/spec/fixtures/path with spaces +0 -0
  75. data/spec/fixtures/script.thor +0 -190
  76. data/spec/fixtures/task.thor +0 -10
  77. data/spec/group_spec.rb +0 -216
  78. data/spec/invocation_spec.rb +0 -100
  79. data/spec/parser/argument_spec.rb +0 -53
  80. data/spec/parser/arguments_spec.rb +0 -66
  81. data/spec/parser/option_spec.rb +0 -202
  82. data/spec/parser/options_spec.rb +0 -330
  83. data/spec/rake_compat_spec.rb +0 -72
  84. data/spec/register_spec.rb +0 -135
  85. data/spec/runner_spec.rb +0 -241
  86. data/spec/shell/basic_spec.rb +0 -300
  87. data/spec/shell/color_spec.rb +0 -81
  88. data/spec/shell/html_spec.rb +0 -32
  89. data/spec/shell_spec.rb +0 -47
  90. data/spec/spec_helper.rb +0 -59
  91. data/spec/task_spec.rb +0 -80
  92. data/spec/thor_spec.rb +0 -418
  93. data/spec/util_spec.rb +0 -196
data/lib/thor/util.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'rbconfig'
1
+ require "rbconfig"
2
2
 
3
3
  class Thor
4
4
  module Sandbox #:nodoc:
@@ -15,252 +15,270 @@ class Thor
15
15
  # Thor::Util.load_thorfile("~/.thor/foo")
16
16
  #
17
17
  module Util
18
+ class << self
19
+ # Receives a namespace and search for it in the Thor::Base subclasses.
20
+ #
21
+ # ==== Parameters
22
+ # namespace<String>:: The namespace to search for.
23
+ #
24
+ def find_by_namespace(namespace)
25
+ namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
26
+ Thor::Base.subclasses.detect { |klass| klass.namespace == namespace }
27
+ end
18
28
 
19
- # Receives a namespace and search for it in the Thor::Base subclasses.
20
- #
21
- # ==== Parameters
22
- # namespace<String>:: The namespace to search for.
23
- #
24
- def self.find_by_namespace(namespace)
25
- namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
26
- Thor::Base.subclasses.find { |klass| klass.namespace == namespace }
27
- end
28
-
29
- # Receives a constant and converts it to a Thor namespace. Since Thor tasks
30
- # can be added to a sandbox, this method is also responsable for removing
31
- # the sandbox namespace.
32
- #
33
- # This method should not be used in general because it's used to deal with
34
- # older versions of Thor. On current versions, if you need to get the
35
- # namespace from a class, just call namespace on it.
36
- #
37
- # ==== Parameters
38
- # constant<Object>:: The constant to be converted to the thor path.
39
- #
40
- # ==== Returns
41
- # String:: If we receive Foo::Bar::Baz it returns "foo:bar:baz"
42
- #
43
- def self.namespace_from_thor_class(constant)
44
- constant = constant.to_s.gsub(/^Thor::Sandbox::/, "")
45
- constant = snake_case(constant).squeeze(":")
46
- constant
47
- end
29
+ # Receives a constant and converts it to a Thor namespace. Since Thor
30
+ # commands can be added to a sandbox, this method is also responsible for
31
+ # removing the sandbox namespace.
32
+ #
33
+ # This method should not be used in general because it's used to deal with
34
+ # older versions of Thor. On current versions, if you need to get the
35
+ # namespace from a class, just call namespace on it.
36
+ #
37
+ # ==== Parameters
38
+ # constant<Object>:: The constant to be converted to the thor path.
39
+ #
40
+ # ==== Returns
41
+ # String:: If we receive Foo::Bar::Baz it returns "foo:bar:baz"
42
+ #
43
+ def namespace_from_thor_class(constant)
44
+ constant = constant.to_s.gsub(/^Thor::Sandbox::/, "")
45
+ constant = snake_case(constant).squeeze(":")
46
+ constant
47
+ end
48
48
 
49
- # Given the contents, evaluate it inside the sandbox and returns the
50
- # namespaces defined in the sandbox.
51
- #
52
- # ==== Parameters
53
- # contents<String>
54
- #
55
- # ==== Returns
56
- # Array[Object]
57
- #
58
- def self.namespaces_in_content(contents, file=__FILE__)
59
- old_constants = Thor::Base.subclasses.dup
60
- Thor::Base.subclasses.clear
49
+ # Given the contents, evaluate it inside the sandbox and returns the
50
+ # namespaces defined in the sandbox.
51
+ #
52
+ # ==== Parameters
53
+ # contents<String>
54
+ #
55
+ # ==== Returns
56
+ # Array[Object]
57
+ #
58
+ def namespaces_in_content(contents, file = __FILE__)
59
+ old_constants = Thor::Base.subclasses.dup
60
+ Thor::Base.subclasses.clear
61
61
 
62
- load_thorfile(file, contents)
62
+ load_thorfile(file, contents)
63
63
 
64
- new_constants = Thor::Base.subclasses.dup
65
- Thor::Base.subclasses.replace(old_constants)
64
+ new_constants = Thor::Base.subclasses.dup
65
+ Thor::Base.subclasses.replace(old_constants)
66
66
 
67
- new_constants.map!{ |c| c.namespace }
68
- new_constants.compact!
69
- new_constants
70
- end
71
-
72
- # Returns the thor classes declared inside the given class.
73
- #
74
- def self.thor_classes_in(klass)
75
- stringfied_constants = klass.constants.map { |c| c.to_s }
76
- Thor::Base.subclasses.select do |subclass|
77
- next unless subclass.name
78
- stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ''))
67
+ new_constants.map!(&:namespace)
68
+ new_constants.compact!
69
+ new_constants
79
70
  end
80
- end
81
71
 
82
- # Receives a string and convert it to snake case. SnakeCase returns snake_case.
83
- #
84
- # ==== Parameters
85
- # String
86
- #
87
- # ==== Returns
88
- # String
89
- #
90
- def self.snake_case(str)
91
- return str.downcase if str =~ /^[A-Z_]+$/
92
- str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
93
- return $+.downcase
94
- end
95
-
96
- # Receives a string and convert it to camel case. camel_case returns CamelCase.
97
- #
98
- # ==== Parameters
99
- # String
100
- #
101
- # ==== Returns
102
- # String
103
- #
104
- def self.camel_case(str)
105
- return str if str !~ /_/ && str =~ /[A-Z]+.*/
106
- str.split('_').map { |i| i.capitalize }.join
107
- end
108
-
109
- # Receives a namespace and tries to retrieve a Thor or Thor::Group class
110
- # from it. It first searches for a class using the all the given namespace,
111
- # if it's not found, removes the highest entry and searches for the class
112
- # again. If found, returns the highest entry as the class name.
113
- #
114
- # ==== Examples
115
- #
116
- # class Foo::Bar < Thor
117
- # def baz
118
- # end
119
- # end
120
- #
121
- # class Baz::Foo < Thor::Group
122
- # end
123
- #
124
- # Thor::Util.namespace_to_thor_class("foo:bar") #=> Foo::Bar, nil # will invoke default task
125
- # Thor::Util.namespace_to_thor_class("baz:foo") #=> Baz::Foo, nil
126
- # Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"
127
- #
128
- # ==== Parameters
129
- # namespace<String>
130
- #
131
- def self.find_class_and_task_by_namespace(namespace, fallback = true)
132
- if namespace.include?(?:) # look for a namespaced task
133
- pieces = namespace.split(":")
134
- task = pieces.pop
135
- klass = Thor::Util.find_by_namespace(pieces.join(":"))
136
- end
137
- unless klass # look for a Thor::Group with the right name
138
- klass, task = Thor::Util.find_by_namespace(namespace), nil
72
+ # Returns the thor classes declared inside the given class.
73
+ #
74
+ def thor_classes_in(klass)
75
+ stringfied_constants = klass.constants.map(&:to_s)
76
+ Thor::Base.subclasses.select do |subclass|
77
+ next unless subclass.name
78
+ stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ""))
79
+ end
139
80
  end
140
- if !klass && fallback # try a task in the default namespace
141
- task = namespace
142
- klass = Thor::Util.find_by_namespace('')
81
+
82
+ # Receives a string and convert it to snake case. SnakeCase returns snake_case.
83
+ #
84
+ # ==== Parameters
85
+ # String
86
+ #
87
+ # ==== Returns
88
+ # String
89
+ #
90
+ def snake_case(str)
91
+ return str.downcase if str =~ /^[A-Z_]+$/
92
+ str.gsub(/\B[A-Z]/, '_\&').squeeze("_") =~ /_*(.*)/
93
+ $+.downcase
143
94
  end
144
- return klass, task
145
- end
146
95
 
147
- # Receives a path and load the thor file in the path. The file is evaluated
148
- # inside the sandbox to avoid namespacing conflicts.
149
- #
150
- def self.load_thorfile(path, content=nil, debug=false)
151
- content ||= File.binread(path)
96
+ # Receives a string and convert it to camel case. camel_case returns CamelCase.
97
+ #
98
+ # ==== Parameters
99
+ # String
100
+ #
101
+ # ==== Returns
102
+ # String
103
+ #
104
+ def camel_case(str)
105
+ return str if str !~ /_/ && str =~ /[A-Z]+.*/
106
+ str.split("_").map(&:capitalize).join
107
+ end
152
108
 
153
- begin
154
- Thor::Sandbox.class_eval(content, path)
155
- rescue Exception => e
156
- $stderr.puts("WARNING: unable to load thorfile #{path.inspect}: #{e.message}")
157
- if debug
158
- $stderr.puts(*e.backtrace)
159
- else
160
- $stderr.puts(e.backtrace.first)
109
+ # Receives a namespace and tries to retrieve a Thor or Thor::Group class
110
+ # from it. It first searches for a class using the all the given namespace,
111
+ # if it's not found, removes the highest entry and searches for the class
112
+ # again. If found, returns the highest entry as the class name.
113
+ #
114
+ # ==== Examples
115
+ #
116
+ # class Foo::Bar < Thor
117
+ # def baz
118
+ # end
119
+ # end
120
+ #
121
+ # class Baz::Foo < Thor::Group
122
+ # end
123
+ #
124
+ # Thor::Util.namespace_to_thor_class("foo:bar") #=> Foo::Bar, nil # will invoke default command
125
+ # Thor::Util.namespace_to_thor_class("baz:foo") #=> Baz::Foo, nil
126
+ # Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"
127
+ #
128
+ # ==== Parameters
129
+ # namespace<String>
130
+ #
131
+ def find_class_and_command_by_namespace(namespace, fallback = true)
132
+ if namespace.include?(":") # look for a namespaced command
133
+ pieces = namespace.split(":")
134
+ command = pieces.pop
135
+ klass = Thor::Util.find_by_namespace(pieces.join(":"))
136
+ end
137
+ unless klass # look for a Thor::Group with the right name
138
+ klass = Thor::Util.find_by_namespace(namespace)
139
+ command = nil
161
140
  end
141
+ if !klass && fallback # try a command in the default namespace
142
+ command = namespace
143
+ klass = Thor::Util.find_by_namespace("")
144
+ end
145
+ [klass, command]
162
146
  end
163
- end
147
+ alias_method :find_class_and_task_by_namespace, :find_class_and_command_by_namespace
148
+
149
+ # Receives a path and load the thor file in the path. The file is evaluated
150
+ # inside the sandbox to avoid namespacing conflicts.
151
+ #
152
+ def load_thorfile(path, content = nil, debug = false)
153
+ content ||= File.binread(path)
164
154
 
165
- def self.user_home
166
- @@user_home ||= if ENV["HOME"]
167
- ENV["HOME"]
168
- elsif ENV["USERPROFILE"]
169
- ENV["USERPROFILE"]
170
- elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
171
- File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"])
172
- elsif ENV["APPDATA"]
173
- ENV["APPDATA"]
174
- else
175
155
  begin
176
- File.expand_path("~")
177
- rescue
178
- if File::ALT_SEPARATOR
179
- "C:/"
156
+ Thor::Sandbox.class_eval(content, path)
157
+ rescue StandardError => e
158
+ $stderr.puts("WARNING: unable to load thorfile #{path.inspect}: #{e.message}")
159
+ if debug
160
+ $stderr.puts(*e.backtrace)
180
161
  else
181
- "/"
162
+ $stderr.puts(e.backtrace.first)
182
163
  end
183
164
  end
184
165
  end
185
- end
186
166
 
187
- # Returns the root where thor files are located, depending on the OS.
188
- #
189
- def self.thor_root
190
- File.join(user_home, ".thor").gsub(/\\/, '/')
191
- end
167
+ def user_home
168
+ @@user_home ||= if ENV["HOME"]
169
+ ENV["HOME"]
170
+ elsif ENV["USERPROFILE"]
171
+ ENV["USERPROFILE"]
172
+ elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
173
+ File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"])
174
+ elsif ENV["APPDATA"]
175
+ ENV["APPDATA"]
176
+ else
177
+ begin
178
+ File.expand_path("~")
179
+ rescue
180
+ if File::ALT_SEPARATOR
181
+ "C:/"
182
+ else
183
+ "/"
184
+ end
185
+ end
186
+ end
187
+ end
188
+
189
+ # Returns the root where thor files are located, depending on the OS.
190
+ #
191
+ def thor_root
192
+ File.join(user_home, ".thor").tr('\\', "/")
193
+ end
192
194
 
193
- # Returns the files in the thor root. On Windows thor_root will be something
194
- # like this:
195
- #
196
- # C:\Documents and Settings\james\.thor
197
- #
198
- # If we don't #gsub the \ character, Dir.glob will fail.
199
- #
200
- def self.thor_root_glob
201
- files = Dir["#{escape_globs(thor_root)}/*"]
195
+ # Returns the files in the thor root. On Windows thor_root will be something
196
+ # like this:
197
+ #
198
+ # C:\Documents and Settings\james\.thor
199
+ #
200
+ # If we don't #gsub the \ character, Dir.glob will fail.
201
+ #
202
+ def thor_root_glob
203
+ files = Dir["#{escape_globs(thor_root)}/*"]
202
204
 
203
- files.map! do |file|
204
- File.directory?(file) ? File.join(file, "main.thor") : file
205
+ files.map! do |file|
206
+ File.directory?(file) ? File.join(file, "main.thor") : file
207
+ end
205
208
  end
206
- end
207
209
 
208
- # Where to look for Thor files.
209
- #
210
- def self.globs_for(path)
211
- path = escape_globs(path)
212
- ["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
213
- end
210
+ # Where to look for Thor files.
211
+ #
212
+ def globs_for(path)
213
+ path = escape_globs(path)
214
+ ["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/**/*.thor"]
215
+ end
214
216
 
215
- # Return the path to the ruby interpreter taking into account multiple
216
- # installations and windows extensions.
217
- #
218
- def self.ruby_command
219
- @ruby_command ||= begin
220
- ruby_name = RbConfig::CONFIG['ruby_install_name']
221
- ruby = File.join(RbConfig::CONFIG['bindir'], ruby_name)
222
- ruby << RbConfig::CONFIG['EXEEXT']
217
+ # Return the path to the ruby interpreter taking into account multiple
218
+ # installations and windows extensions.
219
+ #
220
+ def ruby_command
221
+ @ruby_command ||= begin
222
+ ruby_name = RbConfig::CONFIG["ruby_install_name"]
223
+ ruby = File.join(RbConfig::CONFIG["bindir"], ruby_name)
224
+ ruby << RbConfig::CONFIG["EXEEXT"]
223
225
 
224
- # avoid using different name than ruby (on platforms supporting links)
225
- if ruby_name != 'ruby' && File.respond_to?(:readlink)
226
- begin
227
- alternate_ruby = File.join(RbConfig::CONFIG['bindir'], 'ruby')
228
- alternate_ruby << RbConfig::CONFIG['EXEEXT']
226
+ # avoid using different name than ruby (on platforms supporting links)
227
+ if ruby_name != "ruby" && File.respond_to?(:readlink)
228
+ begin
229
+ alternate_ruby = File.join(RbConfig::CONFIG["bindir"], "ruby")
230
+ alternate_ruby << RbConfig::CONFIG["EXEEXT"]
229
231
 
230
- # ruby is a symlink
231
- if File.symlink? alternate_ruby
232
- linked_ruby = File.readlink alternate_ruby
232
+ # ruby is a symlink
233
+ if File.symlink? alternate_ruby
234
+ linked_ruby = File.readlink alternate_ruby
233
235
 
234
- # symlink points to 'ruby_install_name'
235
- ruby = alternate_ruby if linked_ruby == ruby_name || linked_ruby == ruby
236
+ # symlink points to 'ruby_install_name'
237
+ ruby = alternate_ruby if linked_ruby == ruby_name || linked_ruby == ruby
238
+ end
239
+ rescue NotImplementedError # rubocop:disable HandleExceptions
240
+ # just ignore on windows
236
241
  end
237
- rescue NotImplementedError
238
- # just ignore on windows
239
242
  end
243
+
244
+ # escape string in case path to ruby executable contain spaces.
245
+ ruby.sub!(/.*\s.*/m, '"\&"')
246
+ ruby
240
247
  end
248
+ end
241
249
 
242
- # escape string in case path to ruby executable contain spaces.
243
- ruby.sub!(/.*\s.*/m, '"\&"')
244
- ruby
250
+ # Returns a string that has had any glob characters escaped.
251
+ # The glob characters are `* ? { } [ ]`.
252
+ #
253
+ # ==== Examples
254
+ #
255
+ # Thor::Util.escape_globs('[apps]') # => '\[apps\]'
256
+ #
257
+ # ==== Parameters
258
+ # String
259
+ #
260
+ # ==== Returns
261
+ # String
262
+ #
263
+ def escape_globs(path)
264
+ path.to_s.gsub(/[*?{}\[\]]/, '\\\\\\&')
245
265
  end
246
- end
247
266
 
248
- # Returns a string that has had any glob characters escaped.
249
- # The glob characters are `* ? { } [ ]`.
250
- #
251
- # ==== Examples
252
- #
253
- # Thor::Util.escape_globs('[apps]') # => '\[apps\]'
254
- #
255
- # ==== Parameters
256
- # String
257
- #
258
- # ==== Returns
259
- # String
260
- #
261
- def self.escape_globs(path)
262
- path.to_s.gsub(/[*?{}\[\]]/, '\\\\\\&')
267
+ # Returns a string that has had any HTML characters escaped.
268
+ #
269
+ # ==== Examples
270
+ #
271
+ # Thor::Util.escape_html('<div>') # => "&lt;div&gt;"
272
+ #
273
+ # ==== Parameters
274
+ # String
275
+ #
276
+ # ==== Returns
277
+ # String
278
+ #
279
+ def escape_html(string)
280
+ CGI.escapeHTML(string)
281
+ end
263
282
  end
264
-
265
283
  end
266
284
  end
data/lib/thor/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Thor
2
- VERSION = "0.16.0"
2
+ VERSION = "1.2.1"
3
3
  end