Refactor download strategy detection

Signed-off-by: Jack Nagel <jacknagel@gmail.com>
This commit is contained in:
Jack Nagel 2012-06-25 21:39:28 -05:00
parent 93f8c71e21
commit b2ff74372e
3 changed files with 61 additions and 48 deletions

View File

@ -88,7 +88,7 @@ class FormulaCreator
end end
unless ARGV.include? "--no-fetch" and version unless ARGV.include? "--no-fetch" and version
strategy = detect_download_strategy url strategy = DownloadStrategyDetector.new(url).detect
@sha1 = strategy.new(url, name, version, nil).fetch.sha1 if strategy == CurlDownloadStrategy @sha1 = strategy.new(url, name, version, nil).fetch.sha1 if strategy == CurlDownloadStrategy
end end

View File

@ -562,8 +562,24 @@ class FossilDownloadStrategy < AbstractDownloadStrategy
end end
end end
def detect_download_strategy url class DownloadStrategyDetector
case url def initialize url, strategy=nil
@url = url
@strategy = strategy
end
def detect
case @strategy
when AbstractDownloadStrategy then @strategy
when Symbol then detect_from_symbol
else detect_from_url
end
end
private
def detect_from_url
case @url
# We use a special URL pattern for cvs # We use a special URL pattern for cvs
when %r[^cvs://] then CVSDownloadStrategy when %r[^cvs://] then CVSDownloadStrategy
# Standard URLs # Standard URLs
@ -582,7 +598,25 @@ def detect_download_strategy url
when %r[^http://www.apache.org/dyn/closer.cgi] then CurlApacheMirrorDownloadStrategy when %r[^http://www.apache.org/dyn/closer.cgi] then CurlApacheMirrorDownloadStrategy
# Common URL patterns # Common URL patterns
when %r[^https?://svn\.] then SubversionDownloadStrategy when %r[^https?://svn\.] then SubversionDownloadStrategy
when bottle_native_regex, bottle_regex, old_bottle_regex
CurlBottleDownloadStrategy
# Otherwise just try to download # Otherwise just try to download
else CurlDownloadStrategy else CurlDownloadStrategy
end end
end
def detect_from_symbol
case @strategy
when :bzr then BazaarDownloadStrategy
when :curl then CurlDownloadStrategy
when :cvs then CVSDownloadStrategy
when :git then GitDownloadStrategy
when :hg then MercurialDownloadStrategy
when :nounzip then NoUnzipCurlDownloadStrategy
when :post then CurlPostDownloadStrategy
when :svn then SubversionDownloadStrategy
else
raise "Unknown download strategy #{@strategy} was requested."
end
end
end end

View File

@ -2,35 +2,15 @@ require 'download_strategy'
require 'checksums' require 'checksums'
class SoftwareSpec class SoftwareSpec
attr_reader :checksum, :mirrors, :specs, :strategy attr_reader :checksum, :mirrors, :specs
VCS_SYMBOLS = {
:bzr => BazaarDownloadStrategy,
:curl => CurlDownloadStrategy,
:cvs => CVSDownloadStrategy,
:git => GitDownloadStrategy,
:hg => MercurialDownloadStrategy,
:nounzip => NoUnzipCurlDownloadStrategy,
:post => CurlPostDownloadStrategy,
:svn => SubversionDownloadStrategy,
}
# Was the version defined in the DSL, or detected from the URL? # Was the version defined in the DSL, or detected from the URL?
def explicit_version? def explicit_version?
@explicit_version || false @explicit_version || false
end end
# Returns a suitable DownloadStrategy class that can be
# used to retrieve this software package.
def download_strategy def download_strategy
return detect_download_strategy(@url) if @strategy.nil? @download_strategy ||= DownloadStrategyDetector.new(@url, @using).detect
# If a class is passed, assume it is a download strategy
return @strategy if @strategy.kind_of? Class
detected = VCS_SYMBOLS[@strategy]
raise "Unknown strategy #{@strategy} was requested." unless detected
return detected
end end
def verify_download_integrity fn def verify_download_integrity fn
@ -64,9 +44,9 @@ class SoftwareSpec
return @url if val.nil? return @url if val.nil?
@url = val @url = val
if specs.nil? if specs.nil?
@strategy = nil @using = nil
else else
@strategy = specs.delete :using @using = specs.delete :using
@specs = specs @specs = specs
end end
end end
@ -104,7 +84,6 @@ class Bottle < SoftwareSpec
def initialize def initialize
super super
@revision = 0 @revision = 0
@strategy = CurlBottleDownloadStrategy
end end
# Checksum methods in the DSL's bottle block optionally take # Checksum methods in the DSL's bottle block optionally take