1require 'rubygems/command'
2require 'rubygems/version_option'
3require 'rubygems/rdoc'
4require 'fileutils'
5
6class Gem::Commands::RdocCommand < Gem::Command
7  include Gem::VersionOption
8
9  def initialize
10    super 'rdoc', 'Generates RDoc for pre-installed gems',
11          :version => Gem::Requirement.default,
12          :include_rdoc => false, :include_ri => true, :overwrite => false
13
14    add_option('--all',
15               'Generate RDoc/RI documentation for all',
16               'installed gems') do |value, options|
17      options[:all] = value
18    end
19
20    add_option('--[no-]rdoc',
21               'Generate RDoc HTML') do |value, options|
22      options[:include_rdoc] = value
23    end
24
25    add_option('--[no-]ri',
26               'Generate RI data') do |value, options|
27      options[:include_ri] = value
28    end
29
30    add_option('--[no-]overwrite',
31               'Overwrite installed documents') do |value, options|
32      options[:overwrite] = value
33    end
34
35    add_version_option
36  end
37
38  def arguments # :nodoc:
39    "GEMNAME       gem to generate documentation for (unless --all)"
40  end
41
42  def defaults_str # :nodoc:
43    "--version '#{Gem::Requirement.default}' --ri --no-overwrite"
44  end
45
46  def description # :nodoc:
47    <<-DESC
48The rdoc command builds RDoc and RI documentation for installed gems.  Use
49--overwrite to force rebuilding of documentation.
50    DESC
51  end
52
53  def usage # :nodoc:
54    "#{program_name} [args]"
55  end
56
57  def execute
58    specs = if options[:all] then
59              Gem::Specification.to_a
60            else
61              get_all_gem_names.map do |name|
62                Gem::Specification.find_by_name name, options[:version]
63              end.flatten.uniq
64            end
65
66    if specs.empty? then
67      alert_error 'No matching gems found'
68      terminate_interaction 1
69    end
70
71    specs.each do |spec|
72      doc = Gem::RDoc.new spec, options[:include_rdoc], options[:include_ri]
73
74      doc.force = options[:overwrite]
75
76      if options[:overwrite] then
77        FileUtils.rm_rf File.join(spec.doc_dir, 'ri')
78        FileUtils.rm_rf File.join(spec.doc_dir, 'rdoc')
79      end
80
81      begin
82        doc.generate
83      rescue Errno::ENOENT => e
84        e.message =~ / - /
85        alert_error "Unable to document #{spec.full_name}, #{$'} is missing, skipping"
86        terminate_interaction 1 if specs.length == 1
87      end
88    end
89  end
90
91end
92
93