Reimplement SoftwareSpec on top of Resource
This commit is contained in:
parent
3793c05845
commit
df537528c7
@ -1,11 +1,17 @@
|
|||||||
class Formula
|
class Formula
|
||||||
def self.md5(val)
|
def self.md5(val)
|
||||||
@stable ||= SoftwareSpec.new
|
@stable ||= create_spec(SoftwareSpec)
|
||||||
@stable.md5(val)
|
@stable.md5(val)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class SoftwareSpec
|
class SoftwareSpec
|
||||||
|
def md5(val)
|
||||||
|
@resource.md5(val)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Resource
|
||||||
def md5(val)
|
def md5(val)
|
||||||
@checksum = Checksum.new(:md5, val)
|
@checksum = Checksum.new(:md5, val)
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
require 'resource'
|
require 'resource'
|
||||||
require 'download_strategy'
|
|
||||||
require 'dependency_collector'
|
require 'dependency_collector'
|
||||||
require 'formula_support'
|
require 'formula_support'
|
||||||
require 'formula_lock'
|
require 'formula_lock'
|
||||||
@ -49,7 +48,7 @@ class Formula
|
|||||||
|
|
||||||
@active_spec = determine_active_spec
|
@active_spec = determine_active_spec
|
||||||
validate_attributes :url, :name, :version
|
validate_attributes :url, :name, :version
|
||||||
@downloader = active_spec.download_strategy.new(name, active_spec)
|
@downloader = active_spec.downloader
|
||||||
|
|
||||||
# Combine DSL `option` and `def options`
|
# Combine DSL `option` and `def options`
|
||||||
options.each do |opt, desc|
|
options.each do |opt, desc|
|
||||||
@ -60,15 +59,14 @@ class Formula
|
|||||||
@pin = FormulaPin.new(self)
|
@pin = FormulaPin.new(self)
|
||||||
|
|
||||||
@resources = self.class.resources
|
@resources = self.class.resources
|
||||||
@resources.each_value do |r|
|
@resources.each_value { |r| r.owner = self }
|
||||||
r.set_owner name
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_spec(name)
|
def set_spec(name)
|
||||||
spec = self.class.send(name)
|
spec = self.class.send(name)
|
||||||
return if spec.nil?
|
return if spec.nil?
|
||||||
if block_given? && yield(spec) || !spec.url.nil?
|
if block_given? && yield(spec) || !spec.url.nil?
|
||||||
|
spec.owner = self
|
||||||
instance_variable_set("@#{name}", spec)
|
instance_variable_set("@#{name}", spec)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -515,10 +513,7 @@ class Formula
|
|||||||
|
|
||||||
# For brew-fetch and others.
|
# For brew-fetch and others.
|
||||||
def fetch
|
def fetch
|
||||||
# Ensure the cache exists
|
active_spec.fetch
|
||||||
HOMEBREW_CACHE.mkpath
|
|
||||||
downloader.fetch
|
|
||||||
cached_download
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# For FormulaInstaller.
|
# For FormulaInstaller.
|
||||||
@ -610,11 +605,7 @@ class Formula
|
|||||||
private
|
private
|
||||||
|
|
||||||
def stage
|
def stage
|
||||||
fetched = fetch
|
active_spec.stage do
|
||||||
verify_download_integrity(fetched) if fetched.respond_to?(:file?) and fetched.file?
|
|
||||||
mktemp do
|
|
||||||
downloader.stage
|
|
||||||
# Set path after the downloader changes the working folder.
|
|
||||||
@buildpath = Pathname.pwd
|
@buildpath = Pathname.pwd
|
||||||
yield
|
yield
|
||||||
@buildpath = nil
|
@buildpath = nil
|
||||||
|
@ -2,32 +2,35 @@ require 'download_strategy'
|
|||||||
require 'checksum'
|
require 'checksum'
|
||||||
require 'version'
|
require 'version'
|
||||||
|
|
||||||
# A Resource describes a tarball that a formula needs in addition
|
# Resource is the fundamental representation of an external resource. The
|
||||||
# to the formula's own download.
|
# primary formula download, along with other declared resources, are instances
|
||||||
|
# of this class.
|
||||||
class Resource
|
class Resource
|
||||||
include FileUtils
|
include FileUtils
|
||||||
|
|
||||||
# The mktmp mixin expects a name property
|
|
||||||
# This is the resource name
|
|
||||||
attr_reader :name
|
attr_reader :name
|
||||||
|
|
||||||
attr_reader :checksum, :mirrors, :specs, :using
|
attr_reader :checksum, :mirrors, :specs, :using
|
||||||
|
|
||||||
def initialize name
|
# Formula name must be set after the DSL, as we have no access to the
|
||||||
|
# formula name before initialization of the formula
|
||||||
|
attr_accessor :owner
|
||||||
|
|
||||||
|
# XXX: for bottles, address this later
|
||||||
|
attr_writer :url, :checksum
|
||||||
|
|
||||||
|
def initialize name, url=nil, version=nil
|
||||||
@name = name
|
@name = name
|
||||||
@url = nil
|
@url = url
|
||||||
@version = nil
|
@version = version
|
||||||
@mirrors = []
|
@mirrors = []
|
||||||
@specs = {}
|
@specs = {}
|
||||||
@checksum = nil
|
@checksum = nil
|
||||||
@using = nil
|
@using = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Formula name must be set after the DSL, as we have no access to the
|
def downloader
|
||||||
# formula name before initialization of the formula
|
download_name = name == :default ? owner.name : "#{owner.name}--#{name}"
|
||||||
def set_owner owner
|
@downloader ||= download_strategy.new(download_name, self)
|
||||||
@owner = owner
|
|
||||||
@downloader = download_strategy.new("#{owner}--#{name}", self)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Download the resource
|
# Download the resource
|
||||||
@ -38,7 +41,7 @@ class Resource
|
|||||||
fetched = fetch
|
fetched = fetch
|
||||||
verify_download_integrity(fetched) if fetched.respond_to?(:file?) and fetched.file?
|
verify_download_integrity(fetched) if fetched.respond_to?(:file?) and fetched.file?
|
||||||
mktemp do
|
mktemp do
|
||||||
@downloader.stage
|
downloader.stage
|
||||||
if block_given?
|
if block_given?
|
||||||
yield self
|
yield self
|
||||||
else
|
else
|
||||||
@ -52,22 +55,22 @@ class Resource
|
|||||||
end
|
end
|
||||||
|
|
||||||
def cached_download
|
def cached_download
|
||||||
@downloader.cached_location
|
downloader.cached_location
|
||||||
end
|
end
|
||||||
|
|
||||||
# For brew-fetch and others.
|
# For brew-fetch and others.
|
||||||
def fetch
|
def fetch
|
||||||
# Ensure the cache exists
|
# Ensure the cache exists
|
||||||
HOMEBREW_CACHE.mkpath
|
HOMEBREW_CACHE.mkpath
|
||||||
@downloader.fetch
|
downloader.fetch
|
||||||
cached_download
|
cached_download
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_download_integrity fn
|
def verify_download_integrity fn
|
||||||
fn.verify_checksum(checksum)
|
fn.verify_checksum(checksum)
|
||||||
rescue ChecksumMissingError
|
rescue ChecksumMissingError
|
||||||
opoo "Cannot verify package integrity"
|
opoo "Cannot verify download integrity"
|
||||||
puts "The formula did not provide a download checksum"
|
puts "A checksum was not provided for this resource"
|
||||||
puts "For your reference the SHA1 is: #{fn.sha1}"
|
puts "For your reference the SHA1 is: #{fn.sha1}"
|
||||||
rescue ChecksumMismatchError => e
|
rescue ChecksumMismatchError => e
|
||||||
e.advice = <<-EOS.undent
|
e.advice = <<-EOS.undent
|
||||||
|
@ -1,69 +1,19 @@
|
|||||||
require 'download_strategy'
|
require 'forwardable'
|
||||||
|
require 'resource'
|
||||||
require 'checksum'
|
require 'checksum'
|
||||||
require 'version'
|
require 'version'
|
||||||
|
|
||||||
class SoftwareSpec
|
class SoftwareSpec
|
||||||
attr_reader :checksum, :mirrors, :specs, :using
|
extend Forwardable
|
||||||
|
|
||||||
|
def_delegators :@resource, :owner=
|
||||||
|
def_delegators :@resource, :stage, :fetch
|
||||||
|
def_delegators :@resource, :download_strategy, :verify_download_integrity
|
||||||
|
def_delegators :@resource, :checksum, :mirrors, :specs, :using, :downloader
|
||||||
|
def_delegators :@resource, :url, :version, :mirror, *Checksum::TYPES
|
||||||
|
|
||||||
def initialize url=nil, version=nil
|
def initialize url=nil, version=nil
|
||||||
@url = url
|
@resource = Resource.new(:default, url, version)
|
||||||
@version = version
|
|
||||||
@mirrors = []
|
|
||||||
@specs = {}
|
|
||||||
@checksum = nil
|
|
||||||
@using = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def download_strategy
|
|
||||||
@download_strategy ||= DownloadStrategyDetector.detect(url, using)
|
|
||||||
end
|
|
||||||
|
|
||||||
def verify_download_integrity fn
|
|
||||||
fn.verify_checksum(checksum)
|
|
||||||
rescue ChecksumMissingError
|
|
||||||
opoo "Cannot verify package integrity"
|
|
||||||
puts "The formula did not provide a download checksum"
|
|
||||||
puts "For your reference the SHA1 is: #{fn.sha1}"
|
|
||||||
rescue ChecksumMismatchError => e
|
|
||||||
e.advice = <<-EOS.undent
|
|
||||||
Archive: #{fn}
|
|
||||||
(To retry an incomplete download, remove the file above.)
|
|
||||||
EOS
|
|
||||||
raise e
|
|
||||||
end
|
|
||||||
|
|
||||||
def detect_version(val)
|
|
||||||
case val
|
|
||||||
when nil then Version.detect(url, specs)
|
|
||||||
when String then Version.new(val)
|
|
||||||
when Hash then Version.new_with_scheme(*val.shift)
|
|
||||||
else
|
|
||||||
raise TypeError, "version '#{val.inspect}' should be a string"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# The methods that follow are used in the block-form DSL spec methods
|
|
||||||
Checksum::TYPES.each do |cksum|
|
|
||||||
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
|
||||||
def #{cksum}(val)
|
|
||||||
@checksum = Checksum.new(:#{cksum}, val)
|
|
||||||
end
|
|
||||||
EOS
|
|
||||||
end
|
|
||||||
|
|
||||||
def url val=nil, specs={}
|
|
||||||
return @url if val.nil?
|
|
||||||
@url = val
|
|
||||||
@using = specs.delete(:using)
|
|
||||||
@specs.merge!(specs)
|
|
||||||
end
|
|
||||||
|
|
||||||
def version val=nil
|
|
||||||
@version ||= detect_version(val)
|
|
||||||
end
|
|
||||||
|
|
||||||
def mirror val
|
|
||||||
mirrors << val
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -78,9 +28,10 @@ class HeadSoftwareSpec < SoftwareSpec
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Bottle < SoftwareSpec
|
class Bottle < SoftwareSpec
|
||||||
attr_writer :url
|
|
||||||
attr_rw :root_url, :prefix, :cellar, :revision
|
attr_rw :root_url, :prefix, :cellar, :revision
|
||||||
|
|
||||||
|
def_delegators :@resource, :url=
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
super
|
super
|
||||||
@revision = 0
|
@revision = 0
|
||||||
@ -102,7 +53,7 @@ class Bottle < SoftwareSpec
|
|||||||
end
|
end
|
||||||
|
|
||||||
if @#{cksum}.has_key? bottle_tag
|
if @#{cksum}.has_key? bottle_tag
|
||||||
@checksum = @#{cksum}[bottle_tag]
|
@resource.checksum = @#{cksum}[bottle_tag]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
EOS
|
EOS
|
||||||
|
Loading…
x
Reference in New Issue
Block a user