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,27 +562,61 @@ class FossilDownloadStrategy < AbstractDownloadStrategy
end end
end end
def detect_download_strategy url class DownloadStrategyDetector
case url def initialize url, strategy=nil
# We use a special URL pattern for cvs @url = url
when %r[^cvs://] then CVSDownloadStrategy @strategy = strategy
# Standard URLs end
when %r[^bzr://] then BazaarDownloadStrategy
when %r[^git://] then GitDownloadStrategy def detect
when %r[^https?://.+\.git$] then GitDownloadStrategy case @strategy
when %r[^hg://] then MercurialDownloadStrategy when AbstractDownloadStrategy then @strategy
when %r[^svn://] then SubversionDownloadStrategy when Symbol then detect_from_symbol
when %r[^svn\+http://] then SubversionDownloadStrategy else detect_from_url
when %r[^fossil://] then FossilDownloadStrategy end
# Some well-known source hosts end
when %r[^https?://(.+?\.)?googlecode\.com/hg] then MercurialDownloadStrategy
when %r[^https?://(.+?\.)?googlecode\.com/svn] then SubversionDownloadStrategy private
when %r[^https?://(.+?\.)?sourceforge\.net/svnroot/] then SubversionDownloadStrategy
when %r[^http://svn.apache.org/repos/] then SubversionDownloadStrategy def detect_from_url
when %r[^http://www.apache.org/dyn/closer.cgi] then CurlApacheMirrorDownloadStrategy case @url
# Common URL patterns # We use a special URL pattern for cvs
when %r[^https?://svn\.] then SubversionDownloadStrategy when %r[^cvs://] then CVSDownloadStrategy
# Otherwise just try to download # Standard URLs
else CurlDownloadStrategy when %r[^bzr://] then BazaarDownloadStrategy
when %r[^git://] then GitDownloadStrategy
when %r[^https?://.+\.git$] then GitDownloadStrategy
when %r[^hg://] then MercurialDownloadStrategy
when %r[^svn://] then SubversionDownloadStrategy
when %r[^svn\+http://] then SubversionDownloadStrategy
when %r[^fossil://] then FossilDownloadStrategy
# Some well-known source hosts
when %r[^https?://(.+?\.)?googlecode\.com/hg] then MercurialDownloadStrategy
when %r[^https?://(.+?\.)?googlecode\.com/svn] then SubversionDownloadStrategy
when %r[^https?://(.+?\.)?sourceforge\.net/svnroot/] then SubversionDownloadStrategy
when %r[^http://svn.apache.org/repos/] then SubversionDownloadStrategy
when %r[^http://www.apache.org/dyn/closer.cgi] then CurlApacheMirrorDownloadStrategy
# Common URL patterns
when %r[^https?://svn\.] then SubversionDownloadStrategy
when bottle_native_regex, bottle_regex, old_bottle_regex
CurlBottleDownloadStrategy
# Otherwise just try to download
else CurlDownloadStrategy
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 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