task.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #--
  2. # Copyright (c) 2003, 2004 Jim Weirich, 2009 Eric Hodel
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining
  5. # a copy of this software and associated documentation files (the
  6. # "Software"), to deal in the Software without restriction, including
  7. # without limitation the rights to use, copy, modify, merge, publish,
  8. # distribute, sublicense, and/or sell copies of the Software, and to
  9. # permit persons to whom the Software is furnished to do so, subject to
  10. # the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be
  13. # included in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #++
  23. require 'rubygems'
  24. begin
  25. gem 'rdoc'
  26. rescue Gem::LoadError
  27. end unless defined?(RDoc)
  28. begin
  29. gem 'rake'
  30. rescue Gem::LoadError
  31. end unless defined?(Rake)
  32. require 'rdoc'
  33. require 'rake'
  34. require 'rake/tasklib'
  35. ##
  36. # RDoc::Task creates the following rake tasks to generate and clean up RDoc
  37. # output:
  38. #
  39. # [rdoc]
  40. # Main task for this RDoc task.
  41. #
  42. # [clobber_rdoc]
  43. # Delete all the rdoc files. This target is automatically added to the main
  44. # clobber target.
  45. #
  46. # [rerdoc]
  47. # Rebuild the rdoc files from scratch, even if they are not out of date.
  48. #
  49. # Simple Example:
  50. #
  51. # require 'rdoc/task'
  52. #
  53. # RDoc::Task.new do |rdoc|
  54. # rdoc.main = "README.rdoc"
  55. # rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb")
  56. # end
  57. #
  58. # The +rdoc+ object passed to the block is an RDoc::Task object. See the
  59. # attributes list for the RDoc::Task class for available customization options.
  60. #
  61. # == Specifying different task names
  62. #
  63. # You may wish to give the task a different name, such as if you are
  64. # generating two sets of documentation. For instance, if you want to have a
  65. # development set of documentation including private methods:
  66. #
  67. # require 'rdoc/task'
  68. #
  69. # RDoc::Task.new :rdoc_dev do |rdoc|
  70. # rdoc.main = "README.doc"
  71. # rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb")
  72. # rdoc.options << "--all"
  73. # end
  74. #
  75. # The tasks would then be named :<em>rdoc_dev</em>,
  76. # :clobber_<em>rdoc_dev</em>, and :re<em>rdoc_dev</em>.
  77. #
  78. # If you wish to have completely different task names, then pass a Hash as
  79. # first argument. With the <tt>:rdoc</tt>, <tt>:clobber_rdoc</tt> and
  80. # <tt>:rerdoc</tt> options, you can customize the task names to your liking.
  81. #
  82. # For example:
  83. #
  84. # require 'rdoc/task'
  85. #
  86. # RDoc::Task.new(:rdoc => "rdoc", :clobber_rdoc => "rdoc:clean",
  87. # :rerdoc => "rdoc:force")
  88. #
  89. # This will create the tasks <tt>:rdoc</tt>, <tt>:rdoc:clean</tt> and
  90. # <tt>:rdoc:force</tt>.
  91. class RDoc::Task < Rake::TaskLib
  92. ##
  93. # Name of the main, top level task. (default is :rdoc)
  94. attr_accessor :name
  95. ##
  96. # Comment markup format. rdoc, rd and tomdoc are supported. (default is
  97. # 'rdoc')
  98. attr_accessor :markup
  99. ##
  100. # Name of directory to receive the html output files. (default is "html")
  101. attr_accessor :rdoc_dir
  102. ##
  103. # Title of RDoc documentation. (defaults to rdoc's default)
  104. attr_accessor :title
  105. ##
  106. # Name of file to be used as the main, top level file of the RDoc. (default
  107. # is none)
  108. attr_accessor :main
  109. ##
  110. # Name of template to be used by rdoc. (defaults to rdoc's default)
  111. attr_accessor :template
  112. ##
  113. # Name of format generator (--fmt) used by rdoc. (defaults to rdoc's default)
  114. attr_accessor :generator
  115. ##
  116. # List of files to be included in the rdoc generation. (default is [])
  117. attr_accessor :rdoc_files
  118. ##
  119. # Additional list of options to be passed rdoc. (default is [])
  120. attr_accessor :options
  121. ##
  122. # Whether to run the rdoc process as an external shell (default is false)
  123. attr_accessor :external
  124. ##
  125. # Create an RDoc task with the given name. See the RDoc::Task class overview
  126. # for documentation.
  127. def initialize name = :rdoc # :yield: self
  128. defaults
  129. check_names name
  130. @name = name
  131. yield self if block_given?
  132. define
  133. end
  134. ##
  135. # Ensures that +names+ only includes names for the :rdoc, :clobber_rdoc and
  136. # :rerdoc. If other names are given an ArgumentError is raised.
  137. def check_names names
  138. return unless Hash === names
  139. invalid_options =
  140. names.keys.map { |k| k.to_sym } - [:rdoc, :clobber_rdoc, :rerdoc]
  141. unless invalid_options.empty? then
  142. raise ArgumentError, "invalid options: #{invalid_options.join ', '}"
  143. end
  144. end
  145. ##
  146. # Task description for the clobber rdoc task or its renamed equivalent
  147. def clobber_task_description
  148. "Remove RDoc HTML files"
  149. end
  150. ##
  151. # Sets default task values
  152. def defaults
  153. @name = :rdoc
  154. @rdoc_files = Rake::FileList.new
  155. @rdoc_dir = 'html'
  156. @main = nil
  157. @title = nil
  158. @template = nil
  159. @generator = nil
  160. @options = []
  161. end
  162. ##
  163. # All source is inline now. This method is deprecated
  164. def inline_source # :nodoc:
  165. warn "RDoc::Task#inline_source is deprecated"
  166. true
  167. end
  168. ##
  169. # All source is inline now. This method is deprecated
  170. def inline_source=(value) # :nodoc:
  171. warn "RDoc::Task#inline_source is deprecated"
  172. end
  173. ##
  174. # Create the tasks defined by this task lib.
  175. def define
  176. desc rdoc_task_description
  177. task rdoc_task_name
  178. desc rerdoc_task_description
  179. task rerdoc_task_name => [clobber_task_name, rdoc_task_name]
  180. desc clobber_task_description
  181. task clobber_task_name do
  182. rm_r @rdoc_dir rescue nil
  183. end
  184. task :clobber => [clobber_task_name]
  185. directory @rdoc_dir
  186. rdoc_target_deps = [
  187. @rdoc_files,
  188. Rake.application.rakefile
  189. ].flatten.compact
  190. task rdoc_task_name => [rdoc_target]
  191. file rdoc_target => rdoc_target_deps do
  192. @before_running_rdoc.call if @before_running_rdoc
  193. args = option_list + @rdoc_files
  194. $stderr.puts "rdoc #{args.join ' '}" if Rake.application.options.trace
  195. RDoc::RDoc.new.document args
  196. end
  197. self
  198. end
  199. ##
  200. # List of options that will be supplied to RDoc
  201. def option_list
  202. result = @options.dup
  203. result << "-o" << @rdoc_dir
  204. result << "--main" << main if main
  205. result << "--markup" << markup if markup
  206. result << "--title" << title if title
  207. result << "-T" << template if template
  208. result << '-f' << generator if generator
  209. result
  210. end
  211. ##
  212. # The block passed to this method will be called just before running the
  213. # RDoc generator. It is allowed to modify RDoc::Task attributes inside the
  214. # block.
  215. def before_running_rdoc(&block)
  216. @before_running_rdoc = block
  217. end
  218. ##
  219. # Task description for the rdoc task or its renamed equivalent
  220. def rdoc_task_description
  221. 'Build RDoc HTML files'
  222. end
  223. ##
  224. # Task description for the rerdoc task or its renamed description
  225. def rerdoc_task_description
  226. "Rebuild RDoc HTML files"
  227. end
  228. private
  229. def rdoc_target
  230. "#{rdoc_dir}/index.html"
  231. end
  232. def rdoc_task_name
  233. case name
  234. when Hash then (name[:rdoc] || "rdoc").to_s
  235. else name.to_s
  236. end
  237. end
  238. def clobber_task_name
  239. case name
  240. when Hash then (name[:clobber_rdoc] || "clobber_rdoc").to_s
  241. else "clobber_#{name}"
  242. end
  243. end
  244. def rerdoc_task_name
  245. case name
  246. when Hash then (name[:rerdoc] || "rerdoc").to_s
  247. else "re#{name}"
  248. end
  249. end
  250. end
  251. # :stopdoc:
  252. module Rake
  253. ##
  254. # For backwards compatibility
  255. RDocTask = RDoc::Task
  256. end
  257. # :startdoc: