aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/tasks/documentation.rake
blob: f15efbc30a10f047b9dc64f14f855c26e8cf9bec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
namespace :doc do
  def gem_path(gem_name)
    path = $LOAD_PATH.grep(/#{gem_name}[\w\-\.]*\/lib$/).first

    if path
      yield File.dirname(path)
    end
  end

  desc "Generate documentation for the application. Set custom template with TEMPLATE=/path/to/rdoc/template.rb or title with TITLE=\"Custom Title\""
  Rake::RDocTask.new("app") { |rdoc|
    rdoc.rdoc_dir = 'doc/app'
    rdoc.template = ENV['template'] if ENV['template']
    rdoc.title    = ENV['title'] || "Rails Application Documentation"
    rdoc.options << '--line-numbers' << '--inline-source'
    rdoc.options << '--charset' << 'utf-8'
    rdoc.rdoc_files.include('doc/README_FOR_APP')
    rdoc.rdoc_files.include('app/**/*.rb')
    rdoc.rdoc_files.include('lib/**/*.rb')
  }

  desc 'Generate documentation for the Rails framework. Uses gem paths or the RAILS_PATH environment variable.'
  path = ENV['RAILS_PATH']
  if defined?(Bundler) || (path && File.directory?(path))
    desc 'Generate documentation for the Rails framework.'
    Rake::RDocTask.new("rails") { |rdoc|
      rdoc.rdoc_dir = 'doc/api'
      rdoc.template = "#{ENV['template']}.rb" if ENV['template']
      rdoc.title    = "Rails Framework Documentation"
      rdoc.options << '--line-numbers' << '--inline-source'
      rdoc.rdoc_files.include('README')

      gem_path('actionmailer') do |actionmailer|
        %w(README CHANGELOG MIT-LICENSE lib/action_mailer/base.rb).each do |file|
          rdoc.rdoc_files.include("#{actionmailer}/#{file}")
        end
      end

      gem_path('actionpack') do |actionpack|
        %w(README CHANGELOG MIT-LICENSE lib/action_controller/**/*.rb lib/action_view/**/*.rb).each do |file|
          rdoc.rdoc_files.include("#{actionpack}/#{file}")
        end
      end

      gem_path('activemodel') do |activemodel|
        %w(README CHANGELOG MIT-LICENSE lib/active_model/**/*.rb).each do |file|
          rdoc.rdoc_files.include("#{activemodel}/#{file}")
        end
      end

      gem_path('activerecord') do |activerecord|
        %w(README CHANGELOG lib/active_record/**/*.rb).each do |file|
          rdoc.rdoc_files.include("#{activerecord}/#{file}")
        end
      end

      gem_path('activeresource') do |activeresource|
        %w(README CHANGELOG lib/active_resource.rb lib/active_resource/*).each do |file|
          rdoc.rdoc_files.include("#{activeresource}/#{file}")
        end
      end

      gem_path('activesupport') do |activesupport|
        %w(README CHANGELOG lib/active_support/**/*.rb).each do |file|
          rdoc.rdoc_files.include("#{activesupport}/#{file}")
        end
      end

      gem_path('railties') do |railties|
        %w(README CHANGELOG lib/{*.rb,commands/*.rb,generators/*.rb}).each do |file|
          rdoc.rdoc_files.include("#{railties}/#{file}")
        end
      end
    }
  else
    task :rails do
      if path = ENV['RAILS_PATH']
        $stderr.puts "Skipping doc:rails, missing Rails directory at #{path.inspect}"
      else
        $stderr.puts "Skipping doc:rails, RAILS_PATH environment variable is not set"
      end
    end
  end

  plugins = FileList['vendor/plugins/**'].collect { |plugin| File.basename(plugin) }

  desc "Generate documentation for all installed plugins"
  task :plugins => plugins.collect { |plugin| "doc:plugins:#{plugin}" }

  desc "Remove plugin documentation"
  task :clobber_plugins do
    rm_rf 'doc/plugins' rescue nil
  end

  desc "Generate Rails guides"
  task :guides do
    # FIXME: Reaching outside lib directory is a bad idea
    require File.expand_path('../../../../guides/rails_guides', __FILE__)
    RailsGuides::Generator.new(Rails.root.join("doc/guides")).generate
  end

  namespace :plugins do
    # Define doc tasks for each plugin
    plugins.each do |plugin|
      desc "Generate documentation for the #{plugin} plugin"
      task(plugin => :environment) do
        plugin_base   = "vendor/plugins/#{plugin}"
        options       = []
        files         = Rake::FileList.new
        options << "-o doc/plugins/#{plugin}"
        options << "--title '#{plugin.titlecase} Plugin Documentation'"
        options << '--line-numbers' << '--inline-source'
        options << '--charset' << 'utf-8'
        options << '-T html'

        files.include("#{plugin_base}/lib/**/*.rb")
        if File.exist?("#{plugin_base}/README")
          files.include("#{plugin_base}/README")
          options << "--main '#{plugin_base}/README'"
        end
        files.include("#{plugin_base}/CHANGELOG") if File.exist?("#{plugin_base}/CHANGELOG")

        options << files.to_s

        sh %(rdoc #{options * ' '})
      end
    end
  end
end