diff options
author | Xavier Noria <fxn@hashref.com> | 2019-04-23 03:03:11 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2019-04-23 03:17:34 +0200 |
commit | afc17e5ea21759df5b9ef2ac9421b02154a09b9b (patch) | |
tree | 441b617ca3c98367f0ba319253755ff9a9b19afb | |
parent | 1b2efe5a111941d57edbaf2be7f3a94eb02b5f9d (diff) | |
download | rails-afc17e5ea21759df5b9ef2ac9421b02154a09b9b.tar.gz rails-afc17e5ea21759df5b9ef2ac9421b02154a09b9b.tar.bz2 rails-afc17e5ea21759df5b9ef2ac9421b02154a09b9b.zip |
Implements the task zeitwerk:check
[Matilda Smeds & Xavier Noria]
-rw-r--r-- | railties/CHANGELOG.md | 11 | ||||
-rw-r--r-- | railties/lib/rails/tasks.rb | 1 | ||||
-rw-r--r-- | railties/lib/rails/tasks/zeitwerk.rake | 78 |
3 files changed, 90 insertions, 0 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index e5f9e37c33..84974d8f03 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,14 @@ +* Applications upgrading to Rails 6 can run the command + + ``` + bin/rails zeitwerk:check + ``` + + to check if the project structure they were using with the classic + autoloader is compatible with `:zeitwerk` mode. + + *Matilda Smeds* & *Xavier Noria* + * Allow loading seeds without ActiveJob. Fixes #35782 diff --git a/railties/lib/rails/tasks.rb b/railties/lib/rails/tasks.rb index 2f644a20c9..0e592e15c3 100644 --- a/railties/lib/rails/tasks.rb +++ b/railties/lib/rails/tasks.rb @@ -15,6 +15,7 @@ require "rake" routes tmp yarn + zeitwerk ).tap { |arr| arr << "statistics" if Rake.application.current_scope.empty? }.each do |task| diff --git a/railties/lib/rails/tasks/zeitwerk.rake b/railties/lib/rails/tasks/zeitwerk.rake new file mode 100644 index 0000000000..cf84bb6948 --- /dev/null +++ b/railties/lib/rails/tasks/zeitwerk.rake @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +ensure_classic_mode = ->() do + if Rails.autoloaders.zeitwerk_enabled? + abort <<~EOS + Please, enable temporarily :classic mode: + + # config/application.rb + config.autoloader = :classic + + and try again. When all is good, you can delete that line. + EOS + end +end + +eager_load = ->() do + Rails.configuration.eager_load_namespaces.each(&:eager_load!) +end + +mismatches = [] +check_directory = ->(directory, parent) do + # test/mailers/previews might not exist. + return unless File.exists?(directory) + + Dir.foreach(directory) do |entry| + next if entry.start_with?(".") + next if parent == Object && entry == "concerns" + + abspath = File.join(directory, entry) + + if File.directory?(abspath) || abspath.end_with?(".rb") + print "." + cname = File.basename(abspath, ".rb").camelize.to_sym + if parent.const_defined?(cname, false) + if File.directory?(abspath) + check_directory[abspath, parent.const_get(cname)] + end + else + mismatches << [abspath, parent, cname] + end + end + end +end + +report = ->() do + puts + if mismatches.empty? + puts "All is good!" + puts "Please, remember to delete `config.autoloader = :classic` from config/application.rb." + else + mismatches.each do |abspath, parent, cname| + relpath = abspath.sub(%r{\A#{Regexp.escape(Rails.root.to_path)}/}, "") + cpath = parent == Object ? cname : "#{parent.name}::#{cname}" + puts "expected #{relpath} to define #{cpath}" + end + puts + puts <<~EOS + Please revise the reported mismatches. You can normally fix them by adding + acronyms to config/initializers/inflections.rb or renaming the constants. + EOS + end +end + +namespace :zeitwerk do + desc "Checks project structure for Zeitwerk compatibility" + task check: :environment do + ensure_classic_mode[] + eager_load[] + + $stdout.sync = true + ActiveSupport::Dependencies.autoload_paths.each do |autoload_path| + check_directory[autoload_path, Object] + end + puts + + report[] + end +end |