/tools/Ruby/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/rdoctask.rb

http://github.com/agross/netopenspace · Ruby · 230 lines · 128 code · 32 blank · 70 comment · 18 complexity · 716cfe691905936b1fb4af5d98e3d0ed MD5 · raw file

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