Skip to content

Commit 271467b

Browse files
p8Earlopain
andcommitted
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 these 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 `STATS_DIRECTORIES` 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. Co-authored-by: Earlopain <[email protected]>
1 parent 89f9ca7 commit 271467b

File tree

5 files changed

+72
-59
lines changed

5 files changed

+72
-59
lines changed

railties/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
* Deprecate `::STATS_DIRECTORIES`.
2+
3+
The global constant `STATS_DIRECTORIES` has been deprecated in favor of
4+
`CodeStatistics.add_directory`.
5+
6+
Add extra directories with `CodeStatistics.add_directory(label, path)`:
7+
8+
```ruby
9+
require "rails/code_statistics"
10+
CodeStatistics.add_directory('My Directory', 'path/to/dir')
11+
```
12+
13+
*Petrik de Heus*
14+
115
* Enable query log tags by default on development env
216

317
This can be used to trace troublesome SQL statements back to the application

railties/lib/rails/code_statistics.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,32 @@
33
require "rails/code_statistics_calculator"
44
require "active_support/core_ext/enumerable"
55

6-
class CodeStatistics # :nodoc:
6+
class CodeStatistics
7+
DIRECTORIES = [
8+
%w(Controllers app/controllers),
9+
%w(Helpers app/helpers),
10+
%w(Jobs app/jobs),
11+
%w(Models app/models),
12+
%w(Mailers app/mailers),
13+
%w(Mailboxes app/mailboxes),
14+
%w(Channels app/channels),
15+
%w(Views app/views),
16+
%w(JavaScripts app/assets/javascripts),
17+
%w(Stylesheets app/assets/stylesheets),
18+
%w(JavaScript app/javascript),
19+
%w(Libraries lib/),
20+
%w(APIs app/apis),
21+
%w(Controller\ tests test/controllers),
22+
%w(Helper\ tests test/helpers),
23+
%w(Job\ tests test/jobs),
24+
%w(Model\ tests test/models),
25+
%w(Mailer\ tests test/mailers),
26+
%w(Mailbox\ tests test/mailboxes),
27+
%w(Channel\ tests test/channels),
28+
%w(Integration\ tests test/integration),
29+
%w(System\ tests test/system),
30+
]
31+
732
TEST_TYPES = ["Controller tests",
833
"Helper tests",
934
"Model tests",
@@ -16,6 +41,15 @@ class CodeStatistics # :nodoc:
1641

1742
HEADERS = { lines: " Lines", code_lines: " LOC", classes: "Classes", methods: "Methods" }
1843

44+
class_attribute :directories, default: DIRECTORIES
45+
46+
# Add directories to the output of the `bin/rails stats` command.
47+
#
48+
# CodeStatistics.add_directory("My Directory", "path/to/dir")
49+
def self.add_directory(label, path)
50+
self.directories << [label, path]
51+
end
52+
1953
def initialize(*pairs)
2054
@pairs = pairs
2155
@statistics = calculate_statistics

railties/lib/rails/commands/stats/stats_command.rb

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,14 @@
11
# frozen_string_literal: true
22

3-
# While global constants are bad, many 3rd party tools depend on this one (e.g
4-
# rspec-rails & cucumber-rails). So a deprecation warning is needed if we want
5-
# to remove it.
6-
STATS_DIRECTORIES ||= [
7-
%w(Controllers app/controllers),
8-
%w(Helpers app/helpers),
9-
%w(Jobs app/jobs),
10-
%w(Models app/models),
11-
%w(Mailers app/mailers),
12-
%w(Mailboxes app/mailboxes),
13-
%w(Channels app/channels),
14-
%w(Views app/views),
15-
%w(JavaScripts app/assets/javascripts),
16-
%w(Stylesheets app/assets/stylesheets),
17-
%w(JavaScript app/javascript),
18-
%w(Libraries lib/),
19-
%w(APIs app/apis),
20-
%w(Controller\ tests test/controllers),
21-
%w(Helper\ tests test/helpers),
22-
%w(Job\ tests test/jobs),
23-
%w(Model\ tests test/models),
24-
%w(Mailer\ tests test/mailers),
25-
%w(Mailbox\ tests test/mailboxes),
26-
%w(Channel\ tests test/channels),
27-
%w(Integration\ tests test/integration),
28-
%w(System\ tests test/system),
29-
]
30-
313
module Rails
324
module Command
335
class StatsCommand < Base # :nodoc:
346
desc "stats", "Report code statistics (KLOCs, etc) from the application or engine"
357
def perform
368
require "rails/code_statistics"
9+
boot_application!
3710

38-
stat_directories = STATS_DIRECTORIES.collect do |name, dir|
11+
stat_directories = CodeStatistics.directories.map do |name, dir|
3912
[name, Rails::Command.application_root.join(dir)]
4013
end.select { |name, dir| File.directory?(dir) }
4114

railties/lib/rails/tasks/statistics.rake

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
# frozen_string_literal: true
22

3-
# While global constants are bad, many 3rd party tools depend on this one (e.g
4-
# rspec-rails & cucumber-rails). So a deprecation warning is needed if we want
5-
# to remove it.
6-
STATS_DIRECTORIES ||= [
7-
%w(Controllers app/controllers),
8-
%w(Helpers app/helpers),
9-
%w(Jobs app/jobs),
10-
%w(Models app/models),
11-
%w(Mailers app/mailers),
12-
%w(Mailboxes app/mailboxes),
13-
%w(Channels app/channels),
14-
%w(Views app/views),
15-
%w(JavaScripts app/assets/javascripts),
16-
%w(Stylesheets app/assets/stylesheets),
17-
%w(JavaScript app/javascript),
18-
%w(Libraries lib/),
19-
%w(APIs app/apis),
20-
%w(Controller\ tests test/controllers),
21-
%w(Helper\ tests test/helpers),
22-
%w(Job\ tests test/jobs),
23-
%w(Model\ tests test/models),
24-
%w(Mailer\ tests test/mailers),
25-
%w(Mailbox\ tests test/mailboxes),
26-
%w(Channel\ tests test/channels),
27-
%w(Integration\ tests test/integration),
28-
%w(System\ tests test/system),
29-
]
3+
require "rails/code_statistics"
4+
STATS_DIRECTORIES = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
5+
CodeStatistics::DIRECTORIES,
6+
"`STATS_DIRECTORIES` is deprecated and will be removed in Rails 8.1! Use `CodeStatistics.add_directory('My Directory', 'path/to/dir)` instead.",
7+
Rails.deprecator
8+
)
309

3110
desc "Report code statistics (KLOCs, etc) from the application or engine"
3211
task :stats do

railties/test/commands/stats_test.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,25 @@ class Rails::Command::StatsTest < ActiveSupport::TestCase
88
setup :build_app
99
teardown :teardown_app
1010

11+
test "`bin/rails stats` handles directories added by third parties" do
12+
app_dir "custom/dir"
13+
14+
app_file "config/initializers/custom.rb", <<~CODE
15+
require "rails/code_statistics"
16+
CodeStatistics.add_directory("Custom dir", "custom/dir")
17+
CODE
18+
19+
output = rails "stats"
20+
assert_match "Custom dir", output
21+
end
22+
1123
test "`bin/rails stats` handles non-existing directories added by third parties" do
1224
app_file "config/initializers/custom.rb", <<~CODE
1325
require "rails/code_statistics"
14-
::STATS_DIRECTORIES << ["Non\ Existing", "app/non_existing"]
26+
CodeStatistics.add_directory("Non Existing", "app/non_existing")
1527
CODE
1628

17-
assert rails "stats"
29+
output = rails "stats"
30+
assert_no_match "Non Existing", output
1831
end
1932
end

0 commit comments

Comments
 (0)