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’ (.rb, .so, etc) and placed at the root of your gem‘s require_path. Plugins 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.7' | ||
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 195 195: def self.activate(gem, *version_requirements) 196: if version_requirements.last.is_a?(Hash) 197: options = version_requirements.pop 198: else 199: options = {} 200: end 201: 202: sources = options[:sources] || [] 203: 204: if version_requirements.empty? then 205: version_requirements = Gem::Requirement.default 206: end 207: 208: unless gem.respond_to?(:name) and 209: gem.respond_to?(:requirement) then 210: gem = Gem::Dependency.new(gem, version_requirements) 211: end 212: 213: matches = Gem.source_index.find_name(gem.name, gem.requirement) 214: report_activate_error(gem) if matches.empty? 215: 216: if @loaded_specs[gem.name] then 217: # This gem is already loaded. If the currently loaded gem is not in the 218: # list of candidate gems, then we have a version conflict. 219: existing_spec = @loaded_specs[gem.name] 220: 221: unless matches.any? { |spec| spec.version == existing_spec.version } then 222: sources_message = sources.map { |spec| spec.full_name } 223: stack_message = @loaded_stacks[gem.name].map { |spec| spec.full_name } 224: 225: msg = "can't activate #{gem} for #{sources_message.inspect}, " 226: msg << "already activated #{existing_spec.full_name} for " 227: msg << "#{stack_message.inspect}" 228: 229: e = Gem::LoadError.new msg 230: e.name = gem.name 231: e.version_requirement = gem.requirement 232: 233: raise e 234: end 235: 236: return false 237: end 238: 239: # new load 240: spec = matches.last 241: return false if spec.loaded? 242: 243: spec.loaded = true 244: @loaded_specs[spec.name] = spec 245: @loaded_stacks[spec.name] = sources.dup 246: 247: # Load dependent gems first 248: spec.runtime_dependencies.each do |dep_gem| 249: activate dep_gem, :sources => [spec, *sources] 250: end 251: 252: # bin directory must come before library directories 253: spec.require_paths.unshift spec.bindir if spec.bindir 254: 255: require_paths = spec.require_paths.map do |path| 256: File.join spec.full_gem_path, path 257: end 258: 259: # gem directories must come after -I and ENV['RUBYLIB'] 260: insert_index = load_path_insert_index 261: 262: if insert_index then 263: # gem directories must come after -I and ENV['RUBYLIB'] 264: $LOAD_PATH.insert(insert_index, *require_paths) 265: else 266: # we are probably testing in core, -I and RUBYLIB don't apply 267: $LOAD_PATH.unshift(*require_paths) 268: end 269: 270: return true 271: end
An Array of all possible load paths for all versions of all gems in the Gem installation.
# File lib/rubygems.rb, line 277 277: def self.all_load_paths 278: result = [] 279: 280: Gem.path.each do |gemdir| 281: each_load_path all_partials(gemdir) do |load_path| 282: result << load_path 283: end 284: end 285: 286: result 287: end
See if a given gem is available.
# File lib/rubygems.rb, line 301 301: def self.available?(gem, *requirements) 302: requirements = Gem::Requirement.default if requirements.empty? 303: 304: unless gem.respond_to?(:name) and 305: gem.respond_to?(:requirement) then 306: gem = Gem::Dependency.new gem, requirements 307: end 308: 309: !Gem.source_index.search(gem).empty? 310: 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 specified executable‘s path is returned. version_requirements allows you to specify specific gem versions.
# File lib/rubygems.rb, line 318 318: def self.bin_path(name, exec_name = nil, *version_requirements) 319: version_requirements = Gem::Requirement.default if 320: version_requirements.empty? 321: spec = Gem.source_index.find_name(name, version_requirements).last 322: 323: raise Gem::GemNotFoundException, 324: "can't find gem #{name} (#{version_requirements})" unless spec 325: 326: exec_name ||= spec.default_executable 327: 328: unless exec_name 329: msg = "no default executable for #{spec.full_name}" 330: raise Gem::Exception, msg 331: end 332: 333: unless spec.executables.include? exec_name 334: msg = "can't find executable #{exec_name} for #{spec.full_name}" 335: raise Gem::Exception, msg 336: end 337: 338: File.join(spec.full_gem_path, spec.bindir, exec_name) 339: end
The mode needed to read a file as straight binary.
# File lib/rubygems.rb, line 344 344: def self.binary_mode 345: 'rb' 346: 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 362 362: def self.clear_paths 363: @gem_home = nil 364: @gem_path = nil 365: @user_home = nil 366: 367: @@source_index = nil 368: 369: MUTEX.synchronize do 370: @searcher = nil 371: end 372: end
The standard configuration object for gems.
# File lib/rubygems.rb, line 384 384: def self.configuration 385: @configuration ||= Gem::ConfigFile.new [] 386: end
Use the given configuration object (which implements the ConfigFile protocol) as the standard configuration object.
# File lib/rubygems.rb, line 392 392: def self.configuration=(config) 393: @configuration = config 394: 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 400 400: def self.datadir(gem_name) 401: spec = @loaded_specs[gem_name] 402: return nil if spec.nil? 403: File.join(spec.full_gem_path, 'data', gem_name) 404: 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 66 66: def self.default_bindir 67: File.join('/', 'var', 'lib', 'gems', ConfigMap[:ruby_version], 'bin') 68: 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 50 50: def self.default_exec_format 51: exec_format = ConfigMap[:ruby_install_name].sub('ruby', '%s') rescue '%s' 52: 53: unless exec_format =~ /%s/ then 54: raise Gem::Exception, 55: "[BUG] invalid exec_format #{exec_format.inspect}, no %s" 56: end 57: 58: exec_format 59: end
The default system-wide source info cache directory
# File lib/rubygems/defaults.rb, line 73 73: def self.default_system_source_cache_dir 74: File.join Gem.dir, 'source_cache' 75: end
The default user-specific source info cache directory
# File lib/rubygems/defaults.rb, line 80 80: def self.default_user_source_cache_dir 81: File.join Gem.user_home, '.gem', 'source_cache' 82: end
A Zlib::Deflate.deflate wrapper
# File lib/rubygems.rb, line 409 409: def self.deflate(data) 410: require 'zlib' 411: Zlib::Deflate.deflate data 412: 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 450 450: def self.ensure_gem_subdirectories(gemdir) 451: require 'fileutils' 452: 453: Gem::DIRECTORIES.each do |filename| 454: fn = File.join gemdir, filename 455: FileUtils.mkdir_p fn rescue nil unless File.exist? fn 456: end 457: 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: raise 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 470 470: def self.find_files(path) 471: load_path_files = $LOAD_PATH.map do |load_path| 472: files = Dir["#{File.expand_path path, load_path}#{Gem.suffix_pattern}"] 473: 474: files.select do |load_path_file| 475: File.file? load_path_file.untaint 476: end 477: end.flatten 478: 479: specs = searcher.find_all path 480: 481: specs_files = specs.map do |spec| 482: searcher.matching_files spec, path 483: end.flatten 484: 485: (load_path_files + specs_files).flatten.uniq 486: end
Zlib::GzipReader wrapper that unzips data.
# File lib/rubygems.rb, line 524 524: def self.gunzip(data) 525: require 'stringio' 526: require 'zlib' 527: data = StringIO.new data 528: 529: Zlib::GzipReader.new(data).read 530: end
Zlib::GzipWriter wrapper that zips data.
# File lib/rubygems.rb, line 535 535: def self.gzip(data) 536: require 'stringio' 537: require 'zlib' 538: zipped = StringIO.new 539: 540: Zlib::GzipWriter.wrap zipped do |io| io.write data end 541: 542: zipped.string 543: end
A Zlib::Inflate#inflate wrapper
# File lib/rubygems.rb, line 548 548: def self.inflate(data) 549: require 'zlib' 550: Zlib::Inflate.inflate data 551: 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 557 557: def self.latest_load_paths 558: result = [] 559: 560: Gem.path.each do |gemdir| 561: each_load_path(latest_partials(gemdir)) do |load_path| 562: result << load_path 563: end 564: end 565: 566: result 567: 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 596 596: def self.load_path_insert_index 597: index = $LOAD_PATH.index ConfigMap[:sitelibdir] 598: 599: $LOAD_PATH.each_with_index do |path, i| 600: if path.instance_variables.include?(:@gem_prelude_index) or 601: path.instance_variables.include?('@gem_prelude_index') then 602: index = i 603: break 604: end 605: end 606: 607: index 608: end
Find all ‘rubygems_plugin’ files and load them
# File lib/rubygems.rb, line 982 982: def self.load_plugins 983: plugins = Gem.find_files 'rubygems_plugin' 984: 985: plugins.each do |plugin| 986: 987: # Skip older versions of the GemCutter plugin: Its commands are in 988: # RubyGems proper now. 989: 990: next if plugin =~ /gemcutter-0\.[0-3]/ 991: 992: begin 993: load plugin 994: rescue ::Exception => e 995: details = "#{plugin.inspect}: #{e.message} (#{e.class})" 996: warn "Error loading RubyGems plugin #{details}" 997: end 998: end 999: end
The file name and line number of the caller of the caller of this method.
# File lib/rubygems.rb, line 613 613: def self.location_of_caller 614: caller[1] =~ /(.*?):(\d+).*?$/i 615: file = $1 616: lineno = $2.to_i 617: 618: [file, lineno] 619: end
The version of the Marshal format for your Ruby.
# File lib/rubygems.rb, line 624 624: def self.marshal_version 625: "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}" 626: end
Array of paths to search for Gems.
# File lib/rubygems.rb, line 631 631: def self.path 632: @gem_path ||= nil 633: 634: unless @gem_path then 635: paths = [ENV['GEM_PATH'] || Gem.configuration.path || default_path] 636: 637: if defined?(APPLE_GEM_HOME) and not ENV['GEM_PATH'] then 638: paths << APPLE_GEM_HOME 639: end 640: 641: set_paths paths.compact.join(File::PATH_SEPARATOR) 642: end 643: 644: @gem_path 645: 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 669 669: def self.post_install(&hook) 670: @post_install_hooks << hook 671: 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 678 678: def self.post_uninstall(&hook) 679: @post_uninstall_hooks << hook 680: 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 686 686: def self.pre_install(&hook) 687: @pre_install_hooks << hook 688: 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 695 695: def self.pre_uninstall(&hook) 696: @pre_uninstall_hooks << hook 697: end
The directory prefix this RubyGems was installed at.
# File lib/rubygems.rb, line 702 702: def self.prefix 703: dir = File.dirname File.expand_path(__FILE__) 704: prefix = File.dirname dir 705: 706: if prefix == File.expand_path(ConfigMap[:sitelibdir]) or 707: prefix == File.expand_path(ConfigMap[:libdir]) or 708: 'lib' != File.basename(dir) then 709: nil 710: else 711: prefix 712: end 713: 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 720 720: def self.promote_load_path(gem_name, over_name) 721: gem = Gem.loaded_specs[gem_name] 722: over = Gem.loaded_specs[over_name] 723: 724: raise ArgumentError, "gem #{gem_name} is not activated" if gem.nil? 725: raise ArgumentError, "gem #{over_name} is not activated" if over.nil? 726: 727: last_gem_path = File.join gem.full_gem_path, gem.require_paths.last 728: 729: over_paths = over.require_paths.map do |path| 730: File.join over.full_gem_path, path 731: end 732: 733: over_paths.each do |path| 734: $LOAD_PATH.delete path 735: end 736: 737: gem = $LOAD_PATH.index(last_gem_path) + 1 738: 739: $LOAD_PATH.insert(gem, *over_paths) 740: end
Refresh source_index from disk and clear searcher.
# File lib/rubygems.rb, line 745 745: def self.refresh 746: source_index.refresh! 747: 748: MUTEX.synchronize do 749: @searcher = nil 750: end 751: end
Full path to libfile in gemname. Searches for the latest gem unless requirements is given.
# File lib/rubygems.rb, line 788 788: def self.required_location(gemname, libfile, *requirements) 789: requirements = Gem::Requirement.default if requirements.empty? 790: 791: matches = Gem.source_index.find_name gemname, requirements 792: 793: return nil if matches.empty? 794: 795: spec = matches.last 796: spec.require_paths.each do |path| 797: result = File.join spec.full_gem_path, path, libfile 798: return result if File.exist? result 799: end 800: 801: nil 802: end
The path to the running Ruby interpreter.
# File lib/rubygems.rb, line 807 807: def self.ruby 808: if @ruby.nil? then 809: @ruby = File.join(ConfigMap[:bindir], 810: ConfigMap[:ruby_install_name]) 811: @ruby << ConfigMap[:EXEEXT] 812: 813: # escape string in case path to ruby executable contain spaces. 814: @ruby.sub!(/.*\s.*/m, '"\&"') 815: end 816: 817: @ruby 818: end
A wrapper around RUBY_ENGINE const that may not be defined
# File lib/rubygems/defaults.rb, line 87 87: def self.ruby_engine 88: if defined? RUBY_ENGINE then 89: RUBY_ENGINE 90: else 91: 'ruby' 92: end 93: end
A Gem::Version for the currently running ruby.
# File lib/rubygems.rb, line 823 823: def self.ruby_version 824: return @ruby_version if defined? @ruby_version 825: version = RUBY_VERSION.dup 826: 827: if defined?(RUBY_PATCHLEVEL) && RUBY_PATCHLEVEL != -1 then 828: version << ".#{RUBY_PATCHLEVEL}" 829: elsif defined?(RUBY_REVISION) then 830: version << ".dev.#{RUBY_REVISION}" 831: end 832: 833: @ruby_version = Gem::Version.new version 834: end
The GemPathSearcher object used to search for matching installed gems.
# File lib/rubygems.rb, line 839 839: def self.searcher 840: MUTEX.synchronize do 841: @searcher ||= Gem::GemPathSearcher.new 842: end 843: end
Returns the Gem::SourceIndex of specifications that are in the Gem.path
# File lib/rubygems.rb, line 882 882: def self.source_index 883: @@source_index ||= SourceIndex.from_installed_gems 884: 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 891 891: def self.sources 892: if @sources.empty? then 893: begin 894: gem 'sources', '> 0.0.1' 895: require 'sources' 896: rescue LoadError 897: @sources = default_sources 898: end 899: end 900: 901: @sources 902: 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 908 908: def self.sources=(new_sources) 909: @sources = new_sources 910: 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 922 922: def self.suffixes 923: ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar'] 924: end
Prints the amount of time the supplied block takes to run using the debug UI output.
# File lib/rubygems.rb, line 930 930: def self.time(msg, width = 0, display = Gem.configuration.verbose) 931: now = Time.now 932: 933: value = yield 934: 935: elapsed = Time.now - now 936: 937: ui.say "%2$*1$s: %3$3.3fs" % [-width, msg, elapsed] if display 938: 939: value 940: end
Lazily loads DefaultUserInteraction and returns the default UI.
# File lib/rubygems.rb, line 945 945: def self.ui 946: require 'rubygems/user_interaction' 947: 948: Gem::DefaultUserInteraction.ui 949: 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 955 955: def self.use_paths(home, paths=[]) 956: clear_paths 957: set_home(home) if home 958: set_paths(paths.join(File::PATH_SEPARATOR)) if paths 959: 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, ConfigMap[:ruby_version] 34: end
The home directory for the user.
# File lib/rubygems.rb, line 964 964: def self.user_home 965: @user_home ||= find_home 966: end