3 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
5 # See LICENSE.txt for permissions.
8 require 'rubygems/rubygems_version'
9 require 'rubygems/defaults'
13 class LoadError < ::LoadError
14 attr_accessor :name, :version_requirement
21 # Use Kernel#gem to activate a specific version of +gem_name+.
23 # +version_requirements+ is a list of version requirements that the
24 # specified gem must match, most commonly "= example.version.number". See
25 # Gem::Requirement for how to specify a version requirement.
27 # If you will be activating the latest version of a gem, there is no need to
28 # call Kernel#gem, Kernel#require will do the right thing for you.
30 # Kernel#gem returns true if the gem was activated, otherwise false. If the
31 # gem could not be found, didn't match the version requirements, or a
32 # different version was already activated, an exception will be raised.
34 # Kernel#gem should be called *before* any require statements (otherwise
35 # RubyGems may load a conflicting library version).
37 # In older RubyGems versions, the environment variable GEM_SKIP could be
38 # used to skip activation of specified gems, for example to test out changes
39 # that haven't been installed yet. Now RubyGems defers to -I and the
40 # RUBYLIB environment variable to skip activation of a gem.
44 # GEM_SKIP=libA:libB ruby -I../libA -I../libB ./mycode.rb
46 def gem(gem_name, *version_requirements)
47 skip_list = (ENV['GEM_SKIP'] || "").split(/:/)
48 raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name
49 Gem.activate(gem_name, *version_requirements)
55 # Main module to hold all RubyGem classes/modules.
59 ConfigMap = {} unless defined?(ConfigMap)
61 RbConfig = Config unless defined? ::RbConfig
64 :BASERUBY => RbConfig::CONFIG["BASERUBY"],
65 :EXEEXT => RbConfig::CONFIG["EXEEXT"],
66 :RUBY_INSTALL_NAME => RbConfig::CONFIG["RUBY_INSTALL_NAME"],
67 :RUBY_SO_NAME => RbConfig::CONFIG["RUBY_SO_NAME"],
68 :arch => RbConfig::CONFIG["arch"],
69 :bindir => RbConfig::CONFIG["bindir"],
70 :datadir => RbConfig::CONFIG["datadir"],
71 :libdir => RbConfig::CONFIG["libdir"],
72 :ruby_install_name => RbConfig::CONFIG["ruby_install_name"],
73 :ruby_version => RbConfig::CONFIG["ruby_version"],
74 :sitedir => RbConfig::CONFIG["sitedir"],
75 :sitelibdir => RbConfig::CONFIG["sitelibdir"],
76 :vendordir => RbConfig::CONFIG["vendordir"] ,
77 :vendorlibdir => RbConfig::CONFIG["vendorlibdir"]
80 DIRECTORIES = %w[cache doc gems specifications] unless defined?(DIRECTORIES)
84 RubyGemsPackageVersion = RubyGemsVersion
87 # An Array of Regexps that match windows ruby platforms.
108 # Activates an installed gem matching +gem+. The gem must satisfy
109 # +version_requirements+.
111 # Returns true if the gem is activated, false if it is already
112 # loaded, or an exception otherwise.
114 # Gem#activate adds the library paths in +gem+ to $LOAD_PATH. Before a Gem
115 # is activated its required Gems are activated. If the version information
116 # is omitted, the highest version Gem of the supplied name is loaded. If a
117 # Gem is not found that meets the version requirements or a required Gem is
118 # not found, a Gem::LoadError is raised.
120 # More information on version requirements can be found in the
121 # Gem::Requirement and Gem::Version documentation.
123 def self.activate(gem, *version_requirements)
124 if version_requirements.empty? then
125 version_requirements = Gem::Requirement.default
128 unless gem.respond_to?(:name) and
129 gem.respond_to?(:version_requirements) then
130 gem = Gem::Dependency.new(gem, version_requirements)
133 matches = Gem.source_index.find_name(gem.name, gem.version_requirements)
134 report_activate_error(gem) if matches.empty?
136 if @loaded_specs[gem.name] then
137 # This gem is already loaded. If the currently loaded gem is not in the
138 # list of candidate gems, then we have a version conflict.
139 existing_spec = @loaded_specs[gem.name]
141 unless matches.any? { |spec| spec.version == existing_spec.version } then
142 raise Gem::Exception,
143 "can't activate #{gem}, already activated #{existing_spec.full_name}"
151 return false if spec.loaded?
154 @loaded_specs[spec.name] = spec
156 # Load dependent gems first
157 spec.runtime_dependencies.each do |dep_gem|
161 # bin directory must come before library directories
162 spec.require_paths.unshift spec.bindir if spec.bindir
164 require_paths = spec.require_paths.map do |path|
165 File.join spec.full_gem_path, path
168 sitelibdir = ConfigMap[:sitelibdir]
170 # gem directories must come after -I and ENV['RUBYLIB']
171 insert_index = load_path_insert_index
174 # gem directories must come after -I and ENV['RUBYLIB']
175 $LOAD_PATH.insert(insert_index, *require_paths)
177 # we are probably testing in core, -I and RUBYLIB don't apply
178 $LOAD_PATH.unshift(*require_paths)
185 # An Array of all possible load paths for all versions of all gems in the
188 def self.all_load_paths
191 Gem.path.each do |gemdir|
192 each_load_path all_partials(gemdir) do |load_path|
201 # Return all the partial paths in +gemdir+.
203 def self.all_partials(gemdir)
204 Dir[File.join(gemdir, 'gems/*')]
207 private_class_method :all_partials
210 # See if a given gem is available.
212 def self.available?(gem, *requirements)
213 requirements = Gem::Requirement.default if requirements.empty?
215 unless gem.respond_to?(:name) and
216 gem.respond_to?(:version_requirements) then
217 gem = Gem::Dependency.new gem, requirements
220 !Gem.source_index.search(gem).empty?
224 # The mode needed to read a file as straight binary.
227 @binary_mode ||= RUBY_VERSION > '1.9' ? 'rb:ascii-8bit' : 'rb'
231 # The path where gem executables are to be installed.
233 def self.bindir(install_dir=Gem.dir)
234 return File.join(install_dir, 'bin') unless
235 install_dir.to_s == Gem.default_dir
240 # Reset the +dir+ and +path+ values. The next time +dir+ or +path+
241 # is requested, the values will be calculated from scratch. This is
242 # mainly used by the unit tests to provide test isolation.
254 # The path to standard location of the user's .gemrc file.
257 File.join Gem.user_home, '.gemrc'
261 # The standard configuration object for gems.
263 def self.configuration
264 return @configuration if @configuration
265 require 'rubygems/config_file'
266 @configuration = Gem::ConfigFile.new []
270 # Use the given configuration object (which implements the ConfigFile
271 # protocol) as the standard configuration object.
273 def self.configuration=(config)
274 @configuration = config
278 # The path the the data directory specified by the gem name. If the
279 # package is not available as a gem, return nil.
281 def self.datadir(gem_name)
282 spec = @loaded_specs[gem_name]
283 return nil if spec.nil?
284 File.join(spec.full_gem_path, 'data', gem_name)
288 # A Zlib::Deflate.deflate wrapper
290 def self.deflate(data)
291 Zlib::Deflate.deflate data
295 # The path where gems are to be installed.
299 set_home(ENV['GEM_HOME'] || default_dir) unless @gem_home
304 # Expand each partial gem path with each of the required paths specified
305 # in the Gem spec. Each expanded path is yielded.
307 def self.each_load_path(partials)
308 partials.each do |gp|
309 base = File.basename(gp)
310 specfn = File.join(dir, "specifications", base + ".gemspec")
311 if File.exist?(specfn)
312 spec = eval(File.read(specfn))
313 spec.require_paths.each do |rp|
314 yield(File.join(gp, rp))
317 filename = File.join(gp, 'lib')
318 yield(filename) if File.exist?(filename)
323 private_class_method :each_load_path
326 # Quietly ensure the named Gem directory contains all the proper
327 # subdirectories. If we can't create a directory due to a permission
328 # problem, then we will silently continue.
330 def self.ensure_gem_subdirectories(gemdir)
333 Gem::DIRECTORIES.each do |filename|
334 fn = File.join gemdir, filename
335 FileUtils.mkdir_p fn rescue nil unless File.exist? fn
340 # Finds the user's home directory.
342 # Some comments from the ruby-talk list regarding finding the home
345 # I have HOME, USERPROFILE and HOMEDRIVE + HOMEPATH. Ruby seems
346 # to be depending on HOME in those code samples. I propose that
347 # it should fallback to USERPROFILE and HOMEDRIVE + HOMEPATH (at
351 ['HOME', 'USERPROFILE'].each do |homekey|
352 return ENV[homekey] if ENV[homekey]
355 if ENV['HOMEDRIVE'] && ENV['HOMEPATH'] then
356 return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
360 File.expand_path("~")
362 if File::ALT_SEPARATOR then
370 private_class_method :find_home
373 # Zlib::GzipReader wrapper that unzips +data+.
375 def self.gunzip(data)
376 data = StringIO.new data
378 Zlib::GzipReader.new(data).read
382 # Zlib::GzipWriter wrapper that zips +data+.
385 zipped = StringIO.new
387 Zlib::GzipWriter.wrap zipped do |io| io.write data end
393 # A Zlib::Inflate#inflate wrapper
395 def self.inflate(data)
396 Zlib::Inflate.inflate data
400 # Return a list of all possible load paths for the latest version for all
401 # gems in the Gem installation.
403 def self.latest_load_paths
406 Gem.path.each do |gemdir|
407 each_load_path(latest_partials(gemdir)) do |load_path|
416 # Return only the latest partial paths in the given +gemdir+.
418 def self.latest_partials(gemdir)
420 all_partials(gemdir).each do |gp|
421 base = File.basename(gp)
422 if base =~ /(.*)-((\d+\.)*\d+)/ then
423 name, version = $1, $2
424 ver = Gem::Version.new(version)
425 if latest[name].nil? || ver > latest[name][0]
426 latest[name] = [ver, gp]
430 latest.collect { |k,v| v[1] }
433 private_class_method :latest_partials
436 # The index to insert activated gem paths into the $LOAD_PATH.
438 # Defaults to the site lib directory unless gem_prelude.rb has loaded paths,
439 # then it inserts the activated gem's paths before the gem_prelude.rb paths
440 # so you can override the gem_prelude.rb default $LOAD_PATH paths.
442 def self.load_path_insert_index
443 index = $LOAD_PATH.index ConfigMap[:sitelibdir]
445 $LOAD_PATH.each_with_index do |path, i|
446 if path.instance_variables.include?(:@gem_prelude_index) or
447 path.instance_variables.include?('@gem_prelude_index') then
457 # The file name and line number of the caller of the caller of this method.
459 def self.location_of_caller
460 file, lineno = caller[1].split(':')
465 private_class_method :location_of_caller
468 # manage_gems is useless and deprecated. Don't call it anymore.
470 # TODO warn w/ RubyGems 1.2.x release.
473 #file, lineno = location_of_caller
475 #warn "#{file}:#{lineno}:Warning: Gem#manage_gems is deprecated and will be removed on or after September 2008."
479 # The version of the Marshal format for your Ruby.
481 def self.marshal_version
482 "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}"
486 # Array of paths to search for Gems.
491 unless @gem_path then
492 paths = if ENV['GEM_PATH'] then
498 if defined?(APPLE_GEM_HOME) and not ENV['GEM_PATH'] then
499 paths << APPLE_GEM_HOME
502 set_paths paths.compact.join(File::PATH_SEPARATOR)
509 # Set array of platforms this RubyGems supports (primarily for testing).
511 def self.platforms=(platforms)
512 @platforms = platforms
516 # Array of platforms this RubyGems supports.
521 @platforms = [Gem::Platform::RUBY, Gem::Platform.local]
527 # The directory prefix this RubyGems was installed at.
530 prefix = File.dirname File.expand_path(__FILE__)
532 if File.dirname(prefix) == File.expand_path(ConfigMap[:sitelibdir]) or
533 File.dirname(prefix) == File.expand_path(ConfigMap[:libdir]) or
534 'lib' != File.basename(prefix) then
542 # Refresh source_index from disk and clear searcher.
545 source_index.refresh!
553 # Safely read a file in binary mode on all platforms.
555 def self.read_binary(path)
556 File.open path, binary_mode do |f| f.read end
560 # Report a load error during activation. The message of load error
561 # depends on whether it was a version mismatch or if there are not gems of
562 # any version by the requested name.
564 def self.report_activate_error(gem)
565 matches = Gem.source_index.find_name(gem.name)
567 if matches.empty? then
568 error = Gem::LoadError.new(
569 "Could not find RubyGem #{gem.name} (#{gem.version_requirements})\n")
571 error = Gem::LoadError.new(
572 "RubyGem version error: " +
573 "#{gem.name}(#{matches.first.version} not #{gem.version_requirements})\n")
576 error.name = gem.name
577 error.version_requirement = gem.version_requirements
581 private_class_method :report_activate_error
583 def self.required_location(gemname, libfile, *version_constraints)
584 version_constraints = Gem::Requirement.default if version_constraints.empty?
585 matches = Gem.source_index.find_name(gemname, version_constraints)
586 return nil if matches.empty?
588 spec.require_paths.each do |path|
589 result = File.join(spec.full_gem_path, path, libfile)
590 return result if File.exist?(result)
596 # The path to the running Ruby interpreter.
600 @ruby = File.join(ConfigMap[:bindir],
601 ConfigMap[:ruby_install_name])
602 @ruby << ConfigMap[:EXEEXT]
609 # A Gem::Version for the currently running ruby.
611 def self.ruby_version
612 return @ruby_version if defined? @ruby_version
613 version = RUBY_VERSION.dup
614 version << ".#{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
615 @ruby_version = Gem::Version.new version
619 # The GemPathSearcher object used to search for matching installed gems.
623 @searcher ||= Gem::GemPathSearcher.new
628 # Set the Gem home directory (as reported by Gem.dir).
630 def self.set_home(home)
631 home = home.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
633 ensure_gem_subdirectories(@gem_home)
636 private_class_method :set_home
639 # Set the Gem search path (as reported by Gem.path).
641 def self.set_paths(gpaths)
643 @gem_path = gpaths.split(File::PATH_SEPARATOR)
645 if File::ALT_SEPARATOR then
646 @gem_path.map! do |path|
647 path.gsub File::ALT_SEPARATOR, File::SEPARATOR
653 # TODO: should this be Gem.default_path instead?
654 @gem_path = [Gem.dir]
658 @gem_path.each do |gp| ensure_gem_subdirectories(gp) end
661 private_class_method :set_paths
664 # Returns the Gem::SourceIndex of specifications that are in the Gem.path
666 def self.source_index
667 @@source_index ||= SourceIndex.from_installed_gems
671 # Returns an Array of sources to fetch remote gems from. If the sources
672 # list is empty, attempts to load the "sources" gem, then uses
673 # default_sources if it is not installed.
676 if @sources.empty? then
678 gem 'sources', '> 0.0.1'
681 @sources = default_sources
689 # Glob pattern for require-able path suffixes.
691 def self.suffix_pattern
692 @suffix_pattern ||= "{#{suffixes.join(',')}}"
696 # Suffixes for require-able paths.
699 ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar']
703 # Use the +home+ and +paths+ values for Gem.dir and Gem.path. Used mainly
704 # by the unit tests to provide environment isolation.
706 def self.use_paths(home, paths=[])
708 set_home(home) if home
709 set_paths(paths.join(File::PATH_SEPARATOR)) if paths
713 # The home directory for the user.
716 @user_home ||= find_home
720 # Is this a windows platform?
722 def self.win_platform?
723 if @@win_platform.nil? then
724 @@win_platform = !!WIN_PATTERNS.find { |r| RUBY_PLATFORM =~ r }
732 attr_reader :loaded_specs
736 alias cache source_index # an alias for the old name
742 MARSHAL_SPEC_DIR = "quick/Marshal.#{Gem.marshal_version}/"
744 YAML_SPEC_DIR = 'quick/'
751 # Return the path to the data directory associated with the named
752 # package. If the package is loaded as a gem, return the gem
753 # specific data directory. Otherwise return a path to the share
754 # area as define by "#{ConfigMap[:datadir]}/#{package_name}".
755 def datadir(package_name)
756 Gem.datadir(package_name) ||
757 File.join(Gem::ConfigMap[:datadir], package_name)
763 require 'rubygems/exceptions'
764 require 'rubygems/version'
765 require 'rubygems/requirement'
766 require 'rubygems/dependency'
767 require 'rubygems/gem_path_searcher' # Needed for Kernel#gem
768 require 'rubygems/source_index' # Needed for Kernel#gem
769 require 'rubygems/platform'
770 require 'rubygems/builder' # HACK: Needed for rake's package task.
773 require 'rubygems/defaults/operating_system'
777 if defined?(RUBY_ENGINE) then
779 require "rubygems/defaults/#{RUBY_ENGINE}"
784 if RUBY_VERSION < '1.9' then
785 require 'rubygems/custom_require'