Module Gem
In: lib/rubygems/builder.rb
lib/rubygems/command.rb
lib/rubygems/command_manager.rb
lib/rubygems/commands/rdoc_command.rb
lib/rubygems/commands/search_command.rb
lib/rubygems/commands/uninstall_command.rb
lib/rubygems/defaults.rb
lib/rubygems/digest/digest_adapter.rb
lib/rubygems/digest/sha1.rb
lib/rubygems/digest/sha2.rb
lib/rubygems/format.rb
lib/rubygems/gem_openssl.rb
lib/rubygems/gem_runner.rb
lib/rubygems/old_format.rb
lib/rubygems/require_paths_builder.rb
lib/rubygems/rubygems_version.rb
lib/rubygems/source_index.rb
lib/rubygems/specification.rb
lib/rubygems/user_interaction.rb
lib/rubygems.rb

Main module to hold all RubyGem classes/modules.

Methods

Classes and Modules

Module Gem::Commands
Module Gem::DefaultUserInteraction
Module Gem::Ext
Module Gem::InstallUpdateOptions
Module Gem::LocalRemoteOptions
Module Gem::Package
Module Gem::RequirePathsBuilder
Module Gem::SSL
Module Gem::Security
Module Gem::UserInteraction
Module Gem::VersionOption
Class Gem::Builder
Class Gem::Command
Class Gem::CommandLineError
Class Gem::CommandManager
Class Gem::ConfigFile
Class Gem::ConsoleUI
Class Gem::Dependency
Class Gem::DependencyError
Class Gem::DependencyInstaller
Class Gem::DependencyList
Class Gem::DependencyRemovalException
Class Gem::DigestAdapter
Class Gem::DocManager
Class Gem::DocumentError
Class Gem::EndOfYAMLException
Class Gem::Exception
Class Gem::FakeFetcher
Class Gem::FileOperations
Class Gem::FilePermissionError
Class Gem::Format
Class Gem::FormatException
Class Gem::GemNotFoundException
Class Gem::GemNotInHomeException
Class Gem::GemPathSearcher
Class Gem::GemRunner
Class Gem::Indexer
Class Gem::InstallError
Class Gem::Installer
Class Gem::InvalidSpecificationException
Class Gem::LoadError
Class Gem::OldFormat
Class Gem::OperationNotSupportedError
Class Gem::Platform
Class Gem::RemoteError
Class Gem::RemoteFetcher
Class Gem::RemoteInstallationCancelled
Class Gem::RemoteInstallationSkipped
Class Gem::RemoteSourceException
Class Gem::Requirement
Class Gem::Server
Class Gem::SilentUI
Class Gem::SourceIndex
Class Gem::SourceInfoCache
Class Gem::SourceInfoCacheEntry
Class Gem::SpecFetcher
Class Gem::Specification
Class Gem::StreamUI
Class Gem::SystemExitException
Class Gem::Uninstaller
Class Gem::Validator
Class Gem::VerificationError
Class Gem::Version

Constants

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/'

Attributes

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.

Public Class methods

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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # File lib/rubygems.rb, line 233
233:   def self.binary_mode
234:     @binary_mode ||= RUBY_VERSION > '1.9' ? 'rb:ascii-8bit' : 'rb'
235:   end

The path where gem executables are to be installed.

[Source]

     # File lib/rubygems.rb, line 240
240:   def self.bindir(install_dir=Gem.dir)
241:     return File.join(install_dir, 'bin') unless
242:       install_dir.to_s == Gem.default_dir
243:     Gem.default_bindir
244:   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.

[Source]

     # 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 path to standard location of the user‘s .gemrc file.

[Source]

     # File lib/rubygems.rb, line 266
266:   def self.config_file
267:     File.join Gem.user_home, '.gemrc'
268:   end

The standard configuration object for gems.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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

[Source]

    # 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)

[Source]

    # 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

[Source]

    # File lib/rubygems/defaults.rb, line 47
47:   def self.default_exec_format
48:     baseruby = ConfigMap[:BASERUBY] || 'ruby'
49:     ConfigMap[:RUBY_INSTALL_NAME].sub(baseruby, '%s') rescue '%s'
50:   end

Default gem load path

[Source]

    # File lib/rubygems/defaults.rb, line 40
40:   def self.default_path
41:     [user_dir, default_dir]
42:   end

An Array of the default sources that come with RubyGems

[Source]

    # File lib/rubygems/defaults.rb, line 11
11:   def self.default_sources
12:     %w[http://gems.rubyforge.org/]
13:   end

The default system-wide source info cache directory

[Source]

    # 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

[Source]

    # 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

[Source]

     # File lib/rubygems.rb, line 298
298:   def self.deflate(data)
299:     require 'zlib'
300:     Zlib::Deflate.deflate data
301:   end

The path where gems are to be installed.

[Source]

     # File lib/rubygems.rb, line 306
306:   def self.dir
307:     @gem_home ||= nil
308:     set_home(ENV['GEM_HOME'] || Gem.configuration.home || default_dir) unless @gem_home
309:     @gem_home
310:   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.

[Source]

     # 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

Ensure that SSL is available. Throw an exception if it is not.

[Source]

    # File lib/rubygems/gem_openssl.rb, line 24
24:     def ensure_ssl_available
25:       unless ssl_available?
26:         fail Gem::Exception, "SSL is not installed on this system"
27:       end
28:     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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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

Array of platforms this RubyGems supports.

[Source]

     # File lib/rubygems.rb, line 542
542:   def self.platforms
543:     @platforms ||= []
544:     if @platforms.empty?
545:       @platforms = [Gem::Platform::RUBY, Gem::Platform.local]
546:     end
547:     @platforms
548:   end

Set array of platforms this RubyGems supports (primarily for testing).

[Source]

     # File lib/rubygems.rb, line 535
535:   def self.platforms=(platforms)
536:     @platforms = platforms
537:   end

Adds a post-install hook that will be passed an Gem::Installer instance when Gem::Installer#install is called

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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.

[Source]

     # 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

Safely read a file in binary mode on all platforms.

[Source]

     # File lib/rubygems.rb, line 613
613:   def self.read_binary(path)
614:     File.open path, binary_mode do |f| f.read end
615:   end

Refresh source_index from disk and clear searcher.

[Source]

     # 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

[Source]

     # 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.

[Source]

     # 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

[Source]

    # 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.

[Source]

     # 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.

[Source]

     # 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

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # File lib/rubygems.rb, line 761
761:   def self.sources=(new_sources)
762:     @sources = new_sources
763:   end

Is SSL (used by the signing commands) available on this platform?

[Source]

    # File lib/rubygems/gem_openssl.rb, line 15
15:     def ssl_available?
16:       require 'rubygems/gem_openssl'
17:       @ssl_available
18:     end

Glob pattern for require-able path suffixes.

[Source]

     # File lib/rubygems.rb, line 768
768:   def self.suffix_pattern
769:     @suffix_pattern ||= "{#{suffixes.join(',')}}"
770:   end

Suffixes for require-able paths.

[Source]

     # 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.

[Source]

     # 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

[Source]

    # 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.

[Source]

     # File lib/rubygems.rb, line 792
792:   def self.user_home
793:     @user_home ||= find_home
794:   end

Is this a windows platform?

[Source]

     # File lib/rubygems.rb, line 799
799:   def self.win_platform?
800:     if @@win_platform.nil? then
801:       @@win_platform = !!WIN_PATTERNS.find { |r| RUBY_PLATFORM =~ r }
802:     end
803: 
804:     @@win_platform
805:   end

[Validate]