aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/resource.rb
diff options
context:
space:
mode:
authorAdam Vandenberg2013-08-06 19:52:58 -0700
committerAdam Vandenberg2013-09-11 22:05:26 -0700
commitb37bbbfc315e98ff525e81a132d16bdc14aed53f (patch)
tree12bd4533ec277373ab4369a8937ea390d705e143 /Library/Homebrew/resource.rb
parenta634b70e24d7eb33295e1d0051436fa391e3b19c (diff)
downloadhomebrew-b37bbbfc315e98ff525e81a132d16bdc14aed53f.tar.bz2
Implement Resources
Closes #20212.
Diffstat (limited to 'Library/Homebrew/resource.rb')
-rw-r--r--Library/Homebrew/resource.rb57
1 files changed, 57 insertions, 0 deletions
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