Sha256: 8d93545c8d1dd029a8f09dfe240bd171bc09d07722e7c5deabad8ce2a7d1d72b
Contents?: true
Size: 1.86 KB
Versions: 4
Compression:
Stored size: 1.86 KB
Contents
require 'zeitwerk' require 'benchmark' class MonoRepoDeps::Loaders::Zeitwerk < MonoRepoDeps::Loaders::Base include MonoRepoDeps::Mixins ERROR_MESSAGE_REGEX = /expected file (?<path>[\w\/]+.rb) to define constant (?<klass>[\w:]+)/ def initialize(...) @loader = Zeitwerk::Loader.new @loader.inflector = Class.new(Zeitwerk::Inflector).new super(...) end def setup apply_rules_for(@loader) @loader.setup end def eager_load time = Benchmark.realtime do @loader.eager_load end puts "eager_load dependencies in #{'%.2f' % time} seconds" end def check_classes check_classes_loader = @loader.dup check_classes_loader.on_load { |cpath, value, abspath| puts value } errors = [] begin check_classes_loader.eager_load rescue Zeitwerk::NameError => e match = e.message.match(ERROR_MESSAGE_REGEX) raise e if match.nil? # TODO: use logger errors << "#{match[:path]} => #{match[:klass]}" check_classes_loader.ignore(match[:path]) retry end puts errors raise StandardError.new("loader fails with #{errors.count} errors") if errors.any? nil end def apply_rules_for(entity) @autoload_dirs.uniq.each { entity.push_dir(_1) } @ignore_dirs.uniq .map { File.join(MonoRepoDeps.current_project.root_path, _1) } .each { entity.ignore( _1 ) } entity.inflector.inflect(@inflections) result_overwriter = ->(kname) { @overwriters.each { |o| kname = o.call(kname) }; kname } entity.inflector.define_singleton_method(:camelize) do |basename, abspath| result_overwriter.call( super(basename, abspath) ) end end def ignore(glob) super(glob) # TODO: if loader.setup is true => push dir directly to the @loader @ignore_dirs .uniq .map { File.join(root_path, _1) } .each { @loader.ignore( _1 ) } end end
Version data entries
4 entries across 4 versions & 1 rubygems