Merge pull request #47713 from JuanVqz/railties/thor-stats-task

Use Thor for built-in stats task
This commit is contained in:
Rafael Mendonça França 2024-06-26 17:06:35 -04:00 committed by GitHub
commit 292c127d46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 25 deletions

@ -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

@ -34,5 +34,11 @@ task :stats do
stat_directories = STATS_DIRECTORIES.collect do |name, dir| stat_directories = STATS_DIRECTORIES.collect do |name, dir|
[ name, "#{File.dirname(Rake.application.rakefile_location)}/#{dir}" ] [ name, "#{File.dirname(Rake.application.rakefile_location)}/#{dir}" ]
end.select { |name, dir| File.directory?(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 CodeStatistics.new(*stat_directories).to_s
end end

@ -1,25 +0,0 @@
# frozen_string_literal: true
require "isolation/abstract_unit"
require "rails/command"
class Rails::Command::DevTest < ActiveSupport::TestCase
setup :build_app
teardown :teardown_app
test "`bin/rails stats` handles non-existing directories added by third parties" do
Dir.chdir(app_path) do
app_file("lib/tasks/custom.rake", <<~CODE
task stats: "custom:statsetup"
namespace :custom do
task statsetup: :environment do
require "rails/code_statistics"
::STATS_DIRECTORIES << ["app/non_existing"]
end
end
CODE
)
assert rails "stats"
end
end
end

@ -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