Deprecate `STATS_DIRECTORIES` in favor of `CodeStatistics.add_directory`
STATS_DIRECTORIES is used by third parties to add directories to the statistics output. It's a global constant defined in a Rake file, that gets loaded anytime the Rake commands get loaded.
For example Rspec Rails adds spec directories in a prepended Rake task: https://github.com/rspec/rspec-rails/blob/8c17b4e5020a4d264e8a79e294c58b5c1ef2b005/lib/rspec/rails/tasks/rspec.rake#L43
Rake tasks only get loaded if no matching Thor task has been found. This means this constant is only available when the Rake commands have loaded.
As the stats command has now been moved to a Thor task, calling bin/rails stats will no longer add directories to STATS_DIRECTORIES, as the Rake commands don't get loaded anymore.
To remove the dependency on Rake and avoid a global constant we can add an API to add directories: CodeStatistics.add_directory. STATS_DIRECTORIES is deprecated.
deprecate_constant couldn't be used here as that doesn't seem to work for the root namespace.
This was previously proposed/discussed in https://github.com/rails/rails/pull/49759, but I decided to resubmit it with the recent commit to make stats a Thor task.
Before
bin/rake stats adds custom directories (Model specs in this case):
bin/rake stats
DEPRECATION WARNING: `bin/rails stats` as rake task has been deprecated and will be removed in Rails 8.0.
Please use `bin/rails stats` as Rails command instead.
(called from block in execute at .../gems/rake-13.1.0/lib/rake/task.rb:281)
DEPRECATION WARNING: `bin/rails stats` as rake task has been deprecated and will be removed in Rails 8.0.
Please use `bin/rails stats` as Rails command instead.
(called from block in execute at .../gems/rake-13.1.0/lib/rake/task.rb:281)
+----------------------+--------+--------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+--------+--------+---------+---------+-----+-------+
| Controllers | 7 | 7 | 2 | 1 | 0 | 5 |
...
| System tests | 0 | 0 | 0 | 0 | 0 | 0 |
| Model specs | 5 | 4 | 0 | 0 | 0 | 0 |
+----------------------+--------+--------+---------+---------+-----+-------+
| Total | 161 | 115 | 14 | 1 | 0 | 113 |
+----------------------+--------+--------+---------+---------+-----+-------+
bin/rails stats does not add custom directories:
$ bin/rails stats
+----------------------+--------+--------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+--------+--------+---------+---------+-----+-------+
| Controllers | 7 | 7 | 2 | 1 | 0 | 5 |
...
| System tests | 0 | 0 | 0 | 0 | 0 | 0 |
+----------------------+--------+--------+---------+---------+-----+-------+
| Total | 156 | 111 | 14 | 1 | 0 | 109 |
+----------------------+--------+--------+---------+---------+-----+-------+
After
bin/rake stats adds directories.
$ bin/rake stats
DEPRECATION WARNING: `STATS_DIRECTORIES` is deprecated and will be removed in Rails 8.1! Use `Rails.application.config.code_statistics.directories` instead. (called from block in execute at .../gems/rake-13.1.0/lib/rake/task.rb:281)
DEPRECATION WARNING: `bin/rake stats` as rake task has been deprecated and will be removed in Rails 8.1.
Please use `bin/rails stats` as Rails command instead.
(called from block in execute at .../gems/rake-13.1.0/lib/rake/task.rb:281)
DEPRECATION WARNING: `bin/rake stats` as rake task has been deprecated and will be removed in Rails 8.1.
Please use `bin/rails stats` as Rails command instead.
(called from block in execute at .../gems/rake-13.1.0/lib/rake/task.rb:281)
+----------------------+--------+--------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+--------+--------+---------+---------+-----+-------+
| Controllers | 7 | 7 | 2 | 1 | 0 | 5 |
...
| System tests | 0 | 0 | 0 | 0 | 0 | 0 |
| Model specs | 5 | 4 | 0 | 0 | 0 | 0 |
+----------------------+--------+--------+---------+---------+-----+-------+
| Total | 161 | 115 | 14 | 1 | 0 | 113 |
+----------------------+--------+--------+---------+---------+-----+-------+
bin/rails stats adds directories added in a Raills.application.config.after_initialize block.
$ bin/rails stats
+----------------------+--------+--------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+--------+--------+---------+---------+-----+-------+
| Controllers | 7 | 7 | 2 | 1 | 0 | 5 |
...
| System tests | 0 | 0 | 0 | 0 | 0 | 0 |
| Model specs | 5 | 4 | 0 | 0 | 0 | 0 |
+----------------------+--------+--------+---------+---------+-----+-------+
| Total | 161 | 115 | 14 | 1 | 0 | 113 |
+----------------------+--------+--------+---------+---------+-----+-------+
....
Checklist
Before submitting the PR make sure the following are checked:
- [x] This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
- [x] Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex:
[Fix #issue-number] - [x] Tests are added or updated if you fix a bug or add a feature.
- [x] CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.
Instead of having a config setting that requires a booted application, it would probably be better to have a clear API on CodeStatistics:
CodeStatistics.add_directory("My Directory", "path/to/dir")
I've changed the PR to use CodeStatistics.add_directory instead of config.code_statistics.directories.