honeybadger 1.16.7 → 2.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +19 -0
  3. data/README.md +37 -16
  4. data/bin/honeybadger +5 -0
  5. data/lib/honeybadger.rb +167 -191
  6. data/lib/honeybadger/agent.rb +136 -0
  7. data/lib/honeybadger/backend.rb +26 -0
  8. data/lib/honeybadger/backend/base.rb +66 -0
  9. data/lib/honeybadger/backend/debug.rb +12 -0
  10. data/lib/honeybadger/backend/null.rb +16 -0
  11. data/lib/honeybadger/backend/server.rb +51 -0
  12. data/lib/honeybadger/backend/test.rb +24 -0
  13. data/lib/honeybadger/backtrace.rb +29 -24
  14. data/lib/honeybadger/cli.rb +367 -0
  15. data/lib/honeybadger/config.rb +333 -0
  16. data/lib/honeybadger/config/callbacks.rb +70 -0
  17. data/lib/honeybadger/config/defaults.rb +175 -0
  18. data/lib/honeybadger/config/env.rb +40 -0
  19. data/lib/honeybadger/config/yaml.rb +43 -0
  20. data/lib/honeybadger/const.rb +28 -0
  21. data/lib/honeybadger/init/rails.rb +84 -0
  22. data/lib/honeybadger/init/sinatra.rb +27 -0
  23. data/lib/honeybadger/logging.rb +133 -0
  24. data/lib/honeybadger/notice.rb +243 -280
  25. data/lib/honeybadger/plugin.rb +110 -0
  26. data/lib/honeybadger/plugins/delayed_job.rb +22 -0
  27. data/lib/honeybadger/{integrations → plugins}/delayed_job/plugin.rb +6 -7
  28. data/lib/honeybadger/{integrations → plugins}/local_variables.rb +7 -8
  29. data/lib/honeybadger/{integrations → plugins}/net_http.rb +10 -8
  30. data/lib/honeybadger/plugins/passenger.rb +24 -0
  31. data/lib/honeybadger/plugins/rails.rb +61 -0
  32. data/lib/honeybadger/plugins/sidekiq.rb +35 -0
  33. data/lib/honeybadger/{integrations → plugins}/thor.rb +9 -8
  34. data/lib/honeybadger/{integrations → plugins}/unicorn.rb +10 -9
  35. data/lib/honeybadger/rack/error_notifier.rb +44 -27
  36. data/lib/honeybadger/rack/metrics_reporter.rb +41 -0
  37. data/lib/honeybadger/rack/request_hash.rb +50 -0
  38. data/lib/honeybadger/rack/user_feedback.rb +15 -10
  39. data/lib/honeybadger/rack/user_informer.rb +14 -3
  40. data/lib/honeybadger/trace.rb +185 -0
  41. data/lib/honeybadger/util/http.rb +79 -0
  42. data/lib/honeybadger/util/request_sanitizer.rb +35 -0
  43. data/lib/honeybadger/util/sanitizer.rb +71 -0
  44. data/lib/honeybadger/util/stats.rb +31 -0
  45. data/lib/honeybadger/version.rb +4 -0
  46. data/lib/honeybadger/worker.rb +224 -0
  47. data/lib/honeybadger/worker/batch.rb +50 -0
  48. data/lib/honeybadger/worker/metered_queue.rb +80 -0
  49. data/lib/honeybadger/worker/metrics_collection.rb +61 -0
  50. data/lib/honeybadger/worker/metrics_collector.rb +96 -0
  51. data/{lib/honeybadger/capistrano.rb → vendor/capistrano-honeybadger/lib/capistrano/honeybadger.rb} +1 -3
  52. data/vendor/capistrano-honeybadger/lib/capistrano/tasks/deploy.cap +76 -0
  53. data/vendor/capistrano-honeybadger/lib/honeybadger/capistrano.rb +2 -0
  54. data/{lib → vendor/capistrano-honeybadger/lib}/honeybadger/capistrano/legacy.rb +16 -15
  55. data/vendor/thor/lib/thor.rb +484 -0
  56. data/vendor/thor/lib/thor/actions.rb +319 -0
  57. data/vendor/thor/lib/thor/actions/create_file.rb +103 -0
  58. data/vendor/thor/lib/thor/actions/create_link.rb +59 -0
  59. data/vendor/thor/lib/thor/actions/directory.rb +118 -0
  60. data/vendor/thor/lib/thor/actions/empty_directory.rb +135 -0
  61. data/vendor/thor/lib/thor/actions/file_manipulation.rb +316 -0
  62. data/vendor/thor/lib/thor/actions/inject_into_file.rb +107 -0
  63. data/vendor/thor/lib/thor/base.rb +656 -0
  64. data/vendor/thor/lib/thor/command.rb +133 -0
  65. data/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +77 -0
  66. data/vendor/thor/lib/thor/core_ext/io_binary_read.rb +10 -0
  67. data/vendor/thor/lib/thor/core_ext/ordered_hash.rb +98 -0
  68. data/vendor/thor/lib/thor/error.rb +32 -0
  69. data/vendor/thor/lib/thor/group.rb +281 -0
  70. data/vendor/thor/lib/thor/invocation.rb +178 -0
  71. data/vendor/thor/lib/thor/line_editor.rb +17 -0
  72. data/vendor/thor/lib/thor/line_editor/basic.rb +35 -0
  73. data/vendor/thor/lib/thor/line_editor/readline.rb +88 -0
  74. data/vendor/thor/lib/thor/parser.rb +4 -0
  75. data/vendor/thor/lib/thor/parser/argument.rb +73 -0
  76. data/vendor/thor/lib/thor/parser/arguments.rb +175 -0
  77. data/vendor/thor/lib/thor/parser/option.rb +125 -0
  78. data/vendor/thor/lib/thor/parser/options.rb +218 -0
  79. data/vendor/thor/lib/thor/rake_compat.rb +71 -0
  80. data/vendor/thor/lib/thor/runner.rb +322 -0
  81. data/vendor/thor/lib/thor/shell.rb +81 -0
  82. data/vendor/thor/lib/thor/shell/basic.rb +421 -0
  83. data/vendor/thor/lib/thor/shell/color.rb +149 -0
  84. data/vendor/thor/lib/thor/shell/html.rb +126 -0
  85. data/vendor/thor/lib/thor/util.rb +267 -0
  86. data/vendor/thor/lib/thor/version.rb +3 -0
  87. metadata +97 -305
  88. data/Appraisals +0 -95
  89. data/CHANGELOG.md +0 -422
  90. data/Gemfile +0 -8
  91. data/Gemfile.lock +0 -136
  92. data/Guardfile +0 -5
  93. data/MIT-LICENSE +0 -32
  94. data/Rakefile +0 -159
  95. data/features/metal.feature +0 -20
  96. data/features/rack.feature +0 -55
  97. data/features/rails.feature +0 -343
  98. data/features/rails3.x.feature +0 -26
  99. data/features/rake.feature +0 -25
  100. data/features/sinatra.feature +0 -27
  101. data/features/standalone.feature +0 -73
  102. data/features/step_definitions/metal_steps.rb +0 -24
  103. data/features/step_definitions/rack_steps.rb +0 -18
  104. data/features/step_definitions/rails_steps.rb +0 -270
  105. data/features/step_definitions/rake_steps.rb +0 -17
  106. data/features/step_definitions/standalone_steps.rb +0 -12
  107. data/features/step_definitions/thor_steps.rb +0 -4
  108. data/features/support/env.rb +0 -22
  109. data/features/support/honeybadger_failure_shim.rb.template +0 -5
  110. data/features/support/honeybadger_shim.rb.template +0 -6
  111. data/features/support/rails.rb +0 -202
  112. data/features/support/rake/Rakefile +0 -68
  113. data/features/support/test.thor +0 -22
  114. data/features/thor.feature +0 -5
  115. data/gemfiles/binding_of_caller.gemfile +0 -13
  116. data/gemfiles/delayed_job.gemfile +0 -13
  117. data/gemfiles/rack.gemfile +0 -13
  118. data/gemfiles/rails.gemfile +0 -16
  119. data/gemfiles/rails2.3.gemfile +0 -15
  120. data/gemfiles/rails3.0.gemfile +0 -16
  121. data/gemfiles/rails3.1.gemfile +0 -16
  122. data/gemfiles/rails3.2.gemfile +0 -16
  123. data/gemfiles/rails4.0.gemfile +0 -16
  124. data/gemfiles/rails4.1.gemfile +0 -16
  125. data/gemfiles/rake.gemfile +0 -13
  126. data/gemfiles/sinatra.gemfile +0 -13
  127. data/gemfiles/standalone.gemfile +0 -12
  128. data/gemfiles/thor.gemfile +0 -13
  129. data/generators/honeybadger/honeybadger_generator.rb +0 -95
  130. data/generators/honeybadger/lib/insert_commands.rb +0 -34
  131. data/generators/honeybadger/lib/rake_commands.rb +0 -24
  132. data/generators/honeybadger/templates/capistrano_hook.rb +0 -6
  133. data/generators/honeybadger/templates/honeybadger_tasks.rake +0 -25
  134. data/generators/honeybadger/templates/initializer.rb +0 -6
  135. data/honeybadger.gemspec +0 -174
  136. data/lib/honeybadger/array.rb +0 -53
  137. data/lib/honeybadger/capistrano/tasks.rake +0 -73
  138. data/lib/honeybadger/configuration.rb +0 -397
  139. data/lib/honeybadger/dependency.rb +0 -65
  140. data/lib/honeybadger/integrations.rb +0 -9
  141. data/lib/honeybadger/integrations/delayed_job.rb +0 -20
  142. data/lib/honeybadger/integrations/passenger.rb +0 -18
  143. data/lib/honeybadger/integrations/sidekiq.rb +0 -37
  144. data/lib/honeybadger/monitor.rb +0 -17
  145. data/lib/honeybadger/monitor/railtie.rb +0 -53
  146. data/lib/honeybadger/monitor/sender.rb +0 -44
  147. data/lib/honeybadger/monitor/trace.rb +0 -187
  148. data/lib/honeybadger/monitor/worker.rb +0 -169
  149. data/lib/honeybadger/payload.rb +0 -101
  150. data/lib/honeybadger/rack.rb +0 -12
  151. data/lib/honeybadger/rails.rb +0 -45
  152. data/lib/honeybadger/rails/action_controller_catcher.rb +0 -30
  153. data/lib/honeybadger/rails/controller_methods.rb +0 -78
  154. data/lib/honeybadger/rails/middleware/exceptions_catcher.rb +0 -29
  155. data/lib/honeybadger/rails3_tasks.rb +0 -94
  156. data/lib/honeybadger/railtie.rb +0 -52
  157. data/lib/honeybadger/rake_handler.rb +0 -66
  158. data/lib/honeybadger/sender.rb +0 -185
  159. data/lib/honeybadger/shared_tasks.rb +0 -56
  160. data/lib/honeybadger/stats.rb +0 -29
  161. data/lib/honeybadger/tasks.rb +0 -95
  162. data/lib/honeybadger/user_feedback.rb +0 -8
  163. data/lib/honeybadger/user_informer.rb +0 -8
  164. data/lib/honeybadger_tasks.rb +0 -69
  165. data/lib/rails/generators/honeybadger/honeybadger_generator.rb +0 -99
  166. data/rails/init.rb +0 -1
  167. data/resources/README.md +0 -34
  168. data/script/integration_test.rb +0 -38
  169. data/spec/allocation_stats.rb +0 -32
  170. data/spec/honeybadger/backtrace_spec.rb +0 -242
  171. data/spec/honeybadger/capistrano_spec.rb +0 -36
  172. data/spec/honeybadger/configuration_spec.rb +0 -328
  173. data/spec/honeybadger/dependency_spec.rb +0 -134
  174. data/spec/honeybadger/integrations/delayed_job_spec.rb +0 -82
  175. data/spec/honeybadger/integrations/local_variables_spec.rb +0 -60
  176. data/spec/honeybadger/integrations/net_http_spec.rb +0 -29
  177. data/spec/honeybadger/integrations/passenger_spec.rb +0 -29
  178. data/spec/honeybadger/integrations/sidekiq_spec.rb +0 -60
  179. data/spec/honeybadger/integrations/thor_spec.rb +0 -32
  180. data/spec/honeybadger/integrations/unicorn_spec.rb +0 -40
  181. data/spec/honeybadger/logger_spec.rb +0 -79
  182. data/spec/honeybadger/monitor/trace_spec.rb +0 -65
  183. data/spec/honeybadger/monitor/worker_spec.rb +0 -274
  184. data/spec/honeybadger/notice_spec.rb +0 -669
  185. data/spec/honeybadger/notifier_spec.rb +0 -328
  186. data/spec/honeybadger/payload_spec.rb +0 -162
  187. data/spec/honeybadger/rack_spec.rb +0 -85
  188. data/spec/honeybadger/rails/action_controller_spec.rb +0 -328
  189. data/spec/honeybadger/rails_spec.rb +0 -37
  190. data/spec/honeybadger/sender_spec.rb +0 -317
  191. data/spec/honeybadger/stats_spec.rb +0 -57
  192. data/spec/honeybadger/user_feedback_spec.rb +0 -80
  193. data/spec/honeybadger/user_informer_spec.rb +0 -30
  194. data/spec/honeybadger_tasks_spec.rb +0 -171
  195. data/spec/spec_helper.rb +0 -24
  196. data/spec/support/array_including.rb +0 -31
  197. data/spec/support/backtraced_exception.rb +0 -9
  198. data/spec/support/collected_sender.rb +0 -12
  199. data/spec/support/defines_constants.rb +0 -18
  200. data/spec/support/helpers.rb +0 -101
@@ -0,0 +1,319 @@
1
+ require "fileutils"
2
+ require "uri"
3
+ require "thor/core_ext/io_binary_read"
4
+ require "thor/actions/create_file"
5
+ require "thor/actions/create_link"
6
+ require "thor/actions/directory"
7
+ require "thor/actions/empty_directory"
8
+ require "thor/actions/file_manipulation"
9
+ require "thor/actions/inject_into_file"
10
+
11
+ class Thor
12
+ module Actions
13
+ attr_accessor :behavior
14
+
15
+ def self.included(base) #:nodoc:
16
+ base.extend ClassMethods
17
+ end
18
+
19
+ module ClassMethods
20
+ # Hold source paths for one Thor instance. source_paths_for_search is the
21
+ # method responsible to gather source_paths from this current class,
22
+ # inherited paths and the source root.
23
+ #
24
+ def source_paths
25
+ @_source_paths ||= []
26
+ end
27
+
28
+ # Stores and return the source root for this class
29
+ def source_root(path = nil)
30
+ @_source_root = path if path
31
+ @_source_root ||= nil
32
+ end
33
+
34
+ # Returns the source paths in the following order:
35
+ #
36
+ # 1) This class source paths
37
+ # 2) Source root
38
+ # 3) Parents source paths
39
+ #
40
+ def source_paths_for_search
41
+ paths = []
42
+ paths += source_paths
43
+ paths << source_root if source_root
44
+ paths += from_superclass(:source_paths, [])
45
+ paths
46
+ end
47
+
48
+ # Add runtime options that help actions execution.
49
+ #
50
+ def add_runtime_options!
51
+ class_option :force, :type => :boolean, :aliases => "-f", :group => :runtime,
52
+ :desc => "Overwrite files that already exist"
53
+
54
+ class_option :pretend, :type => :boolean, :aliases => "-p", :group => :runtime,
55
+ :desc => "Run but do not make any changes"
56
+
57
+ class_option :quiet, :type => :boolean, :aliases => "-q", :group => :runtime,
58
+ :desc => "Suppress status output"
59
+
60
+ class_option :skip, :type => :boolean, :aliases => "-s", :group => :runtime,
61
+ :desc => "Skip files that already exist"
62
+ end
63
+ end
64
+
65
+ # Extends initializer to add more configuration options.
66
+ #
67
+ # ==== Configuration
68
+ # behavior<Symbol>:: The actions default behavior. Can be :invoke or :revoke.
69
+ # It also accepts :force, :skip and :pretend to set the behavior
70
+ # and the respective option.
71
+ #
72
+ # destination_root<String>:: The root directory needed for some actions.
73
+ #
74
+ def initialize(args = [], options = {}, config = {})
75
+ self.behavior = case config[:behavior].to_s
76
+ when "force", "skip"
77
+ _cleanup_options_and_set(options, config[:behavior])
78
+ :invoke
79
+ when "revoke"
80
+ :revoke
81
+ else
82
+ :invoke
83
+ end
84
+ super
85
+ self.destination_root = config[:destination_root]
86
+ end
87
+
88
+ # Wraps an action object and call it accordingly to the thor class behavior.
89
+ #
90
+ def action(instance) #:nodoc:
91
+ if behavior == :revoke
92
+ instance.revoke!
93
+ else
94
+ instance.invoke!
95
+ end
96
+ end
97
+
98
+ # Returns the root for this thor class (also aliased as destination root).
99
+ #
100
+ def destination_root
101
+ @destination_stack.last
102
+ end
103
+
104
+ # Sets the root for this thor class. Relatives path are added to the
105
+ # directory where the script was invoked and expanded.
106
+ #
107
+ def destination_root=(root)
108
+ @destination_stack ||= []
109
+ @destination_stack[0] = File.expand_path(root || "")
110
+ end
111
+
112
+ # Returns the given path relative to the absolute root (ie, root where
113
+ # the script started).
114
+ #
115
+ def relative_to_original_destination_root(path, remove_dot = true)
116
+ path = path.dup
117
+ if path.gsub!(@destination_stack[0], ".")
118
+ remove_dot ? (path[2..-1] || "") : path
119
+ else
120
+ path
121
+ end
122
+ end
123
+
124
+ # Holds source paths in instance so they can be manipulated.
125
+ #
126
+ def source_paths
127
+ @source_paths ||= self.class.source_paths_for_search
128
+ end
129
+
130
+ # Receives a file or directory and search for it in the source paths.
131
+ #
132
+ def find_in_source_paths(file) # rubocop:disable MethodLength
133
+ possible_files = [file, file + TEMPLATE_EXTNAME]
134
+ relative_root = relative_to_original_destination_root(destination_root, false)
135
+
136
+ source_paths.each do |source|
137
+ possible_files.each do |f|
138
+ source_file = File.expand_path(f, File.join(source, relative_root))
139
+ return source_file if File.exist?(source_file)
140
+ end
141
+ end
142
+
143
+ message = "Could not find #{file.inspect} in any of your source paths. "
144
+
145
+ unless self.class.source_root
146
+ message << "Please invoke #{self.class.name}.source_root(PATH) with the PATH containing your templates. "
147
+ end
148
+
149
+ if source_paths.empty?
150
+ message << "Currently you have no source paths."
151
+ else
152
+ message << "Your current source paths are: \n#{source_paths.join("\n")}"
153
+ end
154
+
155
+ fail Error, message
156
+ end
157
+
158
+ # Do something in the root or on a provided subfolder. If a relative path
159
+ # is given it's referenced from the current root. The full path is yielded
160
+ # to the block you provide. The path is set back to the previous path when
161
+ # the method exits.
162
+ #
163
+ # ==== Parameters
164
+ # dir<String>:: the directory to move to.
165
+ # config<Hash>:: give :verbose => true to log and use padding.
166
+ #
167
+ def inside(dir = "", config = {}, &block)
168
+ verbose = config.fetch(:verbose, false)
169
+ pretend = options[:pretend]
170
+
171
+ say_status :inside, dir, verbose
172
+ shell.padding += 1 if verbose
173
+ @destination_stack.push File.expand_path(dir, destination_root)
174
+
175
+ # If the directory doesnt exist and we're not pretending
176
+ if !File.exist?(destination_root) && !pretend
177
+ FileUtils.mkdir_p(destination_root)
178
+ end
179
+
180
+ if pretend
181
+ # In pretend mode, just yield down to the block
182
+ block.arity == 1 ? yield(destination_root) : yield
183
+ else
184
+ FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
185
+ end
186
+
187
+ @destination_stack.pop
188
+ shell.padding -= 1 if verbose
189
+ end
190
+
191
+ # Goes to the root and execute the given block.
192
+ #
193
+ def in_root
194
+ inside(@destination_stack.first) { yield }
195
+ end
196
+
197
+ # Loads an external file and execute it in the instance binding.
198
+ #
199
+ # ==== Parameters
200
+ # path<String>:: The path to the file to execute. Can be a web address or
201
+ # a relative path from the source root.
202
+ #
203
+ # ==== Examples
204
+ #
205
+ # apply "http://gist.github.com/103208"
206
+ #
207
+ # apply "recipes/jquery.rb"
208
+ #
209
+ def apply(path, config = {})
210
+ verbose = config.fetch(:verbose, true)
211
+ is_uri = path =~ %r{^https?\://}
212
+ path = find_in_source_paths(path) unless is_uri
213
+
214
+ say_status :apply, path, verbose
215
+ shell.padding += 1 if verbose
216
+
217
+ if is_uri
218
+ contents = open(path, "Accept" => "application/x-thor-template") { |io| io.read }
219
+ else
220
+ contents = open(path) { |io| io.read }
221
+ end
222
+
223
+ instance_eval(contents, path)
224
+ shell.padding -= 1 if verbose
225
+ end
226
+
227
+ # Executes a command returning the contents of the command.
228
+ #
229
+ # ==== Parameters
230
+ # command<String>:: the command to be executed.
231
+ # config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output. Specify :with
232
+ # to append an executable to command execution.
233
+ #
234
+ # ==== Example
235
+ #
236
+ # inside('vendor') do
237
+ # run('ln -s ~/edge rails')
238
+ # end
239
+ #
240
+ def run(command, config = {})
241
+ return unless behavior == :invoke
242
+
243
+ destination = relative_to_original_destination_root(destination_root, false)
244
+ desc = "#{command} from #{destination.inspect}"
245
+
246
+ if config[:with]
247
+ desc = "#{File.basename(config[:with].to_s)} #{desc}"
248
+ command = "#{config[:with]} #{command}"
249
+ end
250
+
251
+ say_status :run, desc, config.fetch(:verbose, true)
252
+
253
+ unless options[:pretend]
254
+ config[:capture] ? `#{command}` : system("#{command}")
255
+ end
256
+ end
257
+
258
+ # Executes a ruby script (taking into account WIN32 platform quirks).
259
+ #
260
+ # ==== Parameters
261
+ # command<String>:: the command to be executed.
262
+ # config<Hash>:: give :verbose => false to not log the status.
263
+ #
264
+ def run_ruby_script(command, config = {})
265
+ return unless behavior == :invoke
266
+ run command, config.merge(:with => Thor::Util.ruby_command)
267
+ end
268
+
269
+ # Run a thor command. A hash of options can be given and it's converted to
270
+ # switches.
271
+ #
272
+ # ==== Parameters
273
+ # command<String>:: the command to be invoked
274
+ # args<Array>:: arguments to the command
275
+ # config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output.
276
+ # Other options are given as parameter to Thor.
277
+ #
278
+ #
279
+ # ==== Examples
280
+ #
281
+ # thor :install, "http://gist.github.com/103208"
282
+ # #=> thor install http://gist.github.com/103208
283
+ #
284
+ # thor :list, :all => true, :substring => 'rails'
285
+ # #=> thor list --all --substring=rails
286
+ #
287
+ def thor(command, *args)
288
+ config = args.last.is_a?(Hash) ? args.pop : {}
289
+ verbose = config.key?(:verbose) ? config.delete(:verbose) : true
290
+ pretend = config.key?(:pretend) ? config.delete(:pretend) : false
291
+ capture = config.key?(:capture) ? config.delete(:capture) : false
292
+
293
+ args.unshift(command)
294
+ args.push Thor::Options.to_switches(config)
295
+ command = args.join(" ").strip
296
+
297
+ run command, :with => :thor, :verbose => verbose, :pretend => pretend, :capture => capture
298
+ end
299
+
300
+ protected
301
+
302
+ # Allow current root to be shared between invocations.
303
+ #
304
+ def _shared_configuration #:nodoc:
305
+ super.merge!(:destination_root => destination_root)
306
+ end
307
+
308
+ def _cleanup_options_and_set(options, key) #:nodoc:
309
+ case options
310
+ when Array
311
+ %w[--force -f --skip -s].each { |i| options.delete(i) }
312
+ options << "--#{key}"
313
+ when Hash
314
+ [:force, :skip, "force", "skip"].each { |i| options.delete(i) }
315
+ options.merge!(key => true)
316
+ end
317
+ end
318
+ end
319
+ end
@@ -0,0 +1,103 @@
1
+ require "thor/actions/empty_directory"
2
+
3
+ class Thor
4
+ module Actions
5
+ # Create a new file relative to the destination root with the given data,
6
+ # which is the return value of a block or a data string.
7
+ #
8
+ # ==== Parameters
9
+ # destination<String>:: the relative path to the destination root.
10
+ # data<String|NilClass>:: the data to append to the file.
11
+ # config<Hash>:: give :verbose => false to not log the status.
12
+ #
13
+ # ==== Examples
14
+ #
15
+ # create_file "lib/fun_party.rb" do
16
+ # hostname = ask("What is the virtual hostname I should use?")
17
+ # "vhost.name = #{hostname}"
18
+ # end
19
+ #
20
+ # create_file "config/apache.conf", "your apache config"
21
+ #
22
+ def create_file(destination, *args, &block)
23
+ config = args.last.is_a?(Hash) ? args.pop : {}
24
+ data = args.first
25
+ action CreateFile.new(self, destination, block || data.to_s, config)
26
+ end
27
+ alias_method :add_file, :create_file
28
+
29
+ # CreateFile is a subset of Template, which instead of rendering a file with
30
+ # ERB, it gets the content from the user.
31
+ #
32
+ class CreateFile < EmptyDirectory #:nodoc:
33
+ attr_reader :data
34
+
35
+ def initialize(base, destination, data, config = {})
36
+ @data = data
37
+ super(base, destination, config)
38
+ end
39
+
40
+ # Checks if the content of the file at the destination is identical to the rendered result.
41
+ #
42
+ # ==== Returns
43
+ # Boolean:: true if it is identical, false otherwise.
44
+ #
45
+ def identical?
46
+ exists? && File.binread(destination) == render
47
+ end
48
+
49
+ # Holds the content to be added to the file.
50
+ #
51
+ def render
52
+ @render ||= if data.is_a?(Proc)
53
+ data.call
54
+ else
55
+ data
56
+ end
57
+ end
58
+
59
+ def invoke!
60
+ invoke_with_conflict_check do
61
+ FileUtils.mkdir_p(File.dirname(destination))
62
+ File.open(destination, "wb") { |f| f.write render }
63
+ end
64
+ given_destination
65
+ end
66
+
67
+ protected
68
+
69
+ # Now on conflict we check if the file is identical or not.
70
+ #
71
+ def on_conflict_behavior(&block)
72
+ if identical?
73
+ say_status :identical, :blue
74
+ else
75
+ options = base.options.merge(config)
76
+ force_or_skip_or_conflict(options[:force], options[:skip], &block)
77
+ end
78
+ end
79
+
80
+ # If force is true, run the action, otherwise check if it's not being
81
+ # skipped. If both are false, show the file_collision menu, if the menu
82
+ # returns true, force it, otherwise skip.
83
+ #
84
+ def force_or_skip_or_conflict(force, skip, &block)
85
+ if force
86
+ say_status :force, :yellow
87
+ block.call unless pretend?
88
+ elsif skip
89
+ say_status :skip, :yellow
90
+ else
91
+ say_status :conflict, :red
92
+ force_or_skip_or_conflict(force_on_collision?, true, &block)
93
+ end
94
+ end
95
+
96
+ # Shows the file collision menu to the user and gets the result.
97
+ #
98
+ def force_on_collision?
99
+ base.shell.file_collision(destination) { render }
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,59 @@
1
+ require "thor/actions/create_file"
2
+
3
+ class Thor
4
+ module Actions
5
+ # Create a new file relative to the destination root from the given source.
6
+ #
7
+ # ==== Parameters
8
+ # destination<String>:: the relative path to the destination root.
9
+ # source<String|NilClass>:: the relative path to the source root.
10
+ # config<Hash>:: give :verbose => false to not log the status.
11
+ # :: give :symbolic => false for hard link.
12
+ #
13
+ # ==== Examples
14
+ #
15
+ # create_link "config/apache.conf", "/etc/apache.conf"
16
+ #
17
+ def create_link(destination, *args, &block)
18
+ config = args.last.is_a?(Hash) ? args.pop : {}
19
+ source = args.first
20
+ action CreateLink.new(self, destination, source, config)
21
+ end
22
+ alias_method :add_link, :create_link
23
+
24
+ # CreateLink is a subset of CreateFile, which instead of taking a block of
25
+ # data, just takes a source string from the user.
26
+ #
27
+ class CreateLink < CreateFile #:nodoc:
28
+ attr_reader :data
29
+
30
+ # Checks if the content of the file at the destination is identical to the rendered result.
31
+ #
32
+ # ==== Returns
33
+ # Boolean:: true if it is identical, false otherwise.
34
+ #
35
+ def identical?
36
+ exists? && File.identical?(render, destination)
37
+ end
38
+
39
+ def invoke!
40
+ invoke_with_conflict_check do
41
+ FileUtils.mkdir_p(File.dirname(destination))
42
+ # Create a symlink by default
43
+ config[:symbolic] = true if config[:symbolic].nil?
44
+ File.unlink(destination) if exists?
45
+ if config[:symbolic]
46
+ File.symlink(render, destination)
47
+ else
48
+ File.link(render, destination)
49
+ end
50
+ end
51
+ given_destination
52
+ end
53
+
54
+ def exists?
55
+ super || File.symlink?(destination)
56
+ end
57
+ end
58
+ end
59
+ end