| 1 |
# rake/rdoctask is deprecated in favor of rdoc/task |
| 2 |
|
| 3 |
warn 'rake/rdoctask is deprecated. Use rdoc/task instead (in RDoc 2.4.2+)' |
| 4 |
|
| 5 |
require 'rubygems' |
| 6 |
|
| 7 |
begin |
| 8 |
gem 'rdoc' |
| 9 |
require 'rdoc' |
| 10 |
require 'rdoc/task' |
| 11 |
rescue LoadError, Gem::LoadError |
| 12 |
end |
| 13 |
|
| 14 |
if defined?(RDoc::Task) then |
| 15 |
module Rake |
| 16 |
RDocTask = RDoc::Task unless const_defined? :RDocTask |
| 17 |
end |
| 18 |
else |
| 19 |
require 'rake' |
| 20 |
require 'rake/tasklib' |
| 21 |
|
| 22 |
module Rake |
| 23 |
|
| 24 |
# NOTE: Rake::RDocTask is deprecated in favor of RDoc:Task which is included |
| 25 |
# in RDoc 2.4.2+. Use require 'rdoc/task' to require it. |
| 26 |
# |
| 27 |
# Create a documentation task that will generate the RDoc files for |
| 28 |
# a project. |
| 29 |
# |
| 30 |
# The RDocTask will create the following targets: |
| 31 |
# |
| 32 |
# [<b><em>rdoc</em></b>] |
| 33 |
# Main task for this RDOC task. |
| 34 |
# |
| 35 |
# [<b>:clobber_<em>rdoc</em></b>] |
| 36 |
# Delete all the rdoc files. This target is automatically |
| 37 |
# added to the main clobber target. |
| 38 |
# |
| 39 |
# [<b>:re<em>rdoc</em></b>] |
| 40 |
# Rebuild the rdoc files from scratch, even if they are not out |
| 41 |
# of date. |
| 42 |
# |
| 43 |
# Simple Example: |
| 44 |
# |
| 45 |
# Rake::RDocTask.new do |rd| |
| 46 |
# rd.main = "README.rdoc" |
| 47 |
# rd.rdoc_files.include("README.rdoc", "lib/**/*.rb") |
| 48 |
# end |
| 49 |
# |
| 50 |
# The +rd+ object passed to the block is an RDocTask object. See the |
| 51 |
# attributes list for the RDocTask class for available customization options. |
| 52 |
# |
| 53 |
# == Specifying different task names |
| 54 |
# |
| 55 |
# You may wish to give the task a different name, such as if you are |
| 56 |
# generating two sets of documentation. For instance, if you want to have a |
| 57 |
# development set of documentation including private methods: |
| 58 |
# |
| 59 |
# Rake::RDocTask.new(:rdoc_dev) do |rd| |
| 60 |
# rd.main = "README.doc" |
| 61 |
# rd.rdoc_files.include("README.rdoc", "lib/**/*.rb") |
| 62 |
# rd.options << "--all" |
| 63 |
# end |
| 64 |
# |
| 65 |
# The tasks would then be named :<em>rdoc_dev</em>, :clobber_<em>rdoc_dev</em>, and |
| 66 |
# :re<em>rdoc_dev</em>. |
| 67 |
# |
| 68 |
# If you wish to have completely different task names, then pass a Hash as |
| 69 |
# first argument. With the <tt>:rdoc</tt>, <tt>:clobber_rdoc</tt> and |
| 70 |
# <tt>:rerdoc</tt> options, you can customize the task names to your liking. |
| 71 |
# For example: |
| 72 |
# |
| 73 |
# Rake::RDocTask.new(:rdoc => "rdoc", :clobber_rdoc => "rdoc:clean", :rerdoc => "rdoc:force") |
| 74 |
# |
| 75 |
# This will create the tasks <tt>:rdoc</tt>, <tt>:rdoc_clean</tt> and |
| 76 |
# <tt>:rdoc:force</tt>. |
| 77 |
# |
| 78 |
class RDocTask < TaskLib |
| 79 |
# Name of the main, top level task. (default is :rdoc) |
| 80 |
attr_accessor :name |
| 81 |
|
| 82 |
# Name of directory to receive the html output files. (default is "html") |
| 83 |
attr_accessor :rdoc_dir |
| 84 |
|
| 85 |
# Title of RDoc documentation. (defaults to rdoc's default) |
| 86 |
attr_accessor :title |
| 87 |
|
| 88 |
# Name of file to be used as the main, top level file of the |
| 89 |
# RDoc. (default is none) |
| 90 |
attr_accessor :main |
| 91 |
|
| 92 |
# Name of template to be used by rdoc. (defaults to rdoc's default) |
| 93 |
attr_accessor :template |
| 94 |
|
| 95 |
# List of files to be included in the rdoc generation. (default is []) |
| 96 |
attr_accessor :rdoc_files |
| 97 |
|
| 98 |
# Additional list of options to be passed rdoc. (default is []) |
| 99 |
attr_accessor :options |
| 100 |
|
| 101 |
# Whether to run the rdoc process as an external shell (default is false) |
| 102 |
attr_accessor :external |
| 103 |
|
| 104 |
attr_accessor :inline_source |
| 105 |
|
| 106 |
# Create an RDoc task with the given name. See the RDocTask class overview |
| 107 |
# for documentation. |
| 108 |
def initialize(name = :rdoc) # :yield: self |
| 109 |
if name.is_a?(Hash) |
| 110 |
invalid_options = name.keys.map { |k| k.to_sym } - [:rdoc, :clobber_rdoc, :rerdoc] |
| 111 |
if !invalid_options.empty? |
| 112 |
raise ArgumentError, "Invalid option(s) passed to RDocTask.new: #{invalid_options.join(", ")}" |
| 113 |
end |
| 114 |
end |
| 115 |
|
| 116 |
@name = name |
| 117 |
@rdoc_files = Rake::FileList.new |
| 118 |
@rdoc_dir = 'html' |
| 119 |
@main = nil |
| 120 |
@title = nil |
| 121 |
@template = nil |
| 122 |
@external = false |
| 123 |
@inline_source = true |
| 124 |
@options = [] |
| 125 |
yield self if block_given? |
| 126 |
define |
| 127 |
end |
| 128 |
|
| 129 |
# Create the tasks defined by this task lib. |
| 130 |
def define |
| 131 |
if rdoc_task_name != "rdoc" |
| 132 |
desc "Build the RDOC HTML Files" |
| 133 |
else |
| 134 |
desc "Build the #{rdoc_task_name} HTML Files" |
| 135 |
end |
| 136 |
task rdoc_task_name |
| 137 |
|
| 138 |
desc "Force a rebuild of the RDOC files" |
| 139 |
task rerdoc_task_name => [clobber_task_name, rdoc_task_name] |
| 140 |
|
| 141 |
desc "Remove rdoc products" |
| 142 |
task clobber_task_name do |
| 143 |
rm_r rdoc_dir rescue nil |
| 144 |
end |
| 145 |
|
| 146 |
task :clobber => [clobber_task_name] |
| 147 |
|
| 148 |
directory @rdoc_dir |
| 149 |
task rdoc_task_name => [rdoc_target] |
| 150 |
file rdoc_target => @rdoc_files + [Rake.application.rakefile] do |
| 151 |
rm_r @rdoc_dir rescue nil |
| 152 |
@before_running_rdoc.call if @before_running_rdoc |
| 153 |
args = option_list + @rdoc_files |
| 154 |
if @external |
| 155 |
argstring = args.join(' ') |
| 156 |
sh %{ruby -Ivendor vendor/rd #{argstring}} |
| 157 |
else |
| 158 |
require 'rdoc/rdoc' |
| 159 |
RDoc::RDoc.new.document(args) |
| 160 |
end |
| 161 |
end |
| 162 |
self |
| 163 |
end |
| 164 |
|
| 165 |
def option_list |
| 166 |
result = @options.dup |
| 167 |
result << "-o" << @rdoc_dir |
| 168 |
result << "--main" << quote(main) if main |
| 169 |
result << "--title" << quote(title) if title |
| 170 |
result << "-T" << quote(template) if template |
| 171 |
result << "--inline-source" if inline_source && [email protected]?("--inline-source") && [email protected]?("-S") |
| 172 |
result |
| 173 |
end |
| 174 |
|
| 175 |
def quote(str) |
| 176 |
if @external |
| 177 |
"'#{str}'" |
| 178 |
else |
| 179 |
str |
| 180 |
end |
| 181 |
end |
| 182 |
|
| 183 |
def option_string |
| 184 |
option_list.join(' ') |
| 185 |
end |
| 186 |
|
| 187 |
# The block passed to this method will be called just before running the |
| 188 |
# RDoc generator. It is allowed to modify RDocTask attributes inside the |
| 189 |
# block. |
| 190 |
def before_running_rdoc(&block) |
| 191 |
@before_running_rdoc = block |
| 192 |
end |
| 193 |
|
| 194 |
private |
| 195 |
|
| 196 |
def rdoc_target |
| 197 |
"#{rdoc_dir}/index.html" |
| 198 |
end |
| 199 |
|
| 200 |
def rdoc_task_name |
| 201 |
case name |
| 202 |
when Hash |
| 203 |
(name[:rdoc] || "rdoc").to_s |
| 204 |
else |
| 205 |
name.to_s |
| 206 |
end |
| 207 |
end |
| 208 |
|
| 209 |
def clobber_task_name |
| 210 |
case name |
| 211 |
when Hash |
| 212 |
(name[:clobber_rdoc] || "clobber_rdoc").to_s |
| 213 |
else |
| 214 |
"clobber_#{name}" |
| 215 |
end |
| 216 |
end |
| 217 |
|
| 218 |
def rerdoc_task_name |
| 219 |
case name |
| 220 |
when Hash |
| 221 |
(name[:rerdoc] || "rerdoc").to_s |
| 222 |
else |
| 223 |
"re#{name}" |
| 224 |
end |
| 225 |
end |
| 226 |
|
| 227 |
end |
| 228 |
end |
| 229 |
end |
| 230 |
|