honeybadger 5.0.2 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +713 -701
  3. data/LICENSE +19 -19
  4. data/README.md +57 -57
  5. data/TROUBLESHOOTING.md +3 -3
  6. data/bin/honeybadger +5 -5
  7. data/lib/honeybadger/agent.rb +488 -488
  8. data/lib/honeybadger/backend/base.rb +116 -116
  9. data/lib/honeybadger/backend/debug.rb +22 -22
  10. data/lib/honeybadger/backend/null.rb +29 -29
  11. data/lib/honeybadger/backend/server.rb +62 -62
  12. data/lib/honeybadger/backend/test.rb +46 -46
  13. data/lib/honeybadger/backend.rb +27 -27
  14. data/lib/honeybadger/backtrace.rb +181 -181
  15. data/lib/honeybadger/breadcrumbs/active_support.rb +119 -119
  16. data/lib/honeybadger/breadcrumbs/breadcrumb.rb +53 -53
  17. data/lib/honeybadger/breadcrumbs/collector.rb +82 -82
  18. data/lib/honeybadger/breadcrumbs/logging.rb +51 -51
  19. data/lib/honeybadger/breadcrumbs/ring_buffer.rb +44 -44
  20. data/lib/honeybadger/breadcrumbs.rb +8 -8
  21. data/lib/honeybadger/cli/deploy.rb +43 -43
  22. data/lib/honeybadger/cli/exec.rb +143 -143
  23. data/lib/honeybadger/cli/helpers.rb +28 -28
  24. data/lib/honeybadger/cli/heroku.rb +129 -129
  25. data/lib/honeybadger/cli/install.rb +101 -101
  26. data/lib/honeybadger/cli/main.rb +237 -237
  27. data/lib/honeybadger/cli/notify.rb +67 -67
  28. data/lib/honeybadger/cli/test.rb +267 -267
  29. data/lib/honeybadger/cli.rb +14 -14
  30. data/lib/honeybadger/config/defaults.rb +336 -333
  31. data/lib/honeybadger/config/env.rb +42 -42
  32. data/lib/honeybadger/config/ruby.rb +146 -146
  33. data/lib/honeybadger/config/yaml.rb +76 -76
  34. data/lib/honeybadger/config.rb +413 -413
  35. data/lib/honeybadger/const.rb +20 -20
  36. data/lib/honeybadger/context_manager.rb +55 -55
  37. data/lib/honeybadger/conversions.rb +16 -16
  38. data/lib/honeybadger/init/rails.rb +38 -38
  39. data/lib/honeybadger/init/rake.rb +66 -66
  40. data/lib/honeybadger/init/ruby.rb +11 -11
  41. data/lib/honeybadger/init/sinatra.rb +51 -51
  42. data/lib/honeybadger/logging.rb +177 -177
  43. data/lib/honeybadger/notice.rb +579 -568
  44. data/lib/honeybadger/plugin.rb +210 -210
  45. data/lib/honeybadger/plugins/breadcrumbs.rb +111 -111
  46. data/lib/honeybadger/plugins/delayed_job/plugin.rb +56 -56
  47. data/lib/honeybadger/plugins/delayed_job.rb +22 -22
  48. data/lib/honeybadger/plugins/faktory.rb +52 -52
  49. data/lib/honeybadger/plugins/lambda.rb +71 -71
  50. data/lib/honeybadger/plugins/local_variables.rb +44 -44
  51. data/lib/honeybadger/plugins/passenger.rb +23 -23
  52. data/lib/honeybadger/plugins/rails.rb +72 -63
  53. data/lib/honeybadger/plugins/resque.rb +72 -72
  54. data/lib/honeybadger/plugins/shoryuken.rb +52 -52
  55. data/lib/honeybadger/plugins/sidekiq.rb +71 -62
  56. data/lib/honeybadger/plugins/sucker_punch.rb +18 -18
  57. data/lib/honeybadger/plugins/thor.rb +32 -32
  58. data/lib/honeybadger/plugins/warden.rb +19 -19
  59. data/lib/honeybadger/rack/error_notifier.rb +92 -92
  60. data/lib/honeybadger/rack/user_feedback.rb +88 -88
  61. data/lib/honeybadger/rack/user_informer.rb +45 -45
  62. data/lib/honeybadger/ruby.rb +2 -2
  63. data/lib/honeybadger/singleton.rb +103 -103
  64. data/lib/honeybadger/tasks.rb +22 -22
  65. data/lib/honeybadger/templates/feedback_form.erb +84 -84
  66. data/lib/honeybadger/util/http.rb +92 -92
  67. data/lib/honeybadger/util/lambda.rb +32 -32
  68. data/lib/honeybadger/util/request_hash.rb +73 -73
  69. data/lib/honeybadger/util/request_payload.rb +41 -41
  70. data/lib/honeybadger/util/revision.rb +39 -39
  71. data/lib/honeybadger/util/sanitizer.rb +214 -214
  72. data/lib/honeybadger/util/sql.rb +34 -34
  73. data/lib/honeybadger/util/stats.rb +50 -50
  74. data/lib/honeybadger/version.rb +4 -4
  75. data/lib/honeybadger/worker.rb +253 -253
  76. data/lib/honeybadger.rb +11 -11
  77. data/resources/ca-bundle.crt +3376 -3376
  78. data/vendor/capistrano-honeybadger/lib/capistrano/honeybadger.rb +5 -5
  79. data/vendor/capistrano-honeybadger/lib/capistrano/tasks/deploy.cap +89 -89
  80. data/vendor/capistrano-honeybadger/lib/honeybadger/capistrano/legacy.rb +47 -47
  81. data/vendor/capistrano-honeybadger/lib/honeybadger/capistrano.rb +2 -2
  82. data/vendor/cli/inifile.rb +628 -628
  83. data/vendor/cli/thor/actions/create_file.rb +103 -103
  84. data/vendor/cli/thor/actions/create_link.rb +59 -59
  85. data/vendor/cli/thor/actions/directory.rb +118 -118
  86. data/vendor/cli/thor/actions/empty_directory.rb +135 -135
  87. data/vendor/cli/thor/actions/file_manipulation.rb +316 -316
  88. data/vendor/cli/thor/actions/inject_into_file.rb +107 -107
  89. data/vendor/cli/thor/actions.rb +319 -319
  90. data/vendor/cli/thor/base.rb +656 -656
  91. data/vendor/cli/thor/command.rb +133 -133
  92. data/vendor/cli/thor/core_ext/hash_with_indifferent_access.rb +77 -77
  93. data/vendor/cli/thor/core_ext/io_binary_read.rb +10 -10
  94. data/vendor/cli/thor/core_ext/ordered_hash.rb +98 -98
  95. data/vendor/cli/thor/error.rb +32 -32
  96. data/vendor/cli/thor/group.rb +281 -281
  97. data/vendor/cli/thor/invocation.rb +178 -178
  98. data/vendor/cli/thor/line_editor/basic.rb +35 -35
  99. data/vendor/cli/thor/line_editor/readline.rb +88 -88
  100. data/vendor/cli/thor/line_editor.rb +17 -17
  101. data/vendor/cli/thor/parser/argument.rb +73 -73
  102. data/vendor/cli/thor/parser/arguments.rb +175 -175
  103. data/vendor/cli/thor/parser/option.rb +125 -125
  104. data/vendor/cli/thor/parser/options.rb +218 -218
  105. data/vendor/cli/thor/parser.rb +4 -4
  106. data/vendor/cli/thor/rake_compat.rb +71 -71
  107. data/vendor/cli/thor/runner.rb +322 -322
  108. data/vendor/cli/thor/shell/basic.rb +421 -421
  109. data/vendor/cli/thor/shell/color.rb +149 -149
  110. data/vendor/cli/thor/shell/html.rb +126 -126
  111. data/vendor/cli/thor/shell.rb +81 -81
  112. data/vendor/cli/thor/util.rb +267 -267
  113. data/vendor/cli/thor/version.rb +3 -3
  114. data/vendor/cli/thor.rb +484 -484
  115. metadata +10 -5
