PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/rubygems.rb

https://github.com/fizx/ruby
Ruby | 1102 lines | 545 code | 237 blank | 320 comment | 45 complexity | 8924e0a71a0fecc282cd70e4e708ed45 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, GPL-2.0, BSD-3-Clause
  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. require 'rubygems/defaults'
  8. require 'thread'
  9. require 'etc'
  10. module Gem
  11. RubyGemsVersion = VERSION = '1.3.5'
  12. ##
  13. # Raised when RubyGems is unable to load or activate a gem. Contains the
  14. # name and version requirements of the gem that either conflicts with
  15. # already activated gems or that RubyGems is otherwise unable to activate.
  16. class LoadError < ::LoadError
  17. ##
  18. # Name of gem
  19. attr_accessor :name
  20. ##
  21. # Version requirement of gem
  22. attr_accessor :version_requirement
  23. end
  24. end
  25. module Kernel
  26. # defined in gem_prelude.rb
  27. undef gem
  28. ##
  29. # Use Kernel#gem to activate a specific version of +gem_name+.
  30. #
  31. # +version_requirements+ is a list of version requirements that the
  32. # specified gem must match, most commonly "= example.version.number". See
  33. # Gem::Requirement for how to specify a version requirement.
  34. #
  35. # If you will be activating the latest version of a gem, there is no need to
  36. # call Kernel#gem, Kernel#require will do the right thing for you.
  37. #
  38. # Kernel#gem returns true if the gem was activated, otherwise false. If the
  39. # gem could not be found, didn't match the version requirements, or a
  40. # different version was already activated, an exception will be raised.
  41. #
  42. # Kernel#gem should be called *before* any require statements (otherwise
  43. # RubyGems may load a conflicting library version).
  44. #
  45. # In older RubyGems versions, the environment variable GEM_SKIP could be
  46. # used to skip activation of specified gems, for example to test out changes
  47. # that haven't been installed yet. Now RubyGems defers to -I and the
  48. # RUBYLIB environment variable to skip activation of a gem.
  49. #
  50. # Example:
  51. #
  52. # GEM_SKIP=libA:libB ruby -I../libA -I../libB ./mycode.rb
  53. def gem(gem_name, *version_requirements) # :doc:
  54. skip_list = (ENV['GEM_SKIP'] || "").split(/:/)
  55. raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name
  56. Gem.activate(gem_name, *version_requirements)
  57. end
  58. private :gem
  59. end
  60. ##
  61. # RubyGems is the Ruby standard for publishing and managing third party
  62. # libraries.
  63. #
  64. # For user documentation, see:
  65. #
  66. # * <tt>gem help</tt> and <tt>gem help [command]</tt>
  67. # * {RubyGems User Guide}[http://docs.rubygems.org/read/book/1]
  68. # * {Frequently Asked Questions}[http://docs.rubygems.org/read/book/3]
  69. #
  70. # For gem developer documentation see:
  71. #
  72. # * {Creating Gems}[http://docs.rubygems.org/read/chapter/5]
  73. # * Gem::Specification
  74. #
  75. # Further RubyGems documentation can be found at:
  76. #
  77. # * {RubyGems API}[http://rubygems.rubyforge.org/rdoc] (also available from
  78. # <tt>gem server</tt>)
  79. # * {RubyGems Bookshelf}[http://rubygem.org]
  80. #
  81. # == RubyGems Plugins
  82. #
  83. # As of RubyGems 1.3.2, RubyGems will load plugins installed in gems or
  84. # $LOAD_PATH. Plugins must be named 'rubygems_plugin' are discovered via
  85. # Gem::find_files then loaded. Take care when implementing a plugin as your
  86. # plugin file may be loaded multiple times if multiple versions of your gem
  87. # are installed.
  88. #
  89. # For an example plugin, see the graph gem which adds a `gem graph` command.
  90. #
  91. # == RubyGems Defaults, Packaging
  92. #
  93. # RubyGems defaults are stored in rubygems/defaults.rb. If you're packaging
  94. # RubyGems or implementing Ruby you can change RubyGems' defaults.
  95. #
  96. # For RubyGems packagers, provide lib/rubygems/operating_system.rb and
  97. # override any defaults from lib/rubygems/defaults.rb.
  98. #
  99. # For Ruby implementers, provide lib/rubygems/#{RUBY_ENGINE}.rb and override
  100. # any defaults from lib/rubygems/defaults.rb.
  101. #
  102. # If you need RubyGems to perform extra work on install or uninstall, your
  103. # defaults override file can set pre and post install and uninstall hooks.
  104. # See Gem::pre_install, Gem::pre_uninstall, Gem::post_install,
  105. # Gem::post_uninstall.
  106. #
  107. # == Bugs
  108. #
  109. # You can submit bugs to the
  110. # {RubyGems bug tracker}[http://rubyforge.org/tracker/?atid=575&group_id=126&func=browse]
  111. # on RubyForge
  112. #
  113. # == Credits
  114. #
  115. # RubyGems is currently maintained by Eric Hodel.
  116. #
  117. # RubyGems was originally developed at RubyConf 2003 by:
  118. #
  119. # * Rich Kilmer -- rich(at)infoether.com
  120. # * Chad Fowler -- chad(at)chadfowler.com
  121. # * David Black -- dblack(at)wobblini.net
  122. # * Paul Brannan -- paul(at)atdesk.com
  123. # * Jim Weirch -- {jim(at)weirichhouse.org}[mailto:jim@weirichhouse.org]
  124. #
  125. # Contributors:
  126. #
  127. # * Gavin Sinclair -- gsinclair(at)soyabean.com.au
  128. # * George Marrows -- george.marrows(at)ntlworld.com
  129. # * Dick Davies -- rasputnik(at)hellooperator.net
  130. # * Mauricio Fernandez -- batsman.geo(at)yahoo.com
  131. # * Simon Strandgaard -- neoneye(at)adslhome.dk
  132. # * Dave Glasser -- glasser(at)mit.edu
  133. # * Paul Duncan -- pabs(at)pablotron.org
  134. # * Ville Aine -- vaine(at)cs.helsinki.fi
  135. # * Eric Hodel -- drbrain(at)segment7.net
  136. # * Daniel Berger -- djberg96(at)gmail.com
  137. # * Phil Hagelberg -- technomancy(at)gmail.com
  138. # * Ryan Davis
  139. #
  140. # (If your name is missing, PLEASE let us know!)
  141. #
  142. # Thanks!
  143. #
  144. # -The RubyGems Team
  145. module Gem
  146. ##
  147. # Configuration settings from ::RbConfig
  148. ConfigMap = {} unless defined?(ConfigMap)
  149. require 'rbconfig'
  150. ConfigMap.merge!(
  151. :EXEEXT => RbConfig::CONFIG["EXEEXT"],
  152. :RUBY_SO_NAME => RbConfig::CONFIG["RUBY_SO_NAME"],
  153. :arch => RbConfig::CONFIG["arch"],
  154. :bindir => RbConfig::CONFIG["bindir"],
  155. :datadir => RbConfig::CONFIG["datadir"],
  156. :libdir => RbConfig::CONFIG["libdir"],
  157. :rubylibprefix => RbConfig::CONFIG["rubylibprefix"],
  158. :ruby_install_name => RbConfig::CONFIG["ruby_install_name"],
  159. :ruby_version => RbConfig::CONFIG["ruby_version"],
  160. :sitedir => RbConfig::CONFIG["sitedir"],
  161. :sitelibdir => RbConfig::CONFIG["sitelibdir"],
  162. :vendordir => RbConfig::CONFIG["vendordir"] ,
  163. :vendorlibdir => RbConfig::CONFIG["vendorlibdir"]
  164. )
  165. ##
  166. # Default directories in a gem repository
  167. DIRECTORIES = %w[cache doc gems specifications] unless defined?(DIRECTORIES)
  168. # :stopdoc:
  169. MUTEX = Mutex.new
  170. RubyGemsPackageVersion = RubyGemsVersion
  171. # :startdoc:
  172. ##
  173. # An Array of Regexps that match windows ruby platforms.
  174. WIN_PATTERNS = [
  175. /bccwin/i,
  176. /cygwin/i,
  177. /djgpp/i,
  178. /mingw/i,
  179. /mswin/i,
  180. /wince/i,
  181. ]
  182. @@source_index = nil
  183. @@win_platform = nil
  184. @configuration = nil
  185. @loaded_specs = {}
  186. @loaded_stacks = {}
  187. @platforms = []
  188. @ruby = nil
  189. @sources = []
  190. @post_install_hooks ||= []
  191. @post_uninstall_hooks ||= []
  192. @pre_uninstall_hooks ||= []
  193. @pre_install_hooks ||= []
  194. ##
  195. # Activates an installed gem matching +gem+. The gem must satisfy
  196. # +version_requirements+.
  197. #
  198. # Returns true if the gem is activated, false if it is already
  199. # loaded, or an exception otherwise.
  200. #
  201. # Gem#activate adds the library paths in +gem+ to $LOAD_PATH. Before a Gem
  202. # is activated its required Gems are activated. If the version information
  203. # is omitted, the highest version Gem of the supplied name is loaded. If a
  204. # Gem is not found that meets the version requirements or a required Gem is
  205. # not found, a Gem::LoadError is raised.
  206. #
  207. # More information on version requirements can be found in the
  208. # Gem::Requirement and Gem::Version documentation.
  209. def self.activate(gem, *version_requirements)
  210. if version_requirements.last.is_a?(Hash)
  211. options = version_requirements.pop
  212. else
  213. options = {}
  214. end
  215. sources = options[:sources] || []
  216. if version_requirements.empty? then
  217. version_requirements = Gem::Requirement.default
  218. end
  219. unless gem.respond_to?(:name) and
  220. gem.respond_to?(:version_requirements) then
  221. gem = Gem::Dependency.new(gem, version_requirements)
  222. end
  223. matches = Gem.source_index.find_name(gem.name, gem.version_requirements)
  224. report_activate_error(gem) if matches.empty?
  225. if @loaded_specs[gem.name] then
  226. # This gem is already loaded. If the currently loaded gem is not in the
  227. # list of candidate gems, then we have a version conflict.
  228. existing_spec = @loaded_specs[gem.name]
  229. unless matches.any? { |spec| spec.version == existing_spec.version } then
  230. sources_message = sources.map { |spec| spec.full_name }
  231. stack_message = @loaded_stacks[gem.name].map { |spec| spec.full_name }
  232. msg = "can't activate #{gem} for #{sources_message.inspect}, "
  233. msg << "already activated #{existing_spec.full_name} for "
  234. msg << "#{stack_message.inspect}"
  235. e = Gem::LoadError.new msg
  236. e.name = gem.name
  237. e.version_requirement = gem.version_requirements
  238. raise e
  239. end
  240. return false
  241. end
  242. # new load
  243. spec = matches.last
  244. return false if spec.loaded?
  245. spec.loaded = true
  246. @loaded_specs[spec.name] = spec
  247. @loaded_stacks[spec.name] = sources.dup
  248. # Load dependent gems first
  249. spec.runtime_dependencies.each do |dep_gem|
  250. activate dep_gem, :sources => [spec, *sources]
  251. end
  252. # bin directory must come before library directories
  253. spec.require_paths.unshift spec.bindir if spec.bindir
  254. require_paths = spec.require_paths.map do |path|
  255. File.join spec.full_gem_path, path
  256. end
  257. sitelibdir = ConfigMap[:sitelibdir]
  258. # gem directories must come after -I and ENV['RUBYLIB']
  259. insert_index = load_path_insert_index
  260. if insert_index then
  261. # gem directories must come after -I and ENV['RUBYLIB']
  262. $LOAD_PATH.insert(insert_index, *require_paths)
  263. else
  264. # we are probably testing in core, -I and RUBYLIB don't apply
  265. $LOAD_PATH.unshift(*require_paths)
  266. end
  267. return true
  268. end
  269. ##
  270. # An Array of all possible load paths for all versions of all gems in the
  271. # Gem installation.
  272. def self.all_load_paths
  273. result = []
  274. Gem.path.each do |gemdir|
  275. each_load_path all_partials(gemdir) do |load_path|
  276. result << load_path
  277. end
  278. end
  279. result
  280. end
  281. ##
  282. # Return all the partial paths in +gemdir+.
  283. def self.all_partials(gemdir)
  284. Dir[File.join(gemdir, 'gems/*')]
  285. end
  286. private_class_method :all_partials
  287. ##
  288. # See if a given gem is available.
  289. def self.available?(gem, *requirements)
  290. requirements = Gem::Requirement.default if requirements.empty?
  291. unless gem.respond_to?(:name) and
  292. gem.respond_to?(:version_requirements) then
  293. gem = Gem::Dependency.new gem, requirements
  294. end
  295. !Gem.source_index.search(gem).empty?
  296. end
  297. ##
  298. # Find the full path to the executable for gem +name+. If the +exec_name+
  299. # is not given, the gem's default_executable is chosen, otherwise the
  300. # specifed executable's path is returned. +version_requirements+ allows you
  301. # to specify specific gem versions.
  302. def self.bin_path(name, exec_name = nil, *version_requirements)
  303. version_requirements = Gem::Requirement.default if
  304. version_requirements.empty?
  305. spec = Gem.source_index.find_name(name, version_requirements).last
  306. raise Gem::GemNotFoundException,
  307. "can't find gem #{name} (#{version_requirements})" unless spec
  308. exec_name ||= spec.default_executable
  309. unless exec_name
  310. msg = "no default executable for #{spec.full_name}"
  311. raise Gem::Exception, msg
  312. end
  313. unless spec.executables.include? exec_name
  314. msg = "can't find executable #{exec_name} for #{spec.full_name}"
  315. raise Gem::Exception, msg
  316. end
  317. File.join(spec.full_gem_path, spec.bindir, exec_name)
  318. end
  319. ##
  320. # The mode needed to read a file as straight binary.
  321. def self.binary_mode
  322. 'rb'
  323. end
  324. ##
  325. # The path where gem executables are to be installed.
  326. def self.bindir(install_dir=Gem.dir)
  327. return File.join(install_dir, 'bin') unless
  328. install_dir.to_s == Gem.default_dir
  329. Gem.default_bindir
  330. end
  331. ##
  332. # Reset the +dir+ and +path+ values. The next time +dir+ or +path+
  333. # is requested, the values will be calculated from scratch. This is
  334. # mainly used by the unit tests to provide test isolation.
  335. def self.clear_paths
  336. @gem_home = nil
  337. @gem_path = nil
  338. @user_home = nil
  339. @@source_index = nil
  340. MUTEX.synchronize do
  341. @searcher = nil
  342. end
  343. end
  344. ##
  345. # The path to standard location of the user's .gemrc file.
  346. def self.config_file
  347. File.join Gem.user_home, '.gemrc'
  348. end
  349. ##
  350. # The standard configuration object for gems.
  351. def self.configuration
  352. @configuration ||= Gem::ConfigFile.new []
  353. end
  354. ##
  355. # Use the given configuration object (which implements the ConfigFile
  356. # protocol) as the standard configuration object.
  357. def self.configuration=(config)
  358. @configuration = config
  359. end
  360. ##
  361. # The path the the data directory specified by the gem name. If the
  362. # package is not available as a gem, return nil.
  363. def self.datadir(gem_name)
  364. spec = @loaded_specs[gem_name]
  365. return nil if spec.nil?
  366. File.join(spec.full_gem_path, 'data', gem_name)
  367. end
  368. ##
  369. # A Zlib::Deflate.deflate wrapper
  370. def self.deflate(data)
  371. require 'zlib'
  372. Zlib::Deflate.deflate data
  373. end
  374. ##
  375. # The path where gems are to be installed.
  376. def self.dir
  377. @gem_home ||= nil
  378. set_home(ENV['GEM_HOME'] || Gem.configuration.home || default_dir) unless @gem_home
  379. @gem_home
  380. end
  381. ##
  382. # Expand each partial gem path with each of the required paths specified
  383. # in the Gem spec. Each expanded path is yielded.
  384. def self.each_load_path(partials)
  385. partials.each do |gp|
  386. base = File.basename(gp)
  387. specfn = File.join(dir, "specifications", base + ".gemspec")
  388. if File.exist?(specfn)
  389. spec = eval(File.read(specfn))
  390. spec.require_paths.each do |rp|
  391. yield(File.join(gp, rp))
  392. end
  393. else
  394. filename = File.join(gp, 'lib')
  395. yield(filename) if File.exist?(filename)
  396. end
  397. end
  398. end
  399. private_class_method :each_load_path
  400. ##
  401. # Quietly ensure the named Gem directory contains all the proper
  402. # subdirectories. If we can't create a directory due to a permission
  403. # problem, then we will silently continue.
  404. def self.ensure_gem_subdirectories(gemdir)
  405. require 'fileutils'
  406. Gem::DIRECTORIES.each do |filename|
  407. fn = File.join gemdir, filename
  408. FileUtils.mkdir_p fn rescue nil unless File.exist? fn
  409. end
  410. end
  411. ##
  412. # Returns a list of paths matching +file+ that can be used by a gem to pick
  413. # up features from other gems. For example:
  414. #
  415. # Gem.find_files('rdoc/discover').each do |path| load path end
  416. #
  417. # find_files search $LOAD_PATH for files as well as gems.
  418. #
  419. # Note that find_files will return all files even if they are from different
  420. # versions of the same gem.
  421. def self.find_files(path)
  422. load_path_files = $LOAD_PATH.map do |load_path|
  423. files = Dir["#{File.expand_path path, load_path}#{Gem.suffix_pattern}"]
  424. files.select do |load_path_file|
  425. File.file? load_path_file.untaint
  426. end
  427. end.flatten
  428. specs = searcher.find_all path
  429. specs_files = specs.map do |spec|
  430. searcher.matching_files spec, path
  431. end.flatten
  432. (load_path_files + specs_files).flatten.uniq
  433. end
  434. ##
  435. # Finds the user's home directory.
  436. def self.find_home
  437. File.expand_path "~"
  438. rescue
  439. if File::ALT_SEPARATOR then
  440. "C:/"
  441. else
  442. "/"
  443. end
  444. end
  445. private_class_method :find_home
  446. ##
  447. # Zlib::GzipReader wrapper that unzips +data+.
  448. def self.gunzip(data)
  449. require 'stringio'
  450. require 'zlib'
  451. data = StringIO.new data
  452. Zlib::GzipReader.new(data).read
  453. end
  454. ##
  455. # Zlib::GzipWriter wrapper that zips +data+.
  456. def self.gzip(data)
  457. require 'stringio'
  458. require 'zlib'
  459. zipped = StringIO.new
  460. Zlib::GzipWriter.wrap zipped do |io| io.write data end
  461. zipped.string
  462. end
  463. ##
  464. # A Zlib::Inflate#inflate wrapper
  465. def self.inflate(data)
  466. require 'zlib'
  467. Zlib::Inflate.inflate data
  468. end
  469. ##
  470. # Return a list of all possible load paths for the latest version for all
  471. # gems in the Gem installation.
  472. def self.latest_load_paths
  473. result = []
  474. Gem.path.each do |gemdir|
  475. each_load_path(latest_partials(gemdir)) do |load_path|
  476. result << load_path
  477. end
  478. end
  479. result
  480. end
  481. ##
  482. # Return only the latest partial paths in the given +gemdir+.
  483. def self.latest_partials(gemdir)
  484. latest = {}
  485. all_partials(gemdir).each do |gp|
  486. base = File.basename(gp)
  487. if base =~ /(.*)-((\d+\.)*\d+)/ then
  488. name, version = $1, $2
  489. ver = Gem::Version.new(version)
  490. if latest[name].nil? || ver > latest[name][0]
  491. latest[name] = [ver, gp]
  492. end
  493. end
  494. end
  495. latest.collect { |k,v| v[1] }
  496. end
  497. private_class_method :latest_partials
  498. ##
  499. # The index to insert activated gem paths into the $LOAD_PATH.
  500. #
  501. # Defaults to the site lib directory unless gem_prelude.rb has loaded paths,
  502. # then it inserts the activated gem's paths before the gem_prelude.rb paths
  503. # so you can override the gem_prelude.rb default $LOAD_PATH paths.
  504. def self.load_path_insert_index
  505. index = $LOAD_PATH.index ConfigMap[:sitelibdir]
  506. $LOAD_PATH.each_with_index do |path, i|
  507. if path.instance_variables.include?(:@gem_prelude_index) or
  508. path.instance_variables.include?('@gem_prelude_index') then
  509. index = i
  510. break
  511. end
  512. end
  513. index
  514. end
  515. ##
  516. # The file name and line number of the caller of the caller of this method.
  517. def self.location_of_caller
  518. caller[1] =~ /(.*?):(\d+).*?$/i
  519. file = $1
  520. lineno = $2.to_i
  521. [file, lineno]
  522. end
  523. ##
  524. # The version of the Marshal format for your Ruby.
  525. def self.marshal_version
  526. "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}"
  527. end
  528. ##
  529. # Array of paths to search for Gems.
  530. def self.path
  531. @gem_path ||= nil
  532. unless @gem_path then
  533. paths = [ENV['GEM_PATH'] || Gem.configuration.path || default_path]
  534. if defined?(APPLE_GEM_HOME) and not ENV['GEM_PATH'] then
  535. paths << APPLE_GEM_HOME
  536. end
  537. set_paths paths.compact.join(File::PATH_SEPARATOR)
  538. end
  539. @gem_path
  540. end
  541. ##
  542. # Set array of platforms this RubyGems supports (primarily for testing).
  543. def self.platforms=(platforms)
  544. @platforms = platforms
  545. end
  546. ##
  547. # Array of platforms this RubyGems supports.
  548. def self.platforms
  549. @platforms ||= []
  550. if @platforms.empty?
  551. @platforms = [Gem::Platform::RUBY, Gem::Platform.local]
  552. end
  553. @platforms
  554. end
  555. ##
  556. # Adds a post-install hook that will be passed an Gem::Installer instance
  557. # when Gem::Installer#install is called
  558. def self.post_install(&hook)
  559. @post_install_hooks << hook
  560. end
  561. ##
  562. # Adds a post-uninstall hook that will be passed a Gem::Uninstaller instance
  563. # and the spec that was uninstalled when Gem::Uninstaller#uninstall is
  564. # called
  565. def self.post_uninstall(&hook)
  566. @post_uninstall_hooks << hook
  567. end
  568. ##
  569. # Adds a pre-install hook that will be passed an Gem::Installer instance
  570. # when Gem::Installer#install is called
  571. def self.pre_install(&hook)
  572. @pre_install_hooks << hook
  573. end
  574. ##
  575. # Adds a pre-uninstall hook that will be passed an Gem::Uninstaller instance
  576. # and the spec that will be uninstalled when Gem::Uninstaller#uninstall is
  577. # called
  578. def self.pre_uninstall(&hook)
  579. @pre_uninstall_hooks << hook
  580. end
  581. ##
  582. # The directory prefix this RubyGems was installed at.
  583. def self.prefix
  584. prefix = File.dirname File.expand_path(__FILE__)
  585. if File.dirname(prefix) == File.expand_path(ConfigMap[:sitelibdir]) or
  586. File.dirname(prefix) == File.expand_path(ConfigMap[:libdir]) or
  587. 'lib' != File.basename(prefix) then
  588. nil
  589. else
  590. File.dirname prefix
  591. end
  592. end
  593. ##
  594. # Promotes the load paths of the +gem_name+ over the load paths of
  595. # +over_name+. Useful for allowing one gem to override features in another
  596. # using #find_files.
  597. def self.promote_load_path(gem_name, over_name)
  598. gem = Gem.loaded_specs[gem_name]
  599. over = Gem.loaded_specs[over_name]
  600. raise ArgumentError, "gem #{gem_name} is not activated" if gem.nil?
  601. raise ArgumentError, "gem #{over_name} is not activated" if over.nil?
  602. last_gem_path = File.join gem.full_gem_path, gem.require_paths.last
  603. over_paths = over.require_paths.map do |path|
  604. File.join over.full_gem_path, path
  605. end
  606. over_paths.each do |path|
  607. $LOAD_PATH.delete path
  608. end
  609. gem = $LOAD_PATH.index(last_gem_path) + 1
  610. $LOAD_PATH.insert(gem, *over_paths)
  611. end
  612. ##
  613. # Refresh source_index from disk and clear searcher.
  614. def self.refresh
  615. source_index.refresh!
  616. MUTEX.synchronize do
  617. @searcher = nil
  618. end
  619. end
  620. ##
  621. # Safely read a file in binary mode on all platforms.
  622. def self.read_binary(path)
  623. File.open path, binary_mode do |f| f.read end
  624. end
  625. ##
  626. # Report a load error during activation. The message of load error
  627. # depends on whether it was a version mismatch or if there are not gems of
  628. # any version by the requested name.
  629. def self.report_activate_error(gem)
  630. matches = Gem.source_index.find_name(gem.name)
  631. if matches.empty? then
  632. error = Gem::LoadError.new(
  633. "Could not find RubyGem #{gem.name} (#{gem.version_requirements})\n")
  634. else
  635. error = Gem::LoadError.new(
  636. "RubyGem version error: " +
  637. "#{gem.name}(#{matches.first.version} not #{gem.version_requirements})\n")
  638. end
  639. error.name = gem.name
  640. error.version_requirement = gem.version_requirements
  641. raise error
  642. end
  643. private_class_method :report_activate_error
  644. ##
  645. # Full path to +libfile+ in +gemname+. Searches for the latest gem unless
  646. # +requirements+ is given.
  647. def self.required_location(gemname, libfile, *requirements)
  648. requirements = Gem::Requirement.default if requirements.empty?
  649. matches = Gem.source_index.find_name gemname, requirements
  650. return nil if matches.empty?
  651. spec = matches.last
  652. spec.require_paths.each do |path|
  653. result = File.join spec.full_gem_path, path, libfile
  654. return result if File.exist? result
  655. end
  656. nil
  657. end
  658. ##
  659. # The path to the running Ruby interpreter.
  660. def self.ruby
  661. if @ruby.nil? then
  662. @ruby = File.join(ConfigMap[:bindir],
  663. ConfigMap[:ruby_install_name])
  664. @ruby << ConfigMap[:EXEEXT]
  665. # escape string in case path to ruby executable contain spaces.
  666. @ruby.sub!(/.*\s.*/m, '"\&"')
  667. end
  668. @ruby
  669. end
  670. ##
  671. # A Gem::Version for the currently running ruby.
  672. def self.ruby_version
  673. return @ruby_version if defined? @ruby_version
  674. version = RUBY_VERSION.dup
  675. if defined?(RUBY_PATCHLEVEL) && RUBY_PATCHLEVEL != -1 then
  676. version << ".#{RUBY_PATCHLEVEL}"
  677. elsif defined?(RUBY_REVISION) then
  678. version << ".dev.#{RUBY_REVISION}"
  679. end
  680. @ruby_version = Gem::Version.new version
  681. end
  682. ##
  683. # The GemPathSearcher object used to search for matching installed gems.
  684. def self.searcher
  685. MUTEX.synchronize do
  686. @searcher ||= Gem::GemPathSearcher.new
  687. end
  688. end
  689. ##
  690. # Set the Gem home directory (as reported by Gem.dir).
  691. def self.set_home(home)
  692. home = home.gsub File::ALT_SEPARATOR, File::SEPARATOR if File::ALT_SEPARATOR
  693. @gem_home = home
  694. end
  695. private_class_method :set_home
  696. ##
  697. # Set the Gem search path (as reported by Gem.path).
  698. def self.set_paths(gpaths)
  699. if gpaths
  700. @gem_path = gpaths.split(File::PATH_SEPARATOR)
  701. if File::ALT_SEPARATOR then
  702. @gem_path.map! do |path|
  703. path.gsub File::ALT_SEPARATOR, File::SEPARATOR
  704. end
  705. end
  706. @gem_path << Gem.dir
  707. else
  708. # TODO: should this be Gem.default_path instead?
  709. @gem_path = [Gem.dir]
  710. end
  711. @gem_path.uniq!
  712. end
  713. private_class_method :set_paths
  714. ##
  715. # Returns the Gem::SourceIndex of specifications that are in the Gem.path
  716. def self.source_index
  717. @@source_index ||= SourceIndex.from_installed_gems
  718. end
  719. ##
  720. # Returns an Array of sources to fetch remote gems from. If the sources
  721. # list is empty, attempts to load the "sources" gem, then uses
  722. # default_sources if it is not installed.
  723. def self.sources
  724. if @sources.empty? then
  725. begin
  726. gem 'sources', '> 0.0.1'
  727. require 'sources'
  728. rescue LoadError
  729. @sources = default_sources
  730. end
  731. end
  732. @sources
  733. end
  734. ##
  735. # Need to be able to set the sources without calling
  736. # Gem.sources.replace since that would cause an infinite loop.
  737. def self.sources=(new_sources)
  738. @sources = new_sources
  739. end
  740. ##
  741. # Glob pattern for require-able path suffixes.
  742. def self.suffix_pattern
  743. @suffix_pattern ||= "{#{suffixes.join(',')}}"
  744. end
  745. ##
  746. # Suffixes for require-able paths.
  747. def self.suffixes
  748. ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar']
  749. end
  750. ##
  751. # Prints the amount of time the supplied block takes to run using the debug
  752. # UI output.
  753. def self.time(msg, width = 0, display = Gem.configuration.verbose)
  754. now = Time.now
  755. value = yield
  756. elapsed = Time.now - now
  757. ui.say "%2$*1$s: %3$3.3fs" % [-width, msg, elapsed] if display
  758. value
  759. end
  760. ##
  761. # Lazily loads DefaultUserInteraction and returns the default UI.
  762. def self.ui
  763. require 'rubygems/user_interaction'
  764. Gem::DefaultUserInteraction.ui
  765. end
  766. ##
  767. # Use the +home+ and +paths+ values for Gem.dir and Gem.path. Used mainly
  768. # by the unit tests to provide environment isolation.
  769. def self.use_paths(home, paths=[])
  770. clear_paths
  771. set_home(home) if home
  772. set_paths(paths.join(File::PATH_SEPARATOR)) if paths
  773. end
  774. ##
  775. # The home directory for the user.
  776. def self.user_home
  777. @user_home ||= find_home
  778. end
  779. ##
  780. # Is this a windows platform?
  781. def self.win_platform?
  782. if @@win_platform.nil? then
  783. @@win_platform = !!WIN_PATTERNS.find { |r| RUBY_PLATFORM =~ r }
  784. end
  785. @@win_platform
  786. end
  787. class << self
  788. ##
  789. # Hash of loaded Gem::Specification keyed by name
  790. attr_reader :loaded_specs
  791. ##
  792. # The list of hooks to be run before Gem::Install#install does any work
  793. attr_reader :post_install_hooks
  794. ##
  795. # The list of hooks to be run before Gem::Uninstall#uninstall does any
  796. # work
  797. attr_reader :post_uninstall_hooks
  798. ##
  799. # The list of hooks to be run after Gem::Install#install is finished
  800. attr_reader :pre_install_hooks
  801. ##
  802. # The list of hooks to be run after Gem::Uninstall#uninstall is finished
  803. attr_reader :pre_uninstall_hooks
  804. # :stopdoc:
  805. alias cache source_index # an alias for the old name
  806. # :startdoc:
  807. end
  808. ##
  809. # Location of Marshal quick gemspecs on remote repositories
  810. MARSHAL_SPEC_DIR = "quick/Marshal.#{Gem.marshal_version}/"
  811. ##
  812. # Location of legacy YAML quick gemspecs on remote repositories
  813. YAML_SPEC_DIR = 'quick/'
  814. end
  815. ##
  816. # Return the path to the data directory associated with the named package. If
  817. # the package is loaded as a gem, return the gem specific data directory.
  818. # Otherwise return a path to the share area as define by
  819. # "#{ConfigMap[:datadir]}/#{package_name}".
  820. def RbConfig.datadir(package_name)
  821. Gem.datadir(package_name) ||
  822. File.join(Gem::ConfigMap[:datadir], package_name)
  823. end
  824. require 'rubygems/exceptions'
  825. require 'rubygems/version'
  826. require 'rubygems/requirement'
  827. require 'rubygems/dependency'
  828. require 'rubygems/gem_path_searcher' # Needed for Kernel#gem
  829. require 'rubygems/source_index' # Needed for Kernel#gem
  830. require 'rubygems/platform'
  831. require 'rubygems/builder' # HACK: Needed for rake's package task.
  832. begin
  833. require 'rubygems/defaults/operating_system'
  834. rescue LoadError
  835. end
  836. if defined?(RUBY_ENGINE) then
  837. begin
  838. require "rubygems/defaults/#{RUBY_ENGINE}"
  839. rescue LoadError
  840. end
  841. end
  842. require 'rubygems/config_file'
  843. Gem.clear_paths
  844. plugins = Gem.find_files 'rubygems_plugin'
  845. plugins.each do |plugin|
  846. begin
  847. load plugin
  848. rescue => e
  849. warn "error loading #{plugin.inspect}: #{e.message} (#{e.class})"
  850. end
  851. end