Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into env_bundle_refactor
Browse files Browse the repository at this point in the history
Conflicts:
	lib/bundler/bundle.rb
  • Loading branch information
Carlhuda committed Jan 4, 2010
2 parents 94e403a + d550ee1 commit bccccf9
Show file tree
Hide file tree
Showing 19 changed files with 225 additions and 114 deletions.
9 changes: 9 additions & 0 deletions README.markdown
Expand Up @@ -66,6 +66,15 @@ information, please refer to Bundler::ManifestBuilder.
# the gemspec is found in.
gem "rspec", "1.1.6", :vendored_at => "vendor/rspec"

# You can also control what will happen when you run Bundler.require_env
# by using the :require_as option, as per the next two examples.

# Don't auto-require this gem.
gem "rspec-rails", "1.2.9", :require_as => nil

# Require something other than the default.
gem "yajl-ruby", "0.6.7", :require_as => "yajl/json_gem"

# Works exactly like :vendored_at, but first downloads the repo from
# git and handles stashing the files for you. As with :vendored_at,
# Bundler will automatically use *.gemspec files in the root or anywhere
Expand Down
2 changes: 1 addition & 1 deletion lib/bundler.rb
Expand Up @@ -19,7 +19,7 @@
require "bundler/remote_specification"

module Bundler
VERSION = "0.7.2.pre"
VERSION = "0.7.2"

class << self
attr_writer :logger
Expand Down
47 changes: 32 additions & 15 deletions lib/bundler/bundle.rb
Expand Up @@ -14,7 +14,11 @@ def initialize(env)
@path = Pathname.new(path)
@bindir = Pathname.new(bindir)

@cache = GemDirectorySource.new(:location => @path.join("cache"))
@cache_path = @path.join('cache')
@cache = GemDirectorySource.new(:location => @cache_path)

@specs_path = @path.join('specifications')
@gems_path = @path.join('gems')
end

def install(options = {})
Expand Down Expand Up @@ -78,10 +82,10 @@ def install(options = {})
end

def cache(*gemfiles)
FileUtils.mkdir_p(@path.join("cache"))
FileUtils.mkdir_p(@cache_path)
gemfiles.each do |gemfile|
Bundler.logger.info "Caching: #{File.basename(gemfile)}"
FileUtils.cp(gemfile, @path.join("cache"))
FileUtils.cp(gemfile, @cache_path)
end
end

Expand Down Expand Up @@ -112,7 +116,7 @@ def prune(options = {})
specs.each do |spec|
unless bundle.any? { |s| s.name == spec.name && s.version == spec.version }
Bundler.logger.info "Pruning #{spec.name} (#{spec.version}) from the cache"
FileUtils.rm @path.join("cache", "#{spec.full_name}.gem")
FileUtils.rm @cache_path.join("#{spec.full_name}.gem")
end
end
end
Expand All @@ -129,9 +133,14 @@ def gems
source_index.gems.values
end

# TODO: Refactor this
def gem_path
@environment.gem_path
end

def source_index
index = Gem::SourceIndex.from_gems_in(@path.join("specifications"))
index.each { |n, spec| spec.loaded_from = @path.join("specifications", "#{spec.full_name}.gemspec") }
index = Gem::SourceIndex.from_gems_in(@specs_path)
index.each { |n, spec| spec.loaded_from = @specs_path.join("#{spec.full_name}.gemspec") }
index
end

Expand Down Expand Up @@ -166,9 +175,9 @@ def download(bundle, options)
def do_install(bundle, options)
bundle.each do |spec|
next if options[:no_bundle].include?(spec.name)
spec.loaded_from = @path.join("specifications", "#{spec.full_name}.gemspec")
spec.loaded_from = @specs_path.join("#{spec.full_name}.gemspec")
# Do nothing if the gem is already expanded
next if @path.join("gems", spec.full_name).directory?
next if @gems_path.join(spec.full_name).directory?

case spec.source
when GemSource, GemDirectorySource, SystemGemSource
Expand All @@ -185,11 +194,12 @@ def generate_bins(bundle, options)
# HAX -- Generate the bin
bin_dir = @bindir
path = @path
gems_path = @gems_path
installer = Gem::Installer.allocate
installer.instance_eval do
@spec = spec
@bin_dir = bin_dir
@gem_dir = path.join("gems", "#{spec.full_name}")
@gem_dir = gems_path.join(spec.full_name)
@gem_home = path
@wrappers = true
@format_executable = false
Expand All @@ -202,7 +212,7 @@ def generate_bins(bundle, options)
def expand_gemfile(spec, options)
Bundler.logger.info "Installing #{spec.name} (#{spec.version})"

gemfile = @path.join("cache", "#{spec.full_name}.gem").to_s
gemfile = @cache_path.join("#{spec.full_name}.gem").to_s

if build_args = options[:build_options] && options[:build_options][spec.name]
Gem::Command.build_args = build_args.map {|k,v| "--with-#{k}=#{v}"}
Expand All @@ -225,12 +235,12 @@ def expand_gemfile(spec, options)

