diff options
author | Andre Arko <andre@arko.net> | 2015-09-09 20:40:40 -0700 |
---|---|---|
committer | Andre Arko <andre@arko.net> | 2015-09-09 20:42:15 -0700 |
commit | 7f04d1a558fc3f492df5ea561f9433e9b849de4c (patch) | |
tree | 17ab8f4e4e59b3a57d357c3b6a70bf210705f6de | |
parent | 588b099a0b6491e0e5ae76a3b9ed63d610fe7f9e (diff) | |
download | bundler-7f04d1a558fc3f492df5ea561f9433e9b849de4c.tar.gz |
cache the downloaded gems
-rw-r--r-- | lib/bundler/settings.rb | 9 | ||||
-rw-r--r-- | lib/bundler/source/rubygems.rb | 28 |
2 files changed, 35 insertions, 2 deletions
diff --git a/lib/bundler/settings.rb b/lib/bundler/settings.rb index afa76fcb79..ea99b63c50 100644 --- a/lib/bundler/settings.rb +++ b/lib/bundler/settings.rb @@ -222,6 +222,14 @@ module Bundler end end + def download_cache_path + if self["path.download_cache"] + Pathname.new(File.expand_path(self["path.download_cache"])) + else + Bundler.user_bundle_path.join("cache") + end + end + private def key_for(key) @@ -321,5 +329,6 @@ module Bundler end uri end + end end diff --git a/lib/bundler/source/rubygems.rb b/lib/bundler/source/rubygems.rb index 12d50e8017..54c9c68e50 100644 --- a/lib/bundler/source/rubygems.rb +++ b/lib/bundler/source/rubygems.rb @@ -398,9 +398,9 @@ module Bundler download_path = Bundler.requires_sudo? ? Bundler.tmp(spec.full_name) : Bundler.rubygems.gem_dir gem_path = "#{Bundler.rubygems.gem_dir}/cache/#{spec.full_name}.gem" - FileUtils.mkdir_p("#{download_path}/cache") - Bundler.rubygems.download_gem(spec, uri, download_path) + + download_gem(spec, uri, download_path) if Bundler.requires_sudo? Bundler.mkdir_p "#{Bundler.rubygems.gem_dir}/cache" @@ -423,6 +423,30 @@ module Bundler def installed?(spec) installed_specs[spec].any? end + + private + + def download_cache_path(*paths) + raise "Caching is only possible for sources with one URL" if remotes.size > 1 + uri = remotes.first + port = uri.port unless uri.port == 80 + path = Digest::MD5.hexdigest(uri.path) unless uri.path =~ /\A\/?\Z/ + source_dir = [uri.hostname, port, path].compact.join(".") + Bundler.settings.download_cache_path.join(source_dir).tap(&:mkpath).join(*paths) + end + + def download_gem(spec, uri, download_path) + cache_path = download_cache_path("#{spec.full_name}.gem") + local_path = File.join(download_path, "cache/#{spec.full_name}.gem") + + if cache_path.exist? + FileUtils.cp(cache_path, local_path) + else + Bundler.rubygems.download_gem(spec, uri, download_path) + FileUtils.cp(local_path, cache_path) + end + end + end end end |