Skip to content

Commit

Permalink
Cache command now prunes stale .gem files from vendor/cache
Browse files Browse the repository at this point in the history
If you don't want to prune, pass --no-prune to either cache or pack
  • Loading branch information
indirect committed Apr 20, 2010
1 parent 20d36fd commit 1d5b069
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 24 deletions.
3 changes: 3 additions & 0 deletions lib/bundler/cli.rb
Expand Up @@ -143,15 +143,18 @@ def show(gem_name = nil)
map %w(list) => "show"

desc "cache", "Cache all the gems to vendor/cache"
method_option "no-prune", :type => :boolean, :banner => "Don't remove stale gems from the cache."
def cache
Bundler.runtime.cache
Bundler.runtime.prune_cache unless options[:no_prune]
rescue GemNotFound => e
Bundler.ui.error(e.message)
Bundler.ui.warn "Run `bundle install` to install missing gems."
exit 128
end

desc "package", "Locks and then caches all of the gems into vendor/cache"
method_option "no-prune", :type => :boolean, :banner => "Don't remove stale gems from the cache."
def package
lock
cache
Expand Down
15 changes: 14 additions & 1 deletion lib/bundler/runtime.rb
Expand Up @@ -79,7 +79,6 @@ def dependencies_for(*groups)
alias gems specs

def cache
cache_path = "#{root}/vendor/cache/"
FileUtils.mkdir_p(cache_path)

Bundler.ui.info "Copying .gem files into vendor/cache"
Expand All @@ -94,12 +93,26 @@ def cache
end
end

def prune_cache
FileUtils.mkdir_p(cache_path)

Bundler.ui.info "Removing outdated .gem files from vendor/cache"
cache_path.children.each do |gemfile|
spec = Gem::Format.from_file_by_path(gemfile).spec
gemfile.rmtree unless specs.include?(spec)
end
end

private

def load_paths
specs.map { |s| s.load_paths }.flatten
end

def cache_path
root.join("vendor/cache")
end

def write_yml_lock
yml = details.to_yaml
File.open("#{root}/Gemfile.lock", 'w') do |f|
Expand Down
50 changes: 49 additions & 1 deletion spec/cache/gems_spec.rb
@@ -1,6 +1,7 @@
require File.expand_path('../../spec_helper', __FILE__)

describe "bundle cache with gems" do
describe "bundle cache" do

describe "when there are only gemsources" do
before :each do
gemfile <<-G
Expand Down Expand Up @@ -69,4 +70,51 @@
end
end

describe "when previously cached" do
before :each do
build_repo2
install_gemfile <<-G
source "file://#{gem_repo2}"
gem "rack"
gem "actionpack"
G
bundle :cache
cached_gem("rack-1.0.0").should exist
cached_gem("actionpack-2.3.2").should exist
cached_gem("activesupport-2.3.2").should exist
end

it "re-caches during install" do
cached_gem("rack-1.0.0").rmtree
bundle :install
out.should include("Copying .gem files into vendor/cache")
cached_gem("rack-1.0.0").should exist
end

it "adds updated gems" do
update_repo2
bundle :install
cached_gem("rack-1.2").should exist
end

it "adds new gems and dependencies" do
install_gemfile <<-G
source "file://#{gem_repo2}"
gem "rails"
G
cached_gem("rails-2.3.2").should exist
cached_gem("activerecord-2.3.2").should exist
end

it "removes .gems for removed gems and dependencies" do
install_gemfile <<-G
source "file://#{gem_repo2}"
gem "rack"
G
cached_gem("rack-1.0.0").should exist
cached_gem("actionpack-2.3.2").should_not exist
cached_gem("activesupport-2.3.2").should_not exist
end
end

end
24 changes: 2 additions & 22 deletions spec/install/gems/packed_spec.rb
Expand Up @@ -32,43 +32,23 @@
err.should be_empty
should_be_installed "rack 1.0"
end
end

describe "when cached" do
it "ignores cached gems for the wrong platform" do
install_gemfile <<-G
Gem.platforms = [#{java}]
source "file://#{gem_repo1}"
gem "platform_specific"
G
bundle :cache
bundle :pack
simulate_new_machine

install_gemfile <<-G
install_gemfile <<-G, :relock => true
Gem.platforms = [#{rb}]
source "file://#{gem_repo1}"
gem "platform_specific"
G
bundle :install
run "require 'platform_specific' ; puts PLATFORM_SPECIFIC"
out.should == "1.0.0 RUBY"
end

it "updates the cache during later installs" do
cached_gem = bundled_app("vendor/cache/rack-1.0.0.gem")
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G

bundle :cache
cached_gem.should exist

FileUtils.rm_rf(cached_gem)

bundle :install
out.should include("Copying .gem files into vendor/cache")
cached_gem.should exist
end
end
end
4 changes: 4 additions & 0 deletions spec/support/path.rb
Expand Up @@ -30,6 +30,10 @@ def bundled_app2(*path)
root.join(*path)
end

def cached_gem(path)
bundled_app("vendor/cache/#{path}.gem")
end

def base_system_gems
tmp.join("gems/base")
end
Expand Down

0 comments on commit 1d5b069

Please sign in to comment.