Skip to content

Instantly share code, notes, and snippets.

@pmahoney
Created January 15, 2013 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmahoney/4539418 to your computer and use it in GitHub Desktop.
Save pmahoney/4539418 to your computer and use it in GitHub Desktop.
require 'bundler'
# First attempt a bundle install without remote resolution. In the
# happy case, all gems are already installed, and checking this is
# quick. If it fails, attempt a remote resolution. This is slow
# (probably largely because of how many dang gem versions we have in
# our local gem server), but it will install anything that is missing.
#
# Bundler uses lots of class-level configuration, and thus isn't
# designed to be called repeatedly with different options, but we're
# giving it a shot.
#
# Developed against bundler 1.2.3. There's no guarantee this will
# continue to work with future versions.
def bundle_install_fast(gemfile = 'Gemfile', lockfile = nil)
# lets pretend ENV['GEM_HOME'] etc. have already been configured
# bundle exec restricts this to what's in the current Gemfile; we
# need to open it up to other Gemfiles
gem_spec_all = Gem::Specification.all
Gem::Specification.all = nil
default_gemfile = ENV['BUNDLE_GEMFILE']
ENV['BUNDLE_GEMFILE'] = gemfile
begin
lockfile ||= gemfile + '.lock'
Bundler.settings[:frozen] = true
unlock = false
make_definition = proc {
definition = Bundler::Definition.build(gemfile, lockfile, unlock)
definition.ensure_equivalent_gemfile_and_lockfile
definition
}
specs = begin
definition = make_definition.call
definition.specs
rescue Bundler::GemNotFound => e
warn "some gems not already installed (#{e})"
warn 'resolving remotely...'
definition = make_definition.call
definition.resolve_remotely!
definition.specs
end
installer = Bundler::Installer.new(File.dirname(gemfile), definition)
Bundler::Installer.post_install_messages = {}
specs.each do |spec|
installer.send(:install_gem_from_spec, spec)
end
ensure
Gem::Specification.all = gem_spec_all
ENV['BUNDLE_GEMFILE'] = default_gemfile
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment