Skip to content
Permalink
Browse files

Implements the task zeitwerk:check

[Matilda Smeds & Xavier Noria]
  • Loading branch information...
fxn committed Apr 23, 2019
1 parent 1b2efe5 commit afc17e5ea21759df5b9ef2ac9421b02154a09b9b
Showing with 90 additions and 0 deletions.
  1. +11 −0 railties/CHANGELOG.md
  2. +1 −0 railties/lib/rails/tasks.rb
  3. +78 −0 railties/lib/rails/tasks/zeitwerk.rake
@@ -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
@@ -15,6 +15,7 @@
routes
tmp
yarn
zeitwerk
).tap { |arr|
arr << "statistics" if Rake.application.current_scope.empty?
}.each do |task|
@@ -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

0 comments on commit afc17e5

Please sign in to comment.
You can’t perform that action at this time.