Skip to content

Instantly share code, notes, and snippets.

@andreimaxim
Created February 15, 2024 07:39
Show Gist options
  • Save andreimaxim/f78ea5e9243ab31fc4fdb923586982ff to your computer and use it in GitHub Desktop.
Save andreimaxim/f78ea5e9243ab31fc4fdb923586982ff to your computer and use it in GitHub Desktop.
Rakefile to check project structure for Zeitwerk compatibility
require File.expand_path("config/environment", __dir__)
require "zeitwerk"
namespace :zeitwerk do
desc "Check project structure for Zeitwerk compatibility"
task :check do
base_directory = Pathname.new "lib"
loader = Zeitwerk::Loader.new
root = File.expand_path __dir__
lib_dir = File.join root, "lib"
config_dir = File.join root, "lib"
ext_dir = File.join root, "lib", "gem-ext"
loader.push_dir lib_dir
loader.push_dir config_dir
loader.collapse ext_dir
loader.inflector.inflect(
"jwt" => "JWT",
"api" => "API",
"api_v1" => "APIv1",
"api_v2" => "APIv2",
"rabbitmq" => "RabbitMQ",
"api_helper" => "APIHelper"
)
loader.setup
failures = []
base_directory.glob("**/*.rb") do |file|
cname = loader.cpath_expected_at(file)
next unless cname
begin
if cname
require_relative file.to_s
cname.constantize
end
rescue NameError=> e
failures << { file: file, cname: cname, error: "NameError" }
rescue LoadError
failures << { file: file, cname: cname, error: "LoadError" }
end
end
failures.each do |failure|
puts "Expected #{failure[:file]} to define #{failure[:cname]} (#{failure[:error]})"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment