Skip to content

Commit df64ba0

Browse files
committed
Use Thor for built-in stats task
Currently, we use both Thor and Rake for `bin/rails` commands. We eventually want to get all the built-ins task promoted to Thor Commands. This migrates the `stats` task to Thor.
1 parent 5bec50b commit df64ba0

File tree

4 files changed

+71
-25
lines changed

4 files changed

+71
-25
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
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+
31+
module Rails
32+
module Command
33+
class StatsCommand < Base # :nodoc:
34+
desc "stats", "Report code statistics (KLOCs, etc) from the application or engine"
35+
def perform
36+
require "rails/code_statistics"
37+
38+
stat_directories = STATS_DIRECTORIES.collect do |name, dir|
39+
[name, Rails::Command.application_root.join(dir)]
40+
end.select { |name, dir| File.directory?(dir) }
41+
42+
CodeStatistics.new(*stat_directories).to_s
43+
end
44+
end
45+
end
46+
end

railties/lib/rails/tasks/statistics.rake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ task :stats do
3434
stat_directories = STATS_DIRECTORIES.collect do |name, dir|
3535
[ name, "#{File.dirname(Rake.application.rakefile_location)}/#{dir}" ]
3636
end.select { |name, dir| File.directory?(dir) }
37+
38+
$stderr.puts Rails.deprecator.warn(<<~MSG, caller_locations(0..1))
39+
`bin/rails stats` as rake task has been deprecated and will be removed in Rails 8.0.
40+
Please use `bin/rails stats` as Rails command instead.\n
41+
MSG
42+
3743
CodeStatistics.new(*stat_directories).to_s
3844
end

railties/test/commands/statistics_test.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

railties/test/commands/stats_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
require "isolation/abstract_unit"
4+
require "rails/command"
5+
6+
class Rails::Command::StatsTest < ActiveSupport::TestCase
7+
include ActiveSupport::Testing::Isolation
8+
setup :build_app
9+
teardown :teardown_app
10+
11+
test "`bin/rails stats` handles non-existing directories added by third parties" do
12+
app_file "config/initializers/custom.rb", <<~CODE
13+
require "rails/code_statistics"
14+
::STATS_DIRECTORIES << ["Non\ Existing", "app/non_existing"]
15+
CODE
16+
17+
assert rails "stats"
18+
end
19+
end

0 commit comments

Comments
 (0)