honeybadger 5.0.2 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +718 -701
- data/LICENSE +19 -19
- data/README.md +57 -57
- data/TROUBLESHOOTING.md +3 -3
- data/bin/honeybadger +5 -5
- data/lib/honeybadger/agent.rb +488 -488
- data/lib/honeybadger/backend/base.rb +116 -116
- data/lib/honeybadger/backend/debug.rb +22 -22
- data/lib/honeybadger/backend/null.rb +29 -29
- data/lib/honeybadger/backend/server.rb +62 -62
- data/lib/honeybadger/backend/test.rb +46 -46
- data/lib/honeybadger/backend.rb +27 -27
- data/lib/honeybadger/backtrace.rb +181 -181
- data/lib/honeybadger/breadcrumbs/active_support.rb +119 -119
- data/lib/honeybadger/breadcrumbs/breadcrumb.rb +53 -53
- data/lib/honeybadger/breadcrumbs/collector.rb +82 -82
- data/lib/honeybadger/breadcrumbs/logging.rb +55 -51
- data/lib/honeybadger/breadcrumbs/ring_buffer.rb +44 -44
- data/lib/honeybadger/breadcrumbs.rb +8 -8
- data/lib/honeybadger/cli/deploy.rb +43 -43
- data/lib/honeybadger/cli/exec.rb +143 -143
- data/lib/honeybadger/cli/helpers.rb +28 -28
- data/lib/honeybadger/cli/heroku.rb +129 -129
- data/lib/honeybadger/cli/install.rb +101 -101
- data/lib/honeybadger/cli/main.rb +237 -237
- data/lib/honeybadger/cli/notify.rb +67 -67
- data/lib/honeybadger/cli/test.rb +267 -267
- data/lib/honeybadger/cli.rb +14 -14
- data/lib/honeybadger/config/defaults.rb +336 -333
- data/lib/honeybadger/config/env.rb +42 -42
- data/lib/honeybadger/config/ruby.rb +146 -146
- data/lib/honeybadger/config/yaml.rb +76 -76
- data/lib/honeybadger/config.rb +416 -413
- data/lib/honeybadger/const.rb +20 -20
- data/lib/honeybadger/context_manager.rb +55 -55
- data/lib/honeybadger/conversions.rb +16 -16
- data/lib/honeybadger/init/hanami.rb +19 -0
- data/lib/honeybadger/init/rails.rb +38 -38
- data/lib/honeybadger/init/rake.rb +66 -66
- data/lib/honeybadger/init/ruby.rb +11 -11
- data/lib/honeybadger/init/sinatra.rb +57 -51
- data/lib/honeybadger/logging.rb +177 -177
- data/lib/honeybadger/notice.rb +579 -568
- data/lib/honeybadger/plugin.rb +210 -210
- data/lib/honeybadger/plugins/breadcrumbs.rb +111 -111
- data/lib/honeybadger/plugins/delayed_job/plugin.rb +56 -56
- data/lib/honeybadger/plugins/delayed_job.rb +22 -22
- data/lib/honeybadger/plugins/faktory.rb +52 -52
- data/lib/honeybadger/plugins/lambda.rb +71 -71
- data/lib/honeybadger/plugins/local_variables.rb +44 -44
- data/lib/honeybadger/plugins/passenger.rb +23 -23
- data/lib/honeybadger/plugins/rails.rb +72 -63
- data/lib/honeybadger/plugins/resque.rb +72 -72
- data/lib/honeybadger/plugins/shoryuken.rb +52 -52
- data/lib/honeybadger/plugins/sidekiq.rb +71 -62
- data/lib/honeybadger/plugins/sucker_punch.rb +18 -18
- data/lib/honeybadger/plugins/thor.rb +32 -32
- data/lib/honeybadger/plugins/warden.rb +19 -19
- data/lib/honeybadger/rack/error_notifier.rb +92 -92
- data/lib/honeybadger/rack/user_feedback.rb +88 -88
- data/lib/honeybadger/rack/user_informer.rb +45 -45
- data/lib/honeybadger/ruby.rb +2 -2
- data/lib/honeybadger/singleton.rb +103 -103
- data/lib/honeybadger/tasks.rb +22 -22
- data/lib/honeybadger/templates/feedback_form.erb +84 -84
- data/lib/honeybadger/util/http.rb +92 -92
- data/lib/honeybadger/util/lambda.rb +32 -32
- data/lib/honeybadger/util/request_hash.rb +73 -73
- data/lib/honeybadger/util/request_payload.rb +41 -41
- data/lib/honeybadger/util/revision.rb +39 -39
- data/lib/honeybadger/util/sanitizer.rb +214 -214
- data/lib/honeybadger/util/sql.rb +34 -34
- data/lib/honeybadger/util/stats.rb +50 -50
- data/lib/honeybadger/version.rb +4 -4
- data/lib/honeybadger/worker.rb +253 -253
- data/lib/honeybadger.rb +13 -11
- data/resources/ca-bundle.crt +3376 -3376
- data/vendor/capistrano-honeybadger/lib/capistrano/honeybadger.rb +5 -5
- data/vendor/capistrano-honeybadger/lib/capistrano/tasks/deploy.cap +89 -89
- data/vendor/capistrano-honeybadger/lib/honeybadger/capistrano/legacy.rb +47 -47
- data/vendor/capistrano-honeybadger/lib/honeybadger/capistrano.rb +2 -2
- data/vendor/cli/inifile.rb +628 -628
- data/vendor/cli/thor/actions/create_file.rb +103 -103
- data/vendor/cli/thor/actions/create_link.rb +59 -59
- data/vendor/cli/thor/actions/directory.rb +118 -118
- data/vendor/cli/thor/actions/empty_directory.rb +135 -135
- data/vendor/cli/thor/actions/file_manipulation.rb +316 -316
- data/vendor/cli/thor/actions/inject_into_file.rb +107 -107
- data/vendor/cli/thor/actions.rb +319 -319
- data/vendor/cli/thor/base.rb +656 -656
- data/vendor/cli/thor/command.rb +133 -133
- data/vendor/cli/thor/core_ext/hash_with_indifferent_access.rb +77 -77
- data/vendor/cli/thor/core_ext/io_binary_read.rb +10 -10
- data/vendor/cli/thor/core_ext/ordered_hash.rb +98 -98
- data/vendor/cli/thor/error.rb +32 -32
- data/vendor/cli/thor/group.rb +281 -281
- data/vendor/cli/thor/invocation.rb +178 -178
- data/vendor/cli/thor/line_editor/basic.rb +35 -35
- data/vendor/cli/thor/line_editor/readline.rb +88 -88
- data/vendor/cli/thor/line_editor.rb +17 -17
- data/vendor/cli/thor/parser/argument.rb +73 -73
- data/vendor/cli/thor/parser/arguments.rb +175 -175
- data/vendor/cli/thor/parser/option.rb +125 -125
- data/vendor/cli/thor/parser/options.rb +218 -218
- data/vendor/cli/thor/parser.rb +4 -4
- data/vendor/cli/thor/rake_compat.rb +71 -71
- data/vendor/cli/thor/runner.rb +322 -322
- data/vendor/cli/thor/shell/basic.rb +421 -421
- data/vendor/cli/thor/shell/color.rb +149 -149
- data/vendor/cli/thor/shell/html.rb +126 -126
- data/vendor/cli/thor/shell.rb +81 -81
- data/vendor/cli/thor/util.rb +267 -267
- data/vendor/cli/thor/version.rb +3 -3
- data/vendor/cli/thor.rb +484 -484
- metadata +11 -5
data/vendor/cli/thor/base.rb
CHANGED
@@ -1,656 +1,656 @@
|
|
1
|
-
require "thor/command"
|
2
|
-
require "thor/core_ext/hash_with_indifferent_access"
|
3
|
-
require "thor/core_ext/ordered_hash"
|
4
|
-
require "thor/error"
|
5
|
-
require "thor/invocation"
|
6
|
-
require "thor/parser"
|
7
|
-
require "thor/shell"
|
8
|
-
require "thor/line_editor"
|
9
|
-
require "thor/util"
|
10
|
-
|
11
|
-
class Thor
|
12
|
-
autoload :Actions, "thor/actions"
|
13
|
-
autoload :RakeCompat, "thor/rake_compat"
|
14
|
-
autoload :Group, "thor/group"
|
15
|
-
|
16
|
-
# Shortcuts for help.
|
17
|
-
HELP_MAPPINGS = %w[-h -? --help -D]
|
18
|
-
|
19
|
-
# Thor methods that should not be overwritten by the user.
|
20
|
-
THOR_RESERVED_WORDS = %w[invoke shell options behavior root destination_root relative_root
|
21
|
-
action add_file create_file in_root inside run run_ruby_script]
|
22
|
-
|
23
|
-
TEMPLATE_EXTNAME = ".tt"
|
24
|
-
|
25
|
-
module Base
|
26
|
-
attr_accessor :options, :parent_options, :args
|
27
|
-
|
28
|
-
# It receives arguments in an Array and two hashes, one for options and
|
29
|
-
# other for configuration.
|
30
|
-
#
|
31
|
-
# Notice that it does not check if all required arguments were supplied.
|
32
|
-
# It should be done by the parser.
|
33
|
-
#
|
34
|
-
# ==== Parameters
|
35
|
-
# args<Array[Object]>:: An array of objects. The objects are applied to their
|
36
|
-
# respective accessors declared with <tt>argument</tt>.
|
37
|
-
#
|
38
|
-
# options<Hash>:: An options hash that will be available as self.options.
|
39
|
-
# The hash given is converted to a hash with indifferent
|
40
|
-
# access, magic predicates (options.skip?) and then frozen.
|
41
|
-
#
|
42
|
-
# config<Hash>:: Configuration for this Thor class.
|
43
|
-
#
|
44
|
-
def initialize(args = [], local_options = {}, config = {}) # rubocop:disable MethodLength
|
45
|
-
parse_options = self.class.class_options
|
46
|
-
|
47
|
-
# The start method splits inbound arguments at the first argument
|
48
|
-
# that looks like an option (starts with - or --). It then calls
|
49
|
-
# new, passing in the two halves of the arguments Array as the
|
50
|
-
# first two parameters.
|
51
|
-
|
52
|
-
command_options = config.delete(:command_options) # hook for start
|
53
|
-
parse_options = parse_options.merge(command_options) if command_options
|
54
|
-
if local_options.is_a?(Array)
|
55
|
-
array_options, hash_options = local_options, {}
|
56
|
-
else
|
57
|
-
# Handle the case where the class was explicitly instantiated
|
58
|
-
# with pre-parsed options.
|
59
|
-
array_options, hash_options = [], local_options
|
60
|
-
end
|
61
|
-
|
62
|
-
# Let Thor::Options parse the options first, so it can remove
|
63
|
-
# declared options from the array. This will leave us with
|
64
|
-
# a list of arguments that weren't declared.
|
65
|
-
stop_on_unknown = self.class.stop_on_unknown_option? config[:current_command]
|
66
|
-
opts = Thor::Options.new(parse_options, hash_options, stop_on_unknown)
|
67
|
-
self.options = opts.parse(array_options)
|
68
|
-
self.options = config[:class_options].merge(options) if config[:class_options]
|
69
|
-
|
70
|
-
# If unknown options are disallowed, make sure that none of the
|
71
|
-
# remaining arguments looks like an option.
|
72
|
-
opts.check_unknown! if self.class.check_unknown_options?(config)
|
73
|
-
|
74
|
-
# Add the remaining arguments from the options parser to the
|
75
|
-
# arguments passed in to initialize. Then remove any positional
|
76
|
-
# arguments declared using #argument (this is primarily used
|
77
|
-
# by Thor::Group). Tis will leave us with the remaining
|
78
|
-
# positional arguments.
|
79
|
-
to_parse = args
|
80
|
-
to_parse += opts.remaining unless self.class.strict_args_position?(config)
|
81
|
-
|
82
|
-
thor_args = Thor::Arguments.new(self.class.arguments)
|
83
|
-
thor_args.parse(to_parse).each { |k, v| __send__("#{k}=", v) }
|
84
|
-
@args = thor_args.remaining
|
85
|
-
end
|
86
|
-
|
87
|
-
class << self
|
88
|
-
def included(base) #:nodoc:
|
89
|
-
base.extend ClassMethods
|
90
|
-
base.send :include, Invocation
|
91
|
-
base.send :include, Shell
|
92
|
-
end
|
93
|
-
|
94
|
-
# Returns the classes that inherits from Thor or Thor::Group.
|
95
|
-
#
|
96
|
-
# ==== Returns
|
97
|
-
# Array[Class]
|
98
|
-
#
|
99
|
-
def subclasses
|
100
|
-
@subclasses ||= []
|
101
|
-
end
|
102
|
-
|
103
|
-
# Returns the files where the subclasses are kept.
|
104
|
-
#
|
105
|
-
# ==== Returns
|
106
|
-
# Hash[path<String> => Class]
|
107
|
-
#
|
108
|
-
def subclass_files
|
109
|
-
@subclass_files ||= Hash.new { |h, k| h[k] = [] }
|
110
|
-
end
|
111
|
-
|
112
|
-
# Whenever a class inherits from Thor or Thor::Group, we should track the
|
113
|
-
# class and the file on Thor::Base. This is the method responsable for it.
|
114
|
-
#
|
115
|
-
def register_klass_file(klass) #:nodoc:
|
116
|
-
file = caller[1].match(/(.*):\d+/)[1]
|
117
|
-
Thor::Base.subclasses << klass unless Thor::Base.subclasses.include?(klass)
|
118
|
-
|
119
|
-
file_subclasses = Thor::Base.subclass_files[File.expand_path(file)]
|
120
|
-
file_subclasses << klass unless file_subclasses.include?(klass)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
module ClassMethods
|
125
|
-
def attr_reader(*) #:nodoc:
|
126
|
-
no_commands { super }
|
127
|
-
end
|
128
|
-
|
129
|
-
def attr_writer(*) #:nodoc:
|
130
|
-
no_commands { super }
|
131
|
-
end
|
132
|
-
|
133
|
-
def attr_accessor(*) #:nodoc:
|
134
|
-
no_commands { super }
|
135
|
-
end
|
136
|
-
|
137
|
-
# If you want to raise an error for unknown options, call check_unknown_options!
|
138
|
-
# This is disabled by default to allow dynamic invocations.
|
139
|
-
def check_unknown_options!
|
140
|
-
@check_unknown_options = true
|
141
|
-
end
|
142
|
-
|
143
|
-
def check_unknown_options #:nodoc:
|
144
|
-
@check_unknown_options ||= from_superclass(:check_unknown_options, false)
|
145
|
-
end
|
146
|
-
|
147
|
-
def check_unknown_options?(config) #:nodoc:
|
148
|
-
!!check_unknown_options
|
149
|
-
end
|
150
|
-
|
151
|
-
# If true, option parsing is suspended as soon as an unknown option or a
|
152
|
-
# regular argument is encountered. All remaining arguments are passed to
|
153
|
-
# the command as regular arguments.
|
154
|
-
def stop_on_unknown_option?(command_name) #:nodoc:
|
155
|
-
false
|
156
|
-
end
|
157
|
-
|
158
|
-
# If you want only strict string args (useful when cascading thor classes),
|
159
|
-
# call strict_args_position! This is disabled by default to allow dynamic
|
160
|
-
# invocations.
|
161
|
-
def strict_args_position!
|
162
|
-
@strict_args_position = true
|
163
|
-
end
|
164
|
-
|
165
|
-
def strict_args_position #:nodoc:
|
166
|
-
@strict_args_position ||= from_superclass(:strict_args_position, false)
|
167
|
-
end
|
168
|
-
|
169
|
-
def strict_args_position?(config) #:nodoc:
|
170
|
-
!!strict_args_position
|
171
|
-
end
|
172
|
-
|
173
|
-
# Adds an argument to the class and creates an attr_accessor for it.
|
174
|
-
#
|
175
|
-
# Arguments are different from options in several aspects. The first one
|
176
|
-
# is how they are parsed from the command line, arguments are retrieved
|
177
|
-
# from position:
|
178
|
-
#
|
179
|
-
# thor command NAME
|
180
|
-
#
|
181
|
-
# Instead of:
|
182
|
-
#
|
183
|
-
# thor command --name=NAME
|
184
|
-
#
|
185
|
-
# Besides, arguments are used inside your code as an accessor (self.argument),
|
186
|
-
# while options are all kept in a hash (self.options).
|
187
|
-
#
|
188
|
-
# Finally, arguments cannot have type :default or :boolean but can be
|
189
|
-
# optional (supplying :optional => :true or :required => false), although
|
190
|
-
# you cannot have a required argument after a non-required argument. If you
|
191
|
-
# try it, an error is raised.
|
192
|
-
#
|
193
|
-
# ==== Parameters
|
194
|
-
# name<Symbol>:: The name of the argument.
|
195
|
-
# options<Hash>:: Described below.
|
196
|
-
#
|
197
|
-
# ==== Options
|
198
|
-
# :desc - Description for the argument.
|
199
|
-
# :required - If the argument is required or not.
|
200
|
-
# :optional - If the argument is optional or not.
|
201
|
-
# :type - The type of the argument, can be :string, :hash, :array, :numeric.
|
202
|
-
# :default - Default value for this argument. It cannot be required and have default values.
|
203
|
-
# :banner - String to show on usage notes.
|
204
|
-
#
|
205
|
-
# ==== Errors
|
206
|
-
# ArgumentError:: Raised if you supply a required argument after a non required one.
|
207
|
-
#
|
208
|
-
def argument(name, options = {}) # rubocop:disable MethodLength
|
209
|
-
is_thor_reserved_word?(name, :argument)
|
210
|
-
no_commands { attr_accessor name }
|
211
|
-
|
212
|
-
required = if options.key?(:optional)
|
213
|
-
!options[:optional]
|
214
|
-
elsif options.key?(:required)
|
215
|
-
options[:required]
|
216
|
-
else
|
217
|
-
options[:default].nil?
|
218
|
-
end
|
219
|
-
|
220
|
-
remove_argument name
|
221
|
-
|
222
|
-
arguments.each do |argument|
|
223
|
-
next if argument.required?
|
224
|
-
fail ArgumentError, "You cannot have #{name.to_s.inspect} as required argument after " <<
|
225
|
-
"the non-required argument #{argument.human_name.inspect}."
|
226
|
-
end if required
|
227
|
-
|
228
|
-
options[:required] = required
|
229
|
-
|
230
|
-
arguments << Thor::Argument.new(name, options)
|
231
|
-
end
|
232
|
-
|
233
|
-
# Returns this class arguments, looking up in the ancestors chain.
|
234
|
-
#
|
235
|
-
# ==== Returns
|
236
|
-
# Array[Thor::Argument]
|
237
|
-
#
|
238
|
-
def arguments
|
239
|
-
@arguments ||= from_superclass(:arguments, [])
|
240
|
-
end
|
241
|
-
|
242
|
-
# Adds a bunch of options to the set of class options.
|
243
|
-
#
|
244
|
-
# class_options :foo => false, :bar => :required, :baz => :string
|
245
|
-
#
|
246
|
-
# If you prefer more detailed declaration, check class_option.
|
247
|
-
#
|
248
|
-
# ==== Parameters
|
249
|
-
# Hash[Symbol => Object]
|
250
|
-
#
|
251
|
-
def class_options(options = nil)
|
252
|
-
@class_options ||= from_superclass(:class_options, {})
|
253
|
-
build_options(options, @class_options) if options
|
254
|
-
@class_options
|
255
|
-
end
|
256
|
-
|
257
|
-
# Adds an option to the set of class options
|
258
|
-
#
|
259
|
-
# ==== Parameters
|
260
|
-
# name<Symbol>:: The name of the argument.
|
261
|
-
# options<Hash>:: Described below.
|
262
|
-
#
|
263
|
-
# ==== Options
|
264
|
-
# :desc:: -- Description for the argument.
|
265
|
-
# :required:: -- If the argument is required or not.
|
266
|
-
# :default:: -- Default value for this argument.
|
267
|
-
# :group:: -- The group for this options. Use by class options to output options in different levels.
|
268
|
-
# :aliases:: -- Aliases for this option. <b>Note:</b> Thor follows a convention of one-dash-one-letter options. Thus aliases like "-something" wouldn't be parsed; use either "\--something" or "-s" instead.
|
269
|
-
# :type:: -- The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
|
270
|
-
# :banner:: -- String to show on usage notes.
|
271
|
-
# :hide:: -- If you want to hide this option from the help.
|
272
|
-
#
|
273
|
-
def class_option(name, options = {})
|
274
|
-
build_option(name, options, class_options)
|
275
|
-
end
|
276
|
-
|
277
|
-
# Removes a previous defined argument. If :undefine is given, undefine
|
278
|
-
# accessors as well.
|
279
|
-
#
|
280
|
-
# ==== Parameters
|
281
|
-
# names<Array>:: Arguments to be removed
|
282
|
-
#
|
283
|
-
# ==== Examples
|
284
|
-
#
|
285
|
-
# remove_argument :foo
|
286
|
-
# remove_argument :foo, :bar, :baz, :undefine => true
|
287
|
-
#
|
288
|
-
def remove_argument(*names)
|
289
|
-
options = names.last.is_a?(Hash) ? names.pop : {}
|
290
|
-
|
291
|
-
names.each do |name|
|
292
|
-
arguments.delete_if { |a| a.name == name.to_s }
|
293
|
-
undef_method name, "#{name}=" if options[:undefine]
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
# Removes a previous defined class option.
|
298
|
-
#
|
299
|
-
# ==== Parameters
|
300
|
-
# names<Array>:: Class options to be removed
|
301
|
-
#
|
302
|
-
# ==== Examples
|
303
|
-
#
|
304
|
-
# remove_class_option :foo
|
305
|
-
# remove_class_option :foo, :bar, :baz
|
306
|
-
#
|
307
|
-
def remove_class_option(*names)
|
308
|
-
names.each do |name|
|
309
|
-
class_options.delete(name)
|
310
|
-
end
|
311
|
-
end
|
312
|
-
|
313
|
-
# Defines the group. This is used when thor list is invoked so you can specify
|
314
|
-
# that only commands from a pre-defined group will be shown. Defaults to standard.
|
315
|
-
#
|
316
|
-
# ==== Parameters
|
317
|
-
# name<String|Symbol>
|
318
|
-
#
|
319
|
-
def group(name = nil)
|
320
|
-
if name
|
321
|
-
@group = name.to_s
|
322
|
-
else
|
323
|
-
@group ||= from_superclass(:group, "standard")
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
# Returns the commands for this Thor class.
|
328
|
-
#
|
329
|
-
# ==== Returns
|
330
|
-
# OrderedHash:: An ordered hash with commands names as keys and Thor::Command
|
331
|
-
# objects as values.
|
332
|
-
#
|
333
|
-
def commands
|
334
|
-
@commands ||= Thor::CoreExt::OrderedHash.new
|
335
|
-
end
|
336
|
-
alias_method :tasks, :commands
|
337
|
-
|
338
|
-
# Returns the commands for this Thor class and all subclasses.
|
339
|
-
#
|
340
|
-
# ==== Returns
|
341
|
-
# OrderedHash:: An ordered hash with commands names as keys and Thor::Command
|
342
|
-
# objects as values.
|
343
|
-
#
|
344
|
-
def all_commands
|
345
|
-
@all_commands ||= from_superclass(:all_commands, Thor::CoreExt::OrderedHash.new)
|
346
|
-
@all_commands.merge(commands)
|
347
|
-
end
|
348
|
-
alias_method :all_tasks, :all_commands
|
349
|
-
|
350
|
-
# Removes a given command from this Thor class. This is usually done if you
|
351
|
-
# are inheriting from another class and don't want it to be available
|
352
|
-
# anymore.
|
353
|
-
#
|
354
|
-
# By default it only remove the mapping to the command. But you can supply
|
355
|
-
# :undefine => true to undefine the method from the class as well.
|
356
|
-
#
|
357
|
-
# ==== Parameters
|
358
|
-
# name<Symbol|String>:: The name of the command to be removed
|
359
|
-
# options<Hash>:: You can give :undefine => true if you want commands the method
|
360
|
-
# to be undefined from the class as well.
|
361
|
-
#
|
362
|
-
def remove_command(*names)
|
363
|
-
options = names.last.is_a?(Hash) ? names.pop : {}
|
364
|
-
|
365
|
-
names.each do |name|
|
366
|
-
commands.delete(name.to_s)
|
367
|
-
all_commands.delete(name.to_s)
|
368
|
-
undef_method name if options[:undefine]
|
369
|
-
end
|
370
|
-
end
|
371
|
-
alias_method :remove_task, :remove_command
|
372
|
-
|
373
|
-
# All methods defined inside the given block are not added as commands.
|
374
|
-
#
|
375
|
-
# So you can do:
|
376
|
-
#
|
377
|
-
# class MyScript < Thor
|
378
|
-
# no_commands do
|
379
|
-
# def this_is_not_a_command
|
380
|
-
# end
|
381
|
-
# end
|
382
|
-
# end
|
383
|
-
#
|
384
|
-
# You can also add the method and remove it from the command list:
|
385
|
-
#
|
386
|
-
# class MyScript < Thor
|
387
|
-
# def this_is_not_a_command
|
388
|
-
# end
|
389
|
-
# remove_command :this_is_not_a_command
|
390
|
-
# end
|
391
|
-
#
|
392
|
-
def no_commands
|
393
|
-
@no_commands = true
|
394
|
-
yield
|
395
|
-
ensure
|
396
|
-
@no_commands = false
|
397
|
-
end
|
398
|
-
alias_method :no_tasks, :no_commands
|
399
|
-
|
400
|
-
# Sets the namespace for the Thor or Thor::Group class. By default the
|
401
|
-
# namespace is retrieved from the class name. If your Thor class is named
|
402
|
-
# Scripts::MyScript, the help method, for example, will be called as:
|
403
|
-
#
|
404
|
-
# thor scripts:my_script -h
|
405
|
-
#
|
406
|
-
# If you change the namespace:
|
407
|
-
#
|
408
|
-
# namespace :my_scripts
|
409
|
-
#
|
410
|
-
# You change how your commands are invoked:
|
411
|
-
#
|
412
|
-
# thor my_scripts -h
|
413
|
-
#
|
414
|
-
# Finally, if you change your namespace to default:
|
415
|
-
#
|
416
|
-
# namespace :default
|
417
|
-
#
|
418
|
-
# Your commands can be invoked with a shortcut. Instead of:
|
419
|
-
#
|
420
|
-
# thor :my_command
|
421
|
-
#
|
422
|
-
def namespace(name = nil)
|
423
|
-
if name
|
424
|
-
@namespace = name.to_s
|
425
|
-
else
|
426
|
-
@namespace ||= Thor::Util.namespace_from_thor_class(self)
|
427
|
-
end
|
428
|
-
end
|
429
|
-
|
430
|
-
# Parses the command and options from the given args, instantiate the class
|
431
|
-
# and invoke the command. This method is used when the arguments must be parsed
|
432
|
-
# from an array. If you are inside Ruby and want to use a Thor class, you
|
433
|
-
# can simply initialize it:
|
434
|
-
#
|
435
|
-
# script = MyScript.new(args, options, config)
|
436
|
-
# script.invoke(:command, first_arg, second_arg, third_arg)
|
437
|
-
#
|
438
|
-
def start(given_args = ARGV, config = {})
|
439
|
-
config[:shell] ||= Thor::Base.shell.new
|
440
|
-
dispatch(nil, given_args.dup, nil, config)
|
441
|
-
rescue Thor::Error => e
|
442
|
-
config[:debug] || ENV["THOR_DEBUG"] == "1" ? (raise e) : config[:shell].error(e.message)
|
443
|
-
exit(1) if exit_on_failure?
|
444
|
-
rescue Errno::EPIPE
|
445
|
-
# This happens if a thor command is piped to something like `head`,
|
446
|
-
# which closes the pipe when it's done reading. This will also
|
447
|
-
# mean that if the pipe is closed, further unnecessary
|
448
|
-
# computation will not occur.
|
449
|
-
exit(0)
|
450
|
-
end
|
451
|
-
|
452
|
-
# Allows to use private methods from parent in child classes as commands.
|
453
|
-
#
|
454
|
-
# ==== Parameters
|
455
|
-
# names<Array>:: Method names to be used as commands
|
456
|
-
#
|
457
|
-
# ==== Examples
|
458
|
-
#
|
459
|
-
# public_command :foo
|
460
|
-
# public_command :foo, :bar, :baz
|
461
|
-
#
|
462
|
-
def public_command(*names)
|
463
|
-
names.each do |name|
|
464
|
-
class_eval "def #{name}(*); super end"
|
465
|
-
end
|
466
|
-
end
|
467
|
-
alias_method :public_task, :public_command
|
468
|
-
|
469
|
-
def handle_no_command_error(command, has_namespace = $thor_runner) #:nodoc:
|
470
|
-
if has_namespace
|
471
|
-
fail UndefinedCommandError, "Could not find command #{command.inspect} in #{namespace.inspect} namespace."
|
472
|
-
else
|
473
|
-
fail UndefinedCommandError, "Could not find command #{command.inspect}."
|
474
|
-
end
|
475
|
-
end
|
476
|
-
alias_method :handle_no_task_error, :handle_no_command_error
|
477
|
-
|
478
|
-
def handle_argument_error(command, error, args, arity) #:nodoc:
|
479
|
-
msg = "ERROR: \"#{basename} #{command.name}\" was called with "
|
480
|
-
msg << "no arguments" if args.empty?
|
481
|
-
msg << "arguments " << args.inspect unless args.empty?
|
482
|
-
msg << "\nUsage: #{banner(command).inspect}"
|
483
|
-
fail InvocationError, msg
|
484
|
-
end
|
485
|
-
|
486
|
-
protected
|
487
|
-
|
488
|
-
# Prints the class options per group. If an option does not belong to
|
489
|
-
# any group, it's printed as Class option.
|
490
|
-
#
|
491
|
-
def class_options_help(shell, groups = {}) #:nodoc:
|
492
|
-
# Group options by group
|
493
|
-
class_options.each do |_, value|
|
494
|
-
groups[value.group] ||= []
|
495
|
-
groups[value.group] << value
|
496
|
-
end
|
497
|
-
|
498
|
-
# Deal with default group
|
499
|
-
global_options = groups.delete(nil) || []
|
500
|
-
print_options(shell, global_options)
|
501
|
-
|
502
|
-
# Print all others
|
503
|
-
groups.each do |group_name, options|
|
504
|
-
print_options(shell, options, group_name)
|
505
|
-
end
|
506
|
-
end
|
507
|
-
|
508
|
-
# Receives a set of options and print them.
|
509
|
-
def print_options(shell, options, group_name = nil)
|
510
|
-
return if options.empty?
|
511
|
-
|
512
|
-
list = []
|
513
|
-
padding = options.map { |o| o.aliases.size }.max.to_i * 4
|
514
|
-
|
515
|
-
options.each do |option|
|
516
|
-
unless option.hide
|
517
|
-
item = [option.usage(padding)]
|
518
|
-
item.push(option.description ? "# #{option.description}" : "")
|
519
|
-
|
520
|
-
list << item
|
521
|
-
list << ["", "# Default: #{option.default}"] if option.show_default?
|
522
|
-
list << ["", "# Possible values: #{option.enum.join(', ')}"] if option.enum
|
523
|
-
end
|
524
|
-
end
|
525
|
-
|
526
|
-
shell.say(group_name ? "#{group_name} options:" : "Options:")
|
527
|
-
shell.print_table(list, :indent => 2)
|
528
|
-
shell.say ""
|
529
|
-
end
|
530
|
-
|
531
|
-
# Raises an error if the word given is a Thor reserved word.
|
532
|
-
def is_thor_reserved_word?(word, type) #:nodoc:
|
533
|
-
return false unless THOR_RESERVED_WORDS.include?(word.to_s)
|
534
|
-
fail "#{word.inspect} is a Thor reserved word and cannot be defined as #{type}"
|
535
|
-
end
|
536
|
-
|
537
|
-
# Build an option and adds it to the given scope.
|
538
|
-
#
|
539
|
-
# ==== Parameters
|
540
|
-
# name<Symbol>:: The name of the argument.
|
541
|
-
# options<Hash>:: Described in both class_option and method_option.
|
542
|
-
# scope<Hash>:: Options hash that is being built up
|
543
|
-
def build_option(name, options, scope) #:nodoc:
|
544
|
-
scope[name] = Thor::Option.new(name, options)
|
545
|
-
end
|
546
|
-
|
547
|
-
# Receives a hash of options, parse them and add to the scope. This is a
|
548
|
-
# fast way to set a bunch of options:
|
549
|
-
#
|
550
|
-
# build_options :foo => true, :bar => :required, :baz => :string
|
551
|
-
#
|
552
|
-
# ==== Parameters
|
553
|
-
# Hash[Symbol => Object]
|
554
|
-
def build_options(options, scope) #:nodoc:
|
555
|
-
options.each do |key, value|
|
556
|
-
scope[key] = Thor::Option.parse(key, value)
|
557
|
-
end
|
558
|
-
end
|
559
|
-
|
560
|
-
# Finds a command with the given name. If the command belongs to the current
|
561
|
-
# class, just return it, otherwise dup it and add the fresh copy to the
|
562
|
-
# current command hash.
|
563
|
-
def find_and_refresh_command(name) #:nodoc:
|
564
|
-
if commands[name.to_s]
|
565
|
-
commands[name.to_s]
|
566
|
-
elsif command = all_commands[name.to_s] # rubocop:disable AssignmentInCondition
|
567
|
-
commands[name.to_s] = command.clone
|
568
|
-
else
|
569
|
-
fail ArgumentError, "You supplied :for => #{name.inspect}, but the command #{name.inspect} could not be found."
|
570
|
-
end
|
571
|
-
end
|
572
|
-
alias_method :find_and_refresh_task, :find_and_refresh_command
|
573
|
-
|
574
|
-
# Everytime someone inherits from a Thor class, register the klass
|
575
|
-
# and file into baseclass.
|
576
|
-
def inherited(klass)
|
577
|
-
Thor::Base.register_klass_file(klass)
|
578
|
-
klass.instance_variable_set(:@no_commands, false)
|
579
|
-
end
|
580
|
-
|
581
|
-
# Fire this callback whenever a method is added. Added methods are
|
582
|
-
# tracked as commands by invoking the create_command method.
|
583
|
-
def method_added(meth)
|
584
|
-
meth = meth.to_s
|
585
|
-
|
586
|
-
if meth == "initialize"
|
587
|
-
initialize_added
|
588
|
-
return
|
589
|
-
end
|
590
|
-
|
591
|
-
# Return if it's not a public instance method
|
592
|
-
return unless public_method_defined?(meth.to_sym)
|
593
|
-
|
594
|
-
@no_commands ||= false
|
595
|
-
return if @no_commands || !create_command(meth)
|
596
|
-
|
597
|
-
is_thor_reserved_word?(meth, :command)
|
598
|
-
Thor::Base.register_klass_file(self)
|
599
|
-
end
|
600
|
-
|
601
|
-
# Retrieves a value from superclass. If it reaches the baseclass,
|
602
|
-
# returns default.
|
603
|
-
def from_superclass(method, default = nil)
|
604
|
-
if self == baseclass || !superclass.respond_to?(method, true)
|
605
|
-
default
|
606
|
-
else
|
607
|
-
value = superclass.send(method)
|
608
|
-
|
609
|
-
# Ruby implements `dup` on Object, but raises a `TypeError`
|
610
|
-
# if the method is called on immediates. As a result, we
|
611
|
-
# don't have a good way to check whether dup will succeed
|
612
|
-
# without calling it and rescuing the TypeError.
|
613
|
-
begin
|
614
|
-
value.dup
|
615
|
-
rescue TypeError
|
616
|
-
value
|
617
|
-
end
|
618
|
-
|
619
|
-
end
|
620
|
-
end
|
621
|
-
|
622
|
-
# A flag that makes the process exit with status 1 if any error happens.
|
623
|
-
def exit_on_failure?
|
624
|
-
false
|
625
|
-
end
|
626
|
-
|
627
|
-
#
|
628
|
-
# The basename of the program invoking the thor class.
|
629
|
-
#
|
630
|
-
def basename
|
631
|
-
File.basename($PROGRAM_NAME).split(" ").first
|
632
|
-
end
|
633
|
-
|
634
|
-
# SIGNATURE: Sets the baseclass. This is where the superclass lookup
|
635
|
-
# finishes.
|
636
|
-
def baseclass #:nodoc:
|
637
|
-
end
|
638
|
-
|
639
|
-
# SIGNATURE: Creates a new command if valid_command? is true. This method is
|
640
|
-
# called when a new method is added to the class.
|
641
|
-
def create_command(meth) #:nodoc:
|
642
|
-
end
|
643
|
-
alias_method :create_task, :create_command
|
644
|
-
|
645
|
-
# SIGNATURE: Defines behavior when the initialize method is added to the
|
646
|
-
# class.
|
647
|
-
def initialize_added #:nodoc:
|
648
|
-
end
|
649
|
-
|
650
|
-
# SIGNATURE: The hook invoked by start.
|
651
|
-
def dispatch(command, given_args, given_opts, config) #:nodoc:
|
652
|
-
fail NotImplementedError
|
653
|
-
end
|
654
|
-
end
|
655
|
-
end
|
656
|
-
end
|
1
|
+
require "thor/command"
|
2
|
+
require "thor/core_ext/hash_with_indifferent_access"
|
3
|
+
require "thor/core_ext/ordered_hash"
|
4
|
+
require "thor/error"
|
5
|
+
require "thor/invocation"
|
6
|
+
require "thor/parser"
|
7
|
+
require "thor/shell"
|
8
|
+
require "thor/line_editor"
|
9
|
+
require "thor/util"
|
10
|
+
|
11
|
+
class Thor
|
12
|
+
autoload :Actions, "thor/actions"
|
13
|
+
autoload :RakeCompat, "thor/rake_compat"
|
14
|
+
autoload :Group, "thor/group"
|
15
|
+
|
16
|
+
# Shortcuts for help.
|
17
|
+
HELP_MAPPINGS = %w[-h -? --help -D]
|
18
|
+
|
19
|
+
# Thor methods that should not be overwritten by the user.
|
20
|
+
THOR_RESERVED_WORDS = %w[invoke shell options behavior root destination_root relative_root
|
21
|
+
action add_file create_file in_root inside run run_ruby_script]
|
22
|
+
|
23
|
+
TEMPLATE_EXTNAME = ".tt"
|
24
|
+
|
25
|
+
module Base
|
26
|
+
attr_accessor :options, :parent_options, :args
|
27
|
+
|
28
|
+
# It receives arguments in an Array and two hashes, one for options and
|
29
|
+
# other for configuration.
|
30
|
+
#
|
31
|
+
# Notice that it does not check if all required arguments were supplied.
|
32
|
+
# It should be done by the parser.
|
33
|
+
#
|
34
|
+
# ==== Parameters
|
35
|
+
# args<Array[Object]>:: An array of objects. The objects are applied to their
|
36
|
+
# respective accessors declared with <tt>argument</tt>.
|
37
|
+
#
|
38
|
+
# options<Hash>:: An options hash that will be available as self.options.
|
39
|
+
# The hash given is converted to a hash with indifferent
|
40
|
+
# access, magic predicates (options.skip?) and then frozen.
|
41
|
+
#
|
42
|
+
# config<Hash>:: Configuration for this Thor class.
|
43
|
+
#
|
44
|
+
def initialize(args = [], local_options = {}, config = {}) # rubocop:disable MethodLength
|
45
|
+
parse_options = self.class.class_options
|
46
|
+
|
47
|
+
# The start method splits inbound arguments at the first argument
|
48
|
+
# that looks like an option (starts with - or --). It then calls
|
49
|
+
# new, passing in the two halves of the arguments Array as the
|
50
|
+
# first two parameters.
|
51
|
+
|
52
|
+
command_options = config.delete(:command_options) # hook for start
|
53
|
+
parse_options = parse_options.merge(command_options) if command_options
|
54
|
+
if local_options.is_a?(Array)
|
55
|
+
array_options, hash_options = local_options, {}
|
56
|
+
else
|
57
|
+
# Handle the case where the class was explicitly instantiated
|
58
|
+
# with pre-parsed options.
|
59
|
+
array_options, hash_options = [], local_options
|
60
|
+
end
|
61
|
+
|
62
|
+
# Let Thor::Options parse the options first, so it can remove
|
63
|
+
# declared options from the array. This will leave us with
|
64
|
+
# a list of arguments that weren't declared.
|
65
|
+
stop_on_unknown = self.class.stop_on_unknown_option? config[:current_command]
|
66
|
+
opts = Thor::Options.new(parse_options, hash_options, stop_on_unknown)
|
67
|
+
self.options = opts.parse(array_options)
|
68
|
+
self.options = config[:class_options].merge(options) if config[:class_options]
|
69
|
+
|
70
|
+
# If unknown options are disallowed, make sure that none of the
|
71
|
+
# remaining arguments looks like an option.
|
72
|
+
opts.check_unknown! if self.class.check_unknown_options?(config)
|
73
|
+
|
74
|
+
# Add the remaining arguments from the options parser to the
|
75
|
+
# arguments passed in to initialize. Then remove any positional
|
76
|
+
# arguments declared using #argument (this is primarily used
|
77
|
+
# by Thor::Group). Tis will leave us with the remaining
|
78
|
+
# positional arguments.
|
79
|
+
to_parse = args
|
80
|
+
to_parse += opts.remaining unless self.class.strict_args_position?(config)
|
81
|
+
|
82
|
+
thor_args = Thor::Arguments.new(self.class.arguments)
|
83
|
+
thor_args.parse(to_parse).each { |k, v| __send__("#{k}=", v) }
|
84
|
+
@args = thor_args.remaining
|
85
|
+
end
|
86
|
+
|
87
|
+
class << self
|
88
|
+
def included(base) #:nodoc:
|
89
|
+
base.extend ClassMethods
|
90
|
+
base.send :include, Invocation
|
91
|
+
base.send :include, Shell
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns the classes that inherits from Thor or Thor::Group.
|
95
|
+
#
|
96
|
+
# ==== Returns
|
97
|
+
# Array[Class]
|
98
|
+
#
|
99
|
+
def subclasses
|
100
|
+
@subclasses ||= []
|
101
|
+
end
|
102
|
+
|
103
|
+
# Returns the files where the subclasses are kept.
|
104
|
+
#
|
105
|
+
# ==== Returns
|
106
|
+
# Hash[path<String> => Class]
|
107
|
+
#
|
108
|
+
def subclass_files
|
109
|
+
@subclass_files ||= Hash.new { |h, k| h[k] = [] }
|
110
|
+
end
|
111
|
+
|
112
|
+
# Whenever a class inherits from Thor or Thor::Group, we should track the
|
113
|
+
# class and the file on Thor::Base. This is the method responsable for it.
|
114
|
+
#
|
115
|
+
def register_klass_file(klass) #:nodoc:
|
116
|
+
file = caller[1].match(/(.*):\d+/)[1]
|
117
|
+
Thor::Base.subclasses << klass unless Thor::Base.subclasses.include?(klass)
|
118
|
+
|
119
|
+
file_subclasses = Thor::Base.subclass_files[File.expand_path(file)]
|
120
|
+
file_subclasses << klass unless file_subclasses.include?(klass)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
module ClassMethods
|
125
|
+
def attr_reader(*) #:nodoc:
|
126
|
+
no_commands { super }
|
127
|
+
end
|
128
|
+
|
129
|
+
def attr_writer(*) #:nodoc:
|
130
|
+
no_commands { super }
|
131
|
+
end
|
132
|
+
|
133
|
+
def attr_accessor(*) #:nodoc:
|
134
|
+
no_commands { super }
|
135
|
+
end
|
136
|
+
|
137
|
+
# If you want to raise an error for unknown options, call check_unknown_options!
|
138
|
+
# This is disabled by default to allow dynamic invocations.
|
139
|
+
def check_unknown_options!
|
140
|
+
@check_unknown_options = true
|
141
|
+
end
|
142
|
+
|
143
|
+
def check_unknown_options #:nodoc:
|
144
|
+
@check_unknown_options ||= from_superclass(:check_unknown_options, false)
|
145
|
+
end
|
146
|
+
|
147
|
+
def check_unknown_options?(config) #:nodoc:
|
148
|
+
!!check_unknown_options
|
149
|
+
end
|
150
|
+
|
151
|
+
# If true, option parsing is suspended as soon as an unknown option or a
|
152
|
+
# regular argument is encountered. All remaining arguments are passed to
|
153
|
+
# the command as regular arguments.
|
154
|
+
def stop_on_unknown_option?(command_name) #:nodoc:
|
155
|
+
false
|
156
|
+
end
|
157
|
+
|
158
|
+
# If you want only strict string args (useful when cascading thor classes),
|
159
|
+
# call strict_args_position! This is disabled by default to allow dynamic
|
160
|
+
# invocations.
|
161
|
+
def strict_args_position!
|
162
|
+
@strict_args_position = true
|
163
|
+
end
|
164
|
+
|
165
|
+
def strict_args_position #:nodoc:
|
166
|
+
@strict_args_position ||= from_superclass(:strict_args_position, false)
|
167
|
+
end
|
168
|
+
|
169
|
+
def strict_args_position?(config) #:nodoc:
|
170
|
+
!!strict_args_position
|
171
|
+
end
|
172
|
+
|
173
|
+
# Adds an argument to the class and creates an attr_accessor for it.
|
174
|
+
#
|
175
|
+
# Arguments are different from options in several aspects. The first one
|
176
|
+
# is how they are parsed from the command line, arguments are retrieved
|
177
|
+
# from position:
|
178
|
+
#
|
179
|
+
# thor command NAME
|
180
|
+
#
|
181
|
+
# Instead of:
|
182
|
+
#
|
183
|
+
# thor command --name=NAME
|
184
|
+
#
|
185
|
+
# Besides, arguments are used inside your code as an accessor (self.argument),
|
186
|
+
# while options are all kept in a hash (self.options).
|
187
|
+
#
|
188
|
+
# Finally, arguments cannot have type :default or :boolean but can be
|
189
|
+
# optional (supplying :optional => :true or :required => false), although
|
190
|
+
# you cannot have a required argument after a non-required argument. If you
|
191
|
+
# try it, an error is raised.
|
192
|
+
#
|
193
|
+
# ==== Parameters
|
194
|
+
# name<Symbol>:: The name of the argument.
|
195
|
+
# options<Hash>:: Described below.
|
196
|
+
#
|
197
|
+
# ==== Options
|
198
|
+
# :desc - Description for the argument.
|
199
|
+
# :required - If the argument is required or not.
|
200
|
+
# :optional - If the argument is optional or not.
|
201
|
+
# :type - The type of the argument, can be :string, :hash, :array, :numeric.
|
202
|
+
# :default - Default value for this argument. It cannot be required and have default values.
|
203
|
+
# :banner - String to show on usage notes.
|
204
|
+
#
|
205
|
+
# ==== Errors
|
206
|
+
# ArgumentError:: Raised if you supply a required argument after a non required one.
|
207
|
+
#
|
208
|
+
def argument(name, options = {}) # rubocop:disable MethodLength
|
209
|
+
is_thor_reserved_word?(name, :argument)
|
210
|
+
no_commands { attr_accessor name }
|
211
|
+
|
212
|
+
required = if options.key?(:optional)
|
213
|
+
!options[:optional]
|
214
|
+
elsif options.key?(:required)
|
215
|
+
options[:required]
|
216
|
+
else
|
217
|
+
options[:default].nil?
|
218
|
+
end
|
219
|
+
|
220
|
+
remove_argument name
|
221
|
+
|
222
|
+
arguments.each do |argument|
|
223
|
+
next if argument.required?
|
224
|
+
fail ArgumentError, "You cannot have #{name.to_s.inspect} as required argument after " <<
|
225
|
+
"the non-required argument #{argument.human_name.inspect}."
|
226
|
+
end if required
|
227
|
+
|
228
|
+
options[:required] = required
|
229
|
+
|
230
|
+
arguments << Thor::Argument.new(name, options)
|
231
|
+
end
|
232
|
+
|
233
|
+
# Returns this class arguments, looking up in the ancestors chain.
|
234
|
+
#
|
235
|
+
# ==== Returns
|
236
|
+
# Array[Thor::Argument]
|
237
|
+
#
|
238
|
+
def arguments
|
239
|
+
@arguments ||= from_superclass(:arguments, [])
|
240
|
+
end
|
241
|
+
|
242
|
+
# Adds a bunch of options to the set of class options.
|
243
|
+
#
|
244
|
+
# class_options :foo => false, :bar => :required, :baz => :string
|
245
|
+
#
|
246
|
+
# If you prefer more detailed declaration, check class_option.
|
247
|
+
#
|
248
|
+
# ==== Parameters
|
249
|
+
# Hash[Symbol => Object]
|
250
|
+
#
|
251
|
+
def class_options(options = nil)
|
252
|
+
@class_options ||= from_superclass(:class_options, {})
|
253
|
+
build_options(options, @class_options) if options
|
254
|
+
@class_options
|
255
|
+
end
|
256
|
+
|
257
|
+
# Adds an option to the set of class options
|
258
|
+
#
|
259
|
+
# ==== Parameters
|
260
|
+
# name<Symbol>:: The name of the argument.
|
261
|
+
# options<Hash>:: Described below.
|
262
|
+
#
|
263
|
+
# ==== Options
|
264
|
+
# :desc:: -- Description for the argument.
|
265
|
+
# :required:: -- If the argument is required or not.
|
266
|
+
# :default:: -- Default value for this argument.
|
267
|
+
# :group:: -- The group for this options. Use by class options to output options in different levels.
|
268
|
+
# :aliases:: -- Aliases for this option. <b>Note:</b> Thor follows a convention of one-dash-one-letter options. Thus aliases like "-something" wouldn't be parsed; use either "\--something" or "-s" instead.
|
269
|
+
# :type:: -- The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
|
270
|
+
# :banner:: -- String to show on usage notes.
|
271
|
+
# :hide:: -- If you want to hide this option from the help.
|
272
|
+
#
|
273
|
+
def class_option(name, options = {})
|
274
|
+
build_option(name, options, class_options)
|
275
|
+
end
|
276
|
+
|
277
|
+
# Removes a previous defined argument. If :undefine is given, undefine
|
278
|
+
# accessors as well.
|
279
|
+
#
|
280
|
+
# ==== Parameters
|
281
|
+
# names<Array>:: Arguments to be removed
|
282
|
+
#
|
283
|
+
# ==== Examples
|
284
|
+
#
|
285
|
+
# remove_argument :foo
|
286
|
+
# remove_argument :foo, :bar, :baz, :undefine => true
|
287
|
+
#
|
288
|
+
def remove_argument(*names)
|
289
|
+
options = names.last.is_a?(Hash) ? names.pop : {}
|
290
|
+
|
291
|
+
names.each do |name|
|
292
|
+
arguments.delete_if { |a| a.name == name.to_s }
|
293
|
+
undef_method name, "#{name}=" if options[:undefine]
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
# Removes a previous defined class option.
|
298
|
+
#
|
299
|
+
# ==== Parameters
|
300
|
+
# names<Array>:: Class options to be removed
|
301
|
+
#
|
302
|
+
# ==== Examples
|
303
|
+
#
|
304
|
+
# remove_class_option :foo
|
305
|
+
# remove_class_option :foo, :bar, :baz
|
306
|
+
#
|
307
|
+
def remove_class_option(*names)
|
308
|
+
names.each do |name|
|
309
|
+
class_options.delete(name)
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
# Defines the group. This is used when thor list is invoked so you can specify
|
314
|
+
# that only commands from a pre-defined group will be shown. Defaults to standard.
|
315
|
+
#
|
316
|
+
# ==== Parameters
|
317
|
+
# name<String|Symbol>
|
318
|
+
#
|
319
|
+
def group(name = nil)
|
320
|
+
if name
|
321
|
+
@group = name.to_s
|
322
|
+
else
|
323
|
+
@group ||= from_superclass(:group, "standard")
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
# Returns the commands for this Thor class.
|
328
|
+
#
|
329
|
+
# ==== Returns
|
330
|
+
# OrderedHash:: An ordered hash with commands names as keys and Thor::Command
|
331
|
+
# objects as values.
|
332
|
+
#
|
333
|
+
def commands
|
334
|
+
@commands ||= Thor::CoreExt::OrderedHash.new
|
335
|
+
end
|
336
|
+
alias_method :tasks, :commands
|
337
|
+
|
338
|
+
# Returns the commands for this Thor class and all subclasses.
|
339
|
+
#
|
340
|
+
# ==== Returns
|
341
|
+
# OrderedHash:: An ordered hash with commands names as keys and Thor::Command
|
342
|
+
# objects as values.
|
343
|
+
#
|
344
|
+
def all_commands
|
345
|
+
@all_commands ||= from_superclass(:all_commands, Thor::CoreExt::OrderedHash.new)
|
346
|
+
@all_commands.merge(commands)
|
347
|
+
end
|
348
|
+
alias_method :all_tasks, :all_commands
|
349
|
+
|
350
|
+
# Removes a given command from this Thor class. This is usually done if you
|
351
|
+
# are inheriting from another class and don't want it to be available
|
352
|
+
# anymore.
|
353
|
+
#
|
354
|
+
# By default it only remove the mapping to the command. But you can supply
|
355
|
+
# :undefine => true to undefine the method from the class as well.
|
356
|
+
#
|
357
|
+
# ==== Parameters
|
358
|
+
# name<Symbol|String>:: The name of the command to be removed
|
359
|
+
# options<Hash>:: You can give :undefine => true if you want commands the method
|
360
|
+
# to be undefined from the class as well.
|
361
|
+
#
|
362
|
+
def remove_command(*names)
|
363
|
+
options = names.last.is_a?(Hash) ? names.pop : {}
|
364
|
+
|
365
|
+
names.each do |name|
|
366
|
+
commands.delete(name.to_s)
|
367
|
+
all_commands.delete(name.to_s)
|
368
|
+
undef_method name if options[:undefine]
|
369
|
+
end
|
370
|
+
end
|
371
|
+
alias_method :remove_task, :remove_command
|
372
|
+
|
373
|
+
# All methods defined inside the given block are not added as commands.
|
374
|
+
#
|
375
|
+
# So you can do:
|
376
|
+
#
|
377
|
+
# class MyScript < Thor
|
378
|
+
# no_commands do
|
379
|
+
# def this_is_not_a_command
|
380
|
+
# end
|
381
|
+
# end
|
382
|
+
# end
|
383
|
+
#
|
384
|
+
# You can also add the method and remove it from the command list:
|
385
|
+
#
|
386
|
+
# class MyScript < Thor
|
387
|
+
# def this_is_not_a_command
|
388
|
+
# end
|
389
|
+
# remove_command :this_is_not_a_command
|
390
|
+
# end
|
391
|
+
#
|
392
|
+
def no_commands
|
393
|
+
@no_commands = true
|
394
|
+
yield
|
395
|
+
ensure
|
396
|
+
@no_commands = false
|
397
|
+
end
|
398
|
+
alias_method :no_tasks, :no_commands
|
399
|
+
|
400
|
+
# Sets the namespace for the Thor or Thor::Group class. By default the
|
401
|
+
# namespace is retrieved from the class name. If your Thor class is named
|
402
|
+
# Scripts::MyScript, the help method, for example, will be called as:
|
403
|
+
#
|
404
|
+
# thor scripts:my_script -h
|
405
|
+
#
|
406
|
+
# If you change the namespace:
|
407
|
+
#
|
408
|
+
# namespace :my_scripts
|
409
|
+
#
|
410
|
+
# You change how your commands are invoked:
|
411
|
+
#
|
412
|
+
# thor my_scripts -h
|
413
|
+
#
|
414
|
+
# Finally, if you change your namespace to default:
|
415
|
+
#
|
416
|
+
# namespace :default
|
417
|
+
#
|
418
|
+
# Your commands can be invoked with a shortcut. Instead of:
|
419
|
+
#
|
420
|
+
# thor :my_command
|
421
|
+
#
|
422
|
+
def namespace(name = nil)
|
423
|
+
if name
|
424
|
+
@namespace = name.to_s
|
425
|
+
else
|
426
|
+
@namespace ||= Thor::Util.namespace_from_thor_class(self)
|
427
|
+
end
|
428
|
+
end
|
429
|
+
|
430
|
+
# Parses the command and options from the given args, instantiate the class
|
431
|
+
# and invoke the command. This method is used when the arguments must be parsed
|
432
|
+
# from an array. If you are inside Ruby and want to use a Thor class, you
|
433
|
+
# can simply initialize it:
|
434
|
+
#
|
435
|
+
# script = MyScript.new(args, options, config)
|
436
|
+
# script.invoke(:command, first_arg, second_arg, third_arg)
|
437
|
+
#
|
438
|
+
def start(given_args = ARGV, config = {})
|
439
|
+
config[:shell] ||= Thor::Base.shell.new
|
440
|
+
dispatch(nil, given_args.dup, nil, config)
|
441
|
+
rescue Thor::Error => e
|
442
|
+
config[:debug] || ENV["THOR_DEBUG"] == "1" ? (raise e) : config[:shell].error(e.message)
|
443
|
+
exit(1) if exit_on_failure?
|
444
|
+
rescue Errno::EPIPE
|
445
|
+
# This happens if a thor command is piped to something like `head`,
|
446
|
+
# which closes the pipe when it's done reading. This will also
|
447
|
+
# mean that if the pipe is closed, further unnecessary
|
448
|
+
# computation will not occur.
|
449
|
+
exit(0)
|
450
|
+
end
|
451
|
+
|
452
|
+
# Allows to use private methods from parent in child classes as commands.
|
453
|
+
#
|
454
|
+
# ==== Parameters
|
455
|
+
# names<Array>:: Method names to be used as commands
|
456
|
+
#
|
457
|
+
# ==== Examples
|
458
|
+
#
|
459
|
+
# public_command :foo
|
460
|
+
# public_command :foo, :bar, :baz
|
461
|
+
#
|
462
|
+
def public_command(*names)
|
463
|
+
names.each do |name|
|
464
|
+
class_eval "def #{name}(*); super end"
|
465
|
+
end
|
466
|
+
end
|
467
|
+
alias_method :public_task, :public_command
|
468
|
+
|
469
|
+
def handle_no_command_error(command, has_namespace = $thor_runner) #:nodoc:
|
470
|
+
if has_namespace
|
471
|
+
fail UndefinedCommandError, "Could not find command #{command.inspect} in #{namespace.inspect} namespace."
|
472
|
+
else
|
473
|
+
fail UndefinedCommandError, "Could not find command #{command.inspect}."
|
474
|
+
end
|
475
|
+
end
|
476
|
+
alias_method :handle_no_task_error, :handle_no_command_error
|
477
|
+
|
478
|
+
def handle_argument_error(command, error, args, arity) #:nodoc:
|
479
|
+
msg = "ERROR: \"#{basename} #{command.name}\" was called with "
|
480
|
+
msg << "no arguments" if args.empty?
|
481
|
+
msg << "arguments " << args.inspect unless args.empty?
|
482
|
+
msg << "\nUsage: #{banner(command).inspect}"
|
483
|
+
fail InvocationError, msg
|
484
|
+
end
|
485
|
+
|
486
|
+
protected
|
487
|
+
|
488
|
+
# Prints the class options per group. If an option does not belong to
|
489
|
+
# any group, it's printed as Class option.
|
490
|
+
#
|
491
|
+
def class_options_help(shell, groups = {}) #:nodoc:
|
492
|
+
# Group options by group
|
493
|
+
class_options.each do |_, value|
|
494
|
+
groups[value.group] ||= []
|
495
|
+
groups[value.group] << value
|
496
|
+
end
|
497
|
+
|
498
|
+
# Deal with default group
|
499
|
+
global_options = groups.delete(nil) || []
|
500
|
+
print_options(shell, global_options)
|
501
|
+
|
502
|
+
# Print all others
|
503
|
+
groups.each do |group_name, options|
|
504
|
+
print_options(shell, options, group_name)
|
505
|
+
end
|
506
|
+
end
|
507
|
+
|
508
|
+
# Receives a set of options and print them.
|
509
|
+
def print_options(shell, options, group_name = nil)
|
510
|
+
return if options.empty?
|
511
|
+
|
512
|
+
list = []
|
513
|
+
padding = options.map { |o| o.aliases.size }.max.to_i * 4
|
514
|
+
|
515
|
+
options.each do |option|
|
516
|
+
unless option.hide
|
517
|
+
item = [option.usage(padding)]
|
518
|
+
item.push(option.description ? "# #{option.description}" : "")
|
519
|
+
|
520
|
+
list << item
|
521
|
+
list << ["", "# Default: #{option.default}"] if option.show_default?
|
522
|
+
list << ["", "# Possible values: #{option.enum.join(', ')}"] if option.enum
|
523
|
+
end
|
524
|
+
end
|
525
|
+
|
526
|
+
shell.say(group_name ? "#{group_name} options:" : "Options:")
|
527
|
+
shell.print_table(list, :indent => 2)
|
528
|
+
shell.say ""
|
529
|
+
end
|
530
|
+
|
531
|
+
# Raises an error if the word given is a Thor reserved word.
|
532
|
+
def is_thor_reserved_word?(word, type) #:nodoc:
|
533
|
+
return false unless THOR_RESERVED_WORDS.include?(word.to_s)
|
534
|
+
fail "#{word.inspect} is a Thor reserved word and cannot be defined as #{type}"
|
535
|
+
end
|
536
|
+
|
537
|
+
# Build an option and adds it to the given scope.
|
538
|
+
#
|
539
|
+
# ==== Parameters
|
540
|
+
# name<Symbol>:: The name of the argument.
|
541
|
+
# options<Hash>:: Described in both class_option and method_option.
|
542
|
+
# scope<Hash>:: Options hash that is being built up
|
543
|
+
def build_option(name, options, scope) #:nodoc:
|
544
|
+
scope[name] = Thor::Option.new(name, options)
|
545
|
+
end
|
546
|
+
|
547
|
+
# Receives a hash of options, parse them and add to the scope. This is a
|
548
|
+
# fast way to set a bunch of options:
|
549
|
+
#
|
550
|
+
# build_options :foo => true, :bar => :required, :baz => :string
|
551
|
+
#
|
552
|
+
# ==== Parameters
|
553
|
+
# Hash[Symbol => Object]
|
554
|
+
def build_options(options, scope) #:nodoc:
|
555
|
+
options.each do |key, value|
|
556
|
+
scope[key] = Thor::Option.parse(key, value)
|
557
|
+
end
|
558
|
+
end
|
559
|
+
|
560
|
+
# Finds a command with the given name. If the command belongs to the current
|
561
|
+
# class, just return it, otherwise dup it and add the fresh copy to the
|
562
|
+
# current command hash.
|
563
|
+
def find_and_refresh_command(name) #:nodoc:
|
564
|
+
if commands[name.to_s]
|
565
|
+
commands[name.to_s]
|
566
|
+
elsif command = all_commands[name.to_s] # rubocop:disable AssignmentInCondition
|
567
|
+
commands[name.to_s] = command.clone
|
568
|
+
else
|
569
|
+
fail ArgumentError, "You supplied :for => #{name.inspect}, but the command #{name.inspect} could not be found."
|
570
|
+
end
|
571
|
+
end
|
572
|
+
alias_method :find_and_refresh_task, :find_and_refresh_command
|
573
|
+
|
574
|
+
# Everytime someone inherits from a Thor class, register the klass
|
575
|
+
# and file into baseclass.
|
576
|
+
def inherited(klass)
|
577
|
+
Thor::Base.register_klass_file(klass)
|
578
|
+
klass.instance_variable_set(:@no_commands, false)
|
579
|
+
end
|
580
|
+
|
581
|
+
# Fire this callback whenever a method is added. Added methods are
|
582
|
+
# tracked as commands by invoking the create_command method.
|
583
|
+
def method_added(meth)
|
584
|
+
meth = meth.to_s
|
585
|
+
|
586
|
+
if meth == "initialize"
|
587
|
+
initialize_added
|
588
|
+
return
|
589
|
+
end
|
590
|
+
|
591
|
+
# Return if it's not a public instance method
|
592
|
+
return unless public_method_defined?(meth.to_sym)
|
593
|
+
|
594
|
+
@no_commands ||= false
|
595
|
+
return if @no_commands || !create_command(meth)
|
596
|
+
|
597
|
+
is_thor_reserved_word?(meth, :command)
|
598
|
+
Thor::Base.register_klass_file(self)
|
599
|
+
end
|
600
|
+
|
601
|
+
# Retrieves a value from superclass. If it reaches the baseclass,
|
602
|
+
# returns default.
|
603
|
+
def from_superclass(method, default = nil)
|
604
|
+
if self == baseclass || !superclass.respond_to?(method, true)
|
605
|
+
default
|
606
|
+
else
|
607
|
+
value = superclass.send(method)
|
608
|
+
|
609
|
+
# Ruby implements `dup` on Object, but raises a `TypeError`
|
610
|
+
# if the method is called on immediates. As a result, we
|
611
|
+
# don't have a good way to check whether dup will succeed
|
612
|
+
# without calling it and rescuing the TypeError.
|
613
|
+
begin
|
614
|
+
value.dup
|
615
|
+
rescue TypeError
|
616
|
+
value
|
617
|
+
end
|
618
|
+
|
619
|
+
end
|
620
|
+
end
|
621
|
+
|
622
|
+
# A flag that makes the process exit with status 1 if any error happens.
|
623
|
+
def exit_on_failure?
|
624
|
+
false
|
625
|
+
end
|
626
|
+
|
627
|
+
#
|
628
|
+
# The basename of the program invoking the thor class.
|
629
|
+
#
|
630
|
+
def basename
|
631
|
+
File.basename($PROGRAM_NAME).split(" ").first
|
632
|
+
end
|
633
|
+
|
634
|
+
# SIGNATURE: Sets the baseclass. This is where the superclass lookup
|
635
|
+
# finishes.
|
636
|
+
def baseclass #:nodoc:
|
637
|
+
end
|
638
|
+
|
639
|
+
# SIGNATURE: Creates a new command if valid_command? is true. This method is
|
640
|
+
# called when a new method is added to the class.
|
641
|
+
def create_command(meth) #:nodoc:
|
642
|
+
end
|
643
|
+
alias_method :create_task, :create_command
|
644
|
+
|
645
|
+
# SIGNATURE: Defines behavior when the initialize method is added to the
|
646
|
+
# class.
|
647
|
+
def initialize_added #:nodoc:
|
648
|
+
end
|
649
|
+
|
650
|
+
# SIGNATURE: The hook invoked by start.
|
651
|
+
def dispatch(command, given_args, given_opts, config) #:nodoc:
|
652
|
+
fail NotImplementedError
|
653
|
+
end
|
654
|
+
end
|
655
|
+
end
|
656
|
+
end
|