Geminstaller C0 Coverage Information - RCov

spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems.rb 1140 538
76.84%
60.04%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 # -*- ruby -*-
2 #--
3 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # All rights reserved.
5 # See LICENSE.txt for permissions.
6 #++
7 
8 gem_disabled = !defined? Gem
9 
10 require 'rubygems/defaults'
11 require 'thread'
12 require 'etc'
13 
14 ##
15 # RubyGems is the Ruby standard for publishing and managing third party
16 # libraries.
17 #
18 # For user documentation, see:
19 #
20 # * <tt>gem help</tt> and <tt>gem help [command]</tt>
21 # * {RubyGems User Guide}[http://docs.rubygems.org/read/book/1]
22 # * {Frequently Asked Questions}[http://docs.rubygems.org/read/book/3]
23 #
24 # For gem developer documentation see:
25 #
26 # * {Creating Gems}[http://docs.rubygems.org/read/chapter/5]
27 # * Gem::Specification
28 # * Gem::Version for version dependency notes
29 #
30 # Further RubyGems documentation can be found at:
31 #
32 # * {RubyGems API}[http://rubygems.rubyforge.org/rdoc] (also available from
33 #   <tt>gem server</tt>)
34 # * {RubyGems Bookshelf}[http://rubygem.org]
35 #
36 # == RubyGems Plugins
37 #
38 # As of RubyGems 1.3.2, RubyGems will load plugins installed in gems or
39 # $LOAD_PATH.  Plugins must be named 'rubygems_plugin' (.rb, .so, etc) and
40 # placed at the root of your gem's #require_path.  Plugins are discovered via
41 # Gem::find_files then loaded.  Take care when implementing a plugin as your
42 # plugin file may be loaded multiple times if multiple versions of your gem
43 # are installed.
44 #
45 # For an example plugin, see the graph gem which adds a `gem graph` command.
46 #
47 # == RubyGems Defaults, Packaging
48 #
49 # RubyGems defaults are stored in rubygems/defaults.rb.  If you're packaging
50 # RubyGems or implementing Ruby you can change RubyGems' defaults.
51 #
52 # For RubyGems packagers, provide lib/rubygems/operating_system.rb and
53 # override any defaults from lib/rubygems/defaults.rb.
54 #
55 # For Ruby implementers, provide lib/rubygems/#{RUBY_ENGINE}.rb and override
56 # any defaults from lib/rubygems/defaults.rb.
57 #
58 # If you need RubyGems to perform extra work on install or uninstall, your
59 # defaults override file can set pre and post install and uninstall hooks.
60 # See Gem::pre_install, Gem::pre_uninstall, Gem::post_install,
61 # Gem::post_uninstall.
62 #
63 # == Bugs
64 #
65 # You can submit bugs to the
66 # {RubyGems bug tracker}[http://rubyforge.org/tracker/?atid=575&group_id=126]
67 # on RubyForge
68 #
69 # == Credits
70 #
71 # RubyGems is currently maintained by Eric Hodel.
72 #
73 # RubyGems was originally developed at RubyConf 2003 by:
74 #
75 # * Rich Kilmer  -- rich(at)infoether.com
76 # * Chad Fowler  -- chad(at)chadfowler.com
77 # * David Black  -- dblack(at)wobblini.net
78 # * Paul Brannan -- paul(at)atdesk.com
79 # * Jim Weirch   -- jim(at)weirichhouse.org
80 #
81 # Contributors:
82 #
83 # * Gavin Sinclair     -- gsinclair(at)soyabean.com.au
84 # * George Marrows     -- george.marrows(at)ntlworld.com
85 # * Dick Davies        -- rasputnik(at)hellooperator.net
86 # * Mauricio Fernandez -- batsman.geo(at)yahoo.com
87 # * Simon Strandgaard  -- neoneye(at)adslhome.dk
88 # * Dave Glasser       -- glasser(at)mit.edu
89 # * Paul Duncan        -- pabs(at)pablotron.org
90 # * Ville Aine         -- vaine(at)cs.helsinki.fi
91 # * Eric Hodel         -- drbrain(at)segment7.net
92 # * Daniel Berger      -- djberg96(at)gmail.com
93 # * Phil Hagelberg     -- technomancy(at)gmail.com
94 # * Ryan Davis         -- ryand-ruby(at)zenspider.com
95 #
96 # (If your name is missing, PLEASE let us know!)
97 #
98 # Thanks!
99 #
100 # -The RubyGems Team
101 
102 module Gem
103   RubyGemsVersion = VERSION = '1.3.7.pre.1'
104 
105   ##
106   # Raised when RubyGems is unable to load or activate a gem.  Contains the
107   # name and version requirements of the gem that either conflicts with
108   # already activated gems or that RubyGems is otherwise unable to activate.
109 
110   class LoadError < ::LoadError
111     # Name of gem
112     attr_accessor :name
113 
114     # Version requirement of gem
115     attr_accessor :version_requirement
116   end
117 
118   ##
119   # Configuration settings from ::RbConfig
120 
121   ConfigMap = {} unless defined?(ConfigMap)
122 
123   require 'rbconfig'
124 
125   ConfigMap.merge!(
126     :EXEEXT            => RbConfig::CONFIG["EXEEXT"],
127     :RUBY_SO_NAME      => RbConfig::CONFIG["RUBY_SO_NAME"],
128     :arch              => RbConfig::CONFIG["arch"],
129     :bindir            => RbConfig::CONFIG["bindir"],
130     :datadir           => RbConfig::CONFIG["datadir"],
131     :libdir            => RbConfig::CONFIG["libdir"],
132     :ruby_install_name => RbConfig::CONFIG["ruby_install_name"],
133     :ruby_version      => RbConfig::CONFIG["ruby_version"],
134     :rubylibprefix     => RbConfig::CONFIG["rubylibprefix"],
135     :sitedir           => RbConfig::CONFIG["sitedir"],
136     :sitelibdir        => RbConfig::CONFIG["sitelibdir"],
137     :vendordir         => RbConfig::CONFIG["vendordir"] ,
138     :vendorlibdir      => RbConfig::CONFIG["vendorlibdir"]
139   )
140 
141   ##
142   # Default directories in a gem repository
143 
144   DIRECTORIES = %w[cache doc gems specifications] unless defined?(DIRECTORIES)
145 
146   # :stopdoc:
147   MUTEX = Mutex.new
148 
149   RubyGemsPackageVersion = VERSION
150   # :startdoc:
151 
152   ##
153   # An Array of Regexps that match windows ruby platforms.
154 
155   WIN_PATTERNS = [
156     /bccwin/i,
157     /cygwin/i,
158     /djgpp/i,
159     /mingw/i,
160     /mswin/i,
161     /wince/i,
162   ]
163 
164   @@source_index = nil
165   @@win_platform = nil
166 
167   @configuration = nil
168   @loaded_specs = {}
169   @loaded_stacks = {}
170   @platforms = []
171   @ruby = nil
172   @sources = []
173 
174   @post_install_hooks   ||= []
175   @post_uninstall_hooks ||= []
176   @pre_uninstall_hooks  ||= []
177   @pre_install_hooks    ||= []
178 
179   ##
180   # Activates an installed gem matching +gem+.  The gem must satisfy
181   # +version_requirements+.
182   #
183   # Returns true if the gem is activated, false if it is already
184   # loaded, or an exception otherwise.
185   #
186   # Gem#activate adds the library paths in +gem+ to $LOAD_PATH.  Before a Gem
187   # is activated its required Gems are activated.  If the version information
188   # is omitted, the highest version Gem of the supplied name is loaded.  If a
189   # Gem is not found that meets the version requirements or a required Gem is
190   # not found, a Gem::LoadError is raised.
191   #
192   # More information on version requirements can be found in the
193   # Gem::Requirement and Gem::Version documentation.
194 
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
272 
273   ##
274   # An Array of all possible load paths for all versions of all gems in the
275   # Gem installation.
276 
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
288 
289   ##
290   # Return all the partial paths in +gemdir+.
291 
292   def self.all_partials(gemdir)
293     Dir[File.join(gemdir, 'gems/*')]
294   end
295 
296   private_class_method :all_partials
297 
298   ##
299   # See if a given gem is available.
300 
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
311 
312   ##
313   # Find the full path to the executable for gem +name+.  If the +exec_name+
314   # is not given, the gem's default_executable is chosen, otherwise the
315   # specified executable's path is returned.  +version_requirements+ allows
316   # you to specify specific gem versions.
317 
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
340 
341   ##
342   # The mode needed to read a file as straight binary.
343 
344   def self.binary_mode
345     'rb'
346   end
347 
348   ##
349   # The path where gem executables are to be installed.
350 
351   def self.bindir(install_dir=Gem.dir)
352     return File.join(install_dir, 'bin') unless
353       install_dir.to_s == Gem.default_dir
354     Gem.default_bindir
355   end
356 
357   ##
358   # Reset the +dir+ and +path+ values.  The next time +dir+ or +path+
359   # is requested, the values will be calculated from scratch.  This is
360   # mainly used by the unit tests to provide test isolation.
361 
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
373 
374   ##
375   # The path to standard location of the user's .gemrc file.
376 
377   def self.config_file
378     File.join Gem.user_home, '.gemrc'
379   end
380 
381   ##
382   # The standard configuration object for gems.
383 
384   def self.configuration
385     @configuration ||= Gem::ConfigFile.new []
386   end
387 
388   ##
389   # Use the given configuration object (which implements the ConfigFile
390   # protocol) as the standard configuration object.
391 
392   def self.configuration=(config)
393     @configuration = config
394   end
395 
396   ##
397   # The path the the data directory specified by the gem name.  If the
398   # package is not available as a gem, return nil.
399 
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
405 
406   ##
407   # A Zlib::Deflate.deflate wrapper
408 
409   def self.deflate(data)
410     require 'zlib'
411     Zlib::Deflate.deflate data
412   end
413 
414   ##
415   # The path where gems are to be installed.
416 
417   def self.dir
418     @gem_home ||= nil
419     set_home(ENV['GEM_HOME'] || Gem.configuration.home || default_dir) unless @gem_home
420     @gem_home
421   end
422 
423   ##
424   # Expand each partial gem path with each of the required paths specified
425   # in the Gem spec.  Each expanded path is yielded.
426 
427   def self.each_load_path(partials)
428     partials.each do |gp|
429       base = File.basename(gp)
430       specfn = File.join(dir, "specifications", base + ".gemspec")
431       if File.exist?(specfn)
432         spec = eval(File.read(specfn))
433         spec.require_paths.each do |rp|
434           yield(File.join(gp, rp))
435         end
436       else
437         filename = File.join(gp, 'lib')
438         yield(filename) if File.exist?(filename)
439       end
440     end
441   end
442 
443   private_class_method :each_load_path
444 
445   ##
446   # Quietly ensure the named Gem directory contains all the proper
447   # subdirectories.  If we can't create a directory due to a permission
448   # problem, then we will silently continue.
449 
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
458 
459   ##
460   # Returns a list of paths matching +file+ that can be used by a gem to pick
461   # up features from other gems.  For example:
462   #
463   #   Gem.find_files('rdoc/discover').each do |path| load path end
464   #
465   # find_files search $LOAD_PATH for files as well as gems.
466   #
467   # Note that find_files will return all files even if they are from different
468   # versions of the same gem.
469 
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
487 
488   ##
489   # Finds the user's home directory.
490   #--
491   # Some comments from the ruby-talk list regarding finding the home
492   # directory:
493   #
494   #   I have HOME, USERPROFILE and HOMEDRIVE + HOMEPATH. Ruby seems
495   #   to be depending on HOME in those code samples. I propose that
496   #   it should fallback to USERPROFILE and HOMEDRIVE + HOMEPATH (at
497   #   least on Win32).
498 
499   def self.find_home
500     unless RUBY_VERSION > '1.9' then
501       ['HOME', 'USERPROFILE'].each do |homekey|
502         return ENV[homekey] if ENV[homekey]
503       end
504 
505       if ENV['HOMEDRIVE'] && ENV['HOMEPATH'] then
506         return "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}"
507       end
508     end
509 
510     File.expand_path "~"
511   rescue
512     if File::ALT_SEPARATOR then
513       "C:/"
514     else
515       "/"
516     end
517   end
518 
519   private_class_method :find_home
520 
521   ##
522   # Zlib::GzipReader wrapper that unzips +data+.
523 
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
531 
532   ##
533   # Zlib::GzipWriter wrapper that zips +data+.
534 
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
544 
545   ##
546   # A Zlib::Inflate#inflate wrapper
547 
548   def self.inflate(data)
549     require 'zlib'
550     Zlib::Inflate.inflate data
551   end
552 
553   ##
554   # Return a list of all possible load paths for the latest version for all
555   # gems in the Gem installation.
556 
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
568 
569   ##
570   # Return only the latest partial paths in the given +gemdir+.
571 
572   def self.latest_partials(gemdir)
573     latest = {}
574     all_partials(gemdir).each do |gp|
575       base = File.basename(gp)
576       if base =~ /(.*)-((\d+\.)*\d+)/ then
577         name, version = $1, $2
578         ver = Gem::Version.new(version)
579         if latest[name].nil? || ver > latest[name][0]
580           latest[name] = [ver, gp]
581         end
582       end
583     end
584     latest.collect { |k,v| v[1] }
585   end
586 
587   private_class_method :latest_partials
588 
589   ##
590   # The index to insert activated gem paths into the $LOAD_PATH.
591   #
592   # Defaults to the site lib directory unless gem_prelude.rb has loaded paths,
593   # then it inserts the activated gem's paths before the gem_prelude.rb paths
594   # so you can override the gem_prelude.rb default $LOAD_PATH paths.
595 
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
609 
610   ##
611   # The file name and line number of the caller of the caller of this method.
612 
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
620 
621   ##
622   # The version of the Marshal format for your Ruby.
623 
624   def self.marshal_version
625     "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}"
626   end
627 
628   ##
629   # Array of paths to search for Gems.
630 
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
646 
647   ##
648   # Set array of platforms this RubyGems supports (primarily for testing).
649 
650   def self.platforms=(platforms)
651     @platforms = platforms
652   end
653 
654   ##
655   # Array of platforms this RubyGems supports.
656 
657   def self.platforms
658     @platforms ||= []
659     if @platforms.empty?
660       @platforms = [Gem::Platform::RUBY, Gem::Platform.local]
661     end
662     @platforms
663   end
664 
665   ##
666   # Adds a post-install hook that will be passed an Gem::Installer instance
667   # when Gem::Installer#install is called
668 
669   def self.post_install(&hook)
670     @post_install_hooks << hook
671   end
672 
673   ##
674   # Adds a post-uninstall hook that will be passed a Gem::Uninstaller instance
675   # and the spec that was uninstalled when Gem::Uninstaller#uninstall is
676   # called
677 
678   def self.post_uninstall(&hook)
679     @post_uninstall_hooks << hook
680   end
681 
682   ##
683   # Adds a pre-install hook that will be passed an Gem::Installer instance
684   # when Gem::Installer#install is called
685 
686   def self.pre_install(&hook)
687     @pre_install_hooks << hook
688   end
689 
690   ##
691   # Adds a pre-uninstall hook that will be passed an Gem::Uninstaller instance
692   # and the spec that will be uninstalled when Gem::Uninstaller#uninstall is
693   # called
694 
695   def self.pre_uninstall(&hook)
696     @pre_uninstall_hooks << hook
697   end
698 
699   ##
700   # The directory prefix this RubyGems was installed at.
701 
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
714 
715   ##
716   # Promotes the load paths of the +gem_name+ over the load paths of
717   # +over_name+.  Useful for allowing one gem to override features in another
718   # using #find_files.
719 
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
741 
742   ##
743   # Refresh source_index from disk and clear searcher.
744 
745   def self.refresh
746     source_index.refresh!
747 
748     MUTEX.synchronize do
749       @searcher = nil
750     end
751   end
752 
753   ##
754   # Safely read a file in binary mode on all platforms.
755 
756   def self.read_binary(path)
757     File.open path, binary_mode do |f| f.read end
758   end
759 
760   ##
761   # Report a load error during activation.  The message of load error
762   # depends on whether it was a version mismatch or if there are not gems of
763   # any version by the requested name.
764 
765   def self.report_activate_error(gem)
766     matches = Gem.source_index.find_name(gem.name)
767 
768     if matches.empty? then
769       error = Gem::LoadError.new(
770           "Could not find RubyGem #{gem.name} (#{gem.requirement})\n")
771     else
772       error = Gem::LoadError.new(
773           "RubyGem version error: " +
774           "#{gem.name}(#{matches.first.version} not #{gem.requirement})\n")
775     end
776 
777     error.name = gem.name
778     error.version_requirement = gem.requirement
779     raise error
780   end
781 
782   private_class_method :report_activate_error
783 
784   ##
785   # Full path to +libfile+ in +gemname+.  Searches for the latest gem unless
786   # +requirements+ is given.
787 
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
803 
804   ##
805   # The path to the running Ruby interpreter.
806 
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
819 
820   ##
821   # A Gem::Version for the currently running ruby.
822 
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
835 
836   ##
837   # The GemPathSearcher object used to search for matching installed gems.
838 
839   def self.searcher
840     MUTEX.synchronize do
841       @searcher ||= Gem::GemPathSearcher.new
842     end
843   end
844 
845   ##
846   # Set the Gem home directory (as reported by Gem.dir).
847 
848   def self.set_home(home)
849     home = home.gsub File::ALT_SEPARATOR, File::SEPARATOR if File::ALT_SEPARATOR
850     @gem_home = home
851   end
852 
853   private_class_method :set_home
854 
855   ##
856   # Set the Gem search path (as reported by Gem.path).
857 
858   def self.set_paths(gpaths)
859     if gpaths
860       @gem_path = gpaths.split(File::PATH_SEPARATOR)
861 
862       if File::ALT_SEPARATOR then
863         @gem_path.map! do |path|
864           path.gsub File::ALT_SEPARATOR, File::SEPARATOR
865         end
866       end
867 
868       @gem_path << Gem.dir
869     else
870       # TODO: should this be Gem.default_path instead?
871       @gem_path = [Gem.dir]
872     end
873 
874     @gem_path.uniq!
875   end
876 
877   private_class_method :set_paths
878 
879   ##
880   # Returns the Gem::SourceIndex of specifications that are in the Gem.path
881 
882   def self.source_index
883     @@source_index ||= SourceIndex.from_installed_gems
884   end
885 
886   ##
887   # Returns an Array of sources to fetch remote gems from.  If the sources
888   # list is empty, attempts to load the "sources" gem, then uses
889   # default_sources if it is not installed.
890 
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
903 
904   ##
905   # Need to be able to set the sources without calling
906   # Gem.sources.replace since that would cause an infinite loop.
907 
908   def self.sources=(new_sources)
909     @sources = new_sources
910   end
911 
912   ##
913   # Glob pattern for require-able path suffixes.
914 
915   def self.suffix_pattern
916     @suffix_pattern ||= "{#{suffixes.join(',')}}"
917   end
918 
919   ##
920   # Suffixes for require-able paths.
921 
922   def self.suffixes
923     ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar']
924   end
925 
926   ##
927   # Prints the amount of time the supplied block takes to run using the debug
928   # UI output.
929 
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
941 
942   ##
943   # Lazily loads DefaultUserInteraction and returns the default UI.
944 
945   def self.ui
946     require 'rubygems/user_interaction'
947 
948     Gem::DefaultUserInteraction.ui
949   end
950 
951   ##
952   # Use the +home+ and +paths+ values for Gem.dir and Gem.path.  Used mainly
953   # by the unit tests to provide environment isolation.
954 
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
960 
961   ##
962   # The home directory for the user.
963 
964   def self.user_home
965     @user_home ||= find_home
966   end
967 
968   ##
969   # Is this a windows platform?
970 
971   def self.win_platform?
972     if @@win_platform.nil? then
973       @@win_platform = !!WIN_PATTERNS.find { |r| RUBY_PLATFORM =~ r }
974     end
975 
976     @@win_platform
977   end
978 
979   ##
980   # Find all 'rubygems_plugin' files and load them
981 
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
1000 
1001   class << self
1002 
1003     ##
1004     # Hash of loaded Gem::Specification keyed by name
1005 
1006     attr_reader :loaded_specs
1007 
1008     ##
1009     # The list of hooks to be run before Gem::Install#install does any work
1010 
1011     attr_reader :post_install_hooks
1012 
1013     ##
1014     # The list of hooks to be run before Gem::Uninstall#uninstall does any
1015     # work
1016 
1017     attr_reader :post_uninstall_hooks
1018 
1019     ##
1020     # The list of hooks to be run after Gem::Install#install is finished
1021 
1022     attr_reader :pre_install_hooks
1023 
1024     ##
1025     # The list of hooks to be run after Gem::Uninstall#uninstall is finished
1026 
1027     attr_reader :pre_uninstall_hooks
1028 
1029     # :stopdoc:
1030 
1031     alias cache source_index # an alias for the old name
1032 
1033     # :startdoc:
1034 
1035   end
1036 
1037   ##
1038   # Location of Marshal quick gemspecs on remote repositories
1039 
1040   MARSHAL_SPEC_DIR = "quick/Marshal.#{Gem.marshal_version}/"
1041 
1042   ##
1043   # Location of legacy YAML quick gemspecs on remote repositories
1044 
1045   YAML_SPEC_DIR = 'quick/'
1046 
1047 end
1048 
1049 module Kernel
1050 
1051   undef gem if respond_to? :gem # defined in gem_prelude.rb on 1.9
1052 
1053   ##
1054   # Use Kernel#gem to activate a specific version of +gem_name+.
1055   #
1056   # +version_requirements+ is a list of version requirements that the
1057   # specified gem must match, most commonly "= example.version.number".  See
1058   # Gem::Requirement for how to specify a version requirement.
1059   #
1060   # If you will be activating the latest version of a gem, there is no need to
1061   # call Kernel#gem, Kernel#require will do the right thing for you.
1062   #
1063   # Kernel#gem returns true if the gem was activated, otherwise false.  If the
1064   # gem could not be found, didn't match the version requirements, or a
1065   # different version was already activated, an exception will be raised.
1066   #
1067   # Kernel#gem should be called *before* any require statements (otherwise
1068   # RubyGems may load a conflicting library version).
1069   #
1070   # In older RubyGems versions, the environment variable GEM_SKIP could be
1071   # used to skip activation of specified gems, for example to test out changes
1072   # that haven't been installed yet.  Now RubyGems defers to -I and the
1073   # RUBYLIB environment variable to skip activation of a gem.
1074   #
1075   # Example:
1076   #
1077   #   GEM_SKIP=libA:libB ruby -I../libA -I../libB ./mycode.rb
1078 
1079   def gem(gem_name, *version_requirements) # :doc:
1080     skip_list = (ENV['GEM_SKIP'] || "").split(/:/)
1081     raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name
1082     Gem.activate(gem_name, *version_requirements)
1083   end
1084 
1085   private :gem
1086 
1087 end
1088 
1089 ##
1090 # Return the path to the data directory associated with the named package.  If
1091 # the package is loaded as a gem, return the gem specific data directory.
1092 # Otherwise return a path to the share area as define by
1093 # "#{ConfigMap[:datadir]}/#{package_name}".
1094 
1095 def RbConfig.datadir(package_name)
1096   Gem.datadir(package_name) ||
1097     File.join(Gem::ConfigMap[:datadir], package_name)
1098 end
1099 
1100 require 'rubygems/exceptions'
1101 require 'rubygems/version'
1102 require 'rubygems/requirement'
1103 require 'rubygems/dependency'
1104 require 'rubygems/gem_path_searcher'    # Needed for Kernel#gem
1105 require 'rubygems/source_index'         # Needed for Kernel#gem
1106 require 'rubygems/platform'
1107 require 'rubygems/builder'              # HACK: Needed for rake's package task.
1108 
1109 begin
1110   ##
1111   # Defaults the operating system (or packager) wants to provide for RubyGems.
1112 
1113   require 'rubygems/defaults/operating_system'
1114 rescue LoadError
1115 end
1116 
1117 if defined?(RUBY_ENGINE) then
1118   begin
1119     ##
1120     # Defaults the ruby implementation wants to provide for RubyGems
1121 
1122     require "rubygems/defaults/#{RUBY_ENGINE}"
1123   rescue LoadError
1124   end
1125 end
1126 
1127 require 'rubygems/config_file'
1128 
1129 ##
1130 # Enables the require hook for RubyGems.
1131 #
1132 # Ruby 1.9 allows --disable-gems, so we require it when we didn't detect a Gem
1133 # constant at rubygems.rb load time.
1134 
1135 require 'rubygems/custom_require' if gem_disabled or RUBY_VERSION < '1.9'
1136 
1137 Gem.clear_paths
1138 
1139 Gem.load_plugins
1140 

Generated on Mon May 10 23:40:28 -0700 2010 with rcov 0.9.8