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