diff options
author | Andre Arko <andre@arko.net> | 2015-01-25 18:38:28 -0800 |
---|---|---|
committer | Andre Arko <andre@arko.net> | 2015-01-25 23:30:18 -0800 |
commit | 3c6c010da1f4d34d9bff7ddb374e0ef6a11de5c3 (patch) | |
tree | 3783c0910bb712db91f0e150d2bc50f821b82486 | |
parent | 748d8ec27db1722e0880c9c4aed50c4bca78a77f (diff) | |
download | bundler-3c6c010da1f4d34d9bff7ddb374e0ef6a11de5c3.tar.gz |
allow cache path to be configured
this adds `package —cache-path`, supports `config cache_path foo`, and
honors the BUNDLE_CACHE_PATH environment variable.
closes #3351
-rw-r--r-- | lib/bundler.rb | 2 | ||||
-rw-r--r-- | lib/bundler/cli.rb | 2 | ||||
-rw-r--r-- | lib/bundler/cli/install.rb | 6 | ||||
-rw-r--r-- | lib/bundler/cli/package.rb | 1 | ||||
-rw-r--r-- | lib/bundler/cli/update.rb | 2 | ||||
-rw-r--r-- | lib/bundler/runtime.rb | 29 | ||||
-rw-r--r-- | lib/bundler/settings.rb | 8 | ||||
-rw-r--r-- | spec/cache/cache_path_spec.rb | 33 |
8 files changed, 61 insertions, 22 deletions
diff --git a/lib/bundler.rb b/lib/bundler.rb index 7315f51692..40cd8aad23 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -210,7 +210,7 @@ module Bundler def app_cache(custom_path = nil) path = custom_path || root - path.join("vendor/cache") + path.join(self.settings.app_cache_path) end def tmp(name = Process.pid.to_s) diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index 7a4873b763..c81da14657 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -239,6 +239,8 @@ module Bundler desc "package [OPTIONS]", "Locks and then caches all of the gems into vendor/cache" method_option "all", :type => :boolean, :banner => "Include all sources (including path, git and svn)." method_option "all-platforms", :type => :boolean, :banner => "Include gems for all platforms, not just the current one" + method_option "cache-path", :type => :string, :banner => + "Specify a different cache path than the default (vendor/cache)." method_option "gemfile", :type => :string, :banner => "Use the specified gemfile instead of Gemfile" method_option "no-install", :type => :boolean, :banner => "Don't actually install the gems, just package." method_option "no-prune", :type => :boolean, :banner => "Don't remove stale gems from the cache." diff --git a/lib/bundler/cli/install.rb b/lib/bundler/cli/install.rb index 78f2a1a522..43dd40a500 100644 --- a/lib/bundler/cli/install.rb +++ b/lib/bundler/cli/install.rb @@ -44,7 +44,7 @@ module Bundler "before deploying." end - if Bundler.root.join("vendor/cache").exist? + if Bundler.app_cache.exist? options[:local] = true end @@ -78,7 +78,7 @@ module Bundler definition = Bundler.definition definition.validate_ruby! Installer.install(Bundler.root, definition, options) - Bundler.load.cache if Bundler.root.join("vendor/cache").exist? && !options["no-cache"] && !Bundler.settings[:frozen] + Bundler.load.cache if Bundler.app_cache.exist? && !options["no-cache"] && !Bundler.settings[:frozen] Bundler.ui.confirm "Bundle complete! #{dependencies_count_for(definition)}, #{gems_installed_for(definition)}." confirm_without_groups @@ -113,7 +113,7 @@ module Bundler end rescue GemNotFound, VersionConflict => e if options[:local] && Bundler.app_cache.exist? - Bundler.ui.warn "Some gems seem to be missing from your vendor/cache directory." + Bundler.ui.warn "Some gems seem to be missing from your #{Bundler.settings.app_cache_path} directory." end unless Bundler.definition.has_rubygems_remotes? diff --git a/lib/bundler/cli/package.rb b/lib/bundler/cli/package.rb index f72a6c5c20..f99678c9ec 100644 --- a/lib/bundler/cli/package.rb +++ b/lib/bundler/cli/package.rb @@ -10,6 +10,7 @@ module Bundler Bundler.ui.level = "error" if options[:quiet] Bundler.settings[:path] = File.expand_path(options[:path]) if options[:path] Bundler.settings[:cache_all_platforms] = options["all-platforms"] if options.key?("all-platforms") + Bundler.settings[:cache_path] = options["cache-path"] if options.key?("cache-path") setup_cache_all install diff --git a/lib/bundler/cli/update.rb b/lib/bundler/cli/update.rb index c3c7c683c9..6ed38f2c3a 100644 --- a/lib/bundler/cli/update.rb +++ b/lib/bundler/cli/update.rb @@ -49,7 +49,7 @@ module Bundler Bundler.definition.validate_ruby! Installer.install Bundler.root, Bundler.definition, opts - Bundler.load.cache if Bundler.root.join("vendor/cache").exist? + Bundler.load.cache if Bundler.app_cache.exist? if Bundler.settings[:clean] && Bundler.settings[:path] require "bundler/cli/clean" diff --git a/lib/bundler/runtime.rb b/lib/bundler/runtime.rb index 85299015d2..f83cb30742 100644 --- a/lib/bundler/runtime.rb +++ b/lib/bundler/runtime.rb @@ -106,10 +106,10 @@ module Bundler alias gems specs def cache(custom_path = nil) - cache_path = cache_path(custom_path) + cache_path = Bundler.app_cache(custom_path) FileUtils.mkdir_p(cache_path) unless File.exist?(cache_path) - Bundler.ui.info "Updating files in vendor/cache" + Bundler.ui.info "Updating files in #{Bundler.settings.app_cache_path}" specs.each do |spec| next if spec.name == 'bundler' spec.source.send(:fetch_gem, spec) if Bundler.settings[:cache_all_platforms] && spec.source.respond_to?(:fetch_gem, true) @@ -121,15 +121,14 @@ module Bundler FileUtils.touch(File.expand_path("../.bundlecache", git_dir)) end - prune_cache(custom_path) unless Bundler.settings[:no_prune] + prune_cache(cache_path) unless Bundler.settings[:no_prune] end - def prune_cache(custom_path) - cache_path = cache_path(custom_path) + def prune_cache(cache_path) FileUtils.mkdir_p(cache_path) unless File.exist?(cache_path) resolve = @definition.resolve - prune_gem_cache(resolve, custom_path) - prune_git_and_path_cache(resolve, custom_path) + prune_gem_cache(resolve, cache_path) + prune_git_and_path_cache(resolve, cache_path) end def clean(dry_run = false) @@ -228,8 +227,8 @@ module Bundler private - def prune_gem_cache(resolve, custom_path) - cached = Dir["#{cache_path(custom_path)}/*.gem"] + def prune_gem_cache(resolve, cache_path) + cached = Dir["#{cache_path}/*.gem"] cached = cached.delete_if do |path| spec = Bundler.rubygems.spec_from_gem path @@ -240,7 +239,7 @@ module Bundler end if cached.any? - Bundler.ui.info "Removing outdated .gem files from vendor/cache" + Bundler.ui.info "Removing outdated .gem files from #{Bundler.settings.app_cache_path}" cached.each do |path| Bundler.ui.info " * #{File.basename(path)}" @@ -249,8 +248,8 @@ module Bundler end end - def prune_git_and_path_cache(resolve, custom_path) - cached = Dir["#{cache_path(custom_path)}/*/.bundlecache"] + def prune_git_and_path_cache(resolve, cache_path) + cached = Dir["#{cache_path}/*/.bundlecache"] cached = cached.delete_if do |path| name = File.basename(File.dirname(path)) @@ -262,7 +261,7 @@ module Bundler end if cached.any? - Bundler.ui.info "Removing outdated git and path gems from vendor/cache" + Bundler.ui.info "Removing outdated git and path gems from #{Bundler.settings.app_cache_path}" cached.each do |path| path = File.dirname(path) @@ -289,9 +288,5 @@ module Bundler end end - def cache_path(custom_path = nil) - path = custom_path || root - path.join("vendor/cache") - end end end diff --git a/lib/bundler/settings.rb b/lib/bundler/settings.rb index cea3e0e98e..97ef4685a6 100644 --- a/lib/bundler/settings.rb +++ b/lib/bundler/settings.rb @@ -115,6 +115,14 @@ module Bundler ENV['BUNDLE_IGNORE_CONFIG'] end + def app_cache_path + @app_cache_path ||= begin + path = self[:cache_path] || "vendor/cache" + raise InvalidOption, "Cache path must be relative to the bundle path" if path.start_with?("/") + path + end + end + private def key_for(key) key = key.to_s.gsub(".", "__").upcase diff --git a/spec/cache/cache_path_spec.rb b/spec/cache/cache_path_spec.rb new file mode 100644 index 0000000000..c0e73ca588 --- /dev/null +++ b/spec/cache/cache_path_spec.rb @@ -0,0 +1,33 @@ +require "spec_helper" + +describe "bundle package" do + before do + gemfile <<-G + source "file://#{gem_repo1}" + gem "rack" + G + end + + context "with --cache-path" do + it "caches gems at given path" do + bundle :package, "cache-path" => "vendor/cache-foo" + expect(bundled_app("vendor/cache-foo/rack-1.0.0.gem")).to exist + end + end + + context "with config cache_path" do + it "caches gems at given path" do + bundle "config cache_path vendor/cache-foo" + bundle :package + expect(bundled_app("vendor/cache-foo/rack-1.0.0.gem")).to exist + end + end + + context "when given an absolute path" do + it "exits with non-zero status" do + bundle :package, "cache-path" => "/tmp/cache-foo" + expect(out).to match(/must be relative/) + expect(exitstatus).to eq(15) if exitstatus + end + end +end |