Module | Gem |
In: |
lib/rubygems/defaults.rb
lib/rubygems/gem_openssl.rb lib/rubygems/rubygems_version.rb lib/rubygems.rb |
RubyGems is the Ruby standard for publishing and managing third party libraries.
For user documentation, see:
For gem developer documentation see:
Further RubyGems documentation can be found at:
As of RubyGems 1.3.2, RubyGems will load plugins installed in gems or $LOAD_PATH. Plugins must be named ‘rubygems_plugin’ are discovered via Gem::find_files then loaded. Take care when implementing a plugin as your plugin file may be loaded multiple times if multiple versions of your gem are installed.
For an example plugin, see the graph gem which adds a `gem graph` command.
RubyGems defaults are stored in rubygems/defaults.rb. If you‘re packaging RubyGems or implementing Ruby you can change RubyGems’ defaults.
For RubyGems packagers, provide lib/rubygems/operating_system.rb and override any defaults from lib/rubygems/defaults.rb.
For Ruby implementers, provide lib/rubygems/#{RUBY_ENGINE}.rb and override any defaults from lib/rubygems/defaults.rb.
If you need RubyGems to perform extra work on install or uninstall, your defaults override file can set pre and post install and uninstall hooks. See Gem::pre_install, Gem::pre_uninstall, Gem::post_install, Gem::post_uninstall.
You can submit bugs to the RubyGems bug tracker on RubyForge
RubyGems is currently maintained by Eric Hodel.
RubyGems was originally developed at RubyConf 2003 by:
Contributors:
(If your name is missing, PLEASE let us know!)
Thanks!
-The RubyGems Team
RubyGemsVersion | = | '1.3.4' | The version of RubyGems you are using | |
VERSION | = | RubyGemsVersion | The version of RubyGems you are using (duplicated for familiarity) | |
ConfigMap | = | {} unless defined?(ConfigMap) | Configuration settings from ::RbConfig | |
DIRECTORIES | = | %w[cache doc gems specifications] unless defined?(DIRECTORIES) | Default directories in a gem repository | |
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}/" | Location of Marshal quick gemspecs on remote repositories | |
YAML_SPEC_DIR | = | 'quick/' | Location of legacy YAML quick gemspecs on remote repositories |
loaded_specs | [R] | Hash of loaded Gem::Specification keyed by name |
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] | Is SSL available? |
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 241 241: def self.activate(gem, *version_requirements) 242: if version_requirements.last.is_a?(Hash) 243: options = version_requirements.pop 244: else 245: options = {} 246: end 247: 248: sources = options[:sources] || [] 249: 250: if version_requirements.empty? then 251: version_requirements = Gem::Requirement.default 252: end 253: 254: unless gem.respond_to?(:name) and 255: gem.respond_to?(:version_requirements) then 256: gem = Gem::Dependency.new(gem, version_requirements) 257: end 258: 259: matches = Gem.source_index.find_name(gem.name, gem.version_requirements) 260: report_activate_error(gem) if matches.empty? 261: 262: if @loaded_specs[gem.name] then 263: # This gem is already loaded. If the currently loaded gem is not in the 264: # list of candidate gems, then we have a version conflict. 265: existing_spec = @loaded_specs[gem.name] 266: 267: unless matches.any? { |spec| spec.version == existing_spec.version } then 268: sources_message = sources.map { |spec| spec.full_name } 269: stack_message = @loaded_stacks[gem.name].map { |spec| spec.full_name } 270: 271: msg = "can't activate #{gem} for #{sources_message.inspect}, " 272: msg << "already activated #{existing_spec.full_name} for " 273: msg << "#{stack_message.inspect}" 274: 275: e = Gem::LoadError.new msg 276: e.name = gem.name 277: e.version_requirement = gem.version_requirements 278: 279: raise e 280: end 281: 282: return false 283: end 284: 285: # new load 286: spec = matches.last 287: return false if spec.loaded? 288: 289: spec.loaded = true 290: @loaded_specs[spec.name] = spec 291: @loaded_stacks[spec.name] = sources.dup 292: 293: # Load dependent gems first 294: spec.runtime_dependencies.each do |dep_gem| 295: activate dep_gem, :sources => [spec, *sources] 296: end 297: 298: # bin directory must come before library directories 299: spec.require_paths.unshift spec.bindir if spec.bindir 300: 301: require_paths = spec.require_paths.map do |path| 302: File.join spec.full_gem_path, path 303: end 304: 305: sitelibdir = ConfigMap[:sitelibdir] 306: 307: # gem directories must come after -I and ENV['RUBYLIB'] 308: insert_index = load_path_insert_index 309: 310: if insert_index then 311: # gem directories must come after -I and ENV['RUBYLIB'] 312: $LOAD_PATH.insert(insert_index, *require_paths) 313: else 314: # we are probably testing in core, -I and RUBYLIB don't apply 315: $LOAD_PATH.unshift(*require_paths) 316: end 317: 318: return true 319: end
An Array of all possible load paths for all versions of all gems in the Gem installation.
# File lib/rubygems.rb, line 325 325: def self.all_load_paths 326: result = [] 327: 328: Gem.path.each do |gemdir| 329: each_load_path all_partials(gemdir) do |load_path| 330: result << load_path 331: end 332: end 333: 334: result 335: end
See if a given gem is available.
# File lib/rubygems.rb, line 349 349: def self.available?(gem, *requirements) 350: requirements = Gem::Requirement.default if requirements.empty? 351: 352: unless gem.respond_to?(:name) and 353: gem.respond_to?(:version_requirements) then 354: gem = Gem::Dependency.new gem, requirements 355: end 356: 357: !Gem.source_index.search(gem).empty? 358: end
Find the full path to the executable for gem name. If the exec_name is not given, the gem‘s default_executable is chosen, otherwise the specifed executable‘s path is returned. version_requirements allows you to specify specific gem versions.
# File lib/rubygems.rb, line 366 366: def self.bin_path(name, exec_name = nil, *version_requirements) 367: version_requirements = Gem::Requirement.default if 368: version_requirements.empty? 369: spec = Gem.source_index.find_name(name, version_requirements).last 370: 371: raise Gem::GemNotFoundException, 372: "can't find gem #{name} (#{version_requirements})" unless spec 373: 374: exec_name ||= spec.default_executable 375: 376: unless exec_name 377: msg = "no default executable for #{spec.full_name}" 378: raise Gem::Exception, msg 379: end 380: 381: unless spec.executables.include? exec_name 382: msg = "can't find executable #{exec_name} for #{spec.full_name}" 383: raise Gem::Exception, msg 384: end 385: 386: File.join(spec.full_gem_path, spec.bindir, exec_name).sub(/.*\s.*/m, '"\&"') 387: end
The mode needed to read a file as straight binary.
# File lib/rubygems.rb, line 392 392: def self.binary_mode 393: @binary_mode ||= RUBY_VERSION > '1.9' ? 'rb:ascii-8bit' : 'rb' 394: 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 410 410: def self.clear_paths 411: @gem_home = nil 412: @gem_path = nil 413: @user_home = nil 414: 415: @@source_index = nil 416: 417: MUTEX.synchronize do 418: @searcher = nil 419: end 420: end
The standard configuration object for gems.
# File lib/rubygems.rb, line 432 432: def self.configuration 433: @configuration ||= Gem::ConfigFile.new [] 434: end
Use the given configuration object (which implements the ConfigFile protocol) as the standard configuration object.
# File lib/rubygems.rb, line 440 440: def self.configuration=(config) 441: @configuration = config 442: 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 448 448: def self.datadir(gem_name) 449: spec = @loaded_specs[gem_name] 450: return nil if spec.nil? 451: File.join(spec.full_gem_path, 'data', gem_name) 452: 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 67 67: def self.default_bindir 68: File.join('/', 'var', 'lib', 'gems', ConfigMap[:ruby_version], 'bin') 69: 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
Deduce Ruby‘s —program-prefix and —program-suffix from its install name
# File lib/rubygems/defaults.rb, line 51 51: def self.default_exec_format 52: exec_format = ConfigMap[:ruby_install_name].sub('ruby', '%s') rescue '%s' 53: 54: unless exec_format =~ /%s/ then 55: raise Gem::Exception, 56: "[BUG] invalid exec_format #{exec_format.inspect}, no %s" 57: end 58: 59: exec_format 60: end
The default system-wide source info cache directory
# File lib/rubygems/defaults.rb, line 74 74: def self.default_system_source_cache_dir 75: File.join Gem.dir, 'source_cache' 76: end
The default user-specific source info cache directory
# File lib/rubygems/defaults.rb, line 81 81: def self.default_user_source_cache_dir 82: File.join Gem.user_home, '.gem', 'source_cache' 83: end
A Zlib::Deflate.deflate wrapper
# File lib/rubygems.rb, line 457 457: def self.deflate(data) 458: require 'zlib' 459: Zlib::Deflate.deflate data 460: 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 498 498: def self.ensure_gem_subdirectories(gemdir) 499: require 'fileutils' 500: 501: Gem::DIRECTORIES.each do |filename| 502: fn = File.join gemdir, filename 503: FileUtils.mkdir_p fn rescue nil unless File.exist? fn 504: end 505: end
Ensure that SSL is available. Throw an exception if it is not.
# File lib/rubygems/gem_openssl.rb, line 31 31: def ensure_ssl_available 32: unless ssl_available? 33: fail Gem::Exception, "SSL is not installed on this system" 34: end 35: 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 search $LOAD_PATH for files as well as gems.
Note that find_files will return all files even if they are from different versions of the same gem.
# File lib/rubygems.rb, line 518 518: def self.find_files(path) 519: load_path_files = $LOAD_PATH.map do |load_path| 520: files = Dir["#{File.expand_path path, load_path}#{Gem.suffix_pattern}"] 521: 522: files.select do |load_path_file| 523: File.file? load_path_file.untaint 524: end 525: end.flatten 526: 527: specs = searcher.find_all path 528: 529: specs_files = specs.map do |spec| 530: searcher.matching_files spec, path 531: end.flatten 532: 533: (load_path_files + specs_files).flatten.uniq 534: end
Zlib::GzipReader wrapper that unzips data.
# File lib/rubygems.rb, line 572 572: def self.gunzip(data) 573: require 'stringio' 574: require 'zlib' 575: data = StringIO.new data 576: 577: Zlib::GzipReader.new(data).read 578: end
Zlib::GzipWriter wrapper that zips data.
# File lib/rubygems.rb, line 583 583: def self.gzip(data) 584: require 'stringio' 585: require 'zlib' 586: zipped = StringIO.new 587: 588: Zlib::GzipWriter.wrap zipped do |io| io.write data end 589: 590: zipped.string 591: end
A Zlib::Inflate#inflate wrapper
# File lib/rubygems.rb, line 596 596: def self.inflate(data) 597: require 'zlib' 598: Zlib::Inflate.inflate data 599: 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 605 605: def self.latest_load_paths 606: result = [] 607: 608: Gem.path.each do |gemdir| 609: each_load_path(latest_partials(gemdir)) do |load_path| 610: result << load_path 611: end 612: end 613: 614: result 615: 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 644 644: def self.load_path_insert_index 645: index = $LOAD_PATH.index ConfigMap[:sitelibdir] 646: 647: $LOAD_PATH.each_with_index do |path, i| 648: if path.instance_variables.include?(:@gem_prelude_index) or 649: path.instance_variables.include?('@gem_prelude_index') then 650: index = i 651: break 652: end 653: end 654: 655: index 656: end
The file name and line number of the caller of the caller of this method.
# File lib/rubygems.rb, line 661 661: def self.location_of_caller 662: caller[1] =~ /(.*?):(\d+).*?$/i 663: file = $1 664: lineno = $2.to_i 665: 666: [file, lineno] 667: end
The version of the Marshal format for your Ruby.
# File lib/rubygems.rb, line 672 672: def self.marshal_version 673: "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}" 674: end
Array of paths to search for Gems.
# File lib/rubygems.rb, line 679 679: def self.path 680: @gem_path ||= nil 681: 682: unless @gem_path then 683: paths = [ENV['GEM_PATH'] || Gem.configuration.path || default_path] 684: 685: if defined?(APPLE_GEM_HOME) and not ENV['GEM_PATH'] then 686: paths << APPLE_GEM_HOME 687: end 688: 689: set_paths paths.compact.join(File::PATH_SEPARATOR) 690: end 691: 692: @gem_path 693: 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 717 717: def self.post_install(&hook) 718: @post_install_hooks << hook 719: 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 726 726: def self.post_uninstall(&hook) 727: @post_uninstall_hooks << hook 728: 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 734 734: def self.pre_install(&hook) 735: @pre_install_hooks << hook 736: 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 743 743: def self.pre_uninstall(&hook) 744: @pre_uninstall_hooks << hook 745: end
The directory prefix this RubyGems was installed at.
# File lib/rubygems.rb, line 750 750: def self.prefix 751: prefix = File.dirname File.expand_path(__FILE__) 752: 753: if File.dirname(prefix) == File.expand_path(ConfigMap[:sitelibdir]) or 754: File.dirname(prefix) == File.expand_path(ConfigMap[:libdir]) or 755: 'lib' != File.basename(prefix) then 756: nil 757: else 758: File.dirname prefix 759: end 760: end
Promotes the load paths of the gem_name over the load paths of over_name. Useful for allowing one gem to override features in another using find_files.
# File lib/rubygems.rb, line 767 767: def self.promote_load_path(gem_name, over_name) 768: gem = Gem.loaded_specs[gem_name] 769: over = Gem.loaded_specs[over_name] 770: 771: raise ArgumentError, "gem #{gem_name} is not activated" if gem.nil? 772: raise ArgumentError, "gem #{over_name} is not activated" if over.nil? 773: 774: last_gem_path = File.join gem.full_gem_path, gem.require_paths.last 775: 776: over_paths = over.require_paths.map do |path| 777: File.join over.full_gem_path, path 778: end 779: 780: over_paths.each do |path| 781: $LOAD_PATH.delete path 782: end 783: 784: gem = $LOAD_PATH.index(last_gem_path) + 1 785: 786: $LOAD_PATH.insert(gem, *over_paths) 787: end
Refresh source_index from disk and clear searcher.
# File lib/rubygems.rb, line 792 792: def self.refresh 793: source_index.refresh! 794: 795: MUTEX.synchronize do 796: @searcher = nil 797: end 798: end
Full path to libfile in gemname. Searches for the latest gem unless requirements is given.
# File lib/rubygems.rb, line 835 835: def self.required_location(gemname, libfile, *requirements) 836: requirements = Gem::Requirement.default if requirements.empty? 837: 838: matches = Gem.source_index.find_name gemname, requirements 839: 840: return nil if matches.empty? 841: 842: spec = matches.last 843: spec.require_paths.each do |path| 844: result = File.join spec.full_gem_path, path, libfile 845: return result if File.exist? result 846: end 847: 848: nil 849: end
The path to the running Ruby interpreter.
# File lib/rubygems.rb, line 854 854: def self.ruby 855: if @ruby.nil? then 856: @ruby = File.join(ConfigMap[:bindir], 857: ConfigMap[:ruby_install_name]) 858: @ruby << ConfigMap[:EXEEXT] 859: 860: # escape string in case path to ruby executable contain spaces. 861: @ruby.sub!(/.*\s.*/m, '"\&"') 862: end 863: 864: @ruby 865: end
A wrapper around RUBY_ENGINE const that may not be defined
# File lib/rubygems/defaults.rb, line 88 88: def self.ruby_engine 89: if defined? RUBY_ENGINE then 90: RUBY_ENGINE 91: else 92: 'ruby' 93: end 94: end
A Gem::Version for the currently running ruby.
# File lib/rubygems.rb, line 870 870: def self.ruby_version 871: return @ruby_version if defined? @ruby_version 872: version = RUBY_VERSION.dup 873: 874: if defined?(RUBY_PATCHLEVEL) && RUBY_PATCHLEVEL != -1 then 875: version << ".#{RUBY_PATCHLEVEL}" 876: elsif defined?(RUBY_REVISION) then 877: version << ".dev.#{RUBY_REVISION}" 878: end 879: 880: @ruby_version = Gem::Version.new version 881: end
The GemPathSearcher object used to search for matching installed gems.
# File lib/rubygems.rb, line 886 886: def self.searcher 887: MUTEX.synchronize do 888: @searcher ||= Gem::GemPathSearcher.new 889: end 890: end
Returns the Gem::SourceIndex of specifications that are in the Gem.path
# File lib/rubygems.rb, line 942 942: def self.source_index 943: @@source_index ||= SourceIndex.from_installed_gems 944: 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 951 951: def self.sources 952: if @sources.empty? then 953: begin 954: gem 'sources', '> 0.0.1' 955: require 'sources' 956: rescue LoadError 957: @sources = default_sources 958: end 959: end 960: 961: @sources 962: 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 968 968: def self.sources=(new_sources) 969: @sources = new_sources 970: end
Is SSL (used by the signing commands) available on this platform?
# File lib/rubygems/gem_openssl.rb, line 19 19: def ssl_available? 20: @ssl_available 21: end
Suffixes for require-able paths.
# File lib/rubygems.rb, line 982 982: def self.suffixes 983: ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar'] 984: end
Prints the amount of time the supplied block takes to run using the debug UI output.
# File lib/rubygems.rb, line 990 990: def self.time(msg, width = 0, display = Gem.configuration.verbose) 991: now = Time.now 992: 993: value = yield 994: 995: elapsed = Time.now - now 996: 997: ui.say "%2$*1$s: %3$3.3fs" % [-width, msg, elapsed] if display 998: 999: value 1000: end
Lazily loads DefaultUserInteraction and returns the default UI.
# File lib/rubygems.rb, line 1005 1005: def self.ui 1006: require 'rubygems/user_interaction' 1007: 1008: Gem::DefaultUserInteraction.ui 1009: 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 1015 1015: def self.use_paths(home, paths=[]) 1016: clear_paths 1017: set_home(home) if home 1018: set_paths(paths.join(File::PATH_SEPARATOR)) if paths 1019: 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 1024 1024: def self.user_home 1025: @user_home ||= find_home 1026: end