1 # depends on: array.rb dir.rb env.rb file.rb hash.rb module.rb regexp.rb
7 if defined?(Gem::Enable) && Gem::Enable then
11 def gem(gem_name, *version_requirements)
12 Gem.push_gem_version_on_load_path(gem_name, *version_requirements)
20 :sitedir => RbConfig::CONFIG["sitedir"],
21 :ruby_version => RbConfig::CONFIG["ruby_version"],
22 :libdir => RbConfig::CONFIG["libdir"],
23 :sitelibdir => RbConfig::CONFIG["sitelibdir"],
24 :arch => RbConfig::CONFIG["arch"],
25 :bindir => RbConfig::CONFIG["bindir"],
26 :EXEEXT => RbConfig::CONFIG["EXEEXT"],
27 :RUBY_SO_NAME => RbConfig::CONFIG["RUBY_SO_NAME"],
28 :ruby_install_name => RbConfig::CONFIG["ruby_install_name"]
34 if defined? RUBY_FRAMEWORK_VERSION
35 return File.join(File.dirname(ConfigMap[:sitedir]), "Gems")
37 File.join(ConfigMap[:libdir], 'ruby', 'gems', ConfigMap[:ruby_version])
43 set_home(ENV['GEM_HOME'] || default_dir) unless @gem_home
50 paths = [ENV['GEM_PATH']]
51 paths << APPLE_GEM_HOME if defined? APPLE_GEM_HOME
52 set_paths(paths.compact.join(File::PATH_SEPARATOR))
57 # Set the Gem home directory (as reported by +dir+).
60 ensure_gem_subdirectories(@gem_home)
65 @gem_path = gpaths.split(File::PATH_SEPARATOR)
71 @gem_path.each do |gp| ensure_gem_subdirectories(gp) end
74 def ensure_gem_subdirectories(path)
82 def load_full_rubygems_library
84 Gem.methods(false).each do |method_name|
85 undef_method method_name
89 Kernel.send :undef_method, :gem
91 $".delete File.join(Gem::ConfigMap[:libdir], 'ruby',
92 Gem::ConfigMap[:ruby_version], 'rubygems.rb')
101 def push_gem_version_on_load_path(gem_name, *version_requirements)
102 if version_requirements.empty?
103 unless GemPaths.has_key?(gem_name)
104 raise LoadError.new("Could not find RubyGem #{gem_name} (>= 0)\n")
107 # highest version gems already active
110 if version_requirements.length > 1
111 QuickLoader.load_full_rubygems_library
112 return gem(gem_name, *version_requirements)
115 requirement, version = version_requirements[0].split
118 if requirement == ">" || requirement == ">="
119 if (GemVersions[gem_name] <=> Gem.calculate_integers_for_gem_version(version)) >= 0
122 elsif requirement == "~>"
123 loaded_version = GemVersions[gem_name]
124 required_version = Gem.calculate_integers_for_gem_version(version)
125 if loaded_version && (loaded_version[0] == required_version[0])
130 QuickLoader.load_full_rubygems_library
131 gem(gem_name, *version_requirements)
135 def calculate_integers_for_gem_version(gem_version)
136 numbers = gem_version.split(".").collect {|n| n.to_i}
137 numbers.pop while numbers.last == 0
138 numbers << 0 if numbers.empty?
142 def push_all_highest_version_gems_on_load_path
143 Gem.path.each do |path|
144 gems_directory = File.join(path, "gems")
145 if File.exist?(gems_directory)
146 Dir.entries(gems_directory).each do |gem_directory_name|
147 next if gem_directory_name == "." || gem_directory_name == ".."
148 dash = gem_directory_name.rindex("-")
150 gem_name = gem_directory_name[0...dash]
151 current_version = GemVersions[gem_name]
152 new_version = calculate_integers_for_gem_version(gem_directory_name[dash+1..-1])
154 if (current_version <=> new_version) == -1
155 GemVersions[gem_name] = new_version
156 GemPaths[gem_name] = File.join(gems_directory, gem_directory_name)
159 GemVersions[gem_name] = new_version
160 GemPaths[gem_name] = File.join(gems_directory, gem_directory_name)
168 GemPaths.values.each do |path|
169 if File.exist?(File.join(path, ".require_paths"))
170 require_paths.concat(File.read(File.join(path, ".require_paths")).split.map {|require_path| File.join(path, require_path)})
172 require_paths << File.join(path, "bin") if File.exist?(File.join(path, "bin"))
173 require_paths << File.join(path, "lib") if File.exist?(File.join(path, "lib"))
177 # "tag" the first require_path inserted into the $LOAD_PATH to enable
178 # indexing correctly with rubygems proper when it inserts an explicitly
180 unless require_paths.empty?
181 require_paths.first.instance_variable_set(:@gem_prelude_index, true)
183 # gem directories must come after -I and ENV['RUBYLIB']
184 $:[$:.index(ConfigMap[:sitelibdir]),0] = require_paths
187 def const_missing(constant)
188 QuickLoader.load_full_rubygems_library
189 Gem.const_get(constant)
192 def method_missing(method, *args, &block)
193 QuickLoader.load_full_rubygems_library
194 super unless Gem.respond_to?(method)
195 Gem.send(method, *args, &block)
204 Gem.push_all_highest_version_gems_on_load_path
205 $" << File.join(Gem::ConfigMap[:libdir], "ruby",
206 Gem::ConfigMap[:ruby_version], "rubygems.rb")
207 rescue Exception => e
208 puts "Error loading gem paths on load path in gem_prelude"
210 puts e.backtrace.join("\n")