rdoctask.rb 6.5 KB

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