Module | Gem |
In: |
lib/rubygems/defaults.rb
lib/rubygems/gem_openssl.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 | = | VERSION = '1.3.5' | ||
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 242 242: def self.activate(gem, *version_requirements) 243: if version_requirements.last.is_a?(Hash) 244: options = version_requirements.pop 245: else 246: options = {} 247: end 248: 249: sources = options[:sources] || [] 250: 251: if version_requirements.empty? then 252: version_requirements = Gem::Requirement.default 253: end 254: 255: unless gem.respond_to?(:name) and 256: gem.respond_to?(:version_requirements) then 257: gem = Gem::Dependency.new(gem, version_requirements) 258: end 259: 260: matches = Gem.source_index.find_name(gem.name, gem.version_requirements) 261: report_activate_error(gem) if matches.empty? 262: 263: if @loaded_specs[gem.name] then 264: # This gem is already loaded. If the currently loaded gem is not in the 265: # list of candidate gems, then we have a version conflict. 266: existing_spec = @loaded_specs[gem.name] 267: 268: unless matches.any? { |spec| spec.version == existing_spec.version } then 269: sources_message = sources.map { |spec| spec.full_name } 270: stack_message = @loaded_stacks[gem.name].map { |spec| spec.full_name } 271: 272: msg = "can't activate #{gem} for #{sources_message.inspect}, " 273: msg << "already activated #{existing_spec.full_name} for " 274: msg << "#{stack_message.inspect}" 275: 276: e = Gem::LoadError.new msg 277: e.name = gem.name 278: e.version_requirement = gem.version_requirements 279: 280: raise e 281: end 282: 283: return false 284: end 285: 286: # new load 287: spec = matches.last 288: return false if spec.loaded? 289: 290: spec.loaded = true 291: @loaded_specs[spec.name] = spec 292: @loaded_stacks[spec.name] = sources.dup 293: 294: # Load dependent gems first 295: spec.runtime_dependencies.each do |dep_gem| 296: activate dep_gem, :sources => [spec, *sources] 297: end 298: 299: # bin directory must come before library directories 300: spec.require_paths.unshift spec.bindir if spec.bindir 301: 302: require_paths = spec.require_paths.map do |path| 303: File.join spec.full_gem_path, path 304: end 305: 306: sitelibdir = ConfigMap[:sitelibdir] 307: 308: # gem directories must come after -I and ENV['RUBYLIB'] 309: insert_index = load_path_insert_index 310: 311: if insert_index then 312: # gem directories must come after -I and ENV['RUBYLIB'] 313: $LOAD_PATH.insert(insert_index, *require_paths) 314: else 315: # we are probably testing in core, -I and RUBYLIB don't apply 316: $LOAD_PATH.unshift(*require_paths) 317: end 318: 319: return true 320: end
An Array of all possible load paths for all versions of all gems in the Gem installation.
# File lib/rubygems.rb, line 326 326: def self.all_load_paths 327: result = [] 328: 329: Gem.path.each do |gemdir| 330: each_load_path all_partials(gemdir) do |load_path| 331: result << load_path 332: end 333: end 334: 335: result 336: end
See if a given gem is available.
# File lib/rubygems.rb, line 350 350: def self.available?(gem, *requirements) 351: requirements = Gem::Requirement.default if requirements.empty? 352: 353: unless gem.respond_to?(:name) and 354: gem.respond_to?(:version_requirements) then 355: gem = Gem::Dependency.new gem, requirements 356: end 357: 358: !Gem.source_index.search(gem).empty? 359: 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 367 367: def self.bin_path(name, exec_name = nil, *version_requirements) 368: version_requirements = Gem::Requirement.default if 369: version_requirements.empty? 370: spec = Gem.source_index.find_name(name, version_requirements).last 371: 372: raise Gem::GemNotFoundException, 373: "can't find gem #{name} (#{version_requirements})" unless spec 374: 375: exec_name ||= spec.default_executable 376: 377: unless exec_name 378: msg = "no default executable for #{spec.full_name}" 379: raise Gem::Exception, msg 380: end 381: 382: unless spec.executables.include? exec_name 383: msg = "can't find executable #{exec_name} for #{spec.full_name}" 384: raise Gem::Exception, msg 385: end 386: 387: File.join(spec.full_gem_path, spec.bindir, exec_name) 388: end
The mode needed to read a file as straight binary.
# File lib/rubygems.rb, line 393 393: def self.binary_mode 394: @binary_mode ||= RUBY_VERSION > '1.9' ? 'rb:ascii-8bit' : 'rb' 395: 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 411 411: def self.clear_paths 412: @gem_home = nil 413: @gem_path = nil 414: @user_home = nil 415: 416: @@source_index = nil 417: 418: MUTEX.synchronize do 419: @searcher = nil 420: end 421: end
The standard configuration object for gems.
# File lib/rubygems.rb, line 433 433: def self.configuration 434: @configuration ||= Gem::ConfigFile.new [] 435: end
Use the given configuration object (which implements the ConfigFile protocol) as the standard configuration object.
# File lib/rubygems.rb, line 441 441: def self.configuration=(config) 442: @configuration = config 443: 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 449 449: def self.datadir(gem_name) 450: spec = @loaded_specs[gem_name] 451: return nil if spec.nil? 452: File.join(spec.full_gem_path, 'data', gem_name) 453: 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 458 458: def self.deflate(data) 459: require 'zlib' 460: Zlib::Deflate.deflate data 461: 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 499 499: def self.ensure_gem_subdirectories(gemdir) 500: require 'fileutils' 501: 502: Gem::DIRECTORIES.each do |filename| 503: fn = File.join gemdir, filename 504: FileUtils.mkdir_p fn rescue nil unless File.exist? fn 505: end 506: 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 519 519: def self.find_files(path) 520: load_path_files = $LOAD_PATH.map do |load_path| 521: files = Dir["#{File.expand_path path, load_path}#{Gem.suffix_pattern}"] 522: 523: files.select do |load_path_file| 524: File.file? load_path_file.untaint 525: end 526: end.flatten 527: 528: specs = searcher.find_all path 529: 530: specs_files = specs.map do |spec| 531: searcher.matching_files spec, path 532: end.flatten 533: 534: (load_path_files + specs_files).flatten.uniq 535: end
Zlib::GzipReader wrapper that unzips data.
# File lib/rubygems.rb, line 573 573: def self.gunzip(data) 574: require 'stringio' 575: require 'zlib' 576: data = StringIO.new data 577: 578: Zlib::GzipReader.new(data).read 579: end
Zlib::GzipWriter wrapper that zips data.
# File lib/rubygems.rb, line 584 584: def self.gzip(data) 585: require 'stringio' 586: require 'zlib' 587: zipped = StringIO.new 588: 589: Zlib::GzipWriter.wrap zipped do |io| io.write data end 590: 591: zipped.string 592: end
A Zlib::Inflate#inflate wrapper
# File lib/rubygems.rb, line 597 597: def self.inflate(data) 598: require 'zlib' 599: Zlib::Inflate.inflate data 600: 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 606 606: def self.latest_load_paths 607: result = [] 608: 609: Gem.path.each do |gemdir| 610: each_load_path(latest_partials(gemdir)) do |load_path| 611: result << load_path 612: end 613: end 614: 615: result 616: 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 645 645: def self.load_path_insert_index 646: index = $LOAD_PATH.index ConfigMap[:sitelibdir] 647: 648: $LOAD_PATH.each_with_index do |path, i| 649: if path.instance_variables.include?(:@gem_prelude_index) or 650: path.instance_variables.include?('@gem_prelude_index') then 651: index = i 652: break 653: end 654: end 655: 656: index 657: end
The file name and line number of the caller of the caller of this method.
# File lib/rubygems.rb, line 662 662: def self.location_of_caller 663: caller[1] =~ /(.*?):(\d+).*?$/i 664: file = $1 665: lineno = $2.to_i 666: 667: [file, lineno] 668: end
The version of the Marshal format for your Ruby.
# File lib/rubygems.rb, line 673 673: def self.marshal_version 674: "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}" 675: end
Array of paths to search for Gems.
# File lib/rubygems.rb, line 680 680: def self.path 681: @gem_path ||= nil 682: 683: unless @gem_path then 684: paths = [ENV['GEM_PATH'] || Gem.configuration.path || default_path] 685: 686: if defined?(APPLE_GEM_HOME) and not ENV['GEM_PATH'] then 687: paths << APPLE_GEM_HOME 688: end 689: 690: set_paths paths.compact.join(File::PATH_SEPARATOR) 691: end 692: 693: @gem_path 694: 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 718 718: def self.post_install(&hook) 719: @post_install_hooks << hook 720: 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 727 727: def self.post_uninstall(&hook) 728: @post_uninstall_hooks << hook 729: 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 735 735: def self.pre_install(&hook) 736: @pre_install_hooks << hook 737: 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 744 744: def self.pre_uninstall(&hook) 745: @pre_uninstall_hooks << hook 746: end
The directory prefix this RubyGems was installed at.
# File lib/rubygems.rb, line 751 751: def self.prefix 752: prefix = File.dirname File.expand_path(__FILE__) 753: 754: if File.dirname(prefix) == File.expand_path(ConfigMap[:sitelibdir]) or 755: File.dirname(prefix) == File.expand_path(ConfigMap[:libdir]) or 756: 'lib' != File.basename(prefix) then 757: nil 758: else 759: File.dirname prefix 760: end 761: 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 768 768: def self.promote_load_path(gem_name, over_name) 769: gem = Gem.loaded_specs[gem_name] 770: over = Gem.loaded_specs[over_name] 771: 772: raise ArgumentError, "gem #{gem_name} is not activated" if gem.nil? 773: raise ArgumentError, "gem #{over_name} is not activated" if over.nil? 774: 775: last_gem_path = File.join gem.full_gem_path, gem.require_paths.last 776: 777: over_paths = over.require_paths.map do |path| 778: File.join over.full_gem_path, path 779: end 780: 781: over_paths.each do |path| 782: $LOAD_PATH.delete path 783: end 784: 785: gem = $LOAD_PATH.index(last_gem_path) + 1 786: 787: $LOAD_PATH.insert(gem, *over_paths) 788: end
Refresh source_index from disk and clear searcher.
# File lib/rubygems.rb, line 793 793: def self.refresh 794: source_index.refresh! 795: 796: MUTEX.synchronize do 797: @searcher = nil 798: end 799: end
Full path to libfile in gemname. Searches for the latest gem unless requirements is given.
# File lib/rubygems.rb, line 836 836: def self.required_location(gemname, libfile, *requirements) 837: requirements = Gem::Requirement.default if requirements.empty? 838: 839: matches = Gem.source_index.find_name gemname, requirements 840: 841: return nil if matches.empty? 842: 843: spec = matches.last 844: spec.require_paths.each do |path| 845: result = File.join spec.full_gem_path, path, libfile 846: return result if File.exist? result 847: end 848: 849: nil 850: end
The path to the running Ruby interpreter.
# File lib/rubygems.rb, line 855 855: def self.ruby 856: if @ruby.nil? then 857: @ruby = File.join(ConfigMap[:bindir], 858: ConfigMap[:ruby_install_name]) 859: @ruby << ConfigMap[:EXEEXT] 860: 861: # escape string in case path to ruby executable contain spaces. 862: @ruby.sub!(/.*\s.*/m, '"\&"') 863: end 864: 865: @ruby 866: 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 871 871: def self.ruby_version 872: return @ruby_version if defined? @ruby_version 873: version = RUBY_VERSION.dup 874: 875: if defined?(RUBY_PATCHLEVEL) && RUBY_PATCHLEVEL != -1 then 876: version << ".#{RUBY_PATCHLEVEL}" 877: elsif defined?(RUBY_REVISION) then 878: version << ".dev.#{RUBY_REVISION}" 879: end 880: 881: @ruby_version = Gem::Version.new version 882: end
The GemPathSearcher object used to search for matching installed gems.
# File lib/rubygems.rb, line 887 887: def self.searcher 888: MUTEX.synchronize do 889: @searcher ||= Gem::GemPathSearcher.new 890: end 891: end
Returns the Gem::SourceIndex of specifications that are in the Gem.path
# File lib/rubygems.rb, line 943 943: def self.source_index 944: @@source_index ||= SourceIndex.from_installed_gems 945: 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 952 952: def self.sources 953: if @sources.empty? then 954: begin 955: gem 'sources', '> 0.0.1' 956: require 'sources' 957: rescue LoadError 958: @sources = default_sources 959: end 960: end 961: 962: @sources 963: 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 969 969: def self.sources=(new_sources) 970: @sources = new_sources 971: 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 983 983: def self.suffixes 984: ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar'] 985: end
Prints the amount of time the supplied block takes to run using the debug UI output.
# File lib/rubygems.rb, line 991 991: def self.time(msg, width = 0, display = Gem.configuration.verbose) 992: now = Time.now 993: 994: value = yield 995: 996: elapsed = Time.now - now 997: 998: ui.say "%2$*1$s: %3$3.3fs" % [-width, msg, elapsed] if display 999: 1000: value 1001: end
Lazily loads DefaultUserInteraction and returns the default UI.
# File lib/rubygems.rb, line 1006 1006: def self.ui 1007: require 'rubygems/user_interaction' 1008: 1009: Gem::DefaultUserInteraction.ui 1010: 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 1016 1016: def self.use_paths(home, paths=[]) 1017: clear_paths 1018: set_home(home) if home 1019: set_paths(paths.join(File::PATH_SEPARATOR)) if paths 1020: 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 1025 1025: def self.user_home 1026: @user_home ||= find_home 1027: end