Skip to content

Commit

Permalink
Added rubygems magic so that everything happy when you require rubyge…
Browse files Browse the repository at this point in the history
…ms in an app with bundled gems
  • Loading branch information
Yehuda Katz + Carl Lerche committed Jul 21, 2009
1 parent 5928cfa commit 6d2fc2a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
30 changes: 30 additions & 0 deletions lib/bundler/manifest.rb
Expand Up @@ -29,6 +29,7 @@ def install(options = {})
installer = Installer.new(@path)
installer.install # options come here
create_load_paths_files(File.join(@path, "environments"))
create_fake_rubygems(File.join(@path, "environments"))
end

def activate(environment = "default")
Expand Down Expand Up @@ -79,13 +80,42 @@ def create_load_paths_files(path)
environments.each do |environment|
gem_specs = gems_for(environment)
File.open(File.join(path, "#{environment}.rb"), "w") do |file|
file.puts <<-RUBY_EVAL
module Bundler
def self.rubygems_required
#{create_gem_stubs(path, gem_specs)}
end
end
RUBY_EVAL
file.puts "$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))"
load_paths_for_specs(gem_specs).each do |load_path|
file.puts "$LOAD_PATH.unshift #{load_path.inspect}"
end
end
end
end

def create_gem_stubs(path, gem_specs)
gem_specs.map do |spec|
path = File.expand_path(File.join(path, '..', 'specifications', "#{spec.full_name}.gemspec"))
%{
Gem.loaded_specs["#{spec.name}"] = eval(File.read("#{path}"))
}
end.join("\n")
end

def create_fake_rubygems(path)
File.open(File.join(path, "rubygems.rb"), "w") do |file|
file.puts <<-RUBY_EVAL
$:.delete File.expand_path(File.dirname(__FILE__))
load "rubygems.rb"
if defined?(Bundler) && Bundler.respond_to?(:rubygems_required)
Bundler.rubygems_required
end
RUBY_EVAL
end
end

def load_paths_for_specs(specs)
load_paths = []
specs.each do |spec|
Expand Down
29 changes: 29 additions & 0 deletions spec/bundler/manifest_spec.rb
Expand Up @@ -143,5 +143,34 @@ def dep(name, version, options = {})

File.exist?(tmp_file('environments', "production.rb")).should be_false
end

it "adds the environments path to the load paths" do
tmp_file('environments', 'testing.rb').should have_load_paths(tmp_dir, [
"environments"
])
end

it "creates a rubygems.rb file in the environments directory" do
File.exist?(tmp_file('environments', 'rubygems.rb')).should be_true
end

it "requires the Rubygems library" do
env = tmp_file('environments', 'default.rb')
out = `#{Gem.ruby} -r #{env} -r rubygems -e 'puts Gem'`.strip
out.should == 'Gem'
end

it "removes the environments path from the load paths after rubygems is required" do
env = tmp_file('environments', 'default.rb')
out = `#{Gem.ruby} -r #{env} -r rubygems -e 'puts $:'`
out.should_not include(tmp_file('environments'))
end

it "Gem.loaded_specs has the gems that are included" do
env = tmp_file('environments', 'default.rb')
out = `#{Gem.ruby} -r #{env} -r rubygems -e 'puts Gem.loaded_specs.map{|k,v|"\#{k} - \#{v.version}"}'`
out = out.split("\n")
out.should include("rack - 1.0.0")
end
end
end
13 changes: 10 additions & 3 deletions spec/matchers.rb
Expand Up @@ -30,12 +30,19 @@ def only_have_specs(*names)

def have_load_paths(root, gem_load_paths)
flattened_paths = []
gem_load_paths.each do |gem_name, paths|
paths.each { |path| flattened_paths << File.join(root, "gems", gem_name, path) }

if gem_load_paths.is_a?(Hash)
gem_load_paths.each do |gem_name, paths|
paths.each { |path| flattened_paths << File.join(root, "gems", gem_name, path) }
end
else
gem_load_paths.each do |path|
flattened_paths << File.join(root, path)
end
end

simple_matcher("have load paths") do |given, matcher|
actual = `ruby -r#{given} -e 'puts $:'`.split("\n")
actual = `#{Gem.ruby} -r#{given} -e 'puts $:'`.split("\n")

flattened_paths.all? do |path|
matcher.failure_message = "expected environment load paths to contain '#{path}', but it was:\n #{actual.join("\n ")}"
Expand Down

0 comments on commit 6d2fc2a

Please sign in to comment.