5 require 'rdoc/ri/paths'
6 require 'rdoc/ri/formatter'
7 require 'rdoc/ri/display'
10 require 'rdoc/markup/to_flow'
12 class RDoc::RI::Driver
15 def self.convert(hash)
16 hash = new.update hash
18 hash.each do |key, value|
19 hash[key] = case value
23 value = value.map do |v|
24 ::Hash === v ? convert(v) : v
35 def method_missing method, *args
39 def merge_enums(other)
45 if String === self[k] and self[k].empty? then
62 class Error < RDoc::RI::Error; end
64 class NotFoundError < Error
66 "Nothing known about #{super}"
70 attr_accessor :homepath # :nodoc:
72 def self.process_args(argv)
74 options[:use_stdout] = !$stdout.tty?
76 options[:formatter] = RDoc::RI::Formatter.for 'plain'
77 options[:list_classes] = false
78 options[:list_names] = false
80 # By default all paths are used. If any of these are true, only those
81 # directories are used.
88 opts = OptionParser.new do |opt|
89 opt.program_name = File.basename $0
90 opt.version = RDoc::VERSION
91 opt.summary_indent = ' ' * 4
94 RDoc::RI::Paths::SYSDIR,
95 RDoc::RI::Paths::SITEDIR,
96 RDoc::RI::Paths::HOMEDIR
99 if RDoc::RI::Paths::GEMDIRS then
100 Gem.path.each do |dir|
101 directories << "#{dir}/doc/*/ri"
106 Usage: #{opt.program_name} [options] [names...]
110 Class | Class::method | Class#method | Class.method | method
112 All class names may be abbreviated to their minimum unambiguous form. If a name
113 is ambiguous, all valid options will be listed.
115 The form '.' method matches either class or instance methods, while #method
116 matches only instance and ::method matches only class methods.
120 #{opt.program_name} Fil
121 #{opt.program_name} File
122 #{opt.program_name} File.new
123 #{opt.program_name} zip
125 Note that shell quoting may be required for method names containing
128 #{opt.program_name} 'Array.[]'
129 #{opt.program_name} compact\\!
131 By default ri searches for documentation in the following directories:
133 #{directories.join "\n "}
135 Specifying the --system, --site, --home, --gems or --doc-dir options will
136 limit ri to searching only the specified directories.
138 Options may also be set in the 'RI' environment variable.
142 opt.separator "Options:"
145 opt.on("--classes", "-c",
146 "Display the names of classes and modules we",
147 "know about.") do |value|
148 options[:list_classes] = value
153 opt.on("--doc-dir=DIRNAME", "-d", Array,
154 "List of directories to search for",
155 "documentation. If not specified, we search",
156 "the standard rdoc/ri directories. May be",
157 "repeated.") do |value|
159 unless File.directory? dir then
160 raise OptionParser::InvalidArgument, "#{dir} is not a directory"
164 doc_dirs.concat value
169 opt.on("--fmt=FORMAT", "--format=FORMAT", "-f",
170 RDoc::RI::Formatter::FORMATTERS.keys,
171 "Format to use when displaying output:",
172 " #{RDoc::RI::Formatter.list}",
173 "Use 'bs' (backspace) with most pager",
174 "programs. To use ANSI, either disable the",
175 "pager or tell the pager to allow control",
176 "characters.") do |value|
177 options[:formatter] = RDoc::RI::Formatter.for value
182 unless RDoc::RI::Paths::GEMDIRS.empty? then
183 opt.on("--[no-]gems",
184 "Include documentation from RubyGems.") do |value|
191 opt.on("--[no-]home",
192 "Include documentation stored in ~/.rdoc.") do |value|
198 opt.on("--[no-]list-names", "-l",
199 "List all the names known to RDoc, one per",
201 options[:list_names] = value
206 opt.on("--no-pager", "-T",
207 "Send output directly to stdout.") do |value|
208 options[:use_stdout] = !value
213 opt.on("--[no-]site",
214 "Include documentation from libraries",
215 "installed in site_lib.") do |value|
221 opt.on("--[no-]system",
222 "Include documentation from Ruby's standard",
223 "library.") do |value|
229 opt.on("--width=WIDTH", "-w", OptionParser::DecimalInteger,
230 "Set the width of the output.") do |value|
231 options[:width] = value
235 argv = ENV['RI'].to_s.split.concat argv
239 options[:names] = argv
241 options[:path] = RDoc::RI::Paths.path(use_system, use_site, use_home,
243 options[:raw_path] = RDoc::RI::Paths.raw_path(use_system, use_site,
244 use_home, use_gems, *doc_dirs)
248 rescue OptionParser::InvalidArgument, OptionParser::InvalidOption => e
255 def self.run(argv = ARGV)
256 options = process_args argv
261 def initialize(options={})
262 options[:formatter] ||= RDoc::RI::Formatter.for('plain')
263 options[:use_stdout] ||= !$stdout.tty?
264 options[:width] ||= 72
265 @names = options[:names]
267 @class_cache_name = 'classes'
268 @all_dirs = RDoc::RI::Paths.path(true, true, true, true)
269 @homepath = RDoc::RI::Paths.raw_path(false, false, true, false).first
270 @homepath = @homepath.sub(/\.rdoc/, '.ri')
271 @sys_dirs = RDoc::RI::Paths.raw_path(true, false, false, false)
273 FileUtils.mkdir_p cache_file_path unless File.directory? cache_file_path
277 @display = RDoc::RI::DefaultDisplay.new(options[:formatter],
279 options[:use_stdout])
283 return @class_cache if @class_cache
285 newest = map_dirs('created.rid', :all) do |f|
286 File.mtime f if test ?f, f
289 up_to_date = (File.exist?(class_cache_file_path) and
290 newest and newest < File.mtime(class_cache_file_path))
292 @class_cache = if up_to_date then
293 load_cache_for @class_cache_name
295 class_cache = RDoc::RI::Driver::Hash.new
297 classes = map_dirs('**/cdesc*.yaml', :sys) { |f| Dir[f] }
298 populate_class_cache class_cache, classes
300 classes = map_dirs('**/cdesc*.yaml') { |f| Dir[f] }
301 warn "Updating class cache with #{classes.size} classes..."
303 populate_class_cache class_cache, classes, true
304 write_cache class_cache, class_cache_file_path
308 def class_cache_file_path
309 File.join cache_file_path, @class_cache_name
312 def cache_file_for(klassname)
313 File.join cache_file_path, klassname.gsub(/:+/, "-")
317 File.join @homepath, 'cache'
320 def display_class(name)
321 klass = class_cache[name]
322 klass = RDoc::RI::Driver::Hash.convert klass
323 @display.display_class_info klass, class_cache
326 def get_info_for(arg)
331 def load_cache_for(klassname)
332 path = cache_file_for klassname
336 if File.exist? path and
337 File.mtime(path) >= File.mtime(class_cache_file_path) then
338 File.open path, 'rb' do |fp|
339 cache = Marshal.load fp.read
344 File.open class_cache_file_path, 'rb' do |fp|
345 class_cache = Marshal.load fp.read
348 klass = class_cache[klassname]
349 return nil unless klass
351 method_files = klass["sources"]
352 cache = RDoc::RI::Driver::Hash.new
354 sys_dir = @sys_dirs.first
355 method_files.each do |f|
356 system_file = f.index(sys_dir) == 0
357 Dir[File.join(File.dirname(f), "*")].each do |yaml|
358 next unless yaml =~ /yaml$/
359 next if yaml =~ /cdesc-[^\/]+yaml$/
360 method = read_yaml yaml
361 name = method["full_name"]
363 ext_path = "gem #{$1}" if f =~ %r%gems/[\d.]+/doc/([^/]+)%
364 method["source_path"] = ext_path unless system_file
365 cache[name] = RDoc::RI::Driver::Hash.convert method
369 write_cache cache, path
372 RDoc::RI::Driver::Hash.convert cache
378 def lookup_method(name, klass)
379 cache = load_cache_for klass
380 raise NotFoundError, name unless cache
382 method = cache[name.gsub('.', '#')]
383 method = cache[name.gsub('.', '::')] unless method
384 raise NotFoundError, name unless method
389 def map_dirs(file_name, system=false)
390 dirs = if system == :all then
396 @all_dirs - @sys_dirs
400 dirs.map { |dir| yield File.join(dir, file_name) }.flatten.compact
404 # Extract the class and method name parts from +name+ like Foo::Bar#baz
407 parts = name.split(/(::|\#|\.)/)
409 if parts[-2] != '::' or parts.last !~ /^[A-Z]/ then
419 def populate_class_cache(class_cache, classes, extension = false)
420 classes.each do |cdesc|
421 desc = read_yaml cdesc
422 klassname = desc["full_name"]
424 unless class_cache.has_key? klassname then
425 desc["display_name"] = "Class"
426 desc["sources"] = [cdesc]
427 desc["instance_method_extensions"] = []
428 desc["class_method_extensions"] = []
429 class_cache[klassname] = desc
431 klass = class_cache[klassname]
434 desc["instance_method_extensions"] = desc.delete "instance_methods"
435 desc["class_method_extensions"] = desc.delete "class_methods"
438 klass.merge_enums desc
439 klass["sources"] << cdesc
445 data = File.read path
446 data = data.gsub(/ \!ruby\/(object|struct):(RDoc::RI|RI).*/, '')
447 data = data.gsub(/ \!ruby\/(object|struct):SM::(\S+)/,
448 ' !ruby/\1:RDoc::Markup::\2')
453 if @names.empty? then
454 @display.list_known_classes class_cache.keys.sort
456 @names.each do |name|
459 if class_cache.key? name then
464 klass, meth = parse_name name
466 method = lookup_method name, klass
468 @display.display_method_info method
471 if class_cache.key? name then
474 methods = select_methods(/^#{name}/)
476 raise NotFoundError, name
477 elsif methods.size == 1
478 @display.display_method_info methods.first
480 @display.display_method_list methods
486 rescue NotFoundError => e
490 def select_methods(pattern)
492 class_cache.keys.sort.each do |klass|
493 class_cache[klass]["instance_methods"].map{|h|h["name"]}.grep(pattern) do |name|
494 method = load_cache_for(klass)[klass+'#'+name]
495 methods << method if method
497 class_cache[klass]["class_methods"].map{|h|h["name"]}.grep(pattern) do |name|
498 method = load_cache_for(klass)[klass+'::'+name]
499 methods << method if method
505 def write_cache(cache, path)
506 File.open path, "wb" do |cache_file|
507 Marshal.dump cache, cache_file