ittayd-buildr 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. data/CHANGELOG +915 -0
  2. data/DISCLAIMER +7 -0
  3. data/LICENSE +176 -0
  4. data/NOTICE +26 -0
  5. data/README.rdoc +146 -0
  6. data/Rakefile +64 -0
  7. data/_buildr +28 -0
  8. data/addon/buildr/antlr.rb +65 -0
  9. data/addon/buildr/cobertura.rb +22 -0
  10. data/addon/buildr/drb.rb +281 -0
  11. data/addon/buildr/emma.rb +22 -0
  12. data/addon/buildr/hibernate.rb +142 -0
  13. data/addon/buildr/javacc.rb +85 -0
  14. data/addon/buildr/jdepend.rb +60 -0
  15. data/addon/buildr/jetty.rb +248 -0
  16. data/addon/buildr/jibx.rb +86 -0
  17. data/addon/buildr/nailgun.rb +221 -0
  18. data/addon/buildr/openjpa.rb +90 -0
  19. data/addon/buildr/org/apache/buildr/BuildrNail$Main.class +0 -0
  20. data/addon/buildr/org/apache/buildr/BuildrNail.class +0 -0
  21. data/addon/buildr/org/apache/buildr/BuildrNail.java +41 -0
  22. data/addon/buildr/org/apache/buildr/JettyWrapper$1.class +0 -0
  23. data/addon/buildr/org/apache/buildr/JettyWrapper$BuildrHandler.class +0 -0
  24. data/addon/buildr/org/apache/buildr/JettyWrapper.class +0 -0
  25. data/addon/buildr/org/apache/buildr/JettyWrapper.java +144 -0
  26. data/addon/buildr/path.rb +136 -0
  27. data/addon/buildr/xmlbeans.rb +93 -0
  28. data/bin/buildr +19 -0
  29. data/buildr.buildfile +53 -0
  30. data/buildr.gemspec +58 -0
  31. data/doc/css/default.css +228 -0
  32. data/doc/css/print.css +100 -0
  33. data/doc/css/syntax.css +52 -0
  34. data/doc/images/apache-incubator-logo.png +0 -0
  35. data/doc/images/buildr-hires.png +0 -0
  36. data/doc/images/buildr.png +0 -0
  37. data/doc/images/favicon.png +0 -0
  38. data/doc/images/growl-icon.tiff +0 -0
  39. data/doc/images/note.png +0 -0
  40. data/doc/images/project-structure.png +0 -0
  41. data/doc/images/tip.png +0 -0
  42. data/doc/images/zbuildr.tif +0 -0
  43. data/doc/pages/artifacts.textile +207 -0
  44. data/doc/pages/building.textile +240 -0
  45. data/doc/pages/contributing.textile +208 -0
  46. data/doc/pages/download.textile +62 -0
  47. data/doc/pages/extending.textile +175 -0
  48. data/doc/pages/getting_started.textile +273 -0
  49. data/doc/pages/index.textile +42 -0
  50. data/doc/pages/languages.textile +407 -0
  51. data/doc/pages/mailing_lists.textile +17 -0
  52. data/doc/pages/more_stuff.textile +314 -0
  53. data/doc/pages/packaging.textile +427 -0
  54. data/doc/pages/projects.textile +274 -0
  55. data/doc/pages/recipes.textile +103 -0
  56. data/doc/pages/settings_profiles.textile +274 -0
  57. data/doc/pages/testing.textile +212 -0
  58. data/doc/pages/troubleshooting.textile +103 -0
  59. data/doc/pages/whats_new.textile +323 -0
  60. data/doc/print.haml +51 -0
  61. data/doc/print.toc.yaml +29 -0
  62. data/doc/scripts/buildr-git.rb +412 -0
  63. data/doc/scripts/install-jruby.sh +44 -0
  64. data/doc/scripts/install-linux.sh +64 -0
  65. data/doc/scripts/install-osx.sh +52 -0
  66. data/doc/site.haml +56 -0
  67. data/doc/site.toc.yaml +47 -0
  68. data/etc/KEYS +151 -0
  69. data/etc/git-svn-authors +16 -0
  70. data/lib/buildr/core/application.rb +665 -0
  71. data/lib/buildr/core/build.rb +311 -0
  72. data/lib/buildr/core/checks.rb +254 -0
  73. data/lib/buildr/core/common.rb +150 -0
  74. data/lib/buildr/core/compile.rb +590 -0
  75. data/lib/buildr/core/environment.rb +117 -0
  76. data/lib/buildr/core/filter.rb +366 -0
  77. data/lib/buildr/core/generate.rb +195 -0
  78. data/lib/buildr/core/help.rb +119 -0
  79. data/lib/buildr/core/osx.rb +49 -0
  80. data/lib/buildr/core/progressbar.rb +156 -0
  81. data/lib/buildr/core/project.rb +923 -0
  82. data/lib/buildr/core/test.rb +715 -0
  83. data/lib/buildr/core/transports.rb +560 -0
  84. data/lib/buildr/core/util.rb +294 -0
  85. data/lib/buildr/core.rb +29 -0
  86. data/lib/buildr/groovy/bdd.rb +106 -0
  87. data/lib/buildr/groovy/compiler.rb +138 -0
  88. data/lib/buildr/groovy.rb +18 -0
  89. data/lib/buildr/ide/eclipse.rb +222 -0
  90. data/lib/buildr/ide/idea.ipr.template +300 -0
  91. data/lib/buildr/ide/idea.rb +190 -0
  92. data/lib/buildr/ide/idea7x.ipr.template +290 -0
  93. data/lib/buildr/ide/idea7x.rb +212 -0
  94. data/lib/buildr/ide.rb +19 -0
  95. data/lib/buildr/java/ant.rb +92 -0
  96. data/lib/buildr/java/bdd.rb +451 -0
  97. data/lib/buildr/java/cobertura.rb +236 -0
  98. data/lib/buildr/java/commands.rb +211 -0
  99. data/lib/buildr/java/compiler.rb +348 -0
  100. data/lib/buildr/java/deprecated.rb +141 -0
  101. data/lib/buildr/java/emma.rb +238 -0
  102. data/lib/buildr/java/jruby.rb +117 -0
  103. data/lib/buildr/java/jtestr_runner.rb.erb +116 -0
  104. data/lib/buildr/java/org/apache/buildr/JavaTestFilter.class +0 -0
  105. data/lib/buildr/java/org/apache/buildr/JavaTestFilter.java +119 -0
  106. data/lib/buildr/java/packaging.rb +717 -0
  107. data/lib/buildr/java/pom.rb +174 -0
  108. data/lib/buildr/java/rjb.rb +155 -0
  109. data/lib/buildr/java/test_result.rb +307 -0
  110. data/lib/buildr/java/tests.rb +329 -0
  111. data/lib/buildr/java/version_requirement.rb +172 -0
  112. data/lib/buildr/java.rb +23 -0
  113. data/lib/buildr/packaging/archive.rb +488 -0
  114. data/lib/buildr/packaging/artifact.rb +759 -0
  115. data/lib/buildr/packaging/artifact_namespace.rb +972 -0
  116. data/lib/buildr/packaging/artifact_search.rb +140 -0
  117. data/lib/buildr/packaging/gems.rb +102 -0
  118. data/lib/buildr/packaging/package.rb +233 -0
  119. data/lib/buildr/packaging/tar.rb +186 -0
  120. data/lib/buildr/packaging/version_requirement.rb +172 -0
  121. data/lib/buildr/packaging/zip.rb +64 -0
  122. data/lib/buildr/packaging/ziptask.rb +313 -0
  123. data/lib/buildr/packaging.rb +24 -0
  124. data/lib/buildr/resources/buildr.icns +0 -0
  125. data/lib/buildr/scala/compiler.rb +109 -0
  126. data/lib/buildr/scala/tests.rb +203 -0
  127. data/lib/buildr/scala.rb +19 -0
  128. data/lib/buildr.rb +33 -0
  129. data/rakelib/apache.rake +191 -0
  130. data/rakelib/changelog.rake +57 -0
  131. data/rakelib/doc.rake +103 -0
  132. data/rakelib/package.rake +76 -0
  133. data/rakelib/release.rake +65 -0
  134. data/rakelib/rspec.rake +83 -0
  135. data/rakelib/rubyforge.rake +56 -0
  136. data/rakelib/scm.rake +49 -0
  137. data/rakelib/setup.rake +81 -0
  138. data/rakelib/stage.rake +48 -0
  139. data/spec/addon/drb_spec.rb +328 -0
  140. data/spec/core/application_spec.rb +419 -0
  141. data/spec/core/build_spec.rb +423 -0
  142. data/spec/core/checks_spec.rb +519 -0
  143. data/spec/core/common_spec.rb +670 -0
  144. data/spec/core/compile_spec.rb +582 -0
  145. data/spec/core/generate_spec.rb +33 -0
  146. data/spec/core/project_spec.rb +776 -0
  147. data/spec/core/test_spec.rb +1098 -0
  148. data/spec/core/transport_spec.rb +500 -0
  149. data/spec/groovy/bdd_spec.rb +80 -0
  150. data/spec/groovy/compiler_spec.rb +239 -0
  151. data/spec/ide/eclipse_spec.rb +311 -0
  152. data/spec/java/ant.rb +33 -0
  153. data/spec/java/bdd_spec.rb +358 -0
  154. data/spec/java/cobertura_spec.rb +77 -0
  155. data/spec/java/compiler_spec.rb +446 -0
  156. data/spec/java/emma_spec.rb +120 -0
  157. data/spec/java/java_spec.rb +96 -0
  158. data/spec/java/packaging_spec.rb +1120 -0
  159. data/spec/java/test_coverage_spec.rb +255 -0
  160. data/spec/java/tests_spec.rb +471 -0
  161. data/spec/packaging/archive_spec.rb +503 -0
  162. data/spec/packaging/artifact_namespace_spec.rb +646 -0
  163. data/spec/packaging/artifact_spec.rb +795 -0
  164. data/spec/packaging/packaging_helper.rb +63 -0
  165. data/spec/packaging/packaging_spec.rb +589 -0
  166. data/spec/sandbox.rb +139 -0
  167. data/spec/scala/compiler_spec.rb +228 -0
  168. data/spec/scala/tests_spec.rb +215 -0
  169. data/spec/spec_helpers.rb +327 -0
  170. data/spec/version_requirement_spec.rb +123 -0
  171. metadata +377 -0
