From bb43e6892c25570605ddb183364e5690c3b22708 Mon Sep 17 00:00:00 2001 From: evan Date: Tue, 20 Jul 2010 18:30:46 +0000 Subject: [PATCH] Pull rubygem's custom require into gem_prelude This solves the gem loading issue by never touching $LOAD_PATH in gem_prelude and instead loading all of rubygems more quickly. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28693 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 ++ gem_prelude.rb | 112 +++++---------------------------- lib/rubygems.rb | 16 ++++- lib/rubygems/custom_require.rb | 14 ++--- test/ruby/test_rubyoptions.rb | 4 +- 5 files changed, 40 insertions(+), 111 deletions(-) diff --git a/ChangeLog b/ChangeLog index 62c95dd108..867f4d07c3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Jul 20 11:27:18 2010 Evan Phoenix + + * gem_prelude.rb: Pull in rubygem's custom require + * lib/rubygems.rb: Handle always having custom require available + Tue Jul 20 18:39:18 2010 Nobuyoshi Nakada * include/ruby/ruby.h (RSTRING_GETMEM): new macro to get ptr and diff --git a/gem_prelude.rb b/gem_prelude.rb index c51f07ef08..700072b8ed 100644 --- a/gem_prelude.rb +++ b/gem_prelude.rb @@ -13,7 +13,8 @@ if defined?(Gem) then module Kernel def gem(gem_name, *version_requirements) - Gem.push_gem_version_on_load_path(gem_name, *version_requirements) + Gem::QuickLoader.load_full_rubygems_library + gem gem_name, *version_requirements end private :gem end @@ -143,10 +144,11 @@ if defined?(Gem) then class << Gem undef_method(*Gem::GEM_PRELUDE_METHODS) - undef_method :const_missing - undef_method :method_missing end + remove_method :const_missing + remove_method :method_missing + Kernel.module_eval do undef_method :gem if method_defined? :gem end @@ -164,11 +166,6 @@ if defined?(Gem) then require 'rubygems' end - def self.fake_rubygems_as_loaded - path = path_to_full_rubygems_library - $" << path unless $".include?(path) - end - def self.path_to_full_rubygems_library installed_path = File.join(Gem::ConfigMap[:rubylibprefix], Gem::ConfigMap[:ruby_version]) if $:.include?(installed_path) @@ -183,94 +180,6 @@ if defined?(Gem) then end end - GemPaths = {} - GemVersions = {} - GemLoadPaths = [] - - def push_gem_version_on_load_path(gem_name, *version_requirements) - if version_requirements.empty? - unless GemPaths.has_key?(gem_name) then - raise Gem::LoadError, "Could not find RubyGem #{gem_name} (>= 0)\n" - end - - # highest version gems already active - return false - else - if version_requirements.length > 1 then - QuickLoader.load_full_rubygems_library - return gem(gem_name, *version_requirements) - end - - requirement, version = version_requirements[0].split - requirement.strip! - - if loaded_version = GemVersions[gem_name] then - case requirement - when ">", ">=" then - return false if - (loaded_version <=> Gem.integers_for(version)) >= 0 - when "~>" then - required_version = Gem.integers_for version - - return false if loaded_version.first == required_version.first - end - end - - QuickLoader.load_full_rubygems_library - gem gem_name, *version_requirements - end - end - - def integers_for(gem_version) - numbers = gem_version.split(".").collect {|n| n.to_i} - numbers.pop while numbers.last == 0 - numbers << 0 if numbers.empty? - numbers - end - - def push_all_highest_version_gems_on_load_path - Gem.path.each do |path| - gems_directory = File.join(path, "gems") - - if File.exist?(gems_directory) then - Dir.entries(gems_directory).each do |gem_directory_name| - next if gem_directory_name == "." || gem_directory_name == ".." - - next unless gem_name = gem_directory_name[/(.*)-(.*)/, 1] - new_version = integers_for($2) - current_version = GemVersions[gem_name] - - if !current_version or (current_version <=> new_version) < 0 then - GemVersions[gem_name] = new_version - GemPaths[gem_name] = File.join(gems_directory, gem_directory_name) - end - end - end - end - - GemPaths.each_value do |path| - if File.exist?(file = File.join(path, ".require_paths")) then - paths = File.read(file).split.map do |require_path| - File.join path, require_path - end - - GemLoadPaths.concat paths - else - GemLoadPaths << file if File.exist?(file = File.join(path, "bin")) - GemLoadPaths << file if File.exist?(file = File.join(path, "lib")) - end - end - - # "tag" the first require_path inserted into the $LOAD_PATH to enable - # indexing correctly with rubygems proper when it inserts an explicitly - # gem version - unless GemLoadPaths.empty? then - GemLoadPaths.first.instance_variable_set(:@gem_prelude_index, true) - end - # gem directories must come after -I and ENV['RUBYLIB'] - $:[$:.index{|e|e.instance_variable_defined?(:@gem_prelude_index)}||-1,0] = GemLoadPaths - end - def const_missing(constant) QuickLoader.load_full_rubygems_library @@ -290,10 +199,19 @@ if defined?(Gem) then extend QuickLoader + def self.try_activate(path) + # Just a stub to make sure rubygems is loaded + QuickLoader.load_full_rubygems_library + + # But doesn't actually load anything, so that custom_require + # can always call try_activate and get some decent response + return false + end + end begin - Gem.push_all_highest_version_gems_on_load_path + require 'lib/rubygems/custom_require.rb' rescue Exception => e puts "Error loading gem paths on load path in gem_prelude" puts e diff --git a/lib/rubygems.rb b/lib/rubygems.rb index 51661159cc..42e189ed4a 100644 --- a/lib/rubygems.rb +++ b/lib/rubygems.rb @@ -1031,7 +1031,7 @@ end module Kernel - undef gem if respond_to? :gem # defined in gem_prelude.rb on 1.9 + remove_method :gem if respond_to?(:gem, true) # defined in gem_prelude.rb on 1.9 ## # Use Kernel#gem to activate a specific version of +gem_name+. @@ -1109,14 +1109,24 @@ end require 'rubygems/config_file' -Gem.remove_prelude_paths +class << Gem + remove_method :try_activate if Gem.respond_to?(:try_activate, true) + + def try_activate(path) + spec = Gem.searcher.find(path) + return false unless spec + + Gem.activate(spec.name, "= #{spec.version}") + return true + end +end ## # Enables the require hook for RubyGems. # # We remove the paths prelude added, so we need custom require to get # any gems now. -require 'rubygems/custom_require' +# require 'rubygems/custom_require' Gem.clear_paths diff --git a/lib/rubygems/custom_require.rb b/lib/rubygems/custom_require.rb index 43b3136b60..a410a3cfe0 100644 --- a/lib/rubygems/custom_require.rb +++ b/lib/rubygems/custom_require.rb @@ -4,8 +4,6 @@ # See LICENSE.txt for permissions. #++ -require 'rubygems' - module Kernel ## @@ -30,17 +28,15 @@ module Kernel def require(path) # :doc: gem_original_require path rescue LoadError => load_error - if load_error.message.end_with?(path) and - spec = Gem.searcher.find(path) then - Gem.activate(spec.name, "= #{spec.version}") - gem_original_require path - else - raise load_error + if load_error.message.end_with?(path) + return true if Gem.try_activate(path) end + + raise load_error end private :require private :gem_original_require -end +end unless Kernel.private_method_defined?(:gem_original_require) diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb index e41bdf2936..828a1225ab 100644 --- a/test/ruby/test_rubyoptions.rb +++ b/test/ruby/test_rubyoptions.rb @@ -323,8 +323,8 @@ class TestRubyOptions < Test::Unit::TestCase pat = Regexp.quote(notexist) bug1573 = '[ruby-core:23717]' assert_equal(false, File.exist?(notexist)) - assert_in_out_err(["-r", notexist, "-ep"], "", [], /\A-e:.* -- #{pat} \(LoadError\)\Z/, bug1573) - assert_in_out_err([notexist], "", [], /\A#{rubybin}:.* -- #{pat} \(LoadError\)\Z/, bug1573) + assert_in_out_err(["-r", notexist, "-ep"], "", [], /.* -- #{pat} \(LoadError\)/, bug1573) + assert_in_out_err([notexist], "", [], /#{rubybin}:.* -- #{pat} \(LoadError\)/, bug1573) end def test_program_name