aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/api/task.rb
blob: c829873da4fc9ca9d8f35c401c33474f8ae7c0b0 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require 'rdoc/task'

module Rails
  module API
    class Task < RDoc::Task
      RDOC_FILES = {
        'activesupport' => {
          :include => %w(
            README.rdoc
            CHANGELOG.md
            lib/active_support/**/*.rb
          ),
          :exclude => 'lib/active_support/vendor/*'
        },

        'activerecord' => {
          :include => %w(
            README.rdoc
            CHANGELOG.md
            lib/active_record/**/*.rb
          ),
          :exclude => 'lib/active_record/vendor/*'
        },

        'activemodel' => {
          :include => %w(
            README.rdoc
            CHANGELOG.md
            lib/active_model/**/*.rb
          )
        },

        'actionpack' => {
          :include => %w(
            README.rdoc
            CHANGELOG.md
            lib/abstract_controller/**/*.rb
            lib/action_controller/**/*.rb
            lib/action_dispatch/**/*.rb
            lib/action_view/**/*.rb
          ),
          :exclude => 'lib/action_controller/vendor/*'
        },

        'actionmailer' => {
          :include => %w(
            README.rdoc
            CHANGELOG.md
            lib/action_mailer/**/*.rb
          ),
          :exclude => 'lib/action_mailer/vendor/*'
        },

        'railties' => {
          :include => %w(
            README.rdoc
            CHANGELOG.md
            MIT-LICENSE
            lib/**/*.rb
          ),
          :exclude => 'lib/rails/generators/rails/**/templates/**/*.rb'
        }
      }

      def initialize(name)
        super

        # Every time rake runs this task is instantiated as all the rest.
        # Be lazy computing stuff to have as light impact as possible to
        # the rest of tasks.
        before_running_rdoc do
          load_and_configure_sdoc
          configure_rdoc_files
          setup_horo_variables
        end
      end

      # Hack, ignore the desc calls performed by the original initializer.
      def desc(description)
        # no-op
      end

      def load_and_configure_sdoc
        require 'sdoc'

        self.title    = 'Ruby on Rails API'
        self.rdoc_dir = api_dir

        options << '-m'  << api_main
        options << '-e'  << 'UTF-8'

        options << '-f'  << 'sdoc'
        options << '-T'  << 'rails'
      rescue LoadError
        $stderr.puts %(Unable to load SDoc, please add\n\n    gem 'sdoc', require: false\n\nto the Gemfile.)
        exit 1
      end

      def configure_rdoc_files
        rdoc_files.include(api_main)

        RDOC_FILES.each do |component, cfg|
          cdr = component_root_dir(component)

          Array(cfg[:include]).each do |pattern|
            rdoc_files.include("#{cdr}/#{pattern}")
          end

          Array(cfg[:exclude]).each do |pattern|
            rdoc_files.exclude("#{cdr}/#{pattern}")
          end
        end
      end

      def setup_horo_variables
        ENV['HORO_PROJECT_NAME']    = 'Ruby on Rails'
        ENV['HORO_PROJECT_VERSION'] = rails_version
      end

      def api_main
        component_root_dir('railties') + '/RDOC_MAIN.rdoc'
      end
    end

    class RepoTask < Task
      def load_and_configure_sdoc
        super
        options << '-g' # link to GitHub, SDoc flag
      end

      def component_root_dir(component)
        component
      end

      def api_dir
        'doc/rdoc'
      end

      def rails_version
        "master@#{`git rev-parse HEAD`[0, 7]}"
      end
    end

    class AppTask < Task
      def component_root_dir(gem_name)
        $:.grep(%r{#{gem_name}[\w.-]*/lib\z}).first[0..-5]
      end

      def api_dir
        'doc/api'
      end

      def rails_version
        Rails::VERSION::STRING
      end
    end
  end
end