Skip to content
Browse files

Group autorequire: pass :require => 'autorequire' option to gem depen…

…dencies. Use Bundler.require(*groups) to mass-require all gem dependencies.

Signed-off-by: Carl Lerche <carllerche@mac.com>
  • Loading branch information...
1 parent 3d0c082 commit a8cbf77bab3d2dd4a1124c4d00f44ebcff957245 @jeremy jeremy committed with Carl Lerche
View
7 lib/bundler.rb
@@ -62,6 +62,11 @@ def setup(*groups)
load(gemfile).setup(*groups)
end
+ def require(*groups)
+ gemfile = default_gemfile
+ load(gemfile).require(*groups)
+ end
+
def load(gemfile = default_gemfile)
root = Pathname.new(gemfile).dirname
Environment.new root, definition(gemfile)
@@ -125,4 +130,4 @@ def configure_gem_home_and_path
Gem.clear_paths
end
end
-end
+end
View
5 lib/bundler/dependency.rb
@@ -2,11 +2,14 @@
module Bundler
class Dependency < Gem::Dependency
+ attr_reader :autorequire
+
def initialize(name, version, options = {}, &blk)
super(name, version)
@group = options["group"] || :default
@source = options["source"]
+ @autorequire = options["require"] || name
end
end
-end
+end
View
28 lib/bundler/environment.rb
@@ -21,6 +21,12 @@ def setup(*groups)
self
end
+ def require(*groups)
+ dependencies_for(*groups).each do |dep|
+ Kernel.require(dep.autorequire)
+ end
+ end
+
def dependencies
@definition.actual_dependencies
end
@@ -48,11 +54,16 @@ def locked?
File.exist?("#{root}/Gemfile.lock")
end
- def specs_for(*groups)
- return specs if groups.empty?
+ def dependencies_for(*groups)
+ @definition.actual_dependencies.select { |d| groups.include?(d.group) }
+ end
- dependencies = @definition.actual_dependencies.select { |d| groups.include?(d.group) }
- Resolver.resolve(dependencies, index)
+ def specs_for(*groups)
+ if groups.empty?
+ specs
+ else
+ Resolver.resolve(dependencies_for(*groups), index)
+ end
end
def specs
@@ -153,5 +164,12 @@ def details
details
end
+ def autorequires_for_groups
+ groups = dependencies.map { |dep| dep.group }.uniq
+ groups.inject({}) do |hash, group|
+ hash[group] = dependencies_for(group).map { |dep| dep.autorequire }.compact
+ hash
+ end
+ end
end
-end
+end
View
2 lib/bundler/rubygems.rb
@@ -19,4 +19,4 @@ def groups
class Dependency
attr_accessor :source, :group
end
-end
+end
View
20 lib/bundler/templates/environment.erb
@@ -1,6 +1,18 @@
# DO NOT MODIFY THIS FILE
module Bundler
-<% load_paths.each do |path| -%>
- $LOAD_PATH.unshift "<%= path %>"
-<% end -%>
-end
+ LOAD_PATHS = <%= load_paths.inspect %>
+ AUTOREQUIRES = <%= autorequires_for_groups.inspect %>
+
+ def self.setup(*groups)
+ LOAD_PATHS.each { |path| $LOAD_PATH.unshift path }
+ end
+
+ def self.require(*groups)
+ groups.each do |group|
+ AUTOREQUIRES[group].each { |file| Kernel.require file }
+ end
+ end
+
+ # Setup bundle when it's required.
+ setup
+end
View
39 spec/runtime/require_spec.rb
@@ -0,0 +1,39 @@
+require File.expand_path('../../spec_helper', __FILE__)
+
+describe "Bundler.require" do
+ before :each do
+ build_lib "one", "1.0.0" do |s|
+ s.write "lib/baz.rb", "puts 'WIN'"
+ end
+
+ build_lib "two", "1.0.0" do |s|
+ s.write "lib/two.rb", "puts 'WIN'"
+ end
+
+ build_lib "three", "1.0.0" do |s|
+ s.write "lib/three.rb", "puts 'LOSE'"
+ end
+
+ gemfile <<-G
+ path "#{lib_path('one-1.0.0')}"
+ path "#{lib_path('two-1.0.0')}"
+ path "#{lib_path('three-1.0.0')}"
+ gem "one", :group => "bar", :require => "baz"
+ gem "two", :group => "bar"
+ gem "three", :group => "not"
+ G
+ end
+
+ it "requires the gems" do
+ run "Bundler.require('bar')"
+ out.should == "WIN\nWIN"
+ end
+
+ it "requires the locked gems" do
+ bundle :lock
+
+ env = bundled_app(".bundle/environment.rb")
+ out = ruby("require '#{env}'; Bundler.setup('bar'); Bundler.require('bar')")
+ out.should == "WIN\nWIN"
+ end
+end

0 comments on commit a8cbf77

Please sign in to comment.
Something went wrong with that request. Please try again.