def expand_vendored_gem(spec, options)
add_spec(spec)
FileUtils.mkdir_p(@path.join("gems"))
File.symlink(spec.location, @path.join("gems", spec.full_name))
FileUtils.mkdir_p(@gems_path)
File.symlink(spec.location, @gems_path.join(spec.full_name))
end

def add_spec(spec)
destination = path.join('specifications')
destination = @specs_path
destination.mkdir unless destination.exist?

File.open(destination.join("#{spec.full_name}.gemspec"), 'w') do |f|
Expand Down Expand Up @@ -259,8 +269,8 @@ def cleanup(valid, options)
end

def cleanup_spec(spec)
FileUtils.rm_rf(@path.join("specifications", "#{spec.full_name}.gemspec"))
FileUtils.rm_rf(@path.join("gems", spec.full_name))
FileUtils.rm_rf(@specs_path.join("#{spec.full_name}.gemspec"))
FileUtils.rm_rf(@gems_path.join(spec.full_name))
end

def expand(options)
Expand All @@ -271,9 +281,16 @@ def expand(options)

def configure(specs, options)
FileUtils.mkdir_p(path)

File.open(path.join("environment.rb"), "w") do |file|
file.puts @environment.environment_rb(specs, options)
end

generate_environment_picker
end

def generate_environment_picker
FileUtils.cp("#{File.dirname(__FILE__)}/templates/environment_picker.erb", path.join("../../environment.rb"))
end

def require_code(file, dep)
Expand Down
4 changes: 2 additions & 2 deletions lib/bundler/dsl.rb
Expand Up @@ -107,7 +107,7 @@ def gem(name, *args)
options = args.last.is_a?(Hash) ? args.pop : {}
version = args.last

keys = %w(vendored_at path only except git path bundle require_as tag branch ref).map(&:to_sym)
keys = :vendored_at, :path, :only, :except, :git, :path, :bundle, :require_as, :tag, :branch, :ref
unless (invalid = options.keys - keys).empty?
raise InvalidKey, "Only #{keys.join(", ")} are valid options to #gem. You used #{invalid.join(", ")}"
end
Expand Down Expand Up @@ -171,7 +171,7 @@ def _find_directory_source(path)

def _handle_git_option(name, version, options)
git = options[:git].to_s
ref = options[:commit] || options[:tag]
ref = options[:ref] || options[:tag]
branch = options[:branch]

if source = @git || @git_sources[git]
Expand Down
6 changes: 5 additions & 1 deletion lib/bundler/environment.rb
Expand Up @@ -9,6 +9,10 @@ class Environment
attr_accessor :rubygems, :system_gems
attr_writer :gem_path, :bindir

def self.default_gem_path(root)
Pathname.new("#{root}/vendor/gems/#{Gem.ruby_engine}/#{Gem::ConfigMap[:ruby_version]}")
end

def initialize(filename)
@filename = filename
@default_sources = default_sources
Expand Down Expand Up @@ -38,7 +42,7 @@ def root
end

def gem_path
@gem_path ||= root.join("vendor", "gems")
@gem_path ||= self.class.default_gem_path(root)
end

def bindir
Expand Down
5 changes: 3 additions & 2 deletions lib/bundler/resolver.rb
Expand Up @@ -77,7 +77,8 @@ def initialize(sources, source_requirements)
sources.each do |source|
source.gems.each do |name, specs|
# Hack to work with a regular Gem::SourceIndex
[specs].flatten.compact.each do |spec|
specs = [specs] unless specs.is_a?(Array)
specs.compact.each do |spec|
next if @specs[spec.name].any? { |s| s.version == spec.version && s.platform == spec.platform }
@specs[spec.name] << spec
end
Expand Down Expand Up @@ -234,7 +235,7 @@ def search(dependency)
found.reject! { |spec| spec.version.prerelease? }
end

found.sort_by {|s| [s.version, s.platform == 'ruby' ? "\0" : s.platform] }
found.sort_by {|s| [s.version, s.platform.to_s == 'ruby' ? "\0" : s.platform.to_s] }
end
end
end
Expand Down
80 changes: 50 additions & 30 deletions lib/bundler/source.rb
Expand Up @@ -69,27 +69,31 @@ def download(spec)

def fetch_specs
Bundler.logger.info "Updating source: #{to_s}"
build_gem_index(fetch_main_specs + fetch_prerelease_specs)
end

fetcher = Gem::RemoteFetcher.fetcher
main_index = fetcher.fetch_path("#{uri}/specs.4.8.gz")
begin
prerelease_index = fetcher.fetch_path("#{uri}/prerelease_specs.4.8.gz")
index = Marshal.load(main_index) + Marshal.load(prerelease_index)
rescue Gem::RemoteFetcher::FetchError
Bundler.logger.warn "Source '#{uri}' does not support prerelease gems"
index = Marshal.load(main_index)
end

def build_gem_index(index)
gems = Hash.new { |h,k| h[k] = [] }
index.each do |name, version, platform|
spec = RemoteSpecification.new(name, version, platform, @uri)
spec.source = self
gems[spec.name] << spec if Gem::Platform.match(spec.platform)
end
gems
end

