carat 1.9.9.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +24 -0
  5. data/CHANGELOG.md +2006 -0
  6. data/CODE_OF_CONDUCT.md +40 -0
  7. data/CONTRIBUTING.md +23 -0
  8. data/DEVELOPMENT.md +119 -0
  9. data/ISSUES.md +96 -0
  10. data/LICENSE.md +23 -0
  11. data/README.md +32 -0
  12. data/Rakefile +308 -0
  13. data/bin/carat +21 -0
  14. data/bin/carat_ruby +56 -0
  15. data/carat.gemspec +32 -0
  16. data/lib/carat.rb +446 -0
  17. data/lib/carat/anonymizable_uri.rb +32 -0
  18. data/lib/carat/capistrano.rb +16 -0
  19. data/lib/carat/cli.rb +407 -0
  20. data/lib/carat/cli/binstubs.rb +38 -0
  21. data/lib/carat/cli/cache.rb +35 -0
  22. data/lib/carat/cli/check.rb +35 -0
  23. data/lib/carat/cli/clean.rb +26 -0
  24. data/lib/carat/cli/common.rb +56 -0
  25. data/lib/carat/cli/config.rb +84 -0
  26. data/lib/carat/cli/console.rb +38 -0
  27. data/lib/carat/cli/exec.rb +44 -0
  28. data/lib/carat/cli/gem.rb +195 -0
  29. data/lib/carat/cli/init.rb +33 -0
  30. data/lib/carat/cli/inject.rb +33 -0
  31. data/lib/carat/cli/install.rb +156 -0
  32. data/lib/carat/cli/open.rb +23 -0
  33. data/lib/carat/cli/outdated.rb +80 -0
  34. data/lib/carat/cli/package.rb +45 -0
  35. data/lib/carat/cli/platform.rb +43 -0
  36. data/lib/carat/cli/show.rb +74 -0
  37. data/lib/carat/cli/update.rb +73 -0
  38. data/lib/carat/cli/viz.rb +27 -0
  39. data/lib/carat/constants.rb +5 -0
  40. data/lib/carat/current_ruby.rb +183 -0
  41. data/lib/carat/definition.rb +628 -0
  42. data/lib/carat/dep_proxy.rb +43 -0
  43. data/lib/carat/dependency.rb +110 -0
  44. data/lib/carat/deployment.rb +59 -0
  45. data/lib/carat/deprecate.rb +15 -0
  46. data/lib/carat/dsl.rb +331 -0
  47. data/lib/carat/endpoint_specification.rb +76 -0
  48. data/lib/carat/env.rb +75 -0
  49. data/lib/carat/environment.rb +42 -0
  50. data/lib/carat/fetcher.rb +423 -0
  51. data/lib/carat/friendly_errors.rb +85 -0
  52. data/lib/carat/gem_helper.rb +180 -0
  53. data/lib/carat/gem_helpers.rb +26 -0
  54. data/lib/carat/gem_installer.rb +9 -0
  55. data/lib/carat/gem_path_manipulation.rb +8 -0
  56. data/lib/carat/gem_tasks.rb +2 -0
  57. data/lib/carat/graph.rb +169 -0
  58. data/lib/carat/index.rb +197 -0
  59. data/lib/carat/injector.rb +64 -0
  60. data/lib/carat/installer.rb +339 -0
  61. data/lib/carat/lazy_specification.rb +83 -0
  62. data/lib/carat/lockfile_parser.rb +167 -0
  63. data/lib/carat/match_platform.rb +13 -0
  64. data/lib/carat/psyched_yaml.rb +26 -0
  65. data/lib/carat/remote_specification.rb +57 -0
  66. data/lib/carat/resolver.rb +334 -0
  67. data/lib/carat/retry.rb +60 -0
  68. data/lib/carat/ruby_dsl.rb +11 -0
  69. data/lib/carat/ruby_version.rb +117 -0
  70. data/lib/carat/rubygems_ext.rb +170 -0
  71. data/lib/carat/rubygems_integration.rb +619 -0
  72. data/lib/carat/runtime.rb +289 -0
  73. data/lib/carat/settings.rb +208 -0
  74. data/lib/carat/setup.rb +24 -0
  75. data/lib/carat/shared_helpers.rb +149 -0
  76. data/lib/carat/similarity_detector.rb +63 -0
  77. data/lib/carat/source.rb +46 -0
  78. data/lib/carat/source/git.rb +294 -0
  79. data/lib/carat/source/git/git_proxy.rb +162 -0
  80. data/lib/carat/source/path.rb +226 -0
  81. data/lib/carat/source/path/installer.rb +43 -0
  82. data/lib/carat/source/rubygems.rb +381 -0
  83. data/lib/carat/source_list.rb +101 -0
  84. data/lib/carat/spec_set.rb +154 -0
  85. data/lib/carat/ssl_certs/.document +1 -0
  86. data/lib/carat/ssl_certs/AddTrustExternalCARoot-2048.pem +25 -0
  87. data/lib/carat/ssl_certs/AddTrustExternalCARoot.pem +32 -0
  88. data/lib/carat/ssl_certs/Class3PublicPrimaryCertificationAuthority.pem +14 -0
  89. data/lib/carat/ssl_certs/DigiCertHighAssuranceEVRootCA.pem +23 -0
  90. data/lib/carat/ssl_certs/EntrustnetSecureServerCertificationAuthority.pem +28 -0
  91. data/lib/carat/ssl_certs/GeoTrustGlobalCA.pem +20 -0
  92. data/lib/carat/ssl_certs/certificate_manager.rb +66 -0
  93. data/lib/carat/ssl_certs/index.rubygems.org/GlobalSignRootCA.pem +21 -0
  94. data/lib/carat/ssl_certs/rubygems.global.ssl.fastly.net/DigiCertHighAssuranceEVRootCA.pem +23 -0
  95. data/lib/carat/ssl_certs/rubygems.org/AddTrustExternalCARoot.pem +25 -0
  96. data/lib/carat/templates/Executable +16 -0
  97. data/lib/carat/templates/Executable.standalone +12 -0
  98. data/lib/carat/templates/Gemfile +4 -0
  99. data/lib/carat/templates/newgem/.travis.yml.tt +3 -0
  100. data/lib/carat/templates/newgem/CODE_OF_CONDUCT.md.tt +13 -0
  101. data/lib/carat/templates/newgem/Gemfile.tt +4 -0
  102. data/lib/carat/templates/newgem/LICENSE.txt.tt +21 -0
  103. data/lib/carat/templates/newgem/README.md.tt +39 -0
  104. data/lib/carat/templates/newgem/Rakefile.tt +25 -0
  105. data/lib/carat/templates/newgem/bin/console.tt +14 -0
  106. data/lib/carat/templates/newgem/bin/setup.tt +7 -0
  107. data/lib/carat/templates/newgem/exe/newgem.tt +3 -0
  108. data/lib/carat/templates/newgem/ext/newgem/extconf.rb.tt +3 -0
  109. data/lib/carat/templates/newgem/ext/newgem/newgem.c.tt +9 -0
  110. data/lib/carat/templates/newgem/ext/newgem/newgem.h.tt +6 -0
  111. data/lib/carat/templates/newgem/gitignore.tt +16 -0
  112. data/lib/carat/templates/newgem/lib/newgem.rb.tt +12 -0
  113. data/lib/carat/templates/newgem/lib/newgem/version.rb.tt +7 -0
  114. data/lib/carat/templates/newgem/newgem.gemspec.tt +43 -0
  115. data/lib/carat/templates/newgem/rspec.tt +2 -0
  116. data/lib/carat/templates/newgem/spec/newgem_spec.rb.tt +11 -0
  117. data/lib/carat/templates/newgem/spec/spec_helper.rb.tt +2 -0
  118. data/lib/carat/templates/newgem/test/minitest_helper.rb.tt +4 -0
  119. data/lib/carat/templates/newgem/test/test_newgem.rb.tt +11 -0
  120. data/lib/carat/ui.rb +7 -0
  121. data/lib/carat/ui/rg_proxy.rb +21 -0
  122. data/lib/carat/ui/shell.rb +103 -0
  123. data/lib/carat/ui/silent.rb +44 -0
  124. data/lib/carat/vendor/molinillo/lib/molinillo.rb +5 -0
  125. data/lib/carat/vendor/molinillo/lib/molinillo/dependency_graph.rb +266 -0
  126. data/lib/carat/vendor/molinillo/lib/molinillo/errors.rb +69 -0
  127. data/lib/carat/vendor/molinillo/lib/molinillo/gem_metadata.rb +3 -0
  128. data/lib/carat/vendor/molinillo/lib/molinillo/modules/specification_provider.rb +90 -0
  129. data/lib/carat/vendor/molinillo/lib/molinillo/modules/ui.rb +63 -0
  130. data/lib/carat/vendor/molinillo/lib/molinillo/resolution.rb +415 -0
  131. data/lib/carat/vendor/molinillo/lib/molinillo/resolver.rb +43 -0
  132. data/lib/carat/vendor/molinillo/lib/molinillo/state.rb +43 -0
  133. data/lib/carat/vendor/net/http/faster.rb +26 -0
  134. data/lib/carat/vendor/net/http/persistent.rb +1230 -0
  135. data/lib/carat/vendor/net/http/persistent/ssl_reuse.rb +128 -0
  136. data/lib/carat/vendor/thor/lib/thor.rb +484 -0
  137. data/lib/carat/vendor/thor/lib/thor/actions.rb +319 -0
  138. data/lib/carat/vendor/thor/lib/thor/actions/create_file.rb +103 -0
  139. data/lib/carat/vendor/thor/lib/thor/actions/create_link.rb +59 -0
  140. data/lib/carat/vendor/thor/lib/thor/actions/directory.rb +118 -0
  141. data/lib/carat/vendor/thor/lib/thor/actions/empty_directory.rb +135 -0
  142. data/lib/carat/vendor/thor/lib/thor/actions/file_manipulation.rb +316 -0
  143. data/lib/carat/vendor/thor/lib/thor/actions/inject_into_file.rb +107 -0
  144. data/lib/carat/vendor/thor/lib/thor/base.rb +656 -0
  145. data/lib/carat/vendor/thor/lib/thor/command.rb +133 -0
  146. data/lib/carat/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +77 -0
  147. data/lib/carat/vendor/thor/lib/thor/core_ext/io_binary_read.rb +10 -0
  148. data/lib/carat/vendor/thor/lib/thor/core_ext/ordered_hash.rb +98 -0
  149. data/lib/carat/vendor/thor/lib/thor/error.rb +32 -0
  150. data/lib/carat/vendor/thor/lib/thor/group.rb +281 -0
  151. data/lib/carat/vendor/thor/lib/thor/invocation.rb +178 -0
  152. data/lib/carat/vendor/thor/lib/thor/line_editor.rb +17 -0
  153. data/lib/carat/vendor/thor/lib/thor/line_editor/basic.rb +35 -0
  154. data/lib/carat/vendor/thor/lib/thor/line_editor/readline.rb +88 -0
  155. data/lib/carat/vendor/thor/lib/thor/parser.rb +4 -0
  156. data/lib/carat/vendor/thor/lib/thor/parser/argument.rb +73 -0
  157. data/lib/carat/vendor/thor/lib/thor/parser/arguments.rb +175 -0
  158. data/lib/carat/vendor/thor/lib/thor/parser/option.rb +125 -0
  159. data/lib/carat/vendor/thor/lib/thor/parser/options.rb +218 -0
  160. data/lib/carat/vendor/thor/lib/thor/rake_compat.rb +71 -0
  161. data/lib/carat/vendor/thor/lib/thor/runner.rb +322 -0
  162. data/lib/carat/vendor/thor/lib/thor/shell.rb +81 -0
  163. data/lib/carat/vendor/thor/lib/thor/shell/basic.rb +421 -0
  164. data/lib/carat/vendor/thor/lib/thor/shell/color.rb +149 -0
  165. data/lib/carat/vendor/thor/lib/thor/shell/html.rb +126 -0
  166. data/lib/carat/vendor/thor/lib/thor/util.rb +267 -0
  167. data/lib/carat/vendor/thor/lib/thor/version.rb +3 -0
  168. data/lib/carat/vendored_fileutils.rb +9 -0
  169. data/lib/carat/vendored_molinillo.rb +2 -0
  170. data/lib/carat/vendored_persistent.rb +11 -0
  171. data/lib/carat/vendored_thor.rb +3 -0
  172. data/lib/carat/version.rb +6 -0
  173. data/lib/carat/vlad.rb +11 -0
  174. data/lib/carat/worker.rb +73 -0
  175. data/man/carat-config.ronn +178 -0
  176. data/man/carat-exec.ronn +136 -0
  177. data/man/carat-install.ronn +383 -0
  178. data/man/carat-package.ronn +66 -0
  179. data/man/carat-platform.ronn +42 -0
  180. data/man/carat-update.ronn +188 -0
  181. data/man/carat.ronn +98 -0
  182. data/man/gemfile.5.ronn +473 -0
  183. data/man/index.txt +7 -0
  184. metadata +321 -0
@@ -0,0 +1,319 @@
1
+ require "fileutils"
2
+ require "uri"
3
+ require "carat/vendor/thor/lib/thor/core_ext/io_binary_read"
4
+ require "carat/vendor/thor/lib/thor/actions/create_file"
5
+ require "carat/vendor/thor/lib/thor/actions/create_link"
6
+ require "carat/vendor/thor/lib/thor/actions/directory"
7
+ require "carat/vendor/thor/lib/thor/actions/empty_directory"
8
+ require "carat/vendor/thor/lib/thor/actions/file_manipulation"
9
+ require "carat/vendor/thor/lib/thor/actions/inject_into_file"
10
+
11
+ class Carat::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 Carat::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 => Carat::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 Carat::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 Carat::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 "carat/vendor/thor/lib/thor/actions/empty_directory"
2
+
3
+ class Carat::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 "carat/vendor/thor/lib/thor/actions/create_file"
2
+
3
+ class Carat::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