2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2008-01-08 05:18:41 -05:00
|
|
|
require 'rdoc/ri'
|
|
|
|
|
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# The directories where ri data lives. Paths can be enumerated via ::each, or
|
|
|
|
# queried individually via ::system_dir, ::site_dir, ::home_dir and ::gem_dir.
|
2008-01-08 05:18:41 -05:00
|
|
|
|
|
|
|
module RDoc::RI::Paths
|
|
|
|
|
|
|
|
#:stopdoc:
|
|
|
|
require 'rbconfig'
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
version = RbConfig::CONFIG['ruby_version']
|
2008-01-08 05:18:41 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
BASE = if RbConfig::CONFIG.key? 'ridir' then
|
2010-12-19 22:22:49 -05:00
|
|
|
File.join RbConfig::CONFIG['ridir'], version
|
|
|
|
else
|
|
|
|
File.join RbConfig::CONFIG['datadir'], 'ri', version
|
|
|
|
end
|
|
|
|
|
2010-12-28 17:08:56 -05:00
|
|
|
homedir = begin
|
|
|
|
File.expand_path('~')
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
|
|
|
|
|
|
|
homedir ||= ENV['HOME'] ||
|
|
|
|
ENV['USERPROFILE'] || ENV['HOMEPATH'] # for 1.8 compatibility
|
|
|
|
|
|
|
|
HOMEDIR = if homedir then
|
|
|
|
File.join homedir, ".rdoc"
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
#:startdoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Iterates over each selected path yielding the directory and type.
|
|
|
|
#
|
|
|
|
# Yielded types:
|
|
|
|
# :system:: Where Ruby's ri data is stored. Yielded when +system+ is
|
|
|
|
# true
|
|
|
|
# :site:: Where ri for installed libraries are stored. Yielded when
|
|
|
|
# +site+ is true. Normally no ri data is stored here.
|
2010-09-16 08:02:34 -04:00
|
|
|
# :home:: ~/.rdoc. Yielded when +home+ is true.
|
2010-04-01 03:45:16 -04:00
|
|
|
# :gem:: ri data for an installed gem. Yielded when +gems+ is true.
|
|
|
|
# :extra:: ri data directory from the command line. Yielded for each
|
|
|
|
# entry in +extra_dirs+
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def self.each system = true, site = true, home = true, gems = :latest, *extra_dirs # :yields: directory, type
|
|
|
|
return enum_for __method__, system, site, home, gems, *extra_dirs unless
|
|
|
|
block_given?
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
extra_dirs.each do |dir|
|
|
|
|
yield dir, :extra
|
2009-05-23 12:07:46 -04:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
yield system_dir, :system if system
|
|
|
|
yield site_dir, :site if site
|
|
|
|
yield home_dir, :home if home and HOMEDIR
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
gemdirs(gems).each do |dir|
|
2010-04-01 03:45:16 -04:00
|
|
|
yield dir, :gem
|
|
|
|
end if gems
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# The ri directory for the gem with +gem_name+.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def self.gem_dir name, version
|
|
|
|
req = Gem::Requirement.new "= #{version}"
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
spec = Gem::Specification.find_by_name name, req
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
File.join spec.doc_dir, 'ri'
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# The latest installed gems' ri directories. +filter+ can be :all or
|
|
|
|
# :latest.
|
|
|
|
#
|
|
|
|
# A +filter+ :all includes all versions of gems and includes gems without
|
|
|
|
# ri documentation.
|
|
|
|
|
|
|
|
def self.gemdirs filter = :latest
|
|
|
|
require 'rubygems' unless defined?(Gem)
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
ri_paths = {}
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
all = Gem::Specification.map do |spec|
|
|
|
|
[File.join(spec.doc_dir, 'ri'), spec.name, spec.version]
|
|
|
|
end
|
|
|
|
|
|
|
|
if filter == :all then
|
|
|
|
gemdirs = []
|
|
|
|
|
|
|
|
all.group_by do |_, name, _|
|
|
|
|
name
|
|
|
|
end.sort_by do |group, _|
|
|
|
|
group
|
|
|
|
end.map do |group, items|
|
|
|
|
items.sort_by do |_, _, version|
|
|
|
|
version
|
|
|
|
end.reverse_each do |dir,|
|
|
|
|
gemdirs << dir
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
return gemdirs
|
|
|
|
end
|
|
|
|
|
|
|
|
all.each do |dir, name, ver|
|
|
|
|
next unless File.exist? dir
|
|
|
|
|
|
|
|
if ri_paths[name].nil? or ver > ri_paths[name].first then
|
|
|
|
ri_paths[name] = [ver, name, dir]
|
|
|
|
end
|
2009-05-23 12:07:46 -04:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
ri_paths.sort_by { |_, (_, name, _)| name }.map { |k, v| v.last }
|
2010-04-01 03:45:16 -04:00
|
|
|
rescue LoadError
|
2012-11-26 23:28:14 -05:00
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# The location of the rdoc data in the user's home directory.
|
|
|
|
#
|
|
|
|
# Like ::system, ri data in the user's home directory is rare and predates
|
|
|
|
# libraries distributed via RubyGems. ri data is rarely generated into this
|
|
|
|
# directory.
|
|
|
|
|
|
|
|
def self.home_dir
|
|
|
|
HOMEDIR
|
2009-03-25 22:33:16 -04:00
|
|
|
end
|
2008-01-08 05:18:41 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Returns existing directories from the selected documentation directories
|
|
|
|
# as an Array.
|
|
|
|
#
|
|
|
|
# See also ::each
|
2008-01-08 05:18:41 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def self.path(system = true, site = true, home = true, gems = :latest, *extra_dirs)
|
2010-04-01 03:45:16 -04:00
|
|
|
path = raw_path system, site, home, gems, *extra_dirs
|
2008-01-08 05:18:41 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
path.select { |directory| File.directory? directory }
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Returns selected documentation directories including nonexistent
|
|
|
|
# directories.
|
|
|
|
#
|
|
|
|
# See also ::each
|
2008-01-08 05:18:41 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def self.raw_path(system, site, home, gems, *extra_dirs)
|
2008-01-08 05:18:41 -05:00
|
|
|
path = []
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
each(system, site, home, gems, *extra_dirs) do |dir, type|
|
|
|
|
path << dir
|
|
|
|
end
|
|
|
|
|
|
|
|
path.compact
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# The location of ri data installed into the site dir.
|
|
|
|
#
|
2013-07-18 22:26:08 -04:00
|
|
|
# Historically this was available for documentation installed by Ruby
|
2012-11-26 23:28:14 -05:00
|
|
|
# libraries predating RubyGems. It is unlikely to contain any content for
|
2013-07-18 22:26:08 -04:00
|
|
|
# modern Ruby installations.
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
def self.site_dir
|
|
|
|
File.join BASE, 'site'
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# The location of the built-in ri data.
|
|
|
|
#
|
2013-07-18 22:26:08 -04:00
|
|
|
# This data is built automatically when `make` is run when Ruby is
|
|
|
|
# installed. If you did not install Ruby by hand you may need to install
|
2012-11-26 23:28:14 -05:00
|
|
|
# the documentation yourself. Please consult the documentation for your
|
2013-07-18 22:26:08 -04:00
|
|
|
# package manager or Ruby installer for details. You can also use the
|
|
|
|
# rdoc-data gem to install system ri data for common versions of Ruby.
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
def self.system_dir
|
|
|
|
File.join BASE, 'system'
|
|
|
|
end
|
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|