@@ -1,322 +1,322 @@
1
- require "thor"
2
- require "thor/group"
3
- require "thor/core_ext/io_binary_read"
4
-
5
- require "fileutils"
6
- require "open-uri"
7
- require "yaml"
8
- require "digest/md5"
9
- require "pathname"
10
-
11
- class Thor::Runner < Thor #:nodoc: # rubocop:disable ClassLength
12
- map "-T" => :list, "-i" => :install, "-u" => :update, "-v" => :version
13
-
14
- # Override Thor#help so it can give information about any class and any method.
15
- #
16
- def help(meth = nil)
17
- if meth && !self.respond_to?(meth)
18
- initialize_thorfiles(meth)
19
- klass, command = Thor::Util.find_class_and_command_by_namespace(meth)
20
- self.class.handle_no_command_error(command, false) if klass.nil?
21
- klass.start(["-h", command].compact, :shell => shell)
22
- else
23
- super
24
- end
25
- end
26
-
27
- # If a command is not found on Thor::Runner, method missing is invoked and
28
- # Thor::Runner is then responsible for finding the command in all classes.
29
- #
30
- def method_missing(meth, *args)
31
- meth = meth.to_s
32
- initialize_thorfiles(meth)
33
- klass, command = Thor::Util.find_class_and_command_by_namespace(meth)
34
- self.class.handle_no_command_error(command, false) if klass.nil?
35
- args.unshift(command) if command
36
- klass.start(args, :shell => shell)
37
- end
38
-
39
- desc "install NAME", "Install an optionally named Thor file into your system commands"
40
- method_options :as => :string, :relative => :boolean, :force => :boolean
41
- def install(name) # rubocop:disable MethodLength
42
- initialize_thorfiles
43
-
44
- # If a directory name is provided as the argument, look for a 'main.thor'
45
- # command in said directory.
46
- begin
47
- if File.directory?(File.expand_path(name))
48
- base, package = File.join(name, "main.thor"), :directory
49
- contents = open(base) { |input| input.read }
50
- else
51
- base, package = name, :file
52
- contents = open(name) { |input| input.read }
53
- end
54
- rescue OpenURI::HTTPError
55
- raise Error, "Error opening URI '#{name}'"
56
- rescue Errno::ENOENT
57
- fail Error, "Error opening file '#{name}'"
58
- end
59
-
60
- say "Your Thorfile contains:"
61
- say contents
62
-
63
- unless options["force"]
64
- return false if no?("Do you wish to continue [y/N]?")
65
- end
66
-
67
- as = options["as"] || begin
68
- first_line = contents.split("\n")[0]
69
- (match = first_line.match(/\s*#\s*module:\s*([^\n]*)/)) ? match[1].strip : nil
70
- end
71
-
72
- unless as
73
- basename = File.basename(name)
74
- as = ask("Please specify a name for #{name} in the system repository [#{basename}]:")
75
- as = basename if as.empty?
76
- end
77
-
78
- location = if options[:relative] || name =~ %r{^https?://}
79
- name
80
- else
81
- File.expand_path(name)
82
- end
83
-
84
- thor_yaml[as] = {
85
- :filename => Digest::MD5.hexdigest(name + as),
86
- :location => location,
87
- :namespaces => Thor::Util.namespaces_in_content(contents, base)
88
- }
89
-
90
- save_yaml(thor_yaml)
91
- say "Storing thor file in your system repository"
92
- destination = File.join(thor_root, thor_yaml[as][:filename])
93
-
94
- if package == :file
95
- File.open(destination, "w") { |f| f.puts contents }
96
- else
97
- FileUtils.cp_r(name, destination)
98
- end
99
-
100
- thor_yaml[as][:filename] # Indicate success
101
- end
102
-
103
- desc "version", "Show Thor version"
104
- def version
105
- require "thor/version"
106
- say "Thor #{Thor::VERSION}"
107
- end
108
-
109
- desc "uninstall NAME", "Uninstall a named Thor module"
110
- def uninstall(name)
111
- fail Error, "Can't find module '#{name}'" unless thor_yaml[name]
112
- say "Uninstalling #{name}."
113
- FileUtils.rm_rf(File.join(thor_root, "#{thor_yaml[name][:filename]}"))
114
-
115
- thor_yaml.delete(name)
116
- save_yaml(thor_yaml)
117
-
118
- puts "Done."
119
- end
120
-
121
- desc "update NAME", "Update a Thor file from its original location"
122
- def update(name)
123
- fail Error, "Can't find module '#{name}'" if !thor_yaml[name] || !thor_yaml[name][:location]
124
-
125
- say "Updating '#{name}' from #{thor_yaml[name][:location]}"
126
-
127
- old_filename = thor_yaml[name][:filename]
128
- self.options = options.merge("as" => name)
129
-
130
- if File.directory? File.expand_path(name)
131
- FileUtils.rm_rf(File.join(thor_root, old_filename))
132
-
133
- thor_yaml.delete(old_filename)
134
- save_yaml(thor_yaml)
135
-
136
- filename = install(name)
137
- else
138
- filename = install(thor_yaml[name][:location])
139
- end
140
-
141
- unless filename == old_filename
142
- File.delete(File.join(thor_root, old_filename))
143
- end
144
- end
145
-
146
- desc "installed", "List the installed Thor modules and commands"
147
- method_options :internal => :boolean
148
- def installed
149
- initialize_thorfiles(nil, true)
150
- display_klasses(true, options["internal"])
151
- end
152
-
153
- desc "list [SEARCH]", "List the available thor commands (--substring means .*SEARCH)"
154
- method_options :substring => :boolean, :group => :string, :all => :boolean, :debug => :boolean
155
- def list(search = "")
156
- initialize_thorfiles
157
-
158
- search = ".*#{search}" if options["substring"]
159
- search = /^#{search}.*/i
160
- group = options[:group] || "standard"
161
-
162
- klasses = Thor::Base.subclasses.select do |k|
163
- (options[:all] || k.group == group) && k.namespace =~ search
164
- end
165
-
166
- display_klasses(false, false, klasses)
167
- end
168
-
169
- private
170
-
171
- def self.banner(command, all = false, subcommand = false)
172
- "thor " + command.formatted_usage(self, all, subcommand)
173
- end
174
-
175
- def thor_root
176
- Thor::Util.thor_root
177
- end
178
-
179
- def thor_yaml
180
- @thor_yaml ||= begin
181
- yaml_file = File.join(thor_root, "thor.yml")
182
- yaml = YAML.load_file(yaml_file) if File.exist?(yaml_file)
183
- yaml || {}
184
- end
185
- end
186
-
187
- # Save the yaml file. If none exists in thor root, creates one.
188
- #
189
- def save_yaml(yaml)
190
- yaml_file = File.join(thor_root, "thor.yml")
191
-
192
- unless File.exist?(yaml_file)
193
- FileUtils.mkdir_p(thor_root)
194
- yaml_file = File.join(thor_root, "thor.yml")
195
- FileUtils.touch(yaml_file)
196
- end
197
-
198
- File.open(yaml_file, "w") { |f| f.puts yaml.to_yaml }
199
- end
200
-
201
- def self.exit_on_failure?
202
- true
203
- end
204
-
205
- # Load the Thorfiles. If relevant_to is supplied, looks for specific files
206
- # in the thor_root instead of loading them all.
207
- #
208
- # By default, it also traverses the current path until find Thor files, as
209
- # described in thorfiles. This look up can be skipped by supplying
210
- # skip_lookup true.
211
- #
212
- def initialize_thorfiles(relevant_to = nil, skip_lookup = false)
213
- thorfiles(relevant_to, skip_lookup).each do |f|
214
- Thor::Util.load_thorfile(f, nil, options[:debug]) unless Thor::Base.subclass_files.keys.include?(File.expand_path(f))
215
- end
216
- end
217
-
218
- # Finds Thorfiles by traversing from your current directory down to the root
219
- # directory of your system. If at any time we find a Thor file, we stop.
220
- #
221
- # We also ensure that system-wide Thorfiles are loaded first, so local
222
- # Thorfiles can override them.
223
- #
224
- # ==== Example
225
- #
226
- # If we start at /Users/wycats/dev/thor ...
227
- #
228
- # 1. /Users/wycats/dev/thor
229
- # 2. /Users/wycats/dev
230
- # 3. /Users/wycats <-- we find a Thorfile here, so we stop
231
- #
232
- # Suppose we start at c:\Documents and Settings\james\dev\thor ...
233
- #
234
- # 1. c:\Documents and Settings\james\dev\thor
235
- # 2. c:\Documents and Settings\james\dev
236
- # 3. c:\Documents and Settings\james
237
- # 4. c:\Documents and Settings
238
- # 5. c:\ <-- no Thorfiles found!
239
- #
240
- def thorfiles(relevant_to = nil, skip_lookup = false)
241
- thorfiles = []
242
-
243
- unless skip_lookup
244
- Pathname.pwd.ascend do |path|
245
- thorfiles = Thor::Util.globs_for(path).map { |g| Dir[g] }.flatten
246
- break unless thorfiles.empty?
247
- end
248
- end
249
-
250
- files = (relevant_to ? thorfiles_relevant_to(relevant_to) : Thor::Util.thor_root_glob)
251
- files += thorfiles
252
- files -= ["#{thor_root}/thor.yml"]
253
-
254
- files.map! do |file|
255
- File.directory?(file) ? File.join(file, "main.thor") : file
256
- end
257
- end
258
-
259
- # Load Thorfiles relevant to the given method. If you provide "foo:bar" it
260
- # will load all thor files in the thor.yaml that has "foo" e "foo:bar"
261
- # namespaces registered.
262
- #
263
- def thorfiles_relevant_to(meth)
264
- lookup = [meth, meth.split(":")[0...-1].join(":")]
265
-
266
- files = thor_yaml.select do |k, v|
267
- v[:namespaces] && !(v[:namespaces] & lookup).empty?
268
- end
269
-
270
- files.map { |k, v| File.join(thor_root, "#{v[:filename]}") }
271
- end
272
-
273
- # Display information about the given klasses. If with_module is given,
274
- # it shows a table with information extracted from the yaml file.
275
- #
276
- def display_klasses(with_modules = false, show_internal = false, klasses = Thor::Base.subclasses)
277
- klasses -= [Thor, Thor::Runner, Thor::Group] unless show_internal
278
-
279
- fail Error, "No Thor commands available" if klasses.empty?
280
- show_modules if with_modules && !thor_yaml.empty?
281
-
282
- list = Hash.new { |h, k| h[k] = [] }
283
- groups = klasses.select { |k| k.ancestors.include?(Thor::Group) }
284
-
285
- # Get classes which inherit from Thor
286
- (klasses - groups).each { |k| list[k.namespace.split(":").first] += k.printable_commands(false) }
287
-
288
- # Get classes which inherit from Thor::Base
289
- groups.map! { |k| k.printable_commands(false).first }
290
- list["root"] = groups
291
-
292
- # Order namespaces with default coming first
293
- list = list.sort { |a, b| a[0].sub(/^default/, "") <=> b[0].sub(/^default/, "") }
294
- list.each { |n, commands| display_commands(n, commands) unless commands.empty? }
295
- end
296
-
297
- def display_commands(namespace, list) #:nodoc:
298
- list.sort! { |a, b| a[0] <=> b[0] }
299
-
300
- say shell.set_color(namespace, :blue, true)
301
- say "-" * namespace.size
302
-
303
- print_table(list, :truncate => true)
304
- say
305
- end
306
- alias_method :display_tasks, :display_commands
307
-
308
- def show_modules #:nodoc:
309
- info = []
310
- labels = %w[Modules Namespaces]
311
-
312
- info << labels
313
- info << ["-" * labels[0].size, "-" * labels[1].size]
314
-
315
- thor_yaml.each do |name, hash|
316
- info << [name, hash[:namespaces].join(", ")]
317
- end
318
-
319
- print_table info
320
- say ""
321
- end
322
- end
1
+ require "thor"
2
+ require "thor/group"
3
+ require "thor/core_ext/io_binary_read"
4
+
5
+ require "fileutils"
6
+ require "open-uri"
7
+ require "yaml"
8
+ require "digest/md5"
9
+ require "pathname"
10
+
11
+ class Thor::Runner < Thor #:nodoc: # rubocop:disable ClassLength
12
+ map "-T" => :list, "-i" => :install, "-u" => :update, "-v" => :version
13
+
14
+ # Override Thor#help so it can give information about any class and any method.
15
+ #
16
+ def help(meth = nil)
17
+ if meth && !self.respond_to?(meth)
18
+ initialize_thorfiles(meth)
19
+ klass, command = Thor::Util.find_class_and_command_by_namespace(meth)
20
+ self.class.handle_no_command_error(command, false) if klass.nil?
21
+ klass.start(["-h", command].compact, :shell => shell)
22
+ else
23
+ super
24
+ end
25
+ end
26
+
27
+ # If a command is not found on Thor::Runner, method missing is invoked and
28
+ # Thor::Runner is then responsible for finding the command in all classes.
29
+ #
30
+ def method_missing(meth, *args)
31
+ meth = meth.to_s
32
+ initialize_thorfiles(meth)
33
+ klass, command = Thor::Util.find_class_and_command_by_namespace(meth)
34
+ self.class.handle_no_command_error(command, false) if klass.nil?
35
+ args.unshift(command) if command
36
+ klass.start(args, :shell => shell)
37
+ end
38
+
39
+ desc "install NAME", "Install an optionally named Thor file into your system commands"
40
+ method_options :as => :string, :relative => :boolean, :force => :boolean
41
+ def install(name) # rubocop:disable MethodLength
42
+ initialize_thorfiles
43
+
44
+ # If a directory name is provided as the argument, look for a 'main.thor'
45
+ # command in said directory.
46
+ begin
47
+ if File.directory?(File.expand_path(name))
48
+ base, package = File.join(name, "main.thor"), :directory
49
+ contents = open(base) { |input| input.read }
50
+ else
51
+ base, package = name, :file
52
+ contents = open(name) { |input| input.read }
53
+ end
54
+ rescue OpenURI::HTTPError
55
+ raise Error, "Error opening URI '#{name}'"
56
+ rescue Errno::ENOENT
57
+ fail Error, "Error opening file '#{name}'"
58
+ end
59
+
60
+ say "Your Thorfile contains:"
61
+ say contents
62
+
63
+ unless options["force"]
64
+ return false if no?("Do you wish to continue [y/N]?")
65
+ end
66
+
67
+ as = options["as"] || begin
68
+ first_line = contents.split("\n")[0]
69
+ (match = first_line.match(/\s*#\s*module:\s*([^\n]*)/)) ? match[1].strip : nil
70
+ end
71
+
72
+ unless as
73
+ basename = File.basename(name)
74
+ as = ask("Please specify a name for #{name} in the system repository [#{basename}]:")
75
+ as = basename if as.empty?
76
+ end
77
+
78
+ location = if options[:relative] || name =~ %r{^https?://}
79
+ name
80
+ else
81
+ File.expand_path(name)
82
+ end
83
+
84
+ thor_yaml[as] = {
85
+ :filename => Digest::MD5.hexdigest(name + as),
86
+ :location => location,
87
+ :namespaces => Thor::Util.namespaces_in_content(contents, base)
88
+ }
89
+
90
+ save_yaml(thor_yaml)
91
+ say "Storing thor file in your system repository"
92
+ destination = File.join(thor_root, thor_yaml[as][:filename])
93
+
94
+ if package == :file
95
+ File.open(destination, "w") { |f| f.puts contents }
96
+ else
97
+ FileUtils.cp_r(name, destination)
98
+ end
99
+
100
+ thor_yaml[as][:filename] # Indicate success
101
+ end
102
+
103
+ desc "version", "Show Thor version"
104
+ def version
105
+ require "thor/version"
106
+ say "Thor #{Thor::VERSION}"
107
+ end
108
+
109
+ desc "uninstall NAME", "Uninstall a named Thor module"
110
+ def uninstall(name)
111
+ fail Error, "Can't find module '#{name}'" unless thor_yaml[name]
112
+ say "Uninstalling #{name}."
113
+ FileUtils.rm_rf(File.join(thor_root, "#{thor_yaml[name][:filename]}"))
114
+
115
+ thor_yaml.delete(name)
116
+ save_yaml(thor_yaml)
117
+
118
+ puts "Done."
119
+ end
120
+
121
+ desc "update NAME", "Update a Thor file from its original location"
122
+ def update(name)
123
+ fail Error, "Can't find module '#{name}'" if !thor_yaml[name] || !thor_yaml[name][:location]
124
+
125
+ say "Updating '#{name}' from #{thor_yaml[name][:location]}"
126
+
127
+ old_filename = thor_yaml[name][:filename]
128
+ self.options = options.merge("as" => name)
129
+
130
+ if File.directory? File.expand_path(name)
131
+ FileUtils.rm_rf(File.join(thor_root, old_filename))
132
+
133
+ thor_yaml.delete(old_filename)
134
+ save_yaml(thor_yaml)
135
+
136
+ filename = install(name)
137
+ else
138
+ filename = install(thor_yaml[name][:location])
139
+ end
140
+
141
+ unless filename == old_filename
142
+ File.delete(File.join(thor_root, old_filename))
143
+ end
144
+ end
145
+
146
+ desc "installed", "List the installed Thor modules and commands"
147
+ method_options :internal => :boolean
148
+ def installed
149
+ initialize_thorfiles(nil, true)
150
+ display_klasses(true, options["internal"])
151
+ end
152
+
153
+ desc "list [SEARCH]", "List the available thor commands (--substring means .*SEARCH)"
154
+ method_options :substring => :boolean, :group => :string, :all => :boolean, :debug => :boolean
155
+ def list(search = "")
156
+ initialize_thorfiles
157
+
158
+ search = ".*#{search}" if options["substring"]
159
+ search = /^#{search}.*/i
160
+ group = options[:group] || "standard"
161
+
162
+ klasses = Thor::Base.subclasses.select do |k|
163
+ (options[:all] || k.group == group) && k.namespace =~ search
164
+ end
165
+
166
+ display_klasses(false, false, klasses)
167
+ end
168
+
169
+ private
170
+
171
+ def self.banner(command, all = false, subcommand = false)
172
+ "thor " + command.formatted_usage(self, all, subcommand)
173
+ end
174
+
175
+ def thor_root
176
+ Thor::Util.thor_root
177
+ end
178
+
179
+ def thor_yaml
180
+ @thor_yaml ||= begin
181
+ yaml_file = File.join(thor_root, "thor.yml")
182
+ yaml = YAML.load_file(yaml_file) if File.exist?(yaml_file)
183
+ yaml || {}
184
+ end
185
+ end
186
+
187
+ # Save the yaml file. If none exists in thor root, creates one.
188
+ #
189
+ def save_yaml(yaml)
190
+ yaml_file = File.join(thor_root, "thor.yml")
191
+
192
+ unless File.exist?(yaml_file)
193
+ FileUtils.mkdir_p(thor_root)
194
+ yaml_file = File.join(thor_root, "thor.yml")
195
+ FileUtils.touch(yaml_file)
196
+ end
197
+
198
+ File.open(yaml_file, "w") { |f| f.puts yaml.to_yaml }
199
+ end
200
+
201
+ def self.exit_on_failure?
202
+ true
203
+ end
204
+
205
+ # Load the Thorfiles. If relevant_to is supplied, looks for specific files
206
+ # in the thor_root instead of loading them all.
207
+ #
208
+ # By default, it also traverses the current path until find Thor files, as
209
+ # described in thorfiles. This look up can be skipped by supplying
210
+ # skip_lookup true.
211
+ #
212
+ def initialize_thorfiles(relevant_to = nil, skip_lookup = false)
213
+ thorfiles(relevant_to, skip_lookup).each do |f|
214
+ Thor::Util.load_thorfile(f, nil, options[:debug]) unless Thor::Base.subclass_files.keys.include?(File.expand_path(f))
215
+ end
216
+ end
217
+
218
+ # Finds Thorfiles by traversing from your current directory down to the root
219
+ # directory of your system. If at any time we find a Thor file, we stop.
220
+ #
221
+ # We also ensure that system-wide Thorfiles are loaded first, so local
222
+ # Thorfiles can override them.
223
+ #
224
+ # ==== Example
225
+ #
226
+ # If we start at /Users/wycats/dev/thor ...
227
+ #
228
+ # 1. /Users/wycats/dev/thor
229
+ # 2. /Users/wycats/dev
230
+ # 3. /Users/wycats <-- we find a Thorfile here, so we stop
231
+ #
232
+ # Suppose we start at c:\Documents and Settings\james\dev\thor ...
233
+ #
234
+ # 1. c:\Documents and Settings\james\dev\thor
235
+ # 2. c:\Documents and Settings\james\dev
236
+ # 3. c:\Documents and Settings\james
237
+ # 4. c:\Documents and Settings
238
+ # 5. c:\ <-- no Thorfiles found!
239
+ #
240
+ def thorfiles(relevant_to = nil, skip_lookup = false)
241
+ thorfiles = []
242
+
243
+ unless skip_lookup
244
+ Pathname.pwd.ascend do |path|
245
+ thorfiles = Thor::Util.globs_for(path).map { |g| Dir[g] }.flatten
246
+ break unless thorfiles.empty?
247
+ end
248
+ end
249
+
250
+ files = (relevant_to ? thorfiles_relevant_to(relevant_to) : Thor::Util.thor_root_glob)
251
+ files += thorfiles
252
+ files -= ["#{thor_root}/thor.yml"]
253
+
254
+ files.map! do |file|
255
+ File.directory?(file) ? File.join(file, "main.thor") : file
256
+ end
257
+ end
258
+
259
+ # Load Thorfiles relevant to the given method. If you provide "foo:bar" it
260
+ # will load all thor files in the thor.yaml that has "foo" e "foo:bar"
261
+ # namespaces registered.
262
+ #
263
+ def thorfiles_relevant_to(meth)
264
+ lookup = [meth, meth.split(":")[0...-1].join(":")]
265
+
266
+ files = thor_yaml.select do |k, v|
267
+ v[:namespaces] && !(v[:namespaces] & lookup).empty?
268
+ end
269
+
270
+ files.map { |k, v| File.join(thor_root, "#{v[:filename]}") }
271
+ end
272
+
273
+ # Display information about the given klasses. If with_module is given,
274
+ # it shows a table with information extracted from the yaml file.
275
+ #
276
+ def display_klasses(with_modules = false, show_internal = false, klasses = Thor::Base.subclasses)
277
+ klasses -= [Thor, Thor::Runner, Thor::Group] unless show_internal
278
+
279
+ fail Error, "No Thor commands available" if klasses.empty?
280
+ show_modules if with_modules && !thor_yaml.empty?
281
+
282
+ list = Hash.new { |h, k| h[k] = [] }
283
+ groups = klasses.select { |k| k.ancestors.include?(Thor::Group) }
284
+
285
+ # Get classes which inherit from Thor
286
+ (klasses - groups).each { |k| list[k.namespace.split(":").first] += k.printable_commands(false) }
287
+
288
+ # Get classes which inherit from Thor::Base
289
+ groups.map! { |k| k.printable_commands(false).first }
290
+ list["root"] = groups
291
+
292
+ # Order namespaces with default coming first
293
+ list = list.sort { |a, b| a[0].sub(/^default/, "") <=> b[0].sub(/^default/, "") }
294
+ list.each { |n, commands| display_commands(n, commands) unless commands.empty? }
295
+ end
296
+
297
+ def display_commands(namespace, list) #:nodoc:
298
+ list.sort! { |a, b| a[0] <=> b[0] }
299
+
300
+ say shell.set_color(namespace, :blue, true)
301
+ say "-" * namespace.size
302
+
303
+ print_table(list, :truncate => true)
304
+ say
305
+ end
306
+ alias_method :display_tasks, :display_commands
307
+
308
+ def show_modules #:nodoc:
309
+ info = []
310
+ labels = %w[Modules Namespaces]
311
+
312
+ info << labels
313
+ info << ["-" * labels[0].size, "-" * labels[1].size]
314
+
315
+ thor_yaml.each do |name, hash|
316
+ info << [name, hash[:namespaces].join(", ")]
317
+ end
318
+
319
+ print_table info
320
+ say ""
321
+ end
322
+ end