Main module to hold all RubyGem classes/modules.
SHA1 | = | Digest::SHA1 | ||
SHA1 | = | DigestAdapter.new(Digest::SHA1) | ||
SHA256 | = | Digest::SHA256 | ||
SHA256 | = | DigestAdapter.new(Digest::SHA256) | ||
RubyGemsVersion | = | '1.3.1' | ||
ConfigMap | = | {} unless defined?(ConfigMap) | ||
RbConfig | = | Config unless defined? ::RbConfig | ||
DIRECTORIES | = | %w[cache doc gems specifications] unless defined?(DIRECTORIES) | ||
MUTEX | = | Mutex.new | ||
RubyGemsPackageVersion | = | RubyGemsVersion | ||
WIN_PATTERNS | = | [ /bccwin/i, /cygwin/i, /djgpp/i, /mingw/i, /mswin/i, /wince/i, ] | An Array of Regexps that match windows ruby platforms. | |
MARSHAL_SPEC_DIR | = | "quick/Marshal.#{Gem.marshal_version}/" | ||
YAML_SPEC_DIR | = | 'quick/' |
loaded_specs | [R] | |
post_install_hooks | [R] | The list of hooks to be run before Gem::Install#install does any work |
post_uninstall_hooks | [R] | The list of hooks to be run before Gem::Uninstall#uninstall does any work |
pre_install_hooks | [R] | The list of hooks to be run after Gem::Install#install is finished |
pre_uninstall_hooks | [R] | The list of hooks to be run after Gem::Uninstall#uninstall is finished |
ssl_available | [W] | Set the value of the ssl_available flag. |
Activates an installed gem matching gem. The gem must satisfy version_requirements.
Returns true if the gem is activated, false if it is already loaded, or an exception otherwise.
Gem#activate adds the library paths in gem to $LOAD_PATH. Before a Gem is activated its required Gems are activated. If the version information is omitted, the highest version Gem of the supplied name is loaded. If a Gem is not found that meets the version requirements or a required Gem is not found, a Gem::LoadError is raised.
More information on version requirements can be found in the Gem::Requirement and Gem::Version documentation.
# File lib/rubygems.rb, line 130 130: def self.activate(gem, *version_requirements) 131: if version_requirements.empty? then 132: version_requirements = Gem::Requirement.default 133: end 134: 135: unless gem.respond_to?(:name) and 136: gem.respond_to?(:version_requirements) then 137: gem = Gem::Dependency.new(gem, version_requirements) 138: end 139: 140: matches = Gem.source_index.find_name(gem.name, gem.version_requirements) 141: report_activate_error(gem) if matches.empty? 142: 143: if @loaded_specs[gem.name] then 144: # This gem is already loaded. If the currently loaded gem is not in the 145: # list of candidate gems, then we have a version conflict. 146: existing_spec = @loaded_specs[gem.name] 147: 148: unless matches.any? { |spec| spec.version == existing_spec.version } then 149: raise Gem::Exception, 150: "can't activate #{gem}, already activated #{existing_spec.full_name}" 151: end 152: 153: return false 154: end 155: 156: # new load 157: spec = matches.last 158: return false if spec.loaded? 159: 160: spec.loaded = true 161: @loaded_specs[spec.name] = spec 162: 163: # Load dependent gems first 164: spec.runtime_dependencies.each do |dep_gem| 165: activate dep_gem 166: end 167: 168: # bin directory must come before library directories 169: spec.require_paths.unshift spec.bindir if spec.bindir 170: 171: require_paths = spec.require_paths.map do |path| 172: File.join spec.full_gem_path, path 173: end 174: 175: sitelibdir = ConfigMap[:sitelibdir] 176: 177: # gem directories must come after -I and ENV['RUBYLIB'] 178: insert_index = load_path_insert_index 179: 180: if insert_index then 181: # gem directories must come after -I and ENV['RUBYLIB'] 182: $LOAD_PATH.insert(insert_index, *require_paths) 183: else 184: # we are probably testing in core, -I and RUBYLIB don't apply 185: $LOAD_PATH.unshift(*require_paths) 186: end 187: 188: return true 189: end
An Array of all possible load paths for all versions of all gems in the Gem installation.
# File lib/rubygems.rb, line 195 195: def self.all_load_paths 196: result = [] 197: 198: Gem.path.each do |gemdir| 199: each_load_path all_partials(gemdir) do |load_path| 200: result << load_path 201: end 202: end 203: 204: result 205: end
See if a given gem is available.
# File lib/rubygems.rb, line 219 219: def self.available?(gem, *requirements) 220: requirements = Gem::Requirement.default if requirements.empty? 221: 222: unless gem.respond_to?(:name) and 223: gem.respond_to?(:version_requirements) then 224: gem = Gem::Dependency.new gem, requirements 225: end 226: 227: !Gem.source_index.search(gem).empty? 228: end
The mode needed to read a file as straight binary.
# File lib/rubygems.rb, line 233 233: def self.binary_mode 234: @binary_mode ||= RUBY_VERSION > '1.9' ? 'rb:ascii-8bit' : 'rb' 235: end
Reset the dir and path values. The next time dir or path is requested, the values will be calculated from scratch. This is mainly used by the unit tests to provide test isolation.
# File lib/rubygems.rb, line 251 251: def self.clear_paths 252: @gem_home = nil 253: @gem_path = nil 254: @user_home = nil 255: 256: @@source_index = nil 257: 258: MUTEX.synchronize do 259: @searcher = nil 260: end 261: end
The standard configuration object for gems.
# File lib/rubygems.rb, line 273 273: def self.configuration 274: @configuration ||= Gem::ConfigFile.new [] 275: end
Use the given configuration object (which implements the ConfigFile protocol) as the standard configuration object.
# File lib/rubygems.rb, line 281 281: def self.configuration=(config) 282: @configuration = config 283: end
The path the the data directory specified by the gem name. If the package is not available as a gem, return nil.
# File lib/rubygems.rb, line 289 289: def self.datadir(gem_name) 290: spec = @loaded_specs[gem_name] 291: return nil if spec.nil? 292: File.join(spec.full_gem_path, 'data', gem_name) 293: end
The default directory for binaries Debian patch:
/var/lib/gems/{ruby version}/bin is the default path in Debian system
# File lib/rubygems/defaults.rb, line 57 57: def self.default_bindir 58: File.join('/', 'var', 'lib', 'gems', ConfigMap[:ruby_version], 'bin') 59: end
Default home directory path to be used if an alternate value is not specified in the environment
Debian patch: search order of this directory.
1. GEM_HOME enviroment variable (Using this, Gems are to be installed in any path as you like) 2. /var/lib/gems/{ruby version} (This is the default path in Debian system)
# File lib/rubygems/defaults.rb, line 25 25: def self.default_dir 26: File.join('/', 'var', 'lib', 'gems', ConfigMap[:ruby_version]) 27: end
The default system-wide source info cache directory
# File lib/rubygems/defaults.rb, line 64 64: def self.default_system_source_cache_dir 65: File.join Gem.dir, 'source_cache' 66: end
The default user-specific source info cache directory
# File lib/rubygems/defaults.rb, line 71 71: def self.default_user_source_cache_dir 72: File.join Gem.user_home, '.gem', 'source_cache' 73: end
A Zlib::Deflate.deflate wrapper
# File lib/rubygems.rb, line 298 298: def self.deflate(data) 299: require 'zlib' 300: Zlib::Deflate.deflate data 301: end
Quietly ensure the named Gem directory contains all the proper subdirectories. If we can‘t create a directory due to a permission problem, then we will silently continue.
# File lib/rubygems.rb, line 339 339: def self.ensure_gem_subdirectories(gemdir) 340: require 'fileutils' 341: 342: Gem::DIRECTORIES.each do |filename| 343: fn = File.join gemdir, filename 344: FileUtils.mkdir_p fn rescue nil unless File.exist? fn 345: end 346: end
Returns a list of paths matching file that can be used by a gem to pick up features from other gems. For example:
Gem.find_files('rdoc/discover').each do |path| load path end
find_files does not search $LOAD_PATH for files, only gems.
# File lib/rubygems.rb, line 356 356: def self.find_files(path) 357: specs = searcher.find_all path 358: 359: specs.map do |spec| 360: searcher.matching_files spec, path 361: end.flatten 362: end
Zlib::GzipReader wrapper that unzips data.
# File lib/rubygems.rb, line 400 400: def self.gunzip(data) 401: require 'stringio' 402: require 'zlib' 403: data = StringIO.new data 404: 405: Zlib::GzipReader.new(data).read 406: end
Zlib::GzipWriter wrapper that zips data.
# File lib/rubygems.rb, line 411 411: def self.gzip(data) 412: require 'stringio' 413: require 'zlib' 414: zipped = StringIO.new 415: 416: Zlib::GzipWriter.wrap zipped do |io| io.write data end 417: 418: zipped.string 419: end
A Zlib::Inflate#inflate wrapper
# File lib/rubygems.rb, line 424 424: def self.inflate(data) 425: require 'zlib' 426: Zlib::Inflate.inflate data 427: end
Return a list of all possible load paths for the latest version for all gems in the Gem installation.
# File lib/rubygems.rb, line 433 433: def self.latest_load_paths 434: result = [] 435: 436: Gem.path.each do |gemdir| 437: each_load_path(latest_partials(gemdir)) do |load_path| 438: result << load_path 439: end 440: end 441: 442: result 443: end
The index to insert activated gem paths into the $LOAD_PATH.
Defaults to the site lib directory unless gem_prelude.rb has loaded paths, then it inserts the activated gem‘s paths before the gem_prelude.rb paths so you can override the gem_prelude.rb default $LOAD_PATH paths.
# File lib/rubygems.rb, line 472 472: def self.load_path_insert_index 473: index = $LOAD_PATH.index ConfigMap[:sitelibdir] 474: 475: $LOAD_PATH.each_with_index do |path, i| 476: if path.instance_variables.include?(:@gem_prelude_index) or 477: path.instance_variables.include?('@gem_prelude_index') then 478: index = i 479: break 480: end 481: end 482: 483: index 484: end
The file name and line number of the caller of the caller of this method.
# File lib/rubygems.rb, line 489 489: def self.location_of_caller 490: caller[1] =~ /(.*?):(\d+)$/i 491: file = $1 492: lineno = $2.to_i 493: 494: [file, lineno] 495: end
The version of the Marshal format for your Ruby.
# File lib/rubygems.rb, line 509 509: def self.marshal_version 510: "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}" 511: end
Array of paths to search for Gems.
# File lib/rubygems.rb, line 516 516: def self.path 517: @gem_path ||= nil 518: 519: unless @gem_path then 520: paths = [ENV['GEM_PATH'] || Gem.configuration.path || default_path] 521: 522: if defined?(APPLE_GEM_HOME) and not ENV['GEM_PATH'] then 523: paths << APPLE_GEM_HOME 524: end 525: 526: set_paths paths.compact.join(File::PATH_SEPARATOR) 527: end 528: 529: @gem_path 530: end
Adds a post-install hook that will be passed an Gem::Installer instance when Gem::Installer#install is called
# File lib/rubygems.rb, line 554 554: def self.post_install(&hook) 555: @post_install_hooks << hook 556: end
Adds a post-uninstall hook that will be passed a Gem::Uninstaller instance and the spec that was uninstalled when Gem::Uninstaller#uninstall is called
# File lib/rubygems.rb, line 563 563: def self.post_uninstall(&hook) 564: @post_uninstall_hooks << hook 565: end
Adds a pre-install hook that will be passed an Gem::Installer instance when Gem::Installer#install is called
# File lib/rubygems.rb, line 571 571: def self.pre_install(&hook) 572: @pre_install_hooks << hook 573: end
Adds a pre-uninstall hook that will be passed an Gem::Uninstaller instance and the spec that will be uninstalled when Gem::Uninstaller#uninstall is called
# File lib/rubygems.rb, line 580 580: def self.pre_uninstall(&hook) 581: @pre_uninstall_hooks << hook 582: end
The directory prefix this RubyGems was installed at.
# File lib/rubygems.rb, line 587 587: def self.prefix 588: prefix = File.dirname File.expand_path(__FILE__) 589: 590: if File.dirname(prefix) == File.expand_path(ConfigMap[:sitelibdir]) or 591: File.dirname(prefix) == File.expand_path(ConfigMap[:libdir]) or 592: 'lib' != File.basename(prefix) then 593: nil 594: else 595: File.dirname prefix 596: end 597: end
Refresh source_index from disk and clear searcher.
# File lib/rubygems.rb, line 602 602: def self.refresh 603: source_index.refresh! 604: 605: MUTEX.synchronize do 606: @searcher = nil 607: end 608: end
# File lib/rubygems.rb, line 641 641: def self.required_location(gemname, libfile, *version_constraints) 642: version_constraints = Gem::Requirement.default if version_constraints.empty? 643: matches = Gem.source_index.find_name(gemname, version_constraints) 644: return nil if matches.empty? 645: spec = matches.last 646: spec.require_paths.each do |path| 647: result = File.join(spec.full_gem_path, path, libfile) 648: return result if File.exist?(result) 649: end 650: nil 651: end
The path to the running Ruby interpreter.
# File lib/rubygems.rb, line 656 656: def self.ruby 657: if @ruby.nil? then 658: @ruby = File.join(ConfigMap[:bindir], 659: ConfigMap[:ruby_install_name]) 660: @ruby << ConfigMap[:EXEEXT] 661: 662: # escape string in case path to ruby executable contain spaces. 663: @ruby.sub!(/.*\s.*/m, '"\&"') 664: end 665: 666: @ruby 667: end
A wrapper around RUBY_ENGINE const that may not be defined
# File lib/rubygems/defaults.rb, line 78 78: def self.ruby_engine 79: if defined? RUBY_ENGINE then 80: RUBY_ENGINE 81: else 82: 'ruby' 83: end 84: end
A Gem::Version for the currently running ruby.
# File lib/rubygems.rb, line 672 672: def self.ruby_version 673: return @ruby_version if defined? @ruby_version 674: version = RUBY_VERSION.dup 675: version << ".#{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL 676: @ruby_version = Gem::Version.new version 677: end
The GemPathSearcher object used to search for matching installed gems.
# File lib/rubygems.rb, line 682 682: def self.searcher 683: MUTEX.synchronize do 684: @searcher ||= Gem::GemPathSearcher.new 685: end 686: end
Returns the Gem::SourceIndex of specifications that are in the Gem.path
# File lib/rubygems.rb, line 735 735: def self.source_index 736: @@source_index ||= SourceIndex.from_installed_gems 737: end
Returns an Array of sources to fetch remote gems from. If the sources list is empty, attempts to load the "sources" gem, then uses default_sources if it is not installed.
# File lib/rubygems.rb, line 744 744: def self.sources 745: if @sources.empty? then 746: begin 747: gem 'sources', '> 0.0.1' 748: require 'sources' 749: rescue LoadError 750: @sources = default_sources 751: end 752: end 753: 754: @sources 755: end
Need to be able to set the sources without calling Gem.sources.replace since that would cause an infinite loop.
# File lib/rubygems.rb, line 761 761: def self.sources=(new_sources) 762: @sources = new_sources 763: end
Suffixes for require-able paths.
# File lib/rubygems.rb, line 775 775: def self.suffixes 776: ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar'] 777: end
Use the home and paths values for Gem.dir and Gem.path. Used mainly by the unit tests to provide environment isolation.
# File lib/rubygems.rb, line 783 783: def self.use_paths(home, paths=[]) 784: clear_paths 785: set_home(home) if home 786: set_paths(paths.join(File::PATH_SEPARATOR)) if paths 787: end
Path for gems in the user‘s home directory
# File lib/rubygems/defaults.rb, line 32 32: def self.user_dir 33: File.join(Gem.user_home, '.gem', ruby_engine, 34: ConfigMap[:ruby_version]) 35: end
The home directory for the user.
# File lib/rubygems.rb, line 792 792: def self.user_home 793: @user_home ||= find_home 794: end