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,316 +1,316 @@
1
- require "erb"
2
- require "open-uri"
3
-
4
- class Thor
5
- module Actions
6
- # Copies the file from the relative source to the relative destination. If
7
- # the destination is not given it's assumed to be equal to the source.
8
- #
9
- # ==== Parameters
10
- # source<String>:: the relative path to the source root.
11
- # destination<String>:: the relative path to the destination root.
12
- # config<Hash>:: give :verbose => false to not log the status, and
13
- # :mode => :preserve, to preserve the file mode from the source.
14
-
15
- #
16
- # ==== Examples
17
- #
18
- # copy_file "README", "doc/README"
19
- #
20
- # copy_file "doc/README"
21
- #
22
- def copy_file(source, *args, &block)
23
- config = args.last.is_a?(Hash) ? args.pop : {}
24
- destination = args.first || source
25
- source = File.expand_path(find_in_source_paths(source.to_s))
26
-
27
- create_file destination, nil, config do
28
- content = File.binread(source)
29
- content = block.call(content) if block
30
- content
31
- end
32
- if config[:mode] == :preserve
33
- mode = File.stat(source).mode
34
- chmod(destination, mode, config)
35
- end
36
- end
37
-
38
- # Links the file from the relative source to the relative destination. If
39
- # the destination is not given it's assumed to be equal to the source.
40
- #
41
- # ==== Parameters
42
- # source<String>:: the relative path to the source root.
43
- # destination<String>:: the relative path to the destination root.
44
- # config<Hash>:: give :verbose => false to not log the status.
45
- #
46
- # ==== Examples
47
- #
48
- # link_file "README", "doc/README"
49
- #
50
- # link_file "doc/README"
51
- #
52
- def link_file(source, *args, &block)
53
- config = args.last.is_a?(Hash) ? args.pop : {}
54
- destination = args.first || source
55
- source = File.expand_path(find_in_source_paths(source.to_s))
56
-
57
- create_link destination, source, config
58
- end
59
-
60
- # Gets the content at the given address and places it at the given relative
61
- # destination. If a block is given instead of destination, the content of
62
- # the url is yielded and used as location.
63
- #
64
- # ==== Parameters
65
- # source<String>:: the address of the given content.
66
- # destination<String>:: the relative path to the destination root.
67
- # config<Hash>:: give :verbose => false to not log the status.
68
- #
69
- # ==== Examples
70
- #
71
- # get "http://gist.github.com/103208", "doc/README"
72
- #
73
- # get "http://gist.github.com/103208" do |content|
74
- # content.split("\n").first
75
- # end
76
- #
77
- def get(source, *args, &block)
78
- config = args.last.is_a?(Hash) ? args.pop : {}
79
- destination = args.first
80
-
81
- source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ %r{^https?\://}
82
- render = open(source) { |input| input.binmode.read }
83
-
84
- destination ||= if block_given?
85
- block.arity == 1 ? block.call(render) : block.call
86
- else
87
- File.basename(source)
88
- end
89
-
90
- create_file destination, render, config
91
- end
92
-
93
- # Gets an ERB template at the relative source, executes it and makes a copy
94
- # at the relative destination. If the destination is not given it's assumed
95
- # to be equal to the source removing .tt from the filename.
96
- #
97
- # ==== Parameters
98
- # source<String>:: the relative path to the source root.
99
- # destination<String>:: the relative path to the destination root.
100
- # config<Hash>:: give :verbose => false to not log the status.
101
- #
102
- # ==== Examples
103
- #
104
- # template "README", "doc/README"
105
- #
106
- # template "doc/README"
107
- #
108
- def template(source, *args, &block)
109
- config = args.last.is_a?(Hash) ? args.pop : {}
110
- destination = args.first || source.sub(/#{TEMPLATE_EXTNAME}$/, "")
111
-
112
- source = File.expand_path(find_in_source_paths(source.to_s))
113
- context = instance_eval("binding")
114
-
115
- create_file destination, nil, config do
116
- content = ERB.new(::File.binread(source), nil, "-", "@output_buffer").result(context)
117
- content = block.call(content) if block
118
- content
119
- end
120
- end
121
-
122
- # Changes the mode of the given file or directory.
123
- #
124
- # ==== Parameters
125
- # mode<Integer>:: the file mode
126
- # path<String>:: the name of the file to change mode
127
- # config<Hash>:: give :verbose => false to not log the status.
128
- #
129
- # ==== Example
130
- #
131
- # chmod "script/server", 0755
132
- #
133
- def chmod(path, mode, config = {})
134
- return unless behavior == :invoke
135
- path = File.expand_path(path, destination_root)
136
- say_status :chmod, relative_to_original_destination_root(path), config.fetch(:verbose, true)
137
- FileUtils.chmod_R(mode, path) unless options[:pretend]
138
- end
139
-
140
- # Prepend text to a file. Since it depends on insert_into_file, it's reversible.
141
- #
142
- # ==== Parameters
143
- # path<String>:: path of the file to be changed
144
- # data<String>:: the data to prepend to the file, can be also given as a block.
145
- # config<Hash>:: give :verbose => false to not log the status.
146
- #
147
- # ==== Example
148
- #
149
- # prepend_to_file 'config/environments/test.rb', 'config.gem "rspec"'
150
- #
151
- # prepend_to_file 'config/environments/test.rb' do
152
- # 'config.gem "rspec"'
153
- # end
154
- #
155
- def prepend_to_file(path, *args, &block)
156
- config = args.last.is_a?(Hash) ? args.pop : {}
157
- config.merge!(:after => /\A/)
158
- insert_into_file(path, *(args << config), &block)
159
- end
160
- alias_method :prepend_file, :prepend_to_file
161
-
162
- # Append text to a file. Since it depends on insert_into_file, it's reversible.
163
- #
164
- # ==== Parameters
165
- # path<String>:: path of the file to be changed
166
- # data<String>:: the data to append to the file, can be also given as a block.
167
- # config<Hash>:: give :verbose => false to not log the status.
168
- #
169
- # ==== Example
170
- #
171
- # append_to_file 'config/environments/test.rb', 'config.gem "rspec"'
172
- #
173
- # append_to_file 'config/environments/test.rb' do
174
- # 'config.gem "rspec"'
175
- # end
176
- #
177
- def append_to_file(path, *args, &block)
178
- config = args.last.is_a?(Hash) ? args.pop : {}
179
- config.merge!(:before => /\z/)
180
- insert_into_file(path, *(args << config), &block)
181
- end
182
- alias_method :append_file, :append_to_file
183
-
184
- # Injects text right after the class definition. Since it depends on
185
- # insert_into_file, it's reversible.
186
- #
187
- # ==== Parameters
188
- # path<String>:: path of the file to be changed
189
- # klass<String|Class>:: the class to be manipulated
190
- # data<String>:: the data to append to the class, can be also given as a block.
191
- # config<Hash>:: give :verbose => false to not log the status.
192
- #
193
- # ==== Examples
194
- #
195
- # inject_into_class "app/controllers/application_controller.rb", ApplicationController, " filter_parameter :password\n"
196
- #
197
- # inject_into_class "app/controllers/application_controller.rb", ApplicationController do
198
- # " filter_parameter :password\n"
199
- # end
200
- #
201
- def inject_into_class(path, klass, *args, &block)
202
- config = args.last.is_a?(Hash) ? args.pop : {}
203
- config.merge!(:after => /class #{klass}\n|class #{klass} .*\n/)
204
- insert_into_file(path, *(args << config), &block)
205
- end
206
-
207
- # Run a regular expression replacement on a file.
208
- #
209
- # ==== Parameters
210
- # path<String>:: path of the file to be changed
211
- # flag<Regexp|String>:: the regexp or string to be replaced
212
- # replacement<String>:: the replacement, can be also given as a block
213
- # config<Hash>:: give :verbose => false to not log the status.
214
- #
215
- # ==== Example
216
- #
217
- # gsub_file 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'
218
- #
219
- # gsub_file 'README', /rake/, :green do |match|
220
- # match << " no more. Use thor!"
221
- # end
222
- #
223
- def gsub_file(path, flag, *args, &block)
224
- return unless behavior == :invoke
225
- config = args.last.is_a?(Hash) ? args.pop : {}
226
-
227
- path = File.expand_path(path, destination_root)
228
- say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)
229
-
230
- unless options[:pretend]
231
- content = File.binread(path)
232
- content.gsub!(flag, *args, &block)
233
- File.open(path, "wb") { |file| file.write(content) }
234
- end
235
- end
236
-
237
- # Uncomment all lines matching a given regex. It will leave the space
238
- # which existed before the comment hash in tact but will remove any spacing
239
- # between the comment hash and the beginning of the line.
240
- #
241
- # ==== Parameters
242
- # path<String>:: path of the file to be changed
243
- # flag<Regexp|String>:: the regexp or string used to decide which lines to uncomment
244
- # config<Hash>:: give :verbose => false to not log the status.
245
- #
246
- # ==== Example
247
- #
248
- # uncomment_lines 'config/initializers/session_store.rb', /active_record/
249
- #
250
- def uncomment_lines(path, flag, *args)
251
- flag = flag.respond_to?(:source) ? flag.source : flag
252
-
253
- gsub_file(path, /^(\s*)#[[:blank:]]*(.*#{flag})/, '\1\2', *args)
254
- end
255
-
256
- # Comment all lines matching a given regex. It will leave the space
257
- # which existed before the beginning of the line in tact and will insert
258
- # a single space after the comment hash.
259
- #
260
- # ==== Parameters
261
- # path<String>:: path of the file to be changed
262
- # flag<Regexp|String>:: the regexp or string used to decide which lines to comment
263
- # config<Hash>:: give :verbose => false to not log the status.
264
- #
265
- # ==== Example
266
- #
267
- # comment_lines 'config/initializers/session_store.rb', /cookie_store/
268
- #
269
- def comment_lines(path, flag, *args)
270
- flag = flag.respond_to?(:source) ? flag.source : flag
271
-
272
- gsub_file(path, /^(\s*)([^#|\n]*#{flag})/, '\1# \2', *args)
273
- end
274
-
275
- # Removes a file at the given location.
276
- #
277
- # ==== Parameters
278
- # path<String>:: path of the file to be changed
279
- # config<Hash>:: give :verbose => false to not log the status.
280
- #
281
- # ==== Example
282
- #
283
- # remove_file 'README'
284
- # remove_file 'app/controllers/application_controller.rb'
285
- #
286
- def remove_file(path, config = {})
287
- return unless behavior == :invoke
288
- path = File.expand_path(path, destination_root)
289
-
290
- say_status :remove, relative_to_original_destination_root(path), config.fetch(:verbose, true)
291
- ::FileUtils.rm_rf(path) if !options[:pretend] && File.exist?(path)
292
- end
293
- alias_method :remove_dir, :remove_file
294
-
295
- attr_accessor :output_buffer
296
- private :output_buffer, :output_buffer=
297
-
298
- private
299
-
300
- def concat(string)
301
- @output_buffer.concat(string)
302
- end
303
-
304
- def capture(*args, &block)
305
- with_output_buffer { block.call(*args) }
306
- end
307
-
308
- def with_output_buffer(buf = "") #:nodoc:
309
- self.output_buffer, old_buffer = buf, output_buffer
310
- yield
311
- output_buffer
312
- ensure
313
- self.output_buffer = old_buffer
314
- end
315
- end
316
- end
1
+ require "erb"
2
+ require "open-uri"
3
+
4
+ class Thor
5
+ module Actions
6
+ # Copies the file from the relative source to the relative destination. If
7
+ # the destination is not given it's assumed to be equal to the source.
8
+ #
9
+ # ==== Parameters
10
+ # source<String>:: the relative path to the source root.
11
+ # destination<String>:: the relative path to the destination root.
12
+ # config<Hash>:: give :verbose => false to not log the status, and
13
+ # :mode => :preserve, to preserve the file mode from the source.
14
+
15
+ #
16
+ # ==== Examples
17
+ #
18
+ # copy_file "README", "doc/README"
19
+ #
20
+ # copy_file "doc/README"
21
+ #
22
+ def copy_file(source, *args, &block)
23
+ config = args.last.is_a?(Hash) ? args.pop : {}
24
+ destination = args.first || source
25
+ source = File.expand_path(find_in_source_paths(source.to_s))
26
+
27
+ create_file destination, nil, config do
28
+ content = File.binread(source)
29
+ content = block.call(content) if block
30
+ content
31
+ end
32
+ if config[:mode] == :preserve
33
+ mode = File.stat(source).mode
34
+ chmod(destination, mode, config)
35
+ end
36
+ end
37
+
38
+ # Links the file from the relative source to the relative destination. If
39
+ # the destination is not given it's assumed to be equal to the source.
40
+ #
41
+ # ==== Parameters
42
+ # source<String>:: the relative path to the source root.
43
+ # destination<String>:: the relative path to the destination root.
44
+ # config<Hash>:: give :verbose => false to not log the status.
45
+ #
46
+ # ==== Examples
47
+ #
48
+ # link_file "README", "doc/README"
49
+ #
50
+ # link_file "doc/README"
51
+ #
52
+ def link_file(source, *args, &block)
53
+ config = args.last.is_a?(Hash) ? args.pop : {}
54
+ destination = args.first || source
55
+ source = File.expand_path(find_in_source_paths(source.to_s))
56
+
57
+ create_link destination, source, config
58
+ end
59
+
60
+ # Gets the content at the given address and places it at the given relative
61
+ # destination. If a block is given instead of destination, the content of
62
+ # the url is yielded and used as location.
63
+ #
64
+ # ==== Parameters
65
+ # source<String>:: the address of the given content.
66
+ # destination<String>:: the relative path to the destination root.
67
+ # config<Hash>:: give :verbose => false to not log the status.
68
+ #
69
+ # ==== Examples
70
+ #
71
+ # get "http://gist.github.com/103208", "doc/README"
72
+ #
73
+ # get "http://gist.github.com/103208" do |content|
74
+ # content.split("\n").first
75
+ # end
76
+ #
77
+ def get(source, *args, &block)
78
+ config = args.last.is_a?(Hash) ? args.pop : {}
79
+ destination = args.first
80
+
81
+ source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ %r{^https?\://}
82
+ render = open(source) { |input| input.binmode.read }
83
+
84
+ destination ||= if block_given?
85
+ block.arity == 1 ? block.call(render) : block.call
86
+ else
87
+ File.basename(source)
88
+ end
89
+
90
+ create_file destination, render, config
91
+ end
92
+
93
+ # Gets an ERB template at the relative source, executes it and makes a copy
94
+ # at the relative destination. If the destination is not given it's assumed
95
+ # to be equal to the source removing .tt from the filename.
96
+ #
97
+ # ==== Parameters
98
+ # source<String>:: the relative path to the source root.
99
+ # destination<String>:: the relative path to the destination root.
100
+ # config<Hash>:: give :verbose => false to not log the status.
101
+ #
102
+ # ==== Examples
103
+ #
104
+ # template "README", "doc/README"
105
+ #
106
+ # template "doc/README"
107
+ #
108
+ def template(source, *args, &block)
109
+ config = args.last.is_a?(Hash) ? args.pop : {}
110
+ destination = args.first || source.sub(/#{TEMPLATE_EXTNAME}$/, "")
111
+
112
+ source = File.expand_path(find_in_source_paths(source.to_s))
113
+ context = instance_eval("binding")
114
+
115
+ create_file destination, nil, config do
116
+ content = ERB.new(::File.binread(source), nil, "-", "@output_buffer").result(context)
117
+ content = block.call(content) if block
118
+ content
119
+ end
120
+ end
121
+
122
+ # Changes the mode of the given file or directory.
123
+ #
124
+ # ==== Parameters
125
+ # mode<Integer>:: the file mode
126
+ # path<String>:: the name of the file to change mode
127
+ # config<Hash>:: give :verbose => false to not log the status.
128
+ #
129
+ # ==== Example
130
+ #
131
+ # chmod "script/server", 0755
132
+ #
133
+ def chmod(path, mode, config = {})
134
+ return unless behavior == :invoke
135
+ path = File.expand_path(path, destination_root)
136
+ say_status :chmod, relative_to_original_destination_root(path), config.fetch(:verbose, true)
137
+ FileUtils.chmod_R(mode, path) unless options[:pretend]
138
+ end
139
+
140
+ # Prepend text to a file. Since it depends on insert_into_file, it's reversible.
141
+ #
142
+ # ==== Parameters
143
+ # path<String>:: path of the file to be changed
144
+ # data<String>:: the data to prepend to the file, can be also given as a block.
145
+ # config<Hash>:: give :verbose => false to not log the status.
146
+ #
147
+ # ==== Example
148
+ #
149
+ # prepend_to_file 'config/environments/test.rb', 'config.gem "rspec"'
150
+ #
151
+ # prepend_to_file 'config/environments/test.rb' do
152
+ # 'config.gem "rspec"'
153
+ # end
154
+ #
155
+ def prepend_to_file(path, *args, &block)
156
+ config = args.last.is_a?(Hash) ? args.pop : {}
157
+ config.merge!(:after => /\A/)
158
+ insert_into_file(path, *(args << config), &block)
159
+ end
160
+ alias_method :prepend_file, :prepend_to_file
161
+
162
+ # Append text to a file. Since it depends on insert_into_file, it's reversible.
163
+ #
164
+ # ==== Parameters
165
+ # path<String>:: path of the file to be changed
166
+ # data<String>:: the data to append to the file, can be also given as a block.
167
+ # config<Hash>:: give :verbose => false to not log the status.
168
+ #
169
+ # ==== Example
170
+ #
171
+ # append_to_file 'config/environments/test.rb', 'config.gem "rspec"'
172
+ #
173
+ # append_to_file 'config/environments/test.rb' do
174
+ # 'config.gem "rspec"'
175
+ # end
176
+ #
177
+ def append_to_file(path, *args, &block)
178
+ config = args.last.is_a?(Hash) ? args.pop : {}
179
+ config.merge!(:before => /\z/)
180
+ insert_into_file(path, *(args << config), &block)
181
+ end
182
+ alias_method :append_file, :append_to_file
183
+
184
+ # Injects text right after the class definition. Since it depends on
185
+ # insert_into_file, it's reversible.
186
+ #
187
+ # ==== Parameters
188
+ # path<String>:: path of the file to be changed
189
+ # klass<String|Class>:: the class to be manipulated
190
+ # data<String>:: the data to append to the class, can be also given as a block.
191
+ # config<Hash>:: give :verbose => false to not log the status.
192
+ #
193
+ # ==== Examples
194
+ #
195
+ # inject_into_class "app/controllers/application_controller.rb", ApplicationController, " filter_parameter :password\n"
196
+ #
197
+ # inject_into_class "app/controllers/application_controller.rb", ApplicationController do
198
+ # " filter_parameter :password\n"
199
+ # end
200
+ #
201
+ def inject_into_class(path, klass, *args, &block)
202
+ config = args.last.is_a?(Hash) ? args.pop : {}
203
+ config.merge!(:after => /class #{klass}\n|class #{klass} .*\n/)
204
+ insert_into_file(path, *(args << config), &block)
205
+ end
206
+
207
+ # Run a regular expression replacement on a file.
208
+ #
209
+ # ==== Parameters
210
+ # path<String>:: path of the file to be changed
211
+ # flag<Regexp|String>:: the regexp or string to be replaced
212
+ # replacement<String>:: the replacement, can be also given as a block
213
+ # config<Hash>:: give :verbose => false to not log the status.
214
+ #
215
+ # ==== Example
216
+ #
217
+ # gsub_file 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'
218
+ #
219
+ # gsub_file 'README', /rake/, :green do |match|
220
+ # match << " no more. Use thor!"
221
+ # end
222
+ #
223
+ def gsub_file(path, flag, *args, &block)
224
+ return unless behavior == :invoke
225
+ config = args.last.is_a?(Hash) ? args.pop : {}
226
+
227
+ path = File.expand_path(path, destination_root)
228
+ say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)
229
+
230
+ unless options[:pretend]
231
+ content = File.binread(path)
232
+ content.gsub!(flag, *args, &block)
233
+ File.open(path, "wb") { |file| file.write(content) }
234
+ end
235
+ end
236
+
237
+ # Uncomment all lines matching a given regex. It will leave the space
238
+ # which existed before the comment hash in tact but will remove any spacing
239
+ # between the comment hash and the beginning of the line.
240
+ #
241
+ # ==== Parameters
242
+ # path<String>:: path of the file to be changed
243
+ # flag<Regexp|String>:: the regexp or string used to decide which lines to uncomment
244
+ # config<Hash>:: give :verbose => false to not log the status.
245
+ #
246
+ # ==== Example
247
+ #
248
+ # uncomment_lines 'config/initializers/session_store.rb', /active_record/
249
+ #
250
+ def uncomment_lines(path, flag, *args)
251
+ flag = flag.respond_to?(:source) ? flag.source : flag
252
+
253
+ gsub_file(path, /^(\s*)#[[:blank:]]*(.*#{flag})/, '\1\2', *args)
254
+ end
255
+
256
+ # Comment all lines matching a given regex. It will leave the space
257
+ # which existed before the beginning of the line in tact and will insert
258
+ # a single space after the comment hash.
259
+ #
260
+ # ==== Parameters
261
+ # path<String>:: path of the file to be changed
262
+ # flag<Regexp|String>:: the regexp or string used to decide which lines to comment
263
+ # config<Hash>:: give :verbose => false to not log the status.
264
+ #
265
+ # ==== Example
266
+ #
267
+ # comment_lines 'config/initializers/session_store.rb', /cookie_store/
268
+ #
269
+ def comment_lines(path, flag, *args)
270
+ flag = flag.respond_to?(:source) ? flag.source : flag
271
+
272
+ gsub_file(path, /^(\s*)([^#|\n]*#{flag})/, '\1# \2', *args)
273
+ end
274
+
275
+ # Removes a file at the given location.
276
+ #
277
+ # ==== Parameters
278
+ # path<String>:: path of the file to be changed
279
+ # config<Hash>:: give :verbose => false to not log the status.
280
+ #
281
+ # ==== Example
282
+ #
283
+ # remove_file 'README'
284
+ # remove_file 'app/controllers/application_controller.rb'
285
+ #
286
+ def remove_file(path, config = {})
287
+ return unless behavior == :invoke
288
+ path = File.expand_path(path, destination_root)
289
+
290
+ say_status :remove, relative_to_original_destination_root(path), config.fetch(:verbose, true)
291
+ ::FileUtils.rm_rf(path) if !options[:pretend] && File.exist?(path)
292
+ end
293
+ alias_method :remove_dir, :remove_file
294
+
295
+ attr_accessor :output_buffer
296
+ private :output_buffer, :output_buffer=
297
+
298
+ private
299
+
300
+ def concat(string)
301
+ @output_buffer.concat(string)
302
+ end
303
+
304
+ def capture(*args, &block)
305
+ with_output_buffer { block.call(*args) }
306
+ end
307
+
308
+ def with_output_buffer(buf = "") #:nodoc:
309
+ self.output_buffer, old_buffer = buf, output_buffer
310
+ yield
311
+ output_buffer
312
+ ensure
313
+ self.output_buffer = old_buffer
314
+ end
315
+ end
316
+ end