1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/gem_prelude.rb
nobu 2b322524cd * gem_prelude.rb: load full rubygems at LoadError for activation
check.  [ruby-core:29486]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28200 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-06-07 21:27:16 +00:00

214 lines
5.4 KiB
Ruby

# vim: filetype=ruby
# NOTICE: Ruby is during initialization here.
# * Encoding.default_external does not reflects -E.
# * Should not expect Encoding.default_internal.
# * Locale encoding is available.
if defined?(Gem) then
# :stopdoc:
module Kernel
def gem(gem_name, *version_requirements)
Gem::QuickLoader.load_full_rubygems_library
gem gem_name, *version_requirements
end
private :gem
end
module Gem
ConfigMap = {
:EXEEXT => RbConfig::CONFIG["EXEEXT"],
:RUBY_SO_NAME => RbConfig::CONFIG["RUBY_SO_NAME"],
:arch => RbConfig::CONFIG["arch"],
:bindir => RbConfig::CONFIG["bindir"],
:libdir => RbConfig::CONFIG["libdir"],
:ruby_install_name => RbConfig::CONFIG["ruby_install_name"],
:ruby_version => RbConfig::CONFIG["ruby_version"],
:rubylibprefix => RbConfig::CONFIG["rubylibprefix"],
:sitedir => RbConfig::CONFIG["sitedir"],
:sitelibdir => RbConfig::CONFIG["sitelibdir"],
}
def self.dir
@gem_home ||= nil
set_home(ENV['GEM_HOME'] || default_dir) unless @gem_home
@gem_home
end
def self.path
@gem_path ||= nil
unless @gem_path
paths = [ENV['GEM_PATH'] || default_path]
paths << APPLE_GEM_HOME if defined? APPLE_GEM_HOME
set_paths(paths.compact.join(File::PATH_SEPARATOR))
end
@gem_path
end
def self.post_install(&hook)
@post_install_hooks << hook
end
def self.post_uninstall(&hook)
@post_uninstall_hooks << hook
end
def self.pre_install(&hook)
@pre_install_hooks << hook
end
def self.pre_uninstall(&hook)
@pre_uninstall_hooks << hook
end
def self.set_home(home)
home = home.dup.force_encoding(Encoding.find('filesystem'))
home.gsub!(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
@gem_home = home
end
def self.set_paths(gpaths)
if gpaths
@gem_path = gpaths.split(File::PATH_SEPARATOR)
if File::ALT_SEPARATOR then
@gem_path.map! do |path|
path.gsub File::ALT_SEPARATOR, File::SEPARATOR
end
end
@gem_path << Gem.dir
else
# TODO: should this be Gem.default_path instead?
@gem_path = [Gem.dir]
end
@gem_path.uniq!
@gem_path.map!{|x|x.force_encoding(Encoding.find('filesystem'))}
end
def self.user_home
@user_home ||= File.expand_path("~").force_encoding(Encoding.find('filesystem'))
rescue
if File::ALT_SEPARATOR then
"C:/"
else
"/"
end
end
# begin rubygems/defaults
# NOTE: this require will be replaced with in-place eval before compilation.
require 'lib/rubygems/defaults.rb'
# end rubygems/defaults
##
# Methods before this line will be removed when QuickLoader is replaced
# with the real RubyGems
GEM_PRELUDE_METHODS = Gem.methods(false)
begin
verbose, debug = $VERBOSE, $DEBUG
$VERBOSE = $DEBUG = nil
begin
require 'rubygems/defaults/operating_system'
rescue ::LoadError
end
if defined?(RUBY_ENGINE) then
begin
require "rubygems/defaults/#{RUBY_ENGINE}"
rescue ::LoadError
end
end
ensure
$VERBOSE, $DEBUG = verbose, debug
end
module QuickLoader
def self.load_full_rubygems_library
return @full_rubygems_library if defined?(@full_rubygems_library)
@full_rubygems_library = false
class << Gem
undef_method(*Gem::GEM_PRELUDE_METHODS)
end
$".delete path_to_full_rubygems_library
if $".any? {|path| path.end_with?('/rubygems.rb')}
raise LoadError, "another rubygems is already loaded from #{path}"
end
verbose, debug = $VERBOSE, $DEBUG
$VERBOSE = $DEBUG = nil
require 'rubygems'
@full_rubygems_library = true
ensure
$VERBOSE, $DEBUG = verbose, debug
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)
return File.join(installed_path, 'rubygems.rb')
else # e.g., on test-all
$:.each do |dir|
if File.exist?( path = File.join(dir, 'rubygems.rb') )
return path
end
end
raise LoadError, 'rubygems.rb'
end
end
def const_missing(constant)
QuickLoader.load_full_rubygems_library
if Gem.const_defined?(constant) then
Gem.const_get constant
else
super
end
end
def method_missing(method, *args, &block)
QuickLoader.load_full_rubygems_library
super unless Gem.respond_to?(method)
Gem.send(method, *args, &block)
end
end
extend QuickLoader
def self.try_activate(path) # :nodoc:
QuickLoader.load_full_rubygems_library
end
end
begin
require 'lib/rubygems/custom_require.rb'
Gem::QuickLoader.fake_rubygems_as_loaded
rescue Exception => e
puts "Error loading gem paths on load path in gem_prelude"
puts e
puts e.backtrace.join("\n")
end
end