Skip to content

Commit

Permalink
Merge pull request rails#47713 from JuanVqz/railties/thor-stats-task
Browse files Browse the repository at this point in the history
Use Thor for built-in stats task
  • Loading branch information
rafaelfranca committed Jun 26, 2024
2 parents fca2a5e + df64ba0 commit 292c127
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
46 changes: 46 additions & 0 deletions railties/lib/rails/commands/stats/stats_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

# While global constants are bad, many 3rd party tools depend on this one (e.g
# rspec-rails & cucumber-rails). So a deprecation warning is needed if we want
# to remove it.
STATS_DIRECTORIES ||= [
%w(Controllers app/controllers),
%w(Helpers app/helpers),
%w(Jobs app/jobs),
%w(Models app/models),
%w(Mailers app/mailers),
%w(Mailboxes app/mailboxes),
%w(Channels app/channels),
%w(Views app/views),
%w(JavaScripts app/assets/javascripts),
%w(Stylesheets app/assets/stylesheets),
%w(JavaScript app/javascript),
%w(Libraries lib/),
%w(APIs app/apis),
%w(Controller\ tests test/controllers),
%w(Helper\ tests test/helpers),
%w(Job\ tests test/jobs),
%w(Model\ tests test/models),
%w(Mailer\ tests test/mailers),
%w(Mailbox\ tests test/mailboxes),
%w(Channel\ tests test/channels),
%w(Integration\ tests test/integration),
%w(System\ tests test/system),
]

module Rails
module Command
class StatsCommand < Base # :nodoc:
desc "stats", "Report code statistics (KLOCs, etc) from the application or engine"
def perform
require "rails/code_statistics"

stat_directories = STATS_DIRECTORIES.collect do |name, dir|
[name, Rails::Command.application_root.join(dir)]
end.select { |name, dir| File.directory?(dir) }

CodeStatistics.new(*stat_directories).to_s
end
end
end
end
6 changes: 6 additions & 0 deletions railties/lib/rails/tasks/statistics.rake
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ task :stats do
stat_directories = STATS_DIRECTORIES.collect do |name, dir|
[ name, "#{File.dirname(Rake.application.rakefile_location)}/#{dir}" ]
end.select { |name, dir| File.directory?(dir) }

$stderr.puts Rails.deprecator.warn(<<~MSG, caller_locations(0..1))
`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.\n
MSG

CodeStatistics.new(*stat_directories).to_s
end
25 changes: 0 additions & 25 deletions railties/test/commands/statistics_test.rb

This file was deleted.

19 changes: 19 additions & 0 deletions railties/test/commands/stats_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "isolation/abstract_unit"
require "rails/command"

class Rails::Command::StatsTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
setup :build_app
teardown :teardown_app

test "`bin/rails stats` handles non-existing directories added by third parties" do
app_file "config/initializers/custom.rb", <<~CODE
require "rails/code_statistics"
::STATS_DIRECTORIES << ["Non\ Existing", "app/non_existing"]
CODE

assert rails "stats"
end
end

0 comments on commit 292c127

Please sign in to comment.