From b37bbbfc315e98ff525e81a132d16bdc14aed53f Mon Sep 17 00:00:00 2001 From: Adam Vandenberg Date: Tue, 6 Aug 2013 19:52:58 -0700 Subject: Implement Resources Closes #20212. --- Library/Homebrew/resource.rb | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Library/Homebrew/resource.rb (limited to 'Library/Homebrew/resource.rb') diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb new file mode 100644 index 000000000..1136af1c0 --- /dev/null +++ b/Library/Homebrew/resource.rb @@ -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 -- cgit v1.2.3