Permalink
Browse files

Support loading the default gems automatically

The default gems should be placed at
#{GEM_HOME}/specifications/default/*.gemspec.
Is it right place? Should we use 'defaults' instead of 'default'?
  • Loading branch information...
1 parent dabde2d commit 66b70bbd77b6b9127cec0cfc4ffbb41d377def2c @kou committed Oct 13, 2012
Showing with 50 additions and 12 deletions.
  1. +4 −1 lib/rubygems.rb
  2. +34 −9 lib/rubygems/specification.rb
  3. +10 −0 lib/rubygems/test_case.rb
  4. +2 −2 test/rubygems/test_require.rb
View
@@ -940,7 +940,6 @@ class << self
# Register a Gem::Specification for default gem
def register_default_spec(spec)
- Specification.add_spec(spec)
spec.files.each do |file|
@path_to_default_spec_map[file] = spec
end
@@ -1064,6 +1063,10 @@ def clear_default_specs
end
end
+##
+# Loads the default specs.
+Gem::Specification.load_defaults
+
require 'rubygems/core_ext/kernel_gem'
require 'rubygems/core_ext/kernel_require'
@@ -611,19 +611,35 @@ def test_files= files
attr_accessor :specification_version
+ def self._each_spec(gemspec_glob) # :nodoc:
+ self.dirs.each { |dir|
+ Dir[File.join(dir, gemspec_glob)].each { |path|
+ spec = Gem::Specification.load path.untaint
+ # #load returns nil if the spec is bad, so we just ignore
+ # it at this stage
+ yield(spec) if spec
+ }
+ }
+ end
+
+ def self._each_default(&block) # :nodoc:
+ _each_spec(File.join("default", "*.gemspec"), &block)
+ end
+
+ def self._each_normal(&block) # :nodoc:
+ _each_spec(File.join("*.gemspec"), &block)
+ end
+
def self._all # :nodoc:
unless defined?(@@all) && @@all then
specs = {}
-
- self.dirs.each { |dir|
- Dir[File.join(dir, "*.gemspec")].each { |path|
- spec = Gem::Specification.load path.untaint
- # #load returns nil if the spec is bad, so we just ignore
- # it at this stage
- specs[spec.full_name] ||= spec if spec
- }
- }
+ _each_default do |spec|
+ specs[spec.full_name] ||= spec
+ end
+ _each_normal do |spec|
+ specs[spec.full_name] ||= spec
+ end
@@all = specs.values
@@ -641,6 +657,15 @@ def self._resort! # :nodoc:
end
##
+ # Loads the default specifications. It should be called only once.
+
+ def self.load_defaults
+ _each_default do |spec|
+ Gem.register_default_spec(spec)
+ end
+ end
+
+ ##
# Adds +spec+ to the known specifications, keeping the collection
# properly sorted.
View
@@ -454,6 +454,16 @@ def install_specs(*specs)
end
##
+ # Install the provided default specs
+
+ def install_default_specs(*specs)
+ install_specs(*specs)
+ specs.each do |spec|
+ Gem.register_default_spec(spec)
+ end
+ end
+
+ ##
# Create a new spec (or gem if passed an array of files) and set it
# up properly. Use this instead of util_spec and util_gem.
@@ -156,7 +156,7 @@ def test_default_gem_only
save_loaded_features do
default_gem_spec = new_default_spec("default", "2.0.0.0",
nil, "default/gem.rb")
- Gem.register_default_spec(default_gem_spec)
+ install_default_specs(default_gem_spec)
assert_require "default/gem"
assert_equal %w(default-2.0.0.0), loaded_spec_names
end
@@ -166,7 +166,7 @@ def test_default_gem_and_normal_gem
save_loaded_features do
default_gem_spec = new_default_spec("default", "2.0.0.0",
nil, "default/gem.rb")
- Gem.register_default_spec(default_gem_spec)
+ install_default_specs(default_gem_spec)
normal_gem_spec = new_spec("default", "3.0", nil,
"lib/default/gem.rb")
install_specs(normal_gem_spec)

0 comments on commit 66b70bb

Please sign in to comment.