def fetch_main_specs
Marshal.load(Gem::RemoteFetcher.fetcher.fetch_path("#{uri}/specs.4.8.gz"))
rescue Gem::RemoteFetcher::FetchError => e
raise ArgumentError, "#{to_s} is not a valid source: #{e.message}"
end

def fetch_prerelease_specs
Marshal.load(Gem::RemoteFetcher.fetcher.fetch_path("#{uri}/prerelease_specs.4.8.gz"))
rescue Gem::RemoteFetcher::FetchError
Bundler.logger.warn "Source '#{uri}' does not support prerelease gems"
[]
end
end

class SystemGemSource < Source
Expand Down Expand Up @@ -288,8 +292,8 @@ class GitSource < DirectorySource
def initialize(options)
super
@uri = options[:uri]
@ref = options[:ref]
@branch = options[:branch]
@branch = options[:branch] || 'master'
@ref = options[:ref] || "origin/#{@branch}"
end

def location
Expand All @@ -298,24 +302,8 @@ def location
end

def gems
unless location.directory?
# Raise an error if the source should run in local mode,
# but it has not been cached yet.
if local
raise SourceNotCached, "Git repository '#{@uri}' has not been cloned yet"
end

FileUtils.mkdir_p(location.dirname)

Bundler.logger.info "Cloning git repository at: #{@uri}"
`git clone #{@uri} #{location} --no-hardlinks`

if @ref
Dir.chdir(location) { `git checkout --quiet #{@ref}` }
elsif @branch && @branch != "master"
Dir.chdir(location) { `git checkout --quiet origin/#{@branch}` }
end
end
update
checkout
super
end

Expand All @@ -326,5 +314,37 @@ def download(spec)
def to_s
"git: #{uri}"
end

private
def update
if location.directory?
fetch
else
clone
end
end

def fetch
unless local
Bundler.logger.info "Fetching git repository at: #{@uri}"
Dir.chdir(location) { `git fetch origin` }
end
end

def clone
# Raise an error if the source should run in local mode,
# but it has not been cached yet.
if local
raise SourceNotCached, "Git repository '#{@uri}' has not been cloned yet"
end

Bundler.logger.info "Cloning git repository at: #{@uri}"
FileUtils.mkdir_p(location.dirname)
`git clone #{@uri} #{location} --no-hardlinks`
end

def checkout
Dir.chdir(location) { `git checkout --quiet #{@ref}` }
end
end
end
4 changes: 2 additions & 2 deletions lib/bundler/templates/app_script.erb
@@ -1,3 +1,3 @@
<%= shebang bin_file_name %>
require File.join(File.dirname(__FILE__), "<%= path.join("environment").relative_path_from(Pathname.new(bin_dir)) %>")
load File.join(File.dirname(__FILE__), "<%= path.join("gems", @spec.full_name, @spec.bindir, bin_file_name).relative_path_from(Pathname.new(bin_dir)) %>")
require File.expand_path(File.join(File.dirname(__FILE__), "<%= path.join("environment").relative_path_from(Pathname.new(bin_dir)) %>"))
load File.expand_path(File.join(File.dirname(__FILE__), "<%= path.join("gems", @spec.full_name, @spec.bindir, bin_file_name).relative_path_from(Pathname.new(bin_dir)) %>"))
13 changes: 12 additions & 1 deletion lib/bundler/templates/environment.erb
Expand Up @@ -6,6 +6,15 @@ module Bundler
<% unless options[:system_gems] -%>
ENV["GEM_HOME"] = dir
ENV["GEM_PATH"] = dir

# handle 1.9 where system gems are always on the load path
if defined?(::Gem)
$LOAD_PATH.reject! do |p|
p != File.dirname(__FILE__) &&
Gem.path.any? { |gp| p.include?(gp) }
end
end

<% end -%>
ENV["PATH"] = "#{dir}/<%= bindir %><%= File::PATH_SEPARATOR %>#{ENV["PATH"]}"
ENV["RUBYOPT"] = "-r#{file} #{ENV["RUBYOPT"]}"
Expand All @@ -17,7 +26,7 @@ module Bundler
@gemfile = "#{dir}/<%= filename %>"

<% if options[:rubygems] -%>
require "rubygems"
require "rubygems" unless respond_to?(:gem) # 1.9 already has RubyGems loaded

@bundled_specs = {}
<% specs.each do |spec| -%>
Expand Down Expand Up @@ -121,6 +130,8 @@ end
# Define all the Gem errors for gems that reference them.
module Gem
def self.ruby ; <%= Gem.ruby.inspect %> ; end
def self.dir ; @dir ||= File.dirname(File.expand_path(__FILE__)) ; end
class << self ; alias default_dir dir; alias path dir ; end
class LoadError < ::LoadError; end
class Exception < RuntimeError; end
class CommandLineError < Exception; end
Expand Down
4 changes: 4 additions & 0 deletions lib/bundler/templates/environment_picker.erb
@@ -0,0 +1,4 @@
require 'rbconfig'
engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
version = Config::CONFIG['ruby_version']
require File.expand_path("../#{engine}/#{version}/environment", __FILE__)

0 comments on commit bccccf9

Please sign in to comment.