From df64ba0a068a03653fa21566ecda0cccfd47cf7f Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sun, 19 Mar 2023 23:14:11 -0600 Subject: [PATCH] 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. --- .../lib/rails/commands/stats/stats_command.rb | 46 +++++++++++++++++++ railties/lib/rails/tasks/statistics.rake | 6 +++ railties/test/commands/statistics_test.rb | 25 ---------- railties/test/commands/stats_test.rb | 19 ++++++++ 4 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 railties/lib/rails/commands/stats/stats_command.rb delete mode 100644 railties/test/commands/statistics_test.rb create mode 100644 railties/test/commands/stats_test.rb diff --git a/railties/lib/rails/commands/stats/stats_command.rb b/railties/lib/rails/commands/stats/stats_command.rb new file mode 100644 index 0000000000..2c6fdb3817 --- /dev/null +++ b/railties/lib/rails/commands/stats/stats_command.rb @@ -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 diff --git a/railties/lib/rails/tasks/statistics.rake b/railties/lib/rails/tasks/statistics.rake index c8198c4ee1..e58964e14f 100644 --- a/railties/lib/rails/tasks/statistics.rake +++ b/railties/lib/rails/tasks/statistics.rake @@ -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 diff --git a/railties/test/commands/statistics_test.rb b/railties/test/commands/statistics_test.rb deleted file mode 100644 index dccc56fca1..0000000000 --- a/railties/test/commands/statistics_test.rb +++ /dev/null @@ -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 diff --git a/railties/test/commands/stats_test.rb b/railties/test/commands/stats_test.rb new file mode 100644 index 0000000000..d8fafc6383 --- /dev/null +++ b/railties/test/commands/stats_test.rb @@ -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