PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/rubygems.rb

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