mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
parent
cf751fd013
commit
d4cf3ef212
@ -260,3 +260,24 @@ class ChecksumMismatchError < RuntimeError
|
||||
super + advice.to_s
|
||||
end
|
||||
end
|
||||
|
||||
class ResourceMissingError < ArgumentError
|
||||
def initialize formula, resource
|
||||
@formula = formula
|
||||
@resource = resource
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Formula #{@formula} does not define resource \"#{@resource}\"."
|
||||
end
|
||||
end
|
||||
|
||||
class DuplicateResourceError < ArgumentError
|
||||
def initialize resource
|
||||
@resource = resource
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Resource \"#{@resource}\" defined more than once."
|
||||
end
|
||||
end
|
||||
|
@ -10,6 +10,8 @@ class Pathname
|
||||
def install *sources
|
||||
sources.each do |src|
|
||||
case src
|
||||
when Resource
|
||||
src.stage(self)
|
||||
when Array
|
||||
if src.empty?
|
||||
opoo "tried to install empty array to #{self}"
|
||||
|
@ -1,3 +1,4 @@
|
||||
require 'resource'
|
||||
require 'download_strategy'
|
||||
require 'dependency_collector'
|
||||
require 'formula_support'
|
||||
@ -56,6 +57,11 @@ class Formula
|
||||
end
|
||||
|
||||
@pin = FormulaPin.new(self)
|
||||
|
||||
@resources = self.class.resources
|
||||
@resources.each_value do |r|
|
||||
r.set_owner name
|
||||
end
|
||||
end
|
||||
|
||||
def set_spec(name)
|
||||
@ -91,6 +97,16 @@ class Formula
|
||||
def version; active_spec.version; end
|
||||
def mirrors; active_spec.mirrors; end
|
||||
|
||||
def resource(name)
|
||||
res = @resources[name]
|
||||
raise ResourceMissingError.new(@name, name) if res.nil?
|
||||
res
|
||||
end
|
||||
|
||||
def resources
|
||||
@resources.values
|
||||
end
|
||||
|
||||
# if the dir is there, but it's empty we consider it not installed
|
||||
def installed?
|
||||
(dir = installed_prefix).directory? && dir.children.length > 0
|
||||
@ -694,6 +710,19 @@ class Formula
|
||||
@stable.mirror(val)
|
||||
end
|
||||
|
||||
# Hold any resources defined by this formula
|
||||
def resources
|
||||
@resources ||= Hash.new
|
||||
end
|
||||
|
||||
# Define a named resource using a SoftwareSpec style block
|
||||
def resource res_name, &block
|
||||
raise DuplicateResourceError.new(res_name) if resources.has_key?(res_name)
|
||||
spec = SoftwareSpec.new
|
||||
spec.instance_eval(&block)
|
||||
resources[res_name] = Resource.new(res_name, spec)
|
||||
end
|
||||
|
||||
def dependencies
|
||||
@dependencies ||= DependencyCollector.new
|
||||
end
|
||||
|
57
Library/Homebrew/resource.rb
Normal file
57
Library/Homebrew/resource.rb
Normal file
@ -0,0 +1,57 @@
|
||||
# A Resource describes a tarball that a formula needs in addition
|
||||
# to the formula's own download.
|
||||
class Resource
|
||||
include FileUtils
|
||||
|
||||
# The mktmp mixin expects a name property
|
||||
# This is the resource name
|
||||
attr_reader :name
|
||||
|
||||
# This is the associated formula name
|
||||
attr_reader :owner_name
|
||||
|
||||
def initialize name, spec
|
||||
@name = name
|
||||
@spec = spec
|
||||
end
|
||||
|
||||
# Formula name must be set after the DSL, as we have no access to the
|
||||
# formula name before initialization of the formula
|
||||
def set_owner owner
|
||||
@owner = owner
|
||||
@downloader = @spec.download_strategy.new("#{owner}--#{name}", @spec)
|
||||
end
|
||||
|
||||
# Download the resource
|
||||
# If a target is given, unpack there; else unpack to a temp folder
|
||||
# If block is given, yield to that block
|
||||
# A target or a block must be given, but not both
|
||||
def stage(target=nil)
|
||||
fetched = fetch
|
||||
verify_download_integrity(fetched) if fetched.file?
|
||||
mktemp do
|
||||
@downloader.stage
|
||||
if block_given?
|
||||
yield self
|
||||
else
|
||||
target.install Dir['*']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def cached_download
|
||||
@downloader.cached_location
|
||||
end
|
||||
|
||||
# For brew-fetch and others.
|
||||
def fetch
|
||||
# Ensure the cache exists
|
||||
HOMEBREW_CACHE.mkpath
|
||||
@downloader.fetch
|
||||
cached_download
|
||||
end
|
||||
|
||||
def verify_download_integrity fn
|
||||
@spec.verify_download_integrity(fn)
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user