@@ -0,0 +1,560 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with this
3
+ # work for additional information regarding copyright ownership. The ASF
4
+ # licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+
17
+ require 'uri'
18
+ require 'net/http'
19
+ Net.autoload :HTTPS, 'net/https'
20
+ # PATCH: On Windows, Net::SSH 2.0.2 attempts to load the Pageant DLLs which break on JRuby.
21
+ $LOADED_FEATURES << 'net/ssh/authentication/pageant.rb' if RUBY_PLATFORM =~ /java/
22
+ gem 'net-ssh' ; Net.autoload :SSH, 'net/ssh'
23
+ gem 'net-sftp' ; Net.autoload :SFTP, 'net/sftp'
24
+ autoload :CGI, 'cgi'
25
+ module Digest
26
+ autoload :MD5, 'digest/md5'
27
+ autoload :SHA1, 'digest/sha1'
28
+ end
29
+ require 'stringio'
30
+ autoload :ProgressBar, 'buildr/core/progressbar'
31
+
32
+
33
+ # Not quite open-uri, but similar. Provides read and write methods for the resource represented by the URI.
34
+ # Currently supports reads for URI::HTTP and writes for URI::SFTP. Also provides convenience methods for
35
+ # downloads and uploads.
36
+ module URI
37
+
38
+ # Raised when trying to read/download a resource that doesn't exist.
39
+ class NotFoundError < RuntimeError
40
+ end
41
+
42
+ # How many bytes to read/write at once.
43
+ RW_CHUNK_SIZE = 2 ** 20 #:nodoc:
44
+
45
+ class << self
46
+
47
+ # :call-seq:
48
+ # read(uri, options?) => content
49
+ # read(uri, options?) { |chunk| ... }
50
+ #
51
+ # Reads from the resource behind this URI. The first form returns the content of the resource,
52
+ # the second form yields to the block with each chunk of content (usually more than one).
53
+ #
54
+ # For example:
55
+ # File.open 'image.jpg', 'w' do |file|
56
+ # URI.read('http://example.com/image.jpg') { |chunk| file.write chunk }
57
+ # end
58
+ # Shorter version:
59
+ # File.open('image.jpg', 'w') { |file| file.write URI.read('http://example.com/image.jpg') }
60
+ #
61
+ # Supported options:
62
+ # * :modified -- Only download if file modified since this timestamp. Returns nil if not modified.
63
+ # * :progress -- Show the progress bar while reading.
64
+ def read(uri, options = nil, &block)
65
+ uri = URI.parse(uri.to_s) unless URI === uri
66
+ uri.read options, &block
67
+ end
68
+
69
+ # :call-seq:
70
+ # download(uri, target, options?)
71
+ #
72
+ # Downloads the resource to the target.
73
+ #
74
+ # The target may be a file name (string or task), in which case the file is created from the resource.
75
+ # The target may also be any object that responds to +write+, e.g. File, StringIO, Pipe.
76
+ #
77
+ # Use the progress bar when running in verbose mode.
78
+ def download(uri, target, options = nil)
79
+ uri = URI.parse(uri.to_s) unless URI === uri
80
+ uri.download target, options
81
+ end
82
+
83
+ # :call-seq:
84
+ # write(uri, content, options?)
85
+ # write(uri, options?) { |bytes| .. }
86
+ #
87
+ # Writes to the resource behind the URI. The first form writes the content from a string or an object
88
+ # that responds to +read+ and optionally +size+. The second form writes the content by yielding to the
89
+ # block. Each yield should return up to the specified number of bytes, the last yield returns nil.
90
+ #
91
+ # For example:
92
+ # File.open 'killer-app.jar', 'rb' do |file|
93
+ # write('sftp://localhost/jars/killer-app.jar') { |chunk| file.read(chunk) }
94
+ # end
95
+ # Or:
96
+ # write 'sftp://localhost/jars/killer-app.jar', File.read('killer-app.jar')
97
+ #
98
+ # Supported options:
99
+ # * :progress -- Show the progress bar while reading.
100
+ def write(uri, *args, &block)
101
+ uri = URI.parse(uri.to_s) unless URI === uri
102
+ uri.write *args, &block
103
+ end
104
+
105
+ # :call-seq:
106
+ # upload(uri, source, options?)
107
+ #
108
+ # Uploads from source to the resource.
109
+ #
110
+ # The source may be a file name (string or task), in which case the file is uploaded to the resource.
111
+ # The source may also be any object that responds to +read+ (and optionally +size+), e.g. File, StringIO, Pipe.
112
+ #
113
+ # Use the progress bar when running in verbose mode.
114
+ def upload(uri, source, options = nil)
115
+ uri = URI.parse(uri.to_s) unless URI === uri
116
+ uri.upload source, options
117
+ end
118
+
119
+ end
120
+
121
+ class Generic
122
+
123
+ # :call-seq:
124
+ # read(options?) => content
125
+ # read(options?) { |chunk| ... }
126
+ #
127
+ # Reads from the resource behind this URI. The first form returns the content of the resource,
128
+ # the second form yields to the block with each chunk of content (usually more than one).
129
+ #
130
+ # For options, see URI::read.
131
+ def read(options = nil, &block)
132
+ fail 'This protocol doesn\'t support reading (yet, how about helping by implementing it?)'
133
+ end
134
+
135
+ # :call-seq:
136
+ # download(target, options?)
137
+ #
138
+ # Downloads the resource to the target.
139
+ #
140
+ # The target may be a file name (string or task), in which case the file is created from the resource.
141
+ # The target may also be any object that responds to +write+, e.g. File, StringIO, Pipe.
142
+ #
143
+ # Use the progress bar when running in verbose mode.
144
+ def download(target, options = nil)
145
+ case target
146
+ when Rake::Task
147
+ download target.name, options
148
+ when String
149
+ # If download breaks we end up with a partial file which is
150
+ # worse than not having a file at all, so download to temporary
151
+ # file and then move over.
152
+ modified = File.stat(target).mtime if File.exist?(target)
153
+ temp = nil
154
+ Tempfile.open File.basename(target) do |temp|
155
+ temp.binmode
156
+ read({:progress=>verbose}.merge(options || {}).merge(:modified=>modified)) { |chunk| temp.write chunk }
157
+ end
158
+ mkpath File.dirname(target)
159
+ FileUtils.mv temp.path, target
160
+ when File
161
+ read({:progress=>verbose}.merge(options || {}).merge(:modified=>target.mtime)) { |chunk| target.write chunk }
162
+ target.flush
163
+ else
164
+ raise ArgumentError, 'Expecting a target that is either a file name (string, task) or object that responds to write (file, pipe).' unless target.respond_to?(:write)
165
+ read({:progress=>verbose}.merge(options || {})) { |chunk| target.write chunk }
166
+ target.flush
167
+ end
168
+ end
169
+
170
+ # :call-seq:
171
+ # write(content, options?)
172
+ # write(options?) { |bytes| .. }
173
+ #
174
+ # Writes to the resource behind the URI. The first form writes the content from a string or an object
175
+ # that responds to +read+ and optionally +size+. The second form writes the content by yielding to the
176
+ # block. Each yield should return up to the specified number of bytes, the last yield returns nil.
177
+ #
178
+ # For options, see URI::write.
179
+ def write(*args, &block)
180
+ options = args.pop if Hash === args.last
181
+ options ||= {}
182
+ if String === args.first
183
+ ios = StringIO.new(args.first, 'r')
184
+ write(options.merge(:size=>args.first.size)) { |bytes| ios.read(bytes) }
185
+ elsif args.first.respond_to?(:read)
186
+ size = args.first.size rescue nil
187
+ write({:size=>size}.merge(options)) { |bytes| args.first.read(bytes) }
188
+ elsif args.empty? && block
189
+ write_internal options, &block
190
+ else
191
+ raise ArgumentError, 'Either give me the content, or pass me a block, otherwise what would I upload?'
192
+ end
193
+ end
194
+
195
+ # :call-seq:
196
+ # upload(source, options?)
197
+ #
198
+ # Uploads from source to the resource.
199
+ #
200
+ # The source may be a file name (string or task), in which case the file is uploaded to the resource.
201
+ # If the source is a directory, uploads all files inside the directory (including nested directories).
202
+ # The source may also be any object that responds to +read+ (and optionally +size+), e.g. File, StringIO, Pipe.
203
+ #
204
+ # Use the progress bar when running in verbose mode.
205
+ def upload(source, options = nil)
206
+ source = source.name if Rake::Task === source
207
+ options ||= {}
208
+ if String === source
209
+ raise NotFoundError, 'No source file/directory to upload.' unless File.exist?(source)
210
+ if File.directory?(source)
211
+ Dir.glob("#{source}/**/*").reject { |file| File.directory?(file) }.each do |file|
212
+ uri = self + (File.join(self.path, file.sub(source, '')))
213
+ uri.upload file, {:digests=>[]}.merge(options)
214
+ end
215
+ else
216
+ File.open(source, 'rb') { |input| upload input, options }
217
+ end
218
+ elsif source.respond_to?(:read)
219
+ digests = (options[:digests] || [:md5, :sha1]).
220
+ inject({}) { |hash, name| hash[name] = Digest.const_get(name.to_s.upcase).new ; hash }
221
+ size = source.size rescue nil
222
+ write (options).merge(:progress=>verbose && size, :size=>size) do |bytes|
223
+ source.read(bytes).tap do |chunk|
224
+ digests.values.each { |digest| digest << chunk } if chunk
225
+ end
226
+ end
227
+ digests.each do |key, digest|
228
+ self.merge("#{self.path}.#{key}").write "#{digest.hexdigest} #{File.basename(path)}",
229
+ (options).merge(:progress=>false)
230
+ end
231
+ else
232
+ raise ArgumentError, 'Expecting source to be a file name (string, task) or any object that responds to read (file, pipe).'
233
+ end
234
+ end
235
+
236
+ protected
237
+
238
+ # :call-seq:
239
+ # with_progress_bar(show, file_name, size) { |progress| ... }
240
+ #
241
+ # Displays a progress bar while executing the block. The first argument must be true for the
242
+ # progress bar to show (TTY output also required), as a convenient for selectively using the
243
+ # progress bar from a single block.
244
+ #
245
+ # The second argument provides a filename to display, the third its size in bytes.
246
+ #
247
+ # The block is yielded with a progress object that implements a single method.
248
+ # Call << for each block of bytes down/uploaded.
249
+ def with_progress_bar(show, file_name, size, &block) #:nodoc:
250
+ options = { :total=>size || 0, :title=>file_name }
251
+ options[:hidden] = true unless show
252
+ ProgressBar.start options, &block
253
+ end
254
+
255
+ # :call-seq:
256
+ # proxy_uri => URI?
257
+ #
258
+ # Returns the proxy server to use. Obtains the proxy from the relevant environment variable (e.g. HTTP_PROXY).
259
+ # Supports exclusions based on host name and port number from environment variable NO_PROXY.
260
+ def proxy_uri
261
+ proxy = ENV["#{scheme.upcase}_PROXY"]
262
+ proxy = URI.parse(proxy) if String === proxy
263
+ excludes = ENV['NO_PROXY'].to_s.split(/\s*,\s*/).compact
264
+ excludes = excludes.map { |exclude| exclude =~ /:\d+$/ ? exclude : "#{exclude}:*" }
265
+ return proxy unless excludes.any? { |exclude| File.fnmatch(exclude, "#{host}:#{port}") }
266
+ end
267
+
268
+ def write_internal(options, &block) #:nodoc:
269
+ fail 'This protocol doesn\'t support writing (yet, how about helping by implementing it?)'
270
+ end
271
+
272
+ end
273
+
274
+
275
+ class HTTP #:nodoc:
276
+
277
+ # See URI::Generic#read
278
+ def read(options = nil, &block)
279
+ options ||= {}
280
+ connect do |http|
281
+ trace "Requesting #{self}"
282
+ headers = { 'If-Modified-Since' => CGI.rfc1123_date(options[:modified].utc) } if options[:modified]
283
+ request = Net::HTTP::Get.new(request_uri.empty? ? '/' : request_uri, headers)
284
+ request.basic_auth self.user, self.password if self.user
285
+ http.request request do |response|
286
+ case response
287
+ when Net::HTTPNotModified
288
+ # No modification, nothing to do.
289
+ trace 'Not modified since last download'
290
+ return nil
291
+ when Net::HTTPRedirection
292
+ # Try to download from the new URI, handle relative redirects.
293
+ trace "Redirected to #{response['Location']}"
294
+ return (self + URI.parse(response['location'])).read(options, &block)
295
+ when Net::HTTPOK
296
+ info "Downloading #{self}"
297
+ result = nil
298
+ with_progress_bar options[:progress], path.split('/').last, response.content_length do |progress|
299
+ if block
300
+ response.read_body do |chunk|
301
+ block.call chunk
302
+ progress << chunk
303
+ end
304
+ else
305
+ result = ''
306
+ response.read_body do |chunk|
307
+ result << chunk
308
+ progress << chunk
309
+ end
310
+ end
311
+ end
312
+ return result
313
+ when Net::HTTPNotFound
314
+ raise NotFoundError, "Looking for #{self} and all I got was a 404!"
315
+ else
316
+ raise RuntimeError, "Failed to download #{self}: #{response.message}"
317
+ end
318
+ end
319
+ end
320
+ end
321
+
322
+ private
323
+
324
+ def write_internal(options, &block) #:nodoc:
325
+ options ||= {}
326
+ connect do |http|
327
+ trace "Uploading to #{path}"
328
+ content = StringIO.new
329
+ while chunk = yield(RW_CHUNK_SIZE)
330
+ content << chunk
331
+ end
332
+ headers = { 'Content-MD5'=>Digest::MD5.hexdigest(content.string) }
333
+ request = Net::HTTP::Put.new(request_uri.empty? ? '/' : request_uri, headers)
334
+ request.basic_auth self.user, self.password if self.user
335
+ response = nil
336
+ with_progress_bar options[:progress], path.split('/').last, content.size do |progress|
337
+ request.content_length = content.size
338
+ content.rewind
339
+ stream = Object.new
340
+ class << stream ; self ;end.send :define_method, :read do |count|
341
+ bytes = content.read(count)
342
+ progress << bytes if bytes
343
+ bytes
344
+ end
345
+ request.body_stream = stream
346
+ response = http.request(request)
347
+ end
348
+
349
+ case response
350
+ when Net::HTTPRedirection
351
+ # Try to download from the new URI, handle relative redirects.
352
+ trace "Redirected to #{response['Location']}"
353
+ content.rewind
354
+ return (self + URI.parse(response['location'])).write_internal(options) { |bytes| content.read(bytes) }
355
+ when Net::HTTPSuccess
356
+ else
357
+ raise RuntimeError, "Failed to upload #{self}: #{response.message}"
358
+ end
359
+ end
360
+ end
361
+
362
+ def connect
363
+ if proxy = proxy_uri
364
+ proxy = URI.parse(proxy) if String === proxy
365
+ http = Net::HTTP.new(host, port, proxy.host, proxy.port, proxy.user, proxy.password)
366
+ else
367
+ http = Net::HTTP.new(host, port)
368
+ end
369
+ http.use_ssl = true if self.instance_of? URI::HTTPS
370
+ yield http
371
+ end
372
+
373
+ end
374
+
375
+
376
+ class SFTP < Generic #:nodoc:
377
+
378
+ DEFAULT_PORT = 22
379
+ COMPONENT = [ :scheme, :userinfo, :host, :port, :path ].freeze
380
+
381
+ class << self
382
+ # Caching of passwords, so we only need to ask once.
383
+ def passwords
384
+ @passwords ||= {}
385
+ end
386
+ end
387
+
388
+ def initialize(*arg)
389
+ super
390
+ end
391
+
392
+ def read(options = {}, &block)
393
+ # SSH options are based on the username/password from the URI.
394
+ ssh_options = { :port=>port, :password=>password }.merge(options[:ssh_options] || {})
395
+ ssh_options[:password] ||= SFTP.passwords[host]
396
+ begin
397
+ trace "Connecting to #{host}"
398
+ result = nil
399
+ Net::SFTP.start(host, user, ssh_options) do |sftp|
400
+ SFTP.passwords[host] = ssh_options[:password]
401
+ trace 'connected'
402
+
403
+ with_progress_bar options[:progress] && options[:size], path.split('/'), options[:size] || 0 do |progress|
404
+ trace "Downloading to #{path}"
405
+ sftp.file.open(path, 'r') do |file|
406
+ if block
407
+ while chunk = file.read(RW_CHUNK_SIZE)
408
+ block.call chunk
409
+ progress << chunk
410
+ end
411
+ else
412
+ result = ''
413
+ while chunk = file.read(RW_CHUNK_SIZE)
414
+ result << chunk
415
+ progress << chunk
416
+ end
417
+ end
418
+ end
419
+ end
420
+ end
421
+ return result
422
+ rescue Net::SSH::AuthenticationFailed=>ex
423
+ # Only if running with console, prompt for password.
424
+ if !ssh_options[:password] && $stdout.isatty
425
+ password = ask("Password for #{host}:") { |q| q.echo = '*' }
426
+ ssh_options[:password] = password
427
+ retry
428
+ end
429
+ raise
430
+ end
431
+ end
432
+
433
+ protected
434
+
435
+ def write_internal(options, &block) #:nodoc:
436
+ # SSH options are based on the username/password from the URI.
437
+ ssh_options = { :port=>port, :password=>password }.merge(options[:ssh_options] || {})
438
+ ssh_options[:password] ||= SFTP.passwords[host]
439
+ begin
440
+ trace "Connecting to #{host}"
441
+ Net::SFTP.start(host, user, ssh_options) do |sftp|
442
+ SFTP.passwords[host] = ssh_options[:password]
443
+ trace 'Connected'
444
+
445
+ # To create a path, we need to create all its parent. We use realpath to determine if
446
+ # the path already exists, otherwise mkdir fails.
447
+ trace "Creating path #{path}"
448
+ File.dirname(path).split('/').reject(&:empty?).inject('/') do |base, part|
449
+ combined = base + part
450
+ sftp.close(sftp.opendir!(combined)) rescue sftp.mkdir! combined, {}
451
+ "#{combined}/"
452
+ end
453
+
454
+ with_progress_bar options[:progress] && options[:size], path.split('/'), options[:size] || 0 do |progress|
455
+ trace "Uploading to #{path}"
456
+ sftp.file.open(path, 'w') do |file|
457
+ while chunk = yield(RW_CHUNK_SIZE)
458
+ file.write chunk
459
+ progress << chunk
460
+ end
461
+ sftp.setstat(path, :permissions => options[:permissions]) if options[:permissions]
462
+ end
463
+ end
464
+ end
465
+ rescue Net::SSH::AuthenticationFailed=>ex
466
+ # Only if running with console, prompt for password.
467
+ if !ssh_options[:password] && $stdout.isatty
468
+ password = ask("Password for #{host}:") { |q| q.echo = '*' }
469
+ ssh_options[:password] = password
470
+ retry
471
+ end
472
+ raise
473
+ end
474
+ end
475
+
476
+ end
477
+
478
+ @@schemes['SFTP'] = SFTP
479
+
480
+
481
+ # File URL. Keep in mind that file URLs take the form of <code>file://host/path</code>, although the host
482
+ # is not used, so typically all you will see are three backslashes. This methods accept common variants,
483
+ # like <code>file:/path</code> but always returns a valid URL.
484
+ class FILE < Generic
485
+
486
+ COMPONENT = [ :host, :path ].freeze
487
+
488
+ def initialize(*args)
489
+ super
490
+ # file:something (opaque) becomes file:///something
491
+ if path.nil?
492
+ set_path "/#{opaque}"
493
+ unless opaque.nil?
494
+ set_opaque nil
495
+ warn "#{caller[2]}: We'll accept this URL, but just so you know, it needs three slashes, as in: #{to_s}"
496
+ end
497
+ end
498
+ # Sadly, file://something really means file://something/ (something being server)
499
+ set_path '/' if path.empty?
500
+
501
+ # On windows, file://c:/something is not a valid URL, but people do it anyway, so if we see a drive-as-host,
502
+ # we'll just be nice enough to fix it. (URI actually strips the colon here)
503
+ if host =~ /^[a-zA-Z]$/
504
+ set_path "/#{host}:#{path}"
505
+ set_host nil
506
+ end
507
+ end
508
+
509
+ # See URI::Generic#read
510
+ def read(options = nil, &block)
511
+ options ||= {}
512
+ raise ArgumentError, 'Either you\'re attempting to read a file from another host (which we don\'t support), or you used two slashes by mistake, where you should have file:///<path>.' if host
513
+
514
+ path = real_path
515
+ # TODO: complain about clunky URLs
516
+ raise NotFoundError, "Looking for #{self} and can't find it." unless File.exists?(path)
517
+ raise NotFoundError, "Looking for the file #{self}, and it happens to be a directory." if File.directory?(path)
518
+ File.open path, 'rb' do |input|
519
+ with_progress_bar options[:progress], path.split('/').last, input.stat.size do |progress|
520
+ block ? block.call(input.read) : input.read
521
+ end
522
+ end
523
+ end
524
+
525
+ def to_s
526
+ "file://#{host}#{path}"
527
+ end
528
+
529
+ # The URL path always starts with a backslash. On most operating systems (Linux, Darwin, BSD) it points
530
+ # to the absolute path on the file system. But on Windows, it comes before the drive letter, creating an
531
+ # unusable path, so real_path fixes that. Ugly but necessary hack.
532
+ def real_path #:nodoc:
533
+ RUBY_PLATFORM =~ /win32/ && path =~ /^\/[a-zA-Z]:\// ? path[1..-1] : path
534
+ end
535
+
536
+ protected
537
+
538
+ def write_internal(options, &block) #:nodoc:
539
+ raise ArgumentError, 'Either you\'re attempting to write a file to another host (which we don\'t support), or you used two slashes by mistake, where you should have file:///<path>.' if host
540
+ temp = nil
541
+ Tempfile.open File.basename(path) do |temp|
542
+ temp.binmode
543
+ with_progress_bar options[:progress] && options[:size], path.split('/'), options[:size] || 0 do |progress|
544
+ while chunk = yield(RW_CHUNK_SIZE)
545
+ temp.write chunk
546
+ progress << chunk
547
+ end
548
+ end
549
+ end
550
+ real_path.tap do |path|
551
+ mkpath File.dirname(path)
552
+ FileUtils.mv temp.path, path
553
+ end
554
+ end
555
+
556
+ @@schemes['FILE'] = FILE
557
+
558
+ end
559
+
560
+ end