Merge pull request #12278 from Homebrew/dependabot/bundler/Library/Homebrew/zeitwerk-2.5.0
build(deps): bump zeitwerk from 2.4.2 to 2.5.0 in /Library/Homebrew
This commit is contained in:
commit
033c365b0d
@ -189,7 +189,7 @@ GEM
|
|||||||
warning (1.2.1)
|
warning (1.2.1)
|
||||||
webrick (1.7.0)
|
webrick (1.7.0)
|
||||||
webrobots (0.1.2)
|
webrobots (0.1.2)
|
||||||
zeitwerk (2.4.2)
|
zeitwerk (2.5.0)
|
||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
|
@ -16,7 +16,26 @@ module Kernel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Zeitwerk; end
|
module Zeitwerk
|
||||||
|
class << self
|
||||||
|
def with_loader; end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Zeitwerk::Autoloads
|
||||||
|
def initialize; end
|
||||||
|
|
||||||
|
def a2c; end
|
||||||
|
def abspath_for(parent, cname); end
|
||||||
|
def c2a; end
|
||||||
|
def clear; end
|
||||||
|
def cref_for(abspath); end
|
||||||
|
def define(parent, cname, abspath); end
|
||||||
|
def delete(abspath); end
|
||||||
|
def each(&block); end
|
||||||
|
def empty?; end
|
||||||
|
end
|
||||||
|
|
||||||
class Zeitwerk::Error < ::StandardError; end
|
class Zeitwerk::Error < ::StandardError; end
|
||||||
|
|
||||||
module Zeitwerk::ExplicitNamespace
|
module Zeitwerk::ExplicitNamespace
|
||||||
@ -27,7 +46,7 @@ module Zeitwerk::ExplicitNamespace
|
|||||||
def mutex; end
|
def mutex; end
|
||||||
def register(cpath, loader); end
|
def register(cpath, loader); end
|
||||||
def tracer; end
|
def tracer; end
|
||||||
def unregister(loader); end
|
def unregister_loader(loader); end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
@ -54,73 +73,35 @@ end
|
|||||||
class Zeitwerk::Loader
|
class Zeitwerk::Loader
|
||||||
include ::Zeitwerk::RealModName
|
include ::Zeitwerk::RealModName
|
||||||
include ::Zeitwerk::Loader::Callbacks
|
include ::Zeitwerk::Loader::Callbacks
|
||||||
|
include ::Zeitwerk::Loader::Helpers
|
||||||
|
include ::Zeitwerk::Loader::Config
|
||||||
|
|
||||||
def initialize; end
|
def initialize; end
|
||||||
|
|
||||||
def autoloaded_dirs; end
|
def autoloaded_dirs; end
|
||||||
def autoloads; end
|
def autoloads; end
|
||||||
def collapse(*glob_patterns); end
|
def eager_load(force: T.unsafe(nil)); end
|
||||||
def collapse_dirs; end
|
|
||||||
def collapse_glob_patterns; end
|
|
||||||
def dirs; end
|
|
||||||
def do_not_eager_load(*paths); end
|
|
||||||
def eager_load; end
|
|
||||||
def eager_load_exclusions; end
|
|
||||||
def enable_reloading; end
|
|
||||||
def ignore(*glob_patterns); end
|
|
||||||
def ignored_glob_patterns; end
|
|
||||||
def ignored_paths; end
|
|
||||||
def inflector; end
|
|
||||||
def inflector=(_arg0); end
|
|
||||||
def lazy_subdirs; end
|
def lazy_subdirs; end
|
||||||
def log!; end
|
|
||||||
def logger; end
|
|
||||||
def logger=(_arg0); end
|
|
||||||
def manages?(dir); end
|
|
||||||
def mutex; end
|
def mutex; end
|
||||||
def mutex2; end
|
def mutex2; end
|
||||||
def on_load(cpath, &block); end
|
|
||||||
def on_load_callbacks; end
|
|
||||||
def preload(*paths); end
|
|
||||||
def preloads; end
|
|
||||||
def push_dir(path, namespace: T.unsafe(nil)); end
|
|
||||||
def reload; end
|
def reload; end
|
||||||
def reloading_enabled?; end
|
|
||||||
def root_dirs; end
|
|
||||||
def setup; end
|
def setup; end
|
||||||
def tag; end
|
|
||||||
def tag=(tag); end
|
|
||||||
def to_unload; end
|
def to_unload; end
|
||||||
def unload; end
|
def unload; end
|
||||||
def unloadable_cpath?(cpath); end
|
def unloadable_cpath?(cpath); end
|
||||||
def unloadable_cpaths; end
|
def unloadable_cpaths; end
|
||||||
|
def unregister; end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def actual_root_dirs; end
|
|
||||||
def autoload_file(parent, cname, file); end
|
def autoload_file(parent, cname, file); end
|
||||||
def autoload_for?(parent, cname); end
|
|
||||||
def autoload_subdir(parent, cname, subdir); end
|
def autoload_subdir(parent, cname, subdir); end
|
||||||
def cdef?(parent, cname); end
|
|
||||||
def cpath(parent, cname); end
|
|
||||||
def dir?(path); end
|
|
||||||
def do_preload; end
|
|
||||||
def do_preload_abspath(abspath); end
|
|
||||||
def do_preload_dir(dir); end
|
|
||||||
def do_preload_file(file); end
|
|
||||||
def expand_glob_patterns(glob_patterns); end
|
|
||||||
def expand_paths(paths); end
|
|
||||||
def log(message); end
|
|
||||||
def ls(dir); end
|
|
||||||
def promote_namespace_from_implicit_to_explicit(dir:, file:, parent:, cname:); end
|
def promote_namespace_from_implicit_to_explicit(dir:, file:, parent:, cname:); end
|
||||||
def raise_if_conflicting_directory(dir); end
|
def raise_if_conflicting_directory(dir); end
|
||||||
def recompute_collapse_dirs; end
|
|
||||||
def recompute_ignored_paths; end
|
|
||||||
def register_explicit_namespace(cpath); end
|
def register_explicit_namespace(cpath); end
|
||||||
def ruby?(path); end
|
def run_on_unload_callbacks(cpath, value, abspath); end
|
||||||
def set_autoload(parent, cname, abspath); end
|
def set_autoload(parent, cname, abspath); end
|
||||||
def set_autoloads_in_dir(dir, parent); end
|
def set_autoloads_in_dir(dir, parent); end
|
||||||
def strict_autoload_path(parent, cname); end
|
|
||||||
def unload_autoload(parent, cname); end
|
def unload_autoload(parent, cname); end
|
||||||
def unload_cref(parent, cname); end
|
def unload_cref(parent, cname); end
|
||||||
|
|
||||||
@ -144,7 +125,64 @@ module Zeitwerk::Loader::Callbacks
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def run_on_load_callbacks(cpath); end
|
def run_on_load_callbacks(cpath, value, abspath); end
|
||||||
|
end
|
||||||
|
|
||||||
|
module Zeitwerk::Loader::Config
|
||||||
|
def initialize; end
|
||||||
|
|
||||||
|
def collapse(*glob_patterns); end
|
||||||
|
def collapse_dirs; end
|
||||||
|
def collapse_glob_patterns; end
|
||||||
|
def dirs; end
|
||||||
|
def do_not_eager_load(*paths); end
|
||||||
|
def eager_load_exclusions; end
|
||||||
|
def enable_reloading; end
|
||||||
|
def ignore(*glob_patterns); end
|
||||||
|
def ignored_glob_patterns; end
|
||||||
|
def ignored_paths; end
|
||||||
|
def ignores?(abspath); end
|
||||||
|
def inflector; end
|
||||||
|
def inflector=(_arg0); end
|
||||||
|
def log!; end
|
||||||
|
def logger; end
|
||||||
|
def logger=(_arg0); end
|
||||||
|
def on_load(cpath = T.unsafe(nil), &block); end
|
||||||
|
def on_load_callbacks; end
|
||||||
|
def on_setup(&block); end
|
||||||
|
def on_setup_callbacks; end
|
||||||
|
def on_unload(cpath = T.unsafe(nil), &block); end
|
||||||
|
def on_unload_callbacks; end
|
||||||
|
def push_dir(path, namespace: T.unsafe(nil)); end
|
||||||
|
def reloading_enabled?; end
|
||||||
|
def root_dirs; end
|
||||||
|
def tag; end
|
||||||
|
def tag=(tag); end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def actual_root_dirs; end
|
||||||
|
def collapse?(dir); end
|
||||||
|
def excluded_from_eager_load?(abspath); end
|
||||||
|
def expand_glob_patterns(glob_patterns); end
|
||||||
|
def expand_paths(paths); end
|
||||||
|
def recompute_collapse_dirs; end
|
||||||
|
def recompute_ignored_paths; end
|
||||||
|
def root_dir?(dir); end
|
||||||
|
end
|
||||||
|
|
||||||
|
module Zeitwerk::Loader::Helpers
|
||||||
|
private
|
||||||
|
|
||||||
|
def cdef?(parent, cname); end
|
||||||
|
def cget(parent, cname); end
|
||||||
|
def cpath(parent, cname); end
|
||||||
|
def dir?(path); end
|
||||||
|
def hidden?(basename); end
|
||||||
|
def log(message); end
|
||||||
|
def ls(dir); end
|
||||||
|
def ruby?(path); end
|
||||||
|
def strict_autoload_path(parent, cname); end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Zeitwerk::NameError < ::NameError; end
|
class Zeitwerk::NameError < ::NameError; end
|
||||||
@ -165,11 +203,13 @@ module Zeitwerk::Registry
|
|||||||
def loaders; end
|
def loaders; end
|
||||||
def loaders_managing_gems; end
|
def loaders_managing_gems; end
|
||||||
def on_unload(loader); end
|
def on_unload(loader); end
|
||||||
def register_autoload(loader, realpath); end
|
def register_autoload(loader, abspath); end
|
||||||
def register_inception(cpath, realpath, loader); end
|
def register_inception(cpath, abspath, loader); end
|
||||||
def register_loader(loader); end
|
def register_loader(loader); end
|
||||||
def unregister_autoload(realpath); end
|
def unregister_autoload(abspath); end
|
||||||
|
def unregister_loader(loader); end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Zeitwerk::ReloadingDisabledError < ::Zeitwerk::Error; end
|
class Zeitwerk::ReloadingDisabledError < ::Zeitwerk::Error; end
|
||||||
|
Zeitwerk::VERSION = T.let(T.unsafe(nil), String)
|
@ -4510,6 +4510,8 @@ end
|
|||||||
module Homebrew::EnvConfig
|
module Homebrew::EnvConfig
|
||||||
extend ::T::Private::Methods::MethodHooks
|
extend ::T::Private::Methods::MethodHooks
|
||||||
extend ::T::Private::Methods::SingletonMethodHooks
|
extend ::T::Private::Methods::SingletonMethodHooks
|
||||||
|
def self.additional_google_analytics_id(); end
|
||||||
|
|
||||||
def self.all_proxy(); end
|
def self.all_proxy(); end
|
||||||
|
|
||||||
def self.arch(); end
|
def self.arch(); end
|
||||||
@ -4628,6 +4630,8 @@ module Homebrew::EnvConfig
|
|||||||
|
|
||||||
def self.sorbet_runtime?(); end
|
def self.sorbet_runtime?(); end
|
||||||
|
|
||||||
|
def self.ssh_config_path(); end
|
||||||
|
|
||||||
def self.sudo_askpass(); end
|
def self.sudo_askpass(); end
|
||||||
|
|
||||||
def self.svn(); end
|
def self.svn(); end
|
||||||
|
@ -7,7 +7,7 @@ $:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/concurrent-ruby-1.1.9
|
|||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/i18n-1.8.10/lib"
|
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/i18n-1.8.10/lib"
|
||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/minitest-5.14.4/lib"
|
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/minitest-5.14.4/lib"
|
||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/tzinfo-2.0.4/lib"
|
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/tzinfo-2.0.4/lib"
|
||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/zeitwerk-2.4.2/lib"
|
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/zeitwerk-2.5.0/lib"
|
||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/activesupport-6.1.4.1/lib"
|
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/activesupport-6.1.4.1/lib"
|
||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/public_suffix-4.0.6/lib"
|
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/public_suffix-4.0.6/lib"
|
||||||
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/addressable-2.8.0/lib"
|
$:.unshift "#{path}/../#{ruby_engine}/#{ruby_version}/gems/addressable-2.8.0/lib"
|
||||||
|
@ -1,812 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require "set"
|
|
||||||
require "securerandom"
|
|
||||||
|
|
||||||
module Zeitwerk
|
|
||||||
class Loader
|
|
||||||
require_relative "loader/callbacks"
|
|
||||||
include Callbacks
|
|
||||||
include RealModName
|
|
||||||
|
|
||||||
# @sig String
|
|
||||||
attr_reader :tag
|
|
||||||
|
|
||||||
# @sig #camelize
|
|
||||||
attr_accessor :inflector
|
|
||||||
|
|
||||||
# @sig #call | #debug | nil
|
|
||||||
attr_accessor :logger
|
|
||||||
|
|
||||||
# Absolute paths of the root directories. Stored in a hash to preserve
|
|
||||||
# order, easily handle duplicates, and also be able to have a fast lookup,
|
|
||||||
# needed for detecting nested paths.
|
|
||||||
#
|
|
||||||
# "/Users/fxn/blog/app/assets" => true,
|
|
||||||
# "/Users/fxn/blog/app/channels" => true,
|
|
||||||
# ...
|
|
||||||
#
|
|
||||||
# This is a private collection maintained by the loader. The public
|
|
||||||
# interface for it is `push_dir` and `dirs`.
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Hash[String, true]
|
|
||||||
attr_reader :root_dirs
|
|
||||||
|
|
||||||
# Absolute paths of files or directories that have to be preloaded.
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Array[String]
|
|
||||||
attr_reader :preloads
|
|
||||||
|
|
||||||
# Absolute paths of files, directories, or glob patterns to be totally
|
|
||||||
# ignored.
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Set[String]
|
|
||||||
attr_reader :ignored_glob_patterns
|
|
||||||
|
|
||||||
# The actual collection of absolute file and directory names at the time the
|
|
||||||
# ignored glob patterns were expanded. Computed on setup, and recomputed on
|
|
||||||
# reload.
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Set[String]
|
|
||||||
attr_reader :ignored_paths
|
|
||||||
|
|
||||||
# Absolute paths of directories or glob patterns to be collapsed.
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Set[String]
|
|
||||||
attr_reader :collapse_glob_patterns
|
|
||||||
|
|
||||||
# The actual collection of absolute directory names at the time the collapse
|
|
||||||
# glob patterns were expanded. Computed on setup, and recomputed on reload.
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Set[String]
|
|
||||||
attr_reader :collapse_dirs
|
|
||||||
|
|
||||||
# Maps real absolute paths for which an autoload has been set ---and not
|
|
||||||
# executed--- to their corresponding parent class or module and constant
|
|
||||||
# name.
|
|
||||||
#
|
|
||||||
# "/Users/fxn/blog/app/models/user.rb" => [Object, :User],
|
|
||||||
# "/Users/fxn/blog/app/models/hotel/pricing.rb" => [Hotel, :Pricing]
|
|
||||||
# ...
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Hash[String, [Module, Symbol]]
|
|
||||||
attr_reader :autoloads
|
|
||||||
|
|
||||||
# We keep track of autoloaded directories to remove them from the registry
|
|
||||||
# at the end of eager loading.
|
|
||||||
#
|
|
||||||
# Files are removed as they are autoloaded, but directories need to wait due
|
|
||||||
# to concurrency (see why in Zeitwerk::Loader::Callbacks#on_dir_autoloaded).
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Array[String]
|
|
||||||
attr_reader :autoloaded_dirs
|
|
||||||
|
|
||||||
# Stores metadata needed for unloading. Its entries look like this:
|
|
||||||
#
|
|
||||||
# "Admin::Role" => [".../admin/role.rb", [Admin, :Role]]
|
|
||||||
#
|
|
||||||
# The cpath as key helps implementing unloadable_cpath? The real file name
|
|
||||||
# is stored in order to be able to delete it from $LOADED_FEATURES, and the
|
|
||||||
# pair [Module, Symbol] is used to remove_const the constant from the class
|
|
||||||
# or module object.
|
|
||||||
#
|
|
||||||
# If reloading is enabled, this hash is filled as constants are autoloaded
|
|
||||||
# or eager loaded. Otherwise, the collection remains empty.
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Hash[String, [String, [Module, Symbol]]]
|
|
||||||
attr_reader :to_unload
|
|
||||||
|
|
||||||
# Maps constant paths of namespaces to arrays of corresponding directories.
|
|
||||||
#
|
|
||||||
# For example, given this mapping:
|
|
||||||
#
|
|
||||||
# "Admin" => [
|
|
||||||
# "/Users/fxn/blog/app/controllers/admin",
|
|
||||||
# "/Users/fxn/blog/app/models/admin",
|
|
||||||
# ...
|
|
||||||
# ]
|
|
||||||
#
|
|
||||||
# when `Admin` gets defined we know that it plays the role of a namespace and
|
|
||||||
# that its children are spread over those directories. We'll visit them to set
|
|
||||||
# up the corresponding autoloads.
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Hash[String, Array[String]]
|
|
||||||
attr_reader :lazy_subdirs
|
|
||||||
|
|
||||||
# Absolute paths of files or directories not to be eager loaded.
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig Set[String]
|
|
||||||
attr_reader :eager_load_exclusions
|
|
||||||
|
|
||||||
# User-oriented callbacks to be fired when a constant is loaded.
|
|
||||||
attr_reader :on_load_callbacks
|
|
||||||
|
|
||||||
# @private
|
|
||||||
# @sig Mutex
|
|
||||||
attr_reader :mutex
|
|
||||||
|
|
||||||
# @private
|
|
||||||
# @sig Mutex
|
|
||||||
attr_reader :mutex2
|
|
||||||
|
|
||||||
def initialize
|
|
||||||
@initialized_at = Time.now
|
|
||||||
|
|
||||||
@tag = SecureRandom.hex(3)
|
|
||||||
@inflector = Inflector.new
|
|
||||||
@logger = self.class.default_logger
|
|
||||||
|
|
||||||
@root_dirs = {}
|
|
||||||
@preloads = []
|
|
||||||
@ignored_glob_patterns = Set.new
|
|
||||||
@ignored_paths = Set.new
|
|
||||||
@collapse_glob_patterns = Set.new
|
|
||||||
@collapse_dirs = Set.new
|
|
||||||
@autoloads = {}
|
|
||||||
@autoloaded_dirs = []
|
|
||||||
@to_unload = {}
|
|
||||||
@lazy_subdirs = {}
|
|
||||||
@eager_load_exclusions = Set.new
|
|
||||||
@on_load_callbacks = {}
|
|
||||||
|
|
||||||
# TODO: find a better name for these mutexes.
|
|
||||||
@mutex = Mutex.new
|
|
||||||
@mutex2 = Mutex.new
|
|
||||||
@setup = false
|
|
||||||
@eager_loaded = false
|
|
||||||
|
|
||||||
@reloading_enabled = false
|
|
||||||
|
|
||||||
Registry.register_loader(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Sets a tag for the loader, useful for logging.
|
|
||||||
#
|
|
||||||
# @param tag [#to_s]
|
|
||||||
# @sig (#to_s) -> void
|
|
||||||
def tag=(tag)
|
|
||||||
@tag = tag.to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
# Absolute paths of the root directories. This is a read-only collection,
|
|
||||||
# please push here via `push_dir`.
|
|
||||||
#
|
|
||||||
# @sig () -> Array[String]
|
|
||||||
def dirs
|
|
||||||
root_dirs.keys.freeze
|
|
||||||
end
|
|
||||||
|
|
||||||
# Pushes `path` to the list of root directories.
|
|
||||||
#
|
|
||||||
# Raises `Zeitwerk::Error` if `path` does not exist, or if another loader in
|
|
||||||
# the same process already manages that directory or one of its ascendants
|
|
||||||
# or descendants.
|
|
||||||
#
|
|
||||||
# @raise [Zeitwerk::Error]
|
|
||||||
# @sig (String | Pathname, Module) -> void
|
|
||||||
def push_dir(path, namespace: Object)
|
|
||||||
# Note that Class < Module.
|
|
||||||
unless namespace.is_a?(Module)
|
|
||||||
raise Error, "#{namespace.inspect} is not a class or module object, should be"
|
|
||||||
end
|
|
||||||
|
|
||||||
abspath = File.expand_path(path)
|
|
||||||
if dir?(abspath)
|
|
||||||
raise_if_conflicting_directory(abspath)
|
|
||||||
root_dirs[abspath] = namespace
|
|
||||||
else
|
|
||||||
raise Error, "the root directory #{abspath} does not exist"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# You need to call this method before setup in order to be able to reload.
|
|
||||||
# There is no way to undo this, either you want to reload or you don't.
|
|
||||||
#
|
|
||||||
# @raise [Zeitwerk::Error]
|
|
||||||
# @sig () -> void
|
|
||||||
def enable_reloading
|
|
||||||
mutex.synchronize do
|
|
||||||
break if @reloading_enabled
|
|
||||||
|
|
||||||
if @setup
|
|
||||||
raise Error, "cannot enable reloading after setup"
|
|
||||||
else
|
|
||||||
@reloading_enabled = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig () -> bool
|
|
||||||
def reloading_enabled?
|
|
||||||
@reloading_enabled
|
|
||||||
end
|
|
||||||
|
|
||||||
# Files or directories to be preloaded instead of lazy loaded.
|
|
||||||
#
|
|
||||||
# @sig (*(String | Pathname | Array[String | Pathname])) -> void
|
|
||||||
def preload(*paths)
|
|
||||||
mutex.synchronize do
|
|
||||||
expand_paths(paths).each do |abspath|
|
|
||||||
preloads << abspath
|
|
||||||
do_preload_abspath(abspath) if @setup
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Configure files, directories, or glob patterns to be totally ignored.
|
|
||||||
#
|
|
||||||
# @sig (*(String | Pathname | Array[String | Pathname])) -> void
|
|
||||||
def ignore(*glob_patterns)
|
|
||||||
glob_patterns = expand_paths(glob_patterns)
|
|
||||||
mutex.synchronize do
|
|
||||||
ignored_glob_patterns.merge(glob_patterns)
|
|
||||||
ignored_paths.merge(expand_glob_patterns(glob_patterns))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Configure directories or glob patterns to be collapsed.
|
|
||||||
#
|
|
||||||
# @sig (*(String | Pathname | Array[String | Pathname])) -> void
|
|
||||||
def collapse(*glob_patterns)
|
|
||||||
glob_patterns = expand_paths(glob_patterns)
|
|
||||||
mutex.synchronize do
|
|
||||||
collapse_glob_patterns.merge(glob_patterns)
|
|
||||||
collapse_dirs.merge(expand_glob_patterns(glob_patterns))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Configure a block to be invoked once a certain constant path is loaded.
|
|
||||||
# Supports multiple callbacks, and if there are many, they are executed in
|
|
||||||
# the order in which they were defined.
|
|
||||||
#
|
|
||||||
# loader.on_load("SomeApiClient") do
|
|
||||||
# SomeApiClient.endpoint = "https://api.dev"
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# @raise [TypeError]
|
|
||||||
# @sig (String) { () -> void } -> void
|
|
||||||
def on_load(cpath, &block)
|
|
||||||
raise TypeError, "on_load only accepts strings" unless cpath.is_a?(String)
|
|
||||||
|
|
||||||
mutex.synchronize do
|
|
||||||
(on_load_callbacks[cpath] ||= []) << block
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Sets autoloads in the root namespace and preloads files, if any.
|
|
||||||
#
|
|
||||||
# @sig () -> void
|
|
||||||
def setup
|
|
||||||
mutex.synchronize do
|
|
||||||
break if @setup
|
|
||||||
|
|
||||||
actual_root_dirs.each do |root_dir, namespace|
|
|
||||||
set_autoloads_in_dir(root_dir, namespace)
|
|
||||||
end
|
|
||||||
do_preload
|
|
||||||
|
|
||||||
@setup = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Removes loaded constants and configured autoloads.
|
|
||||||
#
|
|
||||||
# The objects the constants stored are no longer reachable through them. In
|
|
||||||
# addition, since said objects are normally not referenced from anywhere
|
|
||||||
# else, they are eligible for garbage collection, which would effectively
|
|
||||||
# unload them.
|
|
||||||
#
|
|
||||||
# @private
|
|
||||||
# @sig () -> void
|
|
||||||
def unload
|
|
||||||
mutex.synchronize do
|
|
||||||
# We are going to keep track of the files that were required by our
|
|
||||||
# autoloads to later remove them from $LOADED_FEATURES, thus making them
|
|
||||||
# loadable by Kernel#require again.
|
|
||||||
#
|
|
||||||
# Directories are not stored in $LOADED_FEATURES, keeping track of files
|
|
||||||
# is enough.
|
|
||||||
unloaded_files = Set.new
|
|
||||||
|
|
||||||
autoloads.each do |realpath, (parent, cname)|
|
|
||||||
if parent.autoload?(cname)
|
|
||||||
unload_autoload(parent, cname)
|
|
||||||
else
|
|
||||||
# Could happen if loaded with require_relative. That is unsupported,
|
|
||||||
# and the constant path would escape unloadable_cpath? This is just
|
|
||||||
# defensive code to clean things up as much as we are able to.
|
|
||||||
unload_cref(parent, cname) if cdef?(parent, cname)
|
|
||||||
unloaded_files.add(realpath) if ruby?(realpath)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
to_unload.each_value do |(realpath, (parent, cname))|
|
|
||||||
unload_cref(parent, cname) if cdef?(parent, cname)
|
|
||||||
unloaded_files.add(realpath) if ruby?(realpath)
|
|
||||||
end
|
|
||||||
|
|
||||||
unless unloaded_files.empty?
|
|
||||||
# Bootsnap decorates Kernel#require to speed it up using a cache and
|
|
||||||
# this optimization does not check if $LOADED_FEATURES has the file.
|
|
||||||
#
|
|
||||||
# To make it aware of changes, the gem defines singleton methods in
|
|
||||||
# $LOADED_FEATURES:
|
|
||||||
#
|
|
||||||
# https://github.com/Shopify/bootsnap/blob/master/lib/bootsnap/load_path_cache/core_ext/loaded_features.rb
|
|
||||||
#
|
|
||||||
# Rails applications may depend on bootsnap, so for unloading to work
|
|
||||||
# in that setting it is preferable that we restrict our API choice to
|
|
||||||
# one of those methods.
|
|
||||||
$LOADED_FEATURES.reject! { |file| unloaded_files.member?(file) }
|
|
||||||
end
|
|
||||||
|
|
||||||
autoloads.clear
|
|
||||||
autoloaded_dirs.clear
|
|
||||||
to_unload.clear
|
|
||||||
lazy_subdirs.clear
|
|
||||||
|
|
||||||
Registry.on_unload(self)
|
|
||||||
ExplicitNamespace.unregister(self)
|
|
||||||
|
|
||||||
@setup = false
|
|
||||||
@eager_loaded = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Unloads all loaded code, and calls setup again so that the loader is able
|
|
||||||
# to pick any changes in the file system.
|
|
||||||
#
|
|
||||||
# This method is not thread-safe, please see how this can be achieved by
|
|
||||||
# client code in the README of the project.
|
|
||||||
#
|
|
||||||
# @raise [Zeitwerk::Error]
|
|
||||||
# @sig () -> void
|
|
||||||
def reload
|
|
||||||
if reloading_enabled?
|
|
||||||
unload
|
|
||||||
recompute_ignored_paths
|
|
||||||
recompute_collapse_dirs
|
|
||||||
setup
|
|
||||||
else
|
|
||||||
raise ReloadingDisabledError, "can't reload, please call loader.enable_reloading before setup"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Eager loads all files in the root directories, recursively. Files do not
|
|
||||||
# need to be in `$LOAD_PATH`, absolute file names are used. Ignored files
|
|
||||||
# are not eager loaded. You can opt-out specifically in specific files and
|
|
||||||
# directories with `do_not_eager_load`.
|
|
||||||
#
|
|
||||||
# @sig () -> void
|
|
||||||
def eager_load
|
|
||||||
mutex.synchronize do
|
|
||||||
break if @eager_loaded
|
|
||||||
|
|
||||||
queue = []
|
|
||||||
actual_root_dirs.each do |root_dir, namespace|
|
|
||||||
queue << [namespace, root_dir] unless eager_load_exclusions.member?(root_dir)
|
|
||||||
end
|
|
||||||
|
|
||||||
while to_eager_load = queue.shift
|
|
||||||
namespace, dir = to_eager_load
|
|
||||||
|
|
||||||
ls(dir) do |basename, abspath|
|
|
||||||
next if eager_load_exclusions.member?(abspath)
|
|
||||||
|
|
||||||
if ruby?(abspath)
|
|
||||||
if cref = autoloads[File.realpath(abspath)]
|
|
||||||
cref[0].const_get(cref[1], false)
|
|
||||||
end
|
|
||||||
elsif dir?(abspath) && !root_dirs.key?(abspath)
|
|
||||||
if collapse_dirs.member?(abspath)
|
|
||||||
queue << [namespace, abspath]
|
|
||||||
else
|
|
||||||
cname = inflector.camelize(basename, abspath)
|
|
||||||
queue << [namespace.const_get(cname, false), abspath]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
autoloaded_dirs.each do |autoloaded_dir|
|
|
||||||
Registry.unregister_autoload(autoloaded_dir)
|
|
||||||
end
|
|
||||||
autoloaded_dirs.clear
|
|
||||||
|
|
||||||
@eager_loaded = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Let eager load ignore the given files or directories. The constants
|
|
||||||
# defined in those files are still autoloadable.
|
|
||||||
#
|
|
||||||
# @sig (*(String | Pathname | Array[String | Pathname])) -> void
|
|
||||||
def do_not_eager_load(*paths)
|
|
||||||
mutex.synchronize { eager_load_exclusions.merge(expand_paths(paths)) }
|
|
||||||
end
|
|
||||||
|
|
||||||
# Says if the given constant path would be unloaded on reload. This
|
|
||||||
# predicate returns `false` if reloading is disabled.
|
|
||||||
#
|
|
||||||
# @sig (String) -> bool
|
|
||||||
def unloadable_cpath?(cpath)
|
|
||||||
to_unload.key?(cpath)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Returns an array with the constant paths that would be unloaded on reload.
|
|
||||||
# This predicate returns an empty array if reloading is disabled.
|
|
||||||
#
|
|
||||||
# @sig () -> Array[String]
|
|
||||||
def unloadable_cpaths
|
|
||||||
to_unload.keys.freeze
|
|
||||||
end
|
|
||||||
|
|
||||||
# Logs to `$stdout`, handy shortcut for debugging.
|
|
||||||
#
|
|
||||||
# @sig () -> void
|
|
||||||
def log!
|
|
||||||
@logger = ->(msg) { puts msg }
|
|
||||||
end
|
|
||||||
|
|
||||||
# @private
|
|
||||||
# @sig (String) -> bool
|
|
||||||
def manages?(dir)
|
|
||||||
dir = dir + "/"
|
|
||||||
ignored_paths.each do |ignored_path|
|
|
||||||
return false if dir.start_with?(ignored_path + "/")
|
|
||||||
end
|
|
||||||
|
|
||||||
root_dirs.each_key do |root_dir|
|
|
||||||
return true if root_dir.start_with?(dir) || dir.start_with?(root_dir + "/")
|
|
||||||
end
|
|
||||||
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
# --- Class methods ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
class << self
|
|
||||||
# @sig #call | #debug | nil
|
|
||||||
attr_accessor :default_logger
|
|
||||||
|
|
||||||
# @private
|
|
||||||
# @sig Mutex
|
|
||||||
attr_accessor :mutex
|
|
||||||
|
|
||||||
# This is a shortcut for
|
|
||||||
#
|
|
||||||
# require "zeitwerk"
|
|
||||||
# loader = Zeitwerk::Loader.new
|
|
||||||
# loader.tag = File.basename(__FILE__, ".rb")
|
|
||||||
# loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
|
||||||
# loader.push_dir(__dir__)
|
|
||||||
#
|
|
||||||
# except that this method returns the same object in subsequent calls from
|
|
||||||
# the same file, in the unlikely case the gem wants to be able to reload.
|
|
||||||
#
|
|
||||||
# @sig () -> Zeitwerk::Loader
|
|
||||||
def for_gem
|
|
||||||
called_from = caller_locations(1, 1).first.path
|
|
||||||
Registry.loader_for_gem(called_from)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Broadcasts `eager_load` to all loaders.
|
|
||||||
#
|
|
||||||
# @sig () -> void
|
|
||||||
def eager_load_all
|
|
||||||
Registry.loaders.each(&:eager_load)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Returns an array with the absolute paths of the root directories of all
|
|
||||||
# registered loaders. This is a read-only collection.
|
|
||||||
#
|
|
||||||
# @sig () -> Array[String]
|
|
||||||
def all_dirs
|
|
||||||
Registry.loaders.flat_map(&:dirs).freeze
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self.mutex = Mutex.new
|
|
||||||
|
|
||||||
private # -------------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
# @sig () -> Array[String]
|
|
||||||
def actual_root_dirs
|
|
||||||
root_dirs.reject do |root_dir, _namespace|
|
|
||||||
!dir?(root_dir) || ignored_paths.member?(root_dir)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String, Module) -> void
|
|
||||||
def set_autoloads_in_dir(dir, parent)
|
|
||||||
ls(dir) do |basename, abspath|
|
|
||||||
begin
|
|
||||||
if ruby?(basename)
|
|
||||||
basename[-3..-1] = ''
|
|
||||||
cname = inflector.camelize(basename, abspath).to_sym
|
|
||||||
autoload_file(parent, cname, abspath)
|
|
||||||
elsif dir?(abspath)
|
|
||||||
# In a Rails application, `app/models/concerns` is a subdirectory of
|
|
||||||
# `app/models`, but both of them are root directories.
|
|
||||||
#
|
|
||||||
# To resolve the ambiguity file name -> constant path this introduces,
|
|
||||||
# the `app/models/concerns` directory is totally ignored as a namespace,
|
|
||||||
# it counts only as root. The guard checks that.
|
|
||||||
unless root_dirs.key?(abspath)
|
|
||||||
cname = inflector.camelize(basename, abspath).to_sym
|
|
||||||
if collapse_dirs.member?(abspath)
|
|
||||||
set_autoloads_in_dir(abspath, parent)
|
|
||||||
else
|
|
||||||
autoload_subdir(parent, cname, abspath)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
rescue ::NameError => error
|
|
||||||
path_type = ruby?(abspath) ? "file" : "directory"
|
|
||||||
|
|
||||||
raise NameError.new(<<~MESSAGE, error.name)
|
|
||||||
#{error.message} inferred by #{inflector.class} from #{path_type}
|
|
||||||
|
|
||||||
#{abspath}
|
|
||||||
|
|
||||||
Possible ways to address this:
|
|
||||||
|
|
||||||
* Tell Zeitwerk to ignore this particular #{path_type}.
|
|
||||||
* Tell Zeitwerk to ignore one of its parent directories.
|
|
||||||
* Rename the #{path_type} to comply with the naming conventions.
|
|
||||||
* Modify the inflector to handle this case.
|
|
||||||
MESSAGE
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (Module, Symbol, String) -> void
|
|
||||||
def autoload_subdir(parent, cname, subdir)
|
|
||||||
if autoload_path = autoload_for?(parent, cname)
|
|
||||||
cpath = cpath(parent, cname)
|
|
||||||
register_explicit_namespace(cpath) if ruby?(autoload_path)
|
|
||||||
# We do not need to issue another autoload, the existing one is enough
|
|
||||||
# no matter if it is for a file or a directory. Just remember the
|
|
||||||
# subdirectory has to be visited if the namespace is used.
|
|
||||||
(lazy_subdirs[cpath] ||= []) << subdir
|
|
||||||
elsif !cdef?(parent, cname)
|
|
||||||
# First time we find this namespace, set an autoload for it.
|
|
||||||
(lazy_subdirs[cpath(parent, cname)] ||= []) << subdir
|
|
||||||
set_autoload(parent, cname, subdir)
|
|
||||||
else
|
|
||||||
# For whatever reason the constant that corresponds to this namespace has
|
|
||||||
# already been defined, we have to recurse.
|
|
||||||
set_autoloads_in_dir(subdir, parent.const_get(cname))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (Module, Symbol, String) -> void
|
|
||||||
def autoload_file(parent, cname, file)
|
|
||||||
if autoload_path = autoload_for?(parent, cname)
|
|
||||||
# First autoload for a Ruby file wins, just ignore subsequent ones.
|
|
||||||
if ruby?(autoload_path)
|
|
||||||
log("file #{file} is ignored because #{autoload_path} has precedence") if logger
|
|
||||||
else
|
|
||||||
promote_namespace_from_implicit_to_explicit(
|
|
||||||
dir: autoload_path,
|
|
||||||
file: file,
|
|
||||||
parent: parent,
|
|
||||||
cname: cname
|
|
||||||
)
|
|
||||||
end
|
|
||||||
elsif cdef?(parent, cname)
|
|
||||||
log("file #{file} is ignored because #{cpath(parent, cname)} is already defined") if logger
|
|
||||||
else
|
|
||||||
set_autoload(parent, cname, file)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# `dir` is the directory that would have autovivified a namespace. `file` is
|
|
||||||
# the file where we've found the namespace is explicitly defined.
|
|
||||||
#
|
|
||||||
# @sig (dir: String, file: String, parent: Module, cname: Symbol) -> void
|
|
||||||
def promote_namespace_from_implicit_to_explicit(dir:, file:, parent:, cname:)
|
|
||||||
autoloads.delete(dir)
|
|
||||||
Registry.unregister_autoload(dir)
|
|
||||||
|
|
||||||
set_autoload(parent, cname, file)
|
|
||||||
register_explicit_namespace(cpath(parent, cname))
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (Module, Symbol, String) -> void
|
|
||||||
def set_autoload(parent, cname, abspath)
|
|
||||||
# $LOADED_FEATURES stores real paths since Ruby 2.4.4. We set and save the
|
|
||||||
# real path to be able to delete it from $LOADED_FEATURES on unload, and to
|
|
||||||
# be able to do a lookup later in Kernel#require for manual require calls.
|
|
||||||
#
|
|
||||||
# We freeze realpath because that saves allocations in Module#autoload.
|
|
||||||
# See #125.
|
|
||||||
realpath = File.realpath(abspath).freeze
|
|
||||||
parent.autoload(cname, realpath)
|
|
||||||
if logger
|
|
||||||
if ruby?(realpath)
|
|
||||||
log("autoload set for #{cpath(parent, cname)}, to be loaded from #{realpath}")
|
|
||||||
else
|
|
||||||
log("autoload set for #{cpath(parent, cname)}, to be autovivified from #{realpath}")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
autoloads[realpath] = [parent, cname]
|
|
||||||
Registry.register_autoload(self, realpath)
|
|
||||||
|
|
||||||
# See why in the documentation of Zeitwerk::Registry.inceptions.
|
|
||||||
unless parent.autoload?(cname)
|
|
||||||
Registry.register_inception(cpath(parent, cname), realpath, self)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (Module, Symbol) -> String?
|
|
||||||
def autoload_for?(parent, cname)
|
|
||||||
strict_autoload_path(parent, cname) || Registry.inception?(cpath(parent, cname))
|
|
||||||
end
|
|
||||||
|
|
||||||
# The autoload? predicate takes into account the ancestor chain of the
|
|
||||||
# receiver, like const_defined? and other methods in the constants API do.
|
|
||||||
#
|
|
||||||
# For example, given
|
|
||||||
#
|
|
||||||
# class A
|
|
||||||
# autoload :X, "x.rb"
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# class B < A
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# B.autoload?(:X) returns "x.rb".
|
|
||||||
#
|
|
||||||
# We need a way to strictly check in parent ignoring ancestors.
|
|
||||||
#
|
|
||||||
# @sig (Module, Symbol) -> String?
|
|
||||||
if method(:autoload?).arity == 1
|
|
||||||
def strict_autoload_path(parent, cname)
|
|
||||||
parent.autoload?(cname) if cdef?(parent, cname)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
def strict_autoload_path(parent, cname)
|
|
||||||
parent.autoload?(cname, false)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# This method is called this way because I prefer `preload` to be the method
|
|
||||||
# name to configure preloads in the public interface.
|
|
||||||
#
|
|
||||||
# @sig () -> void
|
|
||||||
def do_preload
|
|
||||||
preloads.each do |abspath|
|
|
||||||
do_preload_abspath(abspath)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String) -> void
|
|
||||||
def do_preload_abspath(abspath)
|
|
||||||
if ruby?(abspath)
|
|
||||||
do_preload_file(abspath)
|
|
||||||
elsif dir?(abspath)
|
|
||||||
do_preload_dir(abspath)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String) -> void
|
|
||||||
def do_preload_dir(dir)
|
|
||||||
ls(dir) do |_basename, abspath|
|
|
||||||
do_preload_abspath(abspath)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String) -> bool
|
|
||||||
def do_preload_file(file)
|
|
||||||
log("preloading #{file}") if logger
|
|
||||||
require file
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (Module, Symbol) -> String
|
|
||||||
def cpath(parent, cname)
|
|
||||||
parent.equal?(Object) ? cname.to_s : "#{real_mod_name(parent)}::#{cname}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String) { (String, String) -> void } -> void
|
|
||||||
def ls(dir)
|
|
||||||
Dir.foreach(dir) do |basename|
|
|
||||||
next if basename.start_with?(".")
|
|
||||||
|
|
||||||
abspath = File.join(dir, basename)
|
|
||||||
next if ignored_paths.member?(abspath)
|
|
||||||
|
|
||||||
# We freeze abspath because that saves allocations when passed later to
|
|
||||||
# File methods. See #125.
|
|
||||||
yield basename, abspath.freeze
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String) -> bool
|
|
||||||
def ruby?(path)
|
|
||||||
path.end_with?(".rb")
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String) -> bool
|
|
||||||
def dir?(path)
|
|
||||||
File.directory?(path)
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String | Pathname | Array[String | Pathname]) -> Array[String]
|
|
||||||
def expand_paths(paths)
|
|
||||||
paths.flatten.map! { |path| File.expand_path(path) }
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (Array[String]) -> Array[String]
|
|
||||||
def expand_glob_patterns(glob_patterns)
|
|
||||||
# Note that Dir.glob works with regular file names just fine. That is,
|
|
||||||
# glob patterns technically need no wildcards.
|
|
||||||
glob_patterns.flat_map { |glob_pattern| Dir.glob(glob_pattern) }
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig () -> void
|
|
||||||
def recompute_ignored_paths
|
|
||||||
ignored_paths.replace(expand_glob_patterns(ignored_glob_patterns))
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig () -> void
|
|
||||||
def recompute_collapse_dirs
|
|
||||||
collapse_dirs.replace(expand_glob_patterns(collapse_glob_patterns))
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String) -> void
|
|
||||||
def log(message)
|
|
||||||
method_name = logger.respond_to?(:debug) ? :debug : :call
|
|
||||||
logger.send(method_name, "Zeitwerk@#{tag}: #{message}")
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (Module, Symbol) -> bool
|
|
||||||
def cdef?(parent, cname)
|
|
||||||
parent.const_defined?(cname, false)
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String) -> void
|
|
||||||
def register_explicit_namespace(cpath)
|
|
||||||
ExplicitNamespace.register(cpath, self)
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (String) -> void
|
|
||||||
def raise_if_conflicting_directory(dir)
|
|
||||||
self.class.mutex.synchronize do
|
|
||||||
Registry.loaders.each do |loader|
|
|
||||||
if loader != self && loader.manages?(dir)
|
|
||||||
require "pp"
|
|
||||||
raise Error,
|
|
||||||
"loader\n\n#{pretty_inspect}\n\nwants to manage directory #{dir}," \
|
|
||||||
" which is already managed by\n\n#{loader.pretty_inspect}\n"
|
|
||||||
EOS
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (Module, Symbol) -> void
|
|
||||||
def unload_autoload(parent, cname)
|
|
||||||
parent.__send__(:remove_const, cname)
|
|
||||||
log("autoload for #{cpath(parent, cname)} removed") if logger
|
|
||||||
end
|
|
||||||
|
|
||||||
# @sig (Module, Symbol) -> void
|
|
||||||
def unload_cref(parent, cname)
|
|
||||||
parent.__send__(:remove_const, cname)
|
|
||||||
log("#{cpath(parent, cname)} unloaded") if logger
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
@ -3,10 +3,23 @@
|
|||||||
module Zeitwerk
|
module Zeitwerk
|
||||||
require_relative "zeitwerk/real_mod_name"
|
require_relative "zeitwerk/real_mod_name"
|
||||||
require_relative "zeitwerk/loader"
|
require_relative "zeitwerk/loader"
|
||||||
|
require_relative "zeitwerk/autoloads"
|
||||||
require_relative "zeitwerk/registry"
|
require_relative "zeitwerk/registry"
|
||||||
require_relative "zeitwerk/explicit_namespace"
|
require_relative "zeitwerk/explicit_namespace"
|
||||||
require_relative "zeitwerk/inflector"
|
require_relative "zeitwerk/inflector"
|
||||||
require_relative "zeitwerk/gem_inflector"
|
require_relative "zeitwerk/gem_inflector"
|
||||||
require_relative "zeitwerk/kernel"
|
require_relative "zeitwerk/kernel"
|
||||||
require_relative "zeitwerk/error"
|
require_relative "zeitwerk/error"
|
||||||
|
require_relative "zeitwerk/version"
|
||||||
|
|
||||||
|
# This is a dangerous method.
|
||||||
|
#
|
||||||
|
# @experimental
|
||||||
|
# @sig () -> void
|
||||||
|
def self.with_loader
|
||||||
|
loader = Zeitwerk::Loader.new
|
||||||
|
yield loader
|
||||||
|
ensure
|
||||||
|
loader.unregister
|
||||||
|
end
|
||||||
end
|
end
|
71
Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.5.0/lib/zeitwerk/autoloads.rb
vendored
Normal file
71
Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.5.0/lib/zeitwerk/autoloads.rb
vendored
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Zeitwerk
|
||||||
|
# @private
|
||||||
|
class Autoloads
|
||||||
|
# Maps crefs for which an autoload has been defined to the corresponding
|
||||||
|
# absolute path.
|
||||||
|
#
|
||||||
|
# [Object, :User] => "/Users/fxn/blog/app/models/user.rb"
|
||||||
|
# [Object, :Hotel] => "/Users/fxn/blog/app/models/hotel"
|
||||||
|
# ...
|
||||||
|
#
|
||||||
|
# This colection is transient, callbacks delete its entries as autoloads get
|
||||||
|
# executed.
|
||||||
|
#
|
||||||
|
# @sig Hash[[Module, Symbol], String]
|
||||||
|
attr_reader :c2a
|
||||||
|
|
||||||
|
# This is the inverse of c2a, for inverse lookups.
|
||||||
|
#
|
||||||
|
# @sig Hash[String, [Module, Symbol]]
|
||||||
|
attr_reader :a2c
|
||||||
|
|
||||||
|
# @sig () -> void
|
||||||
|
def initialize
|
||||||
|
@c2a = {}
|
||||||
|
@a2c = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (Module, Symbol, String) -> void
|
||||||
|
def define(parent, cname, abspath)
|
||||||
|
parent.autoload(cname, abspath)
|
||||||
|
cref = [parent, cname]
|
||||||
|
c2a[cref] = abspath
|
||||||
|
a2c[abspath] = cref
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig () { () -> [[Module, Symbol], String] } -> void
|
||||||
|
def each(&block)
|
||||||
|
c2a.each(&block)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (Module, Symbol) -> String?
|
||||||
|
def abspath_for(parent, cname)
|
||||||
|
c2a[[parent, cname]]
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String) -> [Module, Symbol]?
|
||||||
|
def cref_for(abspath)
|
||||||
|
a2c[abspath]
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String) -> [Module, Symbol]?
|
||||||
|
def delete(abspath)
|
||||||
|
cref = a2c.delete(abspath)
|
||||||
|
c2a.delete(cref)
|
||||||
|
cref
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig () -> void
|
||||||
|
def clear
|
||||||
|
c2a.clear
|
||||||
|
a2c.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig () -> bool
|
||||||
|
def empty?
|
||||||
|
c2a.empty? && a2c.empty?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Zeitwerk
|
module Zeitwerk
|
||||||
class Error < StandardError
|
class Error < StandardError
|
||||||
end
|
end
|
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Zeitwerk
|
module Zeitwerk
|
||||||
# Centralizes the logic for the trace point used to detect the creation of
|
# Centralizes the logic for the trace point used to detect the creation of
|
||||||
# explicit namespaces, needed to descend into matching subdirectories right
|
# explicit namespaces, needed to descend into matching subdirectories right
|
||||||
@ -41,7 +43,7 @@ module Zeitwerk
|
|||||||
|
|
||||||
# @private
|
# @private
|
||||||
# @sig (Zeitwerk::Loader) -> void
|
# @sig (Zeitwerk::Loader) -> void
|
||||||
def unregister(loader)
|
def unregister_loader(loader)
|
||||||
cpaths.delete_if { |_cpath, l| l == loader }
|
cpaths.delete_if { |_cpath, l| l == loader }
|
||||||
disable_tracer_if_unneeded
|
disable_tracer_if_unneeded
|
||||||
end
|
end
|
||||||
@ -62,8 +64,14 @@ module Zeitwerk
|
|||||||
# than accessing its name.
|
# than accessing its name.
|
||||||
return if event.self.singleton_class?
|
return if event.self.singleton_class?
|
||||||
|
|
||||||
# Note that it makes sense to compute the hash code unconditionally,
|
# It might be tempting to return if name.nil?, to avoid the computation
|
||||||
# because the trace point is disabled if cpaths is empty.
|
# of a hash code and delete call. But Ruby does not trigger the :class
|
||||||
|
# event on Class.new or Module.new, so that would incur in an extra call
|
||||||
|
# for nothing.
|
||||||
|
#
|
||||||
|
# On the other hand, if we were called, cpaths is not empty. Otherwise
|
||||||
|
# the tracer is disabled. So we do need to go ahead with the hash code
|
||||||
|
# computation and delete call.
|
||||||
if loader = cpaths.delete(real_mod_name(event.self))
|
if loader = cpaths.delete(real_mod_name(event.self))
|
||||||
loader.on_namespace_loaded(event.self)
|
loader.on_namespace_loaded(event.self)
|
||||||
disable_tracer_if_unneeded
|
disable_tracer_if_unneeded
|
@ -3,7 +3,7 @@
|
|||||||
module Kernel
|
module Kernel
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
# We are going to decorate Kerner#require with two goals.
|
# We are going to decorate Kernel#require with two goals.
|
||||||
#
|
#
|
||||||
# First, by intercepting Kernel#require calls, we are able to autovivify
|
# First, by intercepting Kernel#require calls, we are able to autovivify
|
||||||
# modules on required directories, and also do internal housekeeping when
|
# modules on required directories, and also do internal housekeeping when
|
||||||
@ -12,7 +12,8 @@ module Kernel
|
|||||||
# On the other hand, if you publish a new version of a gem that is now managed
|
# On the other hand, if you publish a new version of a gem that is now managed
|
||||||
# by Zeitwerk, client code can reference directly your classes and modules and
|
# by Zeitwerk, client code can reference directly your classes and modules and
|
||||||
# should not require anything. But if someone has legacy require calls around,
|
# should not require anything. But if someone has legacy require calls around,
|
||||||
# they will work as expected, and in a compatible way.
|
# they will work as expected, and in a compatible way. This feature is by now
|
||||||
|
# EXPERIMENTAL and UNDOCUMENTED.
|
||||||
#
|
#
|
||||||
# We cannot decorate with prepend + super because Kernel has already been
|
# We cannot decorate with prepend + super because Kernel has already been
|
||||||
# included in Object, and changes in ancestors don't get propagated into
|
# included in Object, and changes in ancestors don't get propagated into
|
||||||
@ -33,9 +34,9 @@ module Kernel
|
|||||||
else
|
else
|
||||||
zeitwerk_original_require(path).tap do |required|
|
zeitwerk_original_require(path).tap do |required|
|
||||||
if required
|
if required
|
||||||
realpath = $LOADED_FEATURES.last
|
abspath = $LOADED_FEATURES.last
|
||||||
if loader = Zeitwerk::Registry.loader_for(realpath)
|
if loader = Zeitwerk::Registry.loader_for(abspath)
|
||||||
loader.on_file_autoloaded(realpath)
|
loader.on_file_autoloaded(abspath)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
507
Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.5.0/lib/zeitwerk/loader.rb
vendored
Normal file
507
Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.5.0/lib/zeitwerk/loader.rb
vendored
Normal file
@ -0,0 +1,507 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "set"
|
||||||
|
require "securerandom"
|
||||||
|
|
||||||
|
module Zeitwerk
|
||||||
|
class Loader
|
||||||
|
require_relative "loader/helpers"
|
||||||
|
require_relative "loader/callbacks"
|
||||||
|
require_relative "loader/config"
|
||||||
|
|
||||||
|
include RealModName
|
||||||
|
include Callbacks
|
||||||
|
include Helpers
|
||||||
|
include Config
|
||||||
|
|
||||||
|
# Keeps track of autoloads defined by the loader which have not been
|
||||||
|
# executed so far.
|
||||||
|
#
|
||||||
|
# This metadata helps us implement a few things:
|
||||||
|
#
|
||||||
|
# 1. When autoloads are triggered, ensure they define the expected constant
|
||||||
|
# and invoke user callbacks. If reloading is enabled, remember cref and
|
||||||
|
# abspath for later unloading logic.
|
||||||
|
#
|
||||||
|
# 2. When unloading, remove autoloads that have not been executed.
|
||||||
|
#
|
||||||
|
# 3. Eager load with a recursive const_get, rather than a recursive require,
|
||||||
|
# for consistency with lazy loading.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Zeitwerk::Autoloads
|
||||||
|
attr_reader :autoloads
|
||||||
|
|
||||||
|
# We keep track of autoloaded directories to remove them from the registry
|
||||||
|
# at the end of eager loading.
|
||||||
|
#
|
||||||
|
# Files are removed as they are autoloaded, but directories need to wait due
|
||||||
|
# to concurrency (see why in Zeitwerk::Loader::Callbacks#on_dir_autoloaded).
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Array[String]
|
||||||
|
attr_reader :autoloaded_dirs
|
||||||
|
|
||||||
|
# Stores metadata needed for unloading. Its entries look like this:
|
||||||
|
#
|
||||||
|
# "Admin::Role" => [".../admin/role.rb", [Admin, :Role]]
|
||||||
|
#
|
||||||
|
# The cpath as key helps implementing unloadable_cpath? The file name is
|
||||||
|
# stored in order to be able to delete it from $LOADED_FEATURES, and the
|
||||||
|
# pair [Module, Symbol] is used to remove_const the constant from the class
|
||||||
|
# or module object.
|
||||||
|
#
|
||||||
|
# If reloading is enabled, this hash is filled as constants are autoloaded
|
||||||
|
# or eager loaded. Otherwise, the collection remains empty.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Hash[String, [String, [Module, Symbol]]]
|
||||||
|
attr_reader :to_unload
|
||||||
|
|
||||||
|
# Maps constant paths of namespaces to arrays of corresponding directories.
|
||||||
|
#
|
||||||
|
# For example, given this mapping:
|
||||||
|
#
|
||||||
|
# "Admin" => [
|
||||||
|
# "/Users/fxn/blog/app/controllers/admin",
|
||||||
|
# "/Users/fxn/blog/app/models/admin",
|
||||||
|
# ...
|
||||||
|
# ]
|
||||||
|
#
|
||||||
|
# when `Admin` gets defined we know that it plays the role of a namespace and
|
||||||
|
# that its children are spread over those directories. We'll visit them to set
|
||||||
|
# up the corresponding autoloads.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Hash[String, Array[String]]
|
||||||
|
attr_reader :lazy_subdirs
|
||||||
|
|
||||||
|
# @private
|
||||||
|
# @sig Mutex
|
||||||
|
attr_reader :mutex
|
||||||
|
|
||||||
|
# @private
|
||||||
|
# @sig Mutex
|
||||||
|
attr_reader :mutex2
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
super
|
||||||
|
|
||||||
|
@autoloads = Autoloads.new
|
||||||
|
@autoloaded_dirs = []
|
||||||
|
@to_unload = {}
|
||||||
|
@lazy_subdirs = Hash.new { |h, cpath| h[cpath] = [] }
|
||||||
|
@mutex = Mutex.new
|
||||||
|
@mutex2 = Mutex.new
|
||||||
|
@setup = false
|
||||||
|
@eager_loaded = false
|
||||||
|
|
||||||
|
Registry.register_loader(self)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Sets autoloads in the root namespace.
|
||||||
|
#
|
||||||
|
# @sig () -> void
|
||||||
|
def setup
|
||||||
|
mutex.synchronize do
|
||||||
|
break if @setup
|
||||||
|
|
||||||
|
actual_root_dirs.each do |root_dir, namespace|
|
||||||
|
set_autoloads_in_dir(root_dir, namespace)
|
||||||
|
end
|
||||||
|
|
||||||
|
on_setup_callbacks.each(&:call)
|
||||||
|
|
||||||
|
@setup = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Removes loaded constants and configured autoloads.
|
||||||
|
#
|
||||||
|
# The objects the constants stored are no longer reachable through them. In
|
||||||
|
# addition, since said objects are normally not referenced from anywhere
|
||||||
|
# else, they are eligible for garbage collection, which would effectively
|
||||||
|
# unload them.
|
||||||
|
#
|
||||||
|
# This method is public but undocumented. Main interface is `reload`, which
|
||||||
|
# means `unload` + `setup`. This one is avaiable to be used together with
|
||||||
|
# `unregister`, which is undocumented too.
|
||||||
|
#
|
||||||
|
# @sig () -> void
|
||||||
|
def unload
|
||||||
|
mutex.synchronize do
|
||||||
|
# We are going to keep track of the files that were required by our
|
||||||
|
# autoloads to later remove them from $LOADED_FEATURES, thus making them
|
||||||
|
# loadable by Kernel#require again.
|
||||||
|
#
|
||||||
|
# Directories are not stored in $LOADED_FEATURES, keeping track of files
|
||||||
|
# is enough.
|
||||||
|
unloaded_files = Set.new
|
||||||
|
|
||||||
|
autoloads.each do |(parent, cname), abspath|
|
||||||
|
if parent.autoload?(cname)
|
||||||
|
unload_autoload(parent, cname)
|
||||||
|
else
|
||||||
|
# Could happen if loaded with require_relative. That is unsupported,
|
||||||
|
# and the constant path would escape unloadable_cpath? This is just
|
||||||
|
# defensive code to clean things up as much as we are able to.
|
||||||
|
unload_cref(parent, cname)
|
||||||
|
unloaded_files.add(abspath) if ruby?(abspath)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
to_unload.each do |cpath, (abspath, (parent, cname))|
|
||||||
|
unless on_unload_callbacks.empty?
|
||||||
|
value = parent.const_get(cname)
|
||||||
|
run_on_unload_callbacks(cpath, value, abspath)
|
||||||
|
end
|
||||||
|
|
||||||
|
unload_cref(parent, cname)
|
||||||
|
unloaded_files.add(abspath) if ruby?(abspath)
|
||||||
|
end
|
||||||
|
|
||||||
|
unless unloaded_files.empty?
|
||||||
|
# Bootsnap decorates Kernel#require to speed it up using a cache and
|
||||||
|
# this optimization does not check if $LOADED_FEATURES has the file.
|
||||||
|
#
|
||||||
|
# To make it aware of changes, the gem defines singleton methods in
|
||||||
|
# $LOADED_FEATURES:
|
||||||
|
#
|
||||||
|
# https://github.com/Shopify/bootsnap/blob/master/lib/bootsnap/load_path_cache/core_ext/loaded_features.rb
|
||||||
|
#
|
||||||
|
# Rails applications may depend on bootsnap, so for unloading to work
|
||||||
|
# in that setting it is preferable that we restrict our API choice to
|
||||||
|
# one of those methods.
|
||||||
|
$LOADED_FEATURES.reject! { |file| unloaded_files.member?(file) }
|
||||||
|
end
|
||||||
|
|
||||||
|
autoloads.clear
|
||||||
|
autoloaded_dirs.clear
|
||||||
|
to_unload.clear
|
||||||
|
lazy_subdirs.clear
|
||||||
|
|
||||||
|
Registry.on_unload(self)
|
||||||
|
ExplicitNamespace.unregister_loader(self)
|
||||||
|
|
||||||
|
@setup = false
|
||||||
|
@eager_loaded = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Unloads all loaded code, and calls setup again so that the loader is able
|
||||||
|
# to pick any changes in the file system.
|
||||||
|
#
|
||||||
|
# This method is not thread-safe, please see how this can be achieved by
|
||||||
|
# client code in the README of the project.
|
||||||
|
#
|
||||||
|
# @raise [Zeitwerk::Error]
|
||||||
|
# @sig () -> void
|
||||||
|
def reload
|
||||||
|
if reloading_enabled?
|
||||||
|
unload
|
||||||
|
recompute_ignored_paths
|
||||||
|
recompute_collapse_dirs
|
||||||
|
setup
|
||||||
|
else
|
||||||
|
raise ReloadingDisabledError, "can't reload, please call loader.enable_reloading before setup"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Eager loads all files in the root directories, recursively. Files do not
|
||||||
|
# need to be in `$LOAD_PATH`, absolute file names are used. Ignored files
|
||||||
|
# are not eager loaded. You can opt-out specifically in specific files and
|
||||||
|
# directories with `do_not_eager_load`, and that can be overridden passing
|
||||||
|
# `force: true`.
|
||||||
|
#
|
||||||
|
# @sig (true | false) -> void
|
||||||
|
def eager_load(force: false)
|
||||||
|
mutex.synchronize do
|
||||||
|
break if @eager_loaded
|
||||||
|
|
||||||
|
log("eager load start") if logger
|
||||||
|
|
||||||
|
honour_exclusions = !force
|
||||||
|
|
||||||
|
queue = []
|
||||||
|
actual_root_dirs.each do |root_dir, namespace|
|
||||||
|
queue << [namespace, root_dir] unless honour_exclusions && excluded_from_eager_load?(root_dir)
|
||||||
|
end
|
||||||
|
|
||||||
|
while to_eager_load = queue.shift
|
||||||
|
namespace, dir = to_eager_load
|
||||||
|
|
||||||
|
ls(dir) do |basename, abspath|
|
||||||
|
next if honour_exclusions && excluded_from_eager_load?(abspath)
|
||||||
|
|
||||||
|
if ruby?(abspath)
|
||||||
|
if cref = autoloads.cref_for(abspath)
|
||||||
|
cget(*cref)
|
||||||
|
end
|
||||||
|
elsif dir?(abspath) && !root_dirs.key?(abspath)
|
||||||
|
if collapse?(abspath)
|
||||||
|
queue << [namespace, abspath]
|
||||||
|
else
|
||||||
|
cname = inflector.camelize(basename, abspath)
|
||||||
|
queue << [cget(namespace, cname), abspath]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
autoloaded_dirs.each do |autoloaded_dir|
|
||||||
|
Registry.unregister_autoload(autoloaded_dir)
|
||||||
|
end
|
||||||
|
autoloaded_dirs.clear
|
||||||
|
|
||||||
|
@eager_loaded = true
|
||||||
|
|
||||||
|
log("eager load end") if logger
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Says if the given constant path would be unloaded on reload. This
|
||||||
|
# predicate returns `false` if reloading is disabled.
|
||||||
|
#
|
||||||
|
# @sig (String) -> bool
|
||||||
|
def unloadable_cpath?(cpath)
|
||||||
|
to_unload.key?(cpath)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns an array with the constant paths that would be unloaded on reload.
|
||||||
|
# This predicate returns an empty array if reloading is disabled.
|
||||||
|
#
|
||||||
|
# @sig () -> Array[String]
|
||||||
|
def unloadable_cpaths
|
||||||
|
to_unload.keys.freeze
|
||||||
|
end
|
||||||
|
|
||||||
|
# This is a dangerous method.
|
||||||
|
#
|
||||||
|
# @experimental
|
||||||
|
# @sig () -> void
|
||||||
|
def unregister
|
||||||
|
Registry.unregister_loader(self)
|
||||||
|
ExplicitNamespace.unregister_loader(self)
|
||||||
|
end
|
||||||
|
|
||||||
|
# --- Class methods ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class << self
|
||||||
|
# @sig #call | #debug | nil
|
||||||
|
attr_accessor :default_logger
|
||||||
|
|
||||||
|
# @private
|
||||||
|
# @sig Mutex
|
||||||
|
attr_accessor :mutex
|
||||||
|
|
||||||
|
# This is a shortcut for
|
||||||
|
#
|
||||||
|
# require "zeitwerk"
|
||||||
|
# loader = Zeitwerk::Loader.new
|
||||||
|
# loader.tag = File.basename(__FILE__, ".rb")
|
||||||
|
# loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
||||||
|
# loader.push_dir(__dir__)
|
||||||
|
#
|
||||||
|
# except that this method returns the same object in subsequent calls from
|
||||||
|
# the same file, in the unlikely case the gem wants to be able to reload.
|
||||||
|
#
|
||||||
|
# @sig () -> Zeitwerk::Loader
|
||||||
|
def for_gem
|
||||||
|
called_from = caller_locations(1, 1).first.path
|
||||||
|
Registry.loader_for_gem(called_from)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Broadcasts `eager_load` to all loaders.
|
||||||
|
#
|
||||||
|
# @sig () -> void
|
||||||
|
def eager_load_all
|
||||||
|
Registry.loaders.each(&:eager_load)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns an array with the absolute paths of the root directories of all
|
||||||
|
# registered loaders. This is a read-only collection.
|
||||||
|
#
|
||||||
|
# @sig () -> Array[String]
|
||||||
|
def all_dirs
|
||||||
|
Registry.loaders.flat_map(&:dirs).freeze
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self.mutex = Mutex.new
|
||||||
|
|
||||||
|
private # -------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# @sig (String, Module) -> void
|
||||||
|
def set_autoloads_in_dir(dir, parent)
|
||||||
|
ls(dir) do |basename, abspath|
|
||||||
|
begin
|
||||||
|
if ruby?(basename)
|
||||||
|
basename.delete_suffix!(".rb")
|
||||||
|
cname = inflector.camelize(basename, abspath).to_sym
|
||||||
|
autoload_file(parent, cname, abspath)
|
||||||
|
elsif dir?(abspath)
|
||||||
|
# In a Rails application, `app/models/concerns` is a subdirectory of
|
||||||
|
# `app/models`, but both of them are root directories.
|
||||||
|
#
|
||||||
|
# To resolve the ambiguity file name -> constant path this introduces,
|
||||||
|
# the `app/models/concerns` directory is totally ignored as a namespace,
|
||||||
|
# it counts only as root. The guard checks that.
|
||||||
|
unless root_dir?(abspath)
|
||||||
|
cname = inflector.camelize(basename, abspath).to_sym
|
||||||
|
if collapse?(abspath)
|
||||||
|
set_autoloads_in_dir(abspath, parent)
|
||||||
|
else
|
||||||
|
autoload_subdir(parent, cname, abspath)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
rescue ::NameError => error
|
||||||
|
path_type = ruby?(abspath) ? "file" : "directory"
|
||||||
|
|
||||||
|
raise NameError.new(<<~MESSAGE, error.name)
|
||||||
|
#{error.message} inferred by #{inflector.class} from #{path_type}
|
||||||
|
|
||||||
|
#{abspath}
|
||||||
|
|
||||||
|
Possible ways to address this:
|
||||||
|
|
||||||
|
* Tell Zeitwerk to ignore this particular #{path_type}.
|
||||||
|
* Tell Zeitwerk to ignore one of its parent directories.
|
||||||
|
* Rename the #{path_type} to comply with the naming conventions.
|
||||||
|
* Modify the inflector to handle this case.
|
||||||
|
MESSAGE
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (Module, Symbol, String) -> void
|
||||||
|
def autoload_subdir(parent, cname, subdir)
|
||||||
|
if autoload_path = autoloads.abspath_for(parent, cname)
|
||||||
|
cpath = cpath(parent, cname)
|
||||||
|
register_explicit_namespace(cpath) if ruby?(autoload_path)
|
||||||
|
# We do not need to issue another autoload, the existing one is enough
|
||||||
|
# no matter if it is for a file or a directory. Just remember the
|
||||||
|
# subdirectory has to be visited if the namespace is used.
|
||||||
|
lazy_subdirs[cpath] << subdir
|
||||||
|
elsif !cdef?(parent, cname)
|
||||||
|
# First time we find this namespace, set an autoload for it.
|
||||||
|
lazy_subdirs[cpath(parent, cname)] << subdir
|
||||||
|
set_autoload(parent, cname, subdir)
|
||||||
|
else
|
||||||
|
# For whatever reason the constant that corresponds to this namespace has
|
||||||
|
# already been defined, we have to recurse.
|
||||||
|
log("the namespace #{cpath(parent, cname)} already exists, descending into #{subdir}") if logger
|
||||||
|
set_autoloads_in_dir(subdir, cget(parent, cname))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (Module, Symbol, String) -> void
|
||||||
|
def autoload_file(parent, cname, file)
|
||||||
|
if autoload_path = strict_autoload_path(parent, cname) || Registry.inception?(cpath(parent, cname))
|
||||||
|
# First autoload for a Ruby file wins, just ignore subsequent ones.
|
||||||
|
if ruby?(autoload_path)
|
||||||
|
log("file #{file} is ignored because #{autoload_path} has precedence") if logger
|
||||||
|
else
|
||||||
|
promote_namespace_from_implicit_to_explicit(
|
||||||
|
dir: autoload_path,
|
||||||
|
file: file,
|
||||||
|
parent: parent,
|
||||||
|
cname: cname
|
||||||
|
)
|
||||||
|
end
|
||||||
|
elsif cdef?(parent, cname)
|
||||||
|
log("file #{file} is ignored because #{cpath(parent, cname)} is already defined") if logger
|
||||||
|
else
|
||||||
|
set_autoload(parent, cname, file)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# `dir` is the directory that would have autovivified a namespace. `file` is
|
||||||
|
# the file where we've found the namespace is explicitly defined.
|
||||||
|
#
|
||||||
|
# @sig (dir: String, file: String, parent: Module, cname: Symbol) -> void
|
||||||
|
def promote_namespace_from_implicit_to_explicit(dir:, file:, parent:, cname:)
|
||||||
|
autoloads.delete(dir)
|
||||||
|
Registry.unregister_autoload(dir)
|
||||||
|
|
||||||
|
log("earlier autoload for #{cpath(parent, cname)} discarded, it is actually an explicit namespace defined in #{file}") if logger
|
||||||
|
|
||||||
|
set_autoload(parent, cname, file)
|
||||||
|
register_explicit_namespace(cpath(parent, cname))
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (Module, Symbol, String) -> void
|
||||||
|
def set_autoload(parent, cname, abspath)
|
||||||
|
autoloads.define(parent, cname, abspath)
|
||||||
|
|
||||||
|
if logger
|
||||||
|
if ruby?(abspath)
|
||||||
|
log("autoload set for #{cpath(parent, cname)}, to be loaded from #{abspath}")
|
||||||
|
else
|
||||||
|
log("autoload set for #{cpath(parent, cname)}, to be autovivified from #{abspath}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Registry.register_autoload(self, abspath)
|
||||||
|
|
||||||
|
# See why in the documentation of Zeitwerk::Registry.inceptions.
|
||||||
|
unless parent.autoload?(cname)
|
||||||
|
Registry.register_inception(cpath(parent, cname), abspath, self)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String) -> void
|
||||||
|
def register_explicit_namespace(cpath)
|
||||||
|
ExplicitNamespace.register(cpath, self)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String) -> void
|
||||||
|
def raise_if_conflicting_directory(dir)
|
||||||
|
self.class.mutex.synchronize do
|
||||||
|
Registry.loaders.each do |loader|
|
||||||
|
next if loader == self
|
||||||
|
next if loader.ignores?(dir)
|
||||||
|
|
||||||
|
dir = dir + "/"
|
||||||
|
loader.root_dirs.each do |root_dir, _namespace|
|
||||||
|
next if ignores?(root_dir)
|
||||||
|
|
||||||
|
root_dir = root_dir + "/"
|
||||||
|
if dir.start_with?(root_dir) || root_dir.start_with?(dir)
|
||||||
|
require "pp" # Needed for pretty_inspect, even in Ruby 2.5.
|
||||||
|
raise Error,
|
||||||
|
"loader\n\n#{pretty_inspect}\n\nwants to manage directory #{dir.chop}," \
|
||||||
|
" which is already managed by\n\n#{loader.pretty_inspect}\n"
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String, Object, String) -> void
|
||||||
|
def run_on_unload_callbacks(cpath, value, abspath)
|
||||||
|
# Order matters. If present, run the most specific one.
|
||||||
|
on_unload_callbacks[cpath]&.each { |c| c.call(value, abspath) }
|
||||||
|
on_unload_callbacks[:ANY]&.each { |c| c.call(cpath, value, abspath) }
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (Module, Symbol) -> void
|
||||||
|
def unload_autoload(parent, cname)
|
||||||
|
parent.__send__(:remove_const, cname)
|
||||||
|
log("autoload for #{cpath(parent, cname)} removed") if logger
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (Module, Symbol) -> void
|
||||||
|
def unload_cref(parent, cname)
|
||||||
|
# Let's optimistically remove_const. The way we use it, this is going to
|
||||||
|
# succeed always if all is good.
|
||||||
|
parent.__send__(:remove_const, cname)
|
||||||
|
rescue ::NameError
|
||||||
|
# There are a few edge scenarios in which this may happen. If the constant
|
||||||
|
# is gone, that is OK, anyway.
|
||||||
|
else
|
||||||
|
log("#{cpath(parent, cname)} unloaded") if logger
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Zeitwerk::Loader::Callbacks
|
module Zeitwerk::Loader::Callbacks
|
||||||
include Zeitwerk::RealModName
|
include Zeitwerk::RealModName
|
||||||
|
|
||||||
@ -9,16 +11,19 @@ module Zeitwerk::Loader::Callbacks
|
|||||||
cref = autoloads.delete(file)
|
cref = autoloads.delete(file)
|
||||||
cpath = cpath(*cref)
|
cpath = cpath(*cref)
|
||||||
|
|
||||||
|
# If reloading is enabled, we need to put this constant for unloading
|
||||||
|
# regardless of what cdef? says. In Ruby < 3.1 the internal state is not
|
||||||
|
# fully cleared. Module#constants still includes it, and you need to
|
||||||
|
# remove_const. See https://github.com/ruby/ruby/pull/4715.
|
||||||
to_unload[cpath] = [file, cref] if reloading_enabled?
|
to_unload[cpath] = [file, cref] if reloading_enabled?
|
||||||
Zeitwerk::Registry.unregister_autoload(file)
|
Zeitwerk::Registry.unregister_autoload(file)
|
||||||
|
|
||||||
if logger && cdef?(*cref)
|
if cdef?(*cref)
|
||||||
log("constant #{cpath} loaded from file #{file}")
|
log("constant #{cpath} loaded from file #{file}") if logger
|
||||||
elsif !cdef?(*cref)
|
run_on_load_callbacks(cpath, cget(*cref), file) unless on_load_callbacks.empty?
|
||||||
|
else
|
||||||
raise Zeitwerk::NameError.new("expected file #{file} to define constant #{cpath}, but didn't", cref.last)
|
raise Zeitwerk::NameError.new("expected file #{file} to define constant #{cpath}, but didn't", cref.last)
|
||||||
end
|
end
|
||||||
|
|
||||||
run_on_load_callbacks(cpath)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Invoked from our decorated Kernel#require when a managed directory is
|
# Invoked from our decorated Kernel#require when a managed directory is
|
||||||
@ -54,7 +59,7 @@ module Zeitwerk::Loader::Callbacks
|
|||||||
|
|
||||||
on_namespace_loaded(autovivified_module)
|
on_namespace_loaded(autovivified_module)
|
||||||
|
|
||||||
run_on_load_callbacks(cpath)
|
run_on_load_callbacks(cpath, autovivified_module, dir) unless on_load_callbacks.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -75,12 +80,13 @@ module Zeitwerk::Loader::Callbacks
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# @sig (String) -> void
|
# @sig (String, Object) -> void
|
||||||
def run_on_load_callbacks(cpath)
|
def run_on_load_callbacks(cpath, value, abspath)
|
||||||
# Very common, do not even compute a hash code.
|
# Order matters. If present, run the most specific one.
|
||||||
return if on_load_callbacks.empty?
|
|
||||||
|
|
||||||
callbacks = reloading_enabled? ? on_load_callbacks[cpath] : on_load_callbacks.delete(cpath)
|
callbacks = reloading_enabled? ? on_load_callbacks[cpath] : on_load_callbacks.delete(cpath)
|
||||||
callbacks.each(&:call) if callbacks
|
callbacks&.each { |c| c.call(value, abspath) }
|
||||||
|
|
||||||
|
callbacks = on_load_callbacks[:ANY]
|
||||||
|
callbacks&.each { |c| c.call(cpath, value, abspath) }
|
||||||
end
|
end
|
||||||
end
|
end
|
321
Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.5.0/lib/zeitwerk/loader/config.rb
vendored
Normal file
321
Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.5.0/lib/zeitwerk/loader/config.rb
vendored
Normal file
@ -0,0 +1,321 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "set"
|
||||||
|
require "securerandom"
|
||||||
|
|
||||||
|
module Zeitwerk::Loader::Config
|
||||||
|
# Absolute paths of the root directories. Stored in a hash to preserve
|
||||||
|
# order, easily handle duplicates, and also be able to have a fast lookup,
|
||||||
|
# needed for detecting nested paths.
|
||||||
|
#
|
||||||
|
# "/Users/fxn/blog/app/assets" => true,
|
||||||
|
# "/Users/fxn/blog/app/channels" => true,
|
||||||
|
# ...
|
||||||
|
#
|
||||||
|
# This is a private collection maintained by the loader. The public
|
||||||
|
# interface for it is `push_dir` and `dirs`.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Hash[String, true]
|
||||||
|
attr_reader :root_dirs
|
||||||
|
|
||||||
|
# @sig #camelize
|
||||||
|
attr_accessor :inflector
|
||||||
|
|
||||||
|
# Absolute paths of files, directories, or glob patterns to be totally
|
||||||
|
# ignored.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Set[String]
|
||||||
|
attr_reader :ignored_glob_patterns
|
||||||
|
|
||||||
|
# The actual collection of absolute file and directory names at the time the
|
||||||
|
# ignored glob patterns were expanded. Computed on setup, and recomputed on
|
||||||
|
# reload.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Set[String]
|
||||||
|
attr_reader :ignored_paths
|
||||||
|
|
||||||
|
# Absolute paths of directories or glob patterns to be collapsed.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Set[String]
|
||||||
|
attr_reader :collapse_glob_patterns
|
||||||
|
|
||||||
|
# The actual collection of absolute directory names at the time the collapse
|
||||||
|
# glob patterns were expanded. Computed on setup, and recomputed on reload.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Set[String]
|
||||||
|
attr_reader :collapse_dirs
|
||||||
|
|
||||||
|
# Absolute paths of files or directories not to be eager loaded.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Set[String]
|
||||||
|
attr_reader :eager_load_exclusions
|
||||||
|
|
||||||
|
# User-oriented callbacks to be fired on setup and on reload.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Array[{ () -> void }]
|
||||||
|
attr_reader :on_setup_callbacks
|
||||||
|
|
||||||
|
# User-oriented callbacks to be fired when a constant is loaded.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Hash[String, Array[{ (Object, String) -> void }]]
|
||||||
|
# Hash[Symbol, Array[{ (String, Object, String) -> void }]]
|
||||||
|
attr_reader :on_load_callbacks
|
||||||
|
|
||||||
|
# User-oriented callbacks to be fired before constants are removed.
|
||||||
|
#
|
||||||
|
# @private
|
||||||
|
# @sig Hash[String, Array[{ (Object, String) -> void }]]
|
||||||
|
# Hash[Symbol, Array[{ (String, Object, String) -> void }]]
|
||||||
|
attr_reader :on_unload_callbacks
|
||||||
|
|
||||||
|
# @sig #call | #debug | nil
|
||||||
|
attr_accessor :logger
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@initialized_at = Time.now
|
||||||
|
@root_dirs = {}
|
||||||
|
@inflector = Zeitwerk::Inflector.new
|
||||||
|
@ignored_glob_patterns = Set.new
|
||||||
|
@ignored_paths = Set.new
|
||||||
|
@collapse_glob_patterns = Set.new
|
||||||
|
@collapse_dirs = Set.new
|
||||||
|
@eager_load_exclusions = Set.new
|
||||||
|
@reloading_enabled = false
|
||||||
|
@on_setup_callbacks = []
|
||||||
|
@on_load_callbacks = {}
|
||||||
|
@on_unload_callbacks = {}
|
||||||
|
@logger = self.class.default_logger
|
||||||
|
@tag = SecureRandom.hex(3)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Pushes `path` to the list of root directories.
|
||||||
|
#
|
||||||
|
# Raises `Zeitwerk::Error` if `path` does not exist, or if another loader in
|
||||||
|
# the same process already manages that directory or one of its ascendants or
|
||||||
|
# descendants.
|
||||||
|
#
|
||||||
|
# @raise [Zeitwerk::Error]
|
||||||
|
# @sig (String | Pathname, Module) -> void
|
||||||
|
def push_dir(path, namespace: Object)
|
||||||
|
# Note that Class < Module.
|
||||||
|
unless namespace.is_a?(Module)
|
||||||
|
raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be"
|
||||||
|
end
|
||||||
|
|
||||||
|
abspath = File.expand_path(path)
|
||||||
|
if dir?(abspath)
|
||||||
|
raise_if_conflicting_directory(abspath)
|
||||||
|
root_dirs[abspath] = namespace
|
||||||
|
else
|
||||||
|
raise Zeitwerk::Error, "the root directory #{abspath} does not exist"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the loader's tag.
|
||||||
|
#
|
||||||
|
# Implemented as a method instead of via attr_reader for symmetry with the
|
||||||
|
# writer below.
|
||||||
|
#
|
||||||
|
# @sig () -> String
|
||||||
|
def tag
|
||||||
|
@tag
|
||||||
|
end
|
||||||
|
|
||||||
|
# Sets a tag for the loader, useful for logging.
|
||||||
|
#
|
||||||
|
# @param tag [#to_s]
|
||||||
|
# @sig (#to_s) -> void
|
||||||
|
def tag=(tag)
|
||||||
|
@tag = tag.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
# Absolute paths of the root directories. This is a read-only collection,
|
||||||
|
# please push here via `push_dir`.
|
||||||
|
#
|
||||||
|
# @sig () -> Array[String]
|
||||||
|
def dirs
|
||||||
|
root_dirs.keys.freeze
|
||||||
|
end
|
||||||
|
|
||||||
|
# You need to call this method before setup in order to be able to reload.
|
||||||
|
# There is no way to undo this, either you want to reload or you don't.
|
||||||
|
#
|
||||||
|
# @raise [Zeitwerk::Error]
|
||||||
|
# @sig () -> void
|
||||||
|
def enable_reloading
|
||||||
|
mutex.synchronize do
|
||||||
|
break if @reloading_enabled
|
||||||
|
|
||||||
|
if @setup
|
||||||
|
raise Zeitwerk::Error, "cannot enable reloading after setup"
|
||||||
|
else
|
||||||
|
@reloading_enabled = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig () -> bool
|
||||||
|
def reloading_enabled?
|
||||||
|
@reloading_enabled
|
||||||
|
end
|
||||||
|
|
||||||
|
# Let eager load ignore the given files or directories. The constants defined
|
||||||
|
# in those files are still autoloadable.
|
||||||
|
#
|
||||||
|
# @sig (*(String | Pathname | Array[String | Pathname])) -> void
|
||||||
|
def do_not_eager_load(*paths)
|
||||||
|
mutex.synchronize { eager_load_exclusions.merge(expand_paths(paths)) }
|
||||||
|
end
|
||||||
|
|
||||||
|
# Configure files, directories, or glob patterns to be totally ignored.
|
||||||
|
#
|
||||||
|
# @sig (*(String | Pathname | Array[String | Pathname])) -> void
|
||||||
|
def ignore(*glob_patterns)
|
||||||
|
glob_patterns = expand_paths(glob_patterns)
|
||||||
|
mutex.synchronize do
|
||||||
|
ignored_glob_patterns.merge(glob_patterns)
|
||||||
|
ignored_paths.merge(expand_glob_patterns(glob_patterns))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Configure directories or glob patterns to be collapsed.
|
||||||
|
#
|
||||||
|
# @sig (*(String | Pathname | Array[String | Pathname])) -> void
|
||||||
|
def collapse(*glob_patterns)
|
||||||
|
glob_patterns = expand_paths(glob_patterns)
|
||||||
|
mutex.synchronize do
|
||||||
|
collapse_glob_patterns.merge(glob_patterns)
|
||||||
|
collapse_dirs.merge(expand_glob_patterns(glob_patterns))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Configure a block to be called after setup and on each reload.
|
||||||
|
# If setup was already done, the block runs immediately.
|
||||||
|
#
|
||||||
|
# @sig () { () -> void } -> void
|
||||||
|
def on_setup(&block)
|
||||||
|
mutex.synchronize do
|
||||||
|
on_setup_callbacks << block
|
||||||
|
block.call if @setup
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Configure a block to be invoked once a certain constant path is loaded.
|
||||||
|
# Supports multiple callbacks, and if there are many, they are executed in
|
||||||
|
# the order in which they were defined.
|
||||||
|
#
|
||||||
|
# loader.on_load("SomeApiClient") do |klass, _abspath|
|
||||||
|
# klass.endpoint = "https://api.dev"
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# Can also be configured for any constant loaded:
|
||||||
|
#
|
||||||
|
# loader.on_load do |cpath, value, abspath|
|
||||||
|
# # ...
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# @raise [TypeError]
|
||||||
|
# @sig (String) { (Object, String) -> void } -> void
|
||||||
|
# (:ANY) { (String, Object, String) -> void } -> void
|
||||||
|
def on_load(cpath = :ANY, &block)
|
||||||
|
raise TypeError, "on_load only accepts strings" unless cpath.is_a?(String) || cpath == :ANY
|
||||||
|
|
||||||
|
mutex.synchronize do
|
||||||
|
(on_load_callbacks[cpath] ||= []) << block
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Configure a block to be invoked right before a certain constant is removed.
|
||||||
|
# Supports multiple callbacks, and if there are many, they are executed in the
|
||||||
|
# order in which they were defined.
|
||||||
|
#
|
||||||
|
# loader.on_unload("Country") do |klass, _abspath|
|
||||||
|
# klass.clear_cache
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# Can also be configured for any removed constant:
|
||||||
|
#
|
||||||
|
# loader.on_unload do |cpath, value, abspath|
|
||||||
|
# # ...
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# @raise [TypeError]
|
||||||
|
# @sig (String) { (Object) -> void } -> void
|
||||||
|
# (:ANY) { (String, Object) -> void } -> void
|
||||||
|
def on_unload(cpath = :ANY, &block)
|
||||||
|
raise TypeError, "on_unload only accepts strings" unless cpath.is_a?(String) || cpath == :ANY
|
||||||
|
|
||||||
|
mutex.synchronize do
|
||||||
|
(on_unload_callbacks[cpath] ||= []) << block
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Logs to `$stdout`, handy shortcut for debugging.
|
||||||
|
#
|
||||||
|
# @sig () -> void
|
||||||
|
def log!
|
||||||
|
@logger = ->(msg) { puts msg }
|
||||||
|
end
|
||||||
|
|
||||||
|
# @private
|
||||||
|
# @sig (String) -> bool
|
||||||
|
def ignores?(abspath)
|
||||||
|
ignored_paths.any? do |ignored_path|
|
||||||
|
ignored_path == abspath || (dir?(ignored_path) && abspath.start_with?(ignored_path + "/"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# @sig () -> Array[String]
|
||||||
|
def actual_root_dirs
|
||||||
|
root_dirs.reject do |root_dir, _namespace|
|
||||||
|
!dir?(root_dir) || ignored_paths.member?(root_dir)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String) -> bool
|
||||||
|
def root_dir?(dir)
|
||||||
|
root_dirs.key?(dir)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String) -> bool
|
||||||
|
def excluded_from_eager_load?(abspath)
|
||||||
|
eager_load_exclusions.member?(abspath)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String) -> bool
|
||||||
|
def collapse?(dir)
|
||||||
|
collapse_dirs.member?(dir)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String | Pathname | Array[String | Pathname]) -> Array[String]
|
||||||
|
def expand_paths(paths)
|
||||||
|
paths.flatten.map! { |path| File.expand_path(path) }
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (Array[String]) -> Array[String]
|
||||||
|
def expand_glob_patterns(glob_patterns)
|
||||||
|
# Note that Dir.glob works with regular file names just fine. That is,
|
||||||
|
# glob patterns technically need no wildcards.
|
||||||
|
glob_patterns.flat_map { |glob_pattern| Dir.glob(glob_pattern) }
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig () -> void
|
||||||
|
def recompute_ignored_paths
|
||||||
|
ignored_paths.replace(expand_glob_patterns(ignored_glob_patterns))
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig () -> void
|
||||||
|
def recompute_collapse_dirs
|
||||||
|
collapse_dirs.replace(expand_glob_patterns(collapse_glob_patterns))
|
||||||
|
end
|
||||||
|
end
|
97
Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.5.0/lib/zeitwerk/loader/helpers.rb
vendored
Normal file
97
Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.5.0/lib/zeitwerk/loader/helpers.rb
vendored
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Zeitwerk::Loader::Helpers
|
||||||
|
private
|
||||||
|
|
||||||
|
# --- Logging -----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# @sig (String) -> void
|
||||||
|
def log(message)
|
||||||
|
method_name = logger.respond_to?(:debug) ? :debug : :call
|
||||||
|
logger.send(method_name, "Zeitwerk@#{tag}: #{message}")
|
||||||
|
end
|
||||||
|
|
||||||
|
# --- Files and directories ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
# @sig (String) { (String, String) -> void } -> void
|
||||||
|
def ls(dir)
|
||||||
|
Dir.each_child(dir) do |basename|
|
||||||
|
next if hidden?(basename)
|
||||||
|
|
||||||
|
abspath = File.join(dir, basename)
|
||||||
|
next if ignored_paths.member?(abspath)
|
||||||
|
|
||||||
|
# We freeze abspath because that saves allocations when passed later to
|
||||||
|
# File methods. See #125.
|
||||||
|
yield basename, abspath.freeze
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String) -> bool
|
||||||
|
def ruby?(path)
|
||||||
|
path.end_with?(".rb")
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (String) -> bool
|
||||||
|
def dir?(path)
|
||||||
|
File.directory?(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig String -> bool
|
||||||
|
def hidden?(basename)
|
||||||
|
basename.start_with?(".")
|
||||||
|
end
|
||||||
|
|
||||||
|
# --- Constants ---------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# The autoload? predicate takes into account the ancestor chain of the
|
||||||
|
# receiver, like const_defined? and other methods in the constants API do.
|
||||||
|
#
|
||||||
|
# For example, given
|
||||||
|
#
|
||||||
|
# class A
|
||||||
|
# autoload :X, "x.rb"
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# class B < A
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# B.autoload?(:X) returns "x.rb".
|
||||||
|
#
|
||||||
|
# We need a way to strictly check in parent ignoring ancestors.
|
||||||
|
#
|
||||||
|
# @sig (Module, Symbol) -> String?
|
||||||
|
if method(:autoload?).arity == 1
|
||||||
|
def strict_autoload_path(parent, cname)
|
||||||
|
parent.autoload?(cname) if cdef?(parent, cname)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
def strict_autoload_path(parent, cname)
|
||||||
|
parent.autoload?(cname, false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (Module, Symbol) -> String
|
||||||
|
if Symbol.method_defined?(:name)
|
||||||
|
# Symbol#name was introduced in Ruby 3.0. It returns always the same
|
||||||
|
# frozen object, so we may save a few string allocations.
|
||||||
|
def cpath(parent, cname)
|
||||||
|
Object == parent ? cname.name : "#{real_mod_name(parent)}::#{cname.name}"
|
||||||
|
end
|
||||||
|
else
|
||||||
|
def cpath(parent, cname)
|
||||||
|
Object == parent ? cname.to_s : "#{real_mod_name(parent)}::#{cname}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @sig (Module, Symbol) -> bool
|
||||||
|
def cdef?(parent, cname)
|
||||||
|
parent.const_defined?(cname, false)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @raise [NameError]
|
||||||
|
# @sig (Module, Symbol) -> Object
|
||||||
|
def cget(parent, cname)
|
||||||
|
parent.const_get(cname, false)
|
||||||
|
end
|
||||||
|
end
|
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Zeitwerk::RealModName
|
module Zeitwerk::RealModName
|
||||||
UNBOUND_METHOD_MODULE_NAME = Module.instance_method(:name)
|
UNBOUND_METHOD_MODULE_NAME = Module.instance_method(:name)
|
||||||
private_constant :UNBOUND_METHOD_MODULE_NAME
|
private_constant :UNBOUND_METHOD_MODULE_NAME
|
@ -17,7 +17,7 @@ module Zeitwerk
|
|||||||
# @sig Hash[String, Zeitwerk::Loader]
|
# @sig Hash[String, Zeitwerk::Loader]
|
||||||
attr_reader :loaders_managing_gems
|
attr_reader :loaders_managing_gems
|
||||||
|
|
||||||
# Maps real paths to the loaders responsible for them.
|
# Maps absolute paths to the loaders responsible for them.
|
||||||
#
|
#
|
||||||
# This information is used by our decorated `Kernel#require` to be able to
|
# This information is used by our decorated `Kernel#require` to be able to
|
||||||
# invoke callbacks and autovivify modules.
|
# invoke callbacks and autovivify modules.
|
||||||
@ -73,6 +73,15 @@ module Zeitwerk
|
|||||||
loaders << loader
|
loaders << loader
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @private
|
||||||
|
# @sig (Zeitwerk::Loader) -> void
|
||||||
|
def unregister_loader(loader)
|
||||||
|
loaders.delete(loader)
|
||||||
|
loaders_managing_gems.delete_if { |_, l| l == loader }
|
||||||
|
autoloads.delete_if { |_, l| l == loader }
|
||||||
|
inceptions.delete_if { |_, (_, l)| l == loader }
|
||||||
|
end
|
||||||
|
|
||||||
# This method returns always a loader, the same instance for the same root
|
# This method returns always a loader, the same instance for the same root
|
||||||
# file. That is how Zeitwerk::Loader.for_gem is idempotent.
|
# file. That is how Zeitwerk::Loader.for_gem is idempotent.
|
||||||
#
|
#
|
||||||
@ -90,20 +99,20 @@ module Zeitwerk
|
|||||||
|
|
||||||
# @private
|
# @private
|
||||||
# @sig (Zeitwerk::Loader, String) -> String
|
# @sig (Zeitwerk::Loader, String) -> String
|
||||||
def register_autoload(loader, realpath)
|
def register_autoload(loader, abspath)
|
||||||
autoloads[realpath] = loader
|
autoloads[abspath] = loader
|
||||||
end
|
end
|
||||||
|
|
||||||
# @private
|
# @private
|
||||||
# @sig (String) -> void
|
# @sig (String) -> void
|
||||||
def unregister_autoload(realpath)
|
def unregister_autoload(abspath)
|
||||||
autoloads.delete(realpath)
|
autoloads.delete(abspath)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @private
|
# @private
|
||||||
# @sig (String, String, Zeitwerk::Loader) -> void
|
# @sig (String, String, Zeitwerk::Loader) -> void
|
||||||
def register_inception(cpath, realpath, loader)
|
def register_inception(cpath, abspath, loader)
|
||||||
inceptions[cpath] = [realpath, loader]
|
inceptions[cpath] = [abspath, loader]
|
||||||
end
|
end
|
||||||
|
|
||||||
# @private
|
# @private
|
@ -1,5 +1,5 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Zeitwerk
|
module Zeitwerk
|
||||||
VERSION = "2.4.2"
|
VERSION = "2.5.0"
|
||||||
end
|
end
|
Loading…
x
Reference in New Issue
Block a user