zeitwerk 2.7.3 → 2.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +145 -72
- data/lib/zeitwerk/core_ext/kernel.rb +1 -1
- data/lib/zeitwerk/cref/map.rb +3 -3
- data/lib/zeitwerk/cref.rb +8 -1
- data/lib/zeitwerk/error.rb +12 -1
- data/lib/zeitwerk/gem_inflector.rb +3 -3
- data/lib/zeitwerk/gem_loader.rb +6 -6
- data/lib/zeitwerk/inflector.rb +8 -8
- data/lib/zeitwerk/loader/callbacks.rb +4 -4
- data/lib/zeitwerk/loader/config.rb +96 -33
- data/lib/zeitwerk/loader/constant_path_validator.rb +17 -0
- data/lib/zeitwerk/loader/eager_load.rb +44 -48
- data/lib/zeitwerk/loader/file_system.rb +212 -0
- data/lib/zeitwerk/loader/helpers.rb +3 -105
- data/lib/zeitwerk/loader.rb +200 -163
- data/lib/zeitwerk/real_mod_name.rb +1 -1
- data/lib/zeitwerk/registry.rb +30 -4
- data/lib/zeitwerk/version.rb +1 -1
- data/lib/zeitwerk.rb +13 -13
- metadata +5 -3
data/lib/zeitwerk/inflector.rb
CHANGED
|
@@ -5,9 +5,9 @@ module Zeitwerk
|
|
|
5
5
|
# Very basic snake case -> camel case conversion.
|
|
6
6
|
#
|
|
7
7
|
# inflector = Zeitwerk::Inflector.new
|
|
8
|
-
# inflector.camelize(
|
|
9
|
-
# inflector.camelize(
|
|
10
|
-
# inflector.camelize(
|
|
8
|
+
# inflector.camelize('post', ...) # => 'Post'
|
|
9
|
+
# inflector.camelize('users_controller', ...) # => 'UsersController'
|
|
10
|
+
# inflector.camelize('api', ...) # => 'Api'
|
|
11
11
|
#
|
|
12
12
|
# Takes into account hard-coded mappings configured with `inflect`.
|
|
13
13
|
#
|
|
@@ -20,13 +20,13 @@ module Zeitwerk
|
|
|
20
20
|
#
|
|
21
21
|
# inflector = Zeitwerk::Inflector.new
|
|
22
22
|
# inflector.inflect(
|
|
23
|
-
#
|
|
24
|
-
#
|
|
23
|
+
# 'html_parser' => 'HTMLParser',
|
|
24
|
+
# 'mysql_adapter' => 'MySQLAdapter'
|
|
25
25
|
# )
|
|
26
26
|
#
|
|
27
|
-
# inflector.camelize(
|
|
28
|
-
# inflector.camelize(
|
|
29
|
-
# inflector.camelize(
|
|
27
|
+
# inflector.camelize('html_parser', abspath) # => 'HTMLParser'
|
|
28
|
+
# inflector.camelize('mysql_adapter', abspath) # => 'MySQLAdapter'
|
|
29
|
+
# inflector.camelize('users_controller', abspath) # => 'UsersController'
|
|
30
30
|
#
|
|
31
31
|
#: (Hash[String, String]) -> void
|
|
32
32
|
def inflect(inflections)
|
|
@@ -12,12 +12,12 @@ module Zeitwerk::Loader::Callbacks # :nodoc: all
|
|
|
12
12
|
Zeitwerk::Registry.autoloads.unregister(file)
|
|
13
13
|
|
|
14
14
|
if cref.defined?
|
|
15
|
-
log
|
|
15
|
+
log { "constant #{cref} loaded from file #{file}" }
|
|
16
16
|
to_unload[file] = cref if reloading_enabled?
|
|
17
17
|
run_on_load_callbacks(cref.path, cref.get, file) unless on_load_callbacks.empty?
|
|
18
18
|
else
|
|
19
19
|
msg = "expected file #{file} to define constant #{cref}, but didn't"
|
|
20
|
-
log
|
|
20
|
+
log { msg }
|
|
21
21
|
|
|
22
22
|
# Ruby still keeps the autoload defined, but we remove it because the
|
|
23
23
|
# contract in Zeitwerk is more strict.
|
|
@@ -52,7 +52,7 @@ module Zeitwerk::Loader::Callbacks # :nodoc: all
|
|
|
52
52
|
dirs_autoload_monitor.synchronize do
|
|
53
53
|
if cref = autoloads.delete(dir)
|
|
54
54
|
implicit_namespace = cref.set(Module.new)
|
|
55
|
-
log
|
|
55
|
+
log { "module #{cref} autovivified from directory #{dir}" }
|
|
56
56
|
|
|
57
57
|
to_unload[dir] = cref if reloading_enabled?
|
|
58
58
|
|
|
@@ -77,7 +77,7 @@ module Zeitwerk::Loader::Callbacks # :nodoc: all
|
|
|
77
77
|
internal def on_namespace_loaded(cref, namespace)
|
|
78
78
|
if dirs = namespace_dirs.delete(cref)
|
|
79
79
|
dirs.each do |dir|
|
|
80
|
-
define_autoloads_for_dir(dir, namespace)
|
|
80
|
+
define_autoloads_for_dir(dir, namespace, external: false)
|
|
81
81
|
end
|
|
82
82
|
end
|
|
83
83
|
end
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
3
|
+
require 'set'
|
|
4
|
+
require 'securerandom'
|
|
5
5
|
|
|
6
6
|
module Zeitwerk::Loader::Config
|
|
7
7
|
extend Zeitwerk::Internal
|
|
8
8
|
include Zeitwerk::RealModName
|
|
9
9
|
|
|
10
|
+
UNDEFINED = Object.new.freeze
|
|
11
|
+
private_constant :UNDEFINED
|
|
12
|
+
|
|
10
13
|
#: camelize(String, String) -> String
|
|
11
14
|
attr_accessor :inflector
|
|
12
15
|
|
|
@@ -15,8 +18,8 @@ module Zeitwerk::Loader::Config
|
|
|
15
18
|
|
|
16
19
|
# Absolute paths of the root directories, mapped to their respective root namespaces:
|
|
17
20
|
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
21
|
+
# '/Users/fxn/blog/app/channels' => Object,
|
|
22
|
+
# '/Users/fxn/blog/app/adapters' => ActiveJob::QueueAdapters,
|
|
20
23
|
# ...
|
|
21
24
|
#
|
|
22
25
|
# Stored in a hash to preserve order, easily handle duplicates, and have a
|
|
@@ -29,6 +32,12 @@ module Zeitwerk::Loader::Config
|
|
|
29
32
|
attr_reader :roots
|
|
30
33
|
internal :roots
|
|
31
34
|
|
|
35
|
+
# Basename of files that define namespaces. For example, if `nsfile` is
|
|
36
|
+
# 'ns.rb', then `foo/ns.rb` defines the `Foo` namespace.
|
|
37
|
+
#
|
|
38
|
+
#: String?
|
|
39
|
+
attr_reader :nsfile
|
|
40
|
+
|
|
32
41
|
# Absolute paths of files, directories, or glob patterns to be ignored.
|
|
33
42
|
#
|
|
34
43
|
#: Set[String]
|
|
@@ -50,12 +59,20 @@ module Zeitwerk::Loader::Config
|
|
|
50
59
|
private :collapse_glob_patterns
|
|
51
60
|
|
|
52
61
|
# The actual collection of absolute directory names at the time the collapse
|
|
53
|
-
# glob patterns were expanded. Computed on setup
|
|
62
|
+
# glob patterns were expanded. Computed on setup and recomputed on reload.
|
|
54
63
|
#
|
|
55
64
|
#: Set[String]
|
|
56
65
|
attr_reader :collapse_dirs
|
|
57
66
|
private :collapse_dirs
|
|
58
67
|
|
|
68
|
+
# Absolute paths of directories that are parents of collapsed directories.
|
|
69
|
+
# This is a cache to optimize some tree walks. Computed on setup and
|
|
70
|
+
# recomputed on reload.
|
|
71
|
+
#
|
|
72
|
+
#: Set[String]
|
|
73
|
+
attr_reader :collapse_parents
|
|
74
|
+
private :collapse_parents
|
|
75
|
+
|
|
59
76
|
# Absolute paths of files or directories not to be eager loaded.
|
|
60
77
|
#
|
|
61
78
|
#: Set[String]
|
|
@@ -82,16 +99,19 @@ module Zeitwerk::Loader::Config
|
|
|
82
99
|
attr_reader :on_unload_callbacks
|
|
83
100
|
private :on_unload_callbacks
|
|
84
101
|
|
|
102
|
+
#: () -> void
|
|
85
103
|
def initialize
|
|
86
104
|
@inflector = Zeitwerk::Inflector.new
|
|
87
105
|
@logger = self.class.default_logger
|
|
88
106
|
@tag = SecureRandom.hex(3)
|
|
89
107
|
@initialized_at = Time.now
|
|
90
108
|
@roots = {}
|
|
109
|
+
@nsfile = nil
|
|
91
110
|
@ignored_glob_patterns = Set.new
|
|
92
111
|
@ignored_paths = Set.new
|
|
93
112
|
@collapse_glob_patterns = Set.new
|
|
94
113
|
@collapse_dirs = Set.new
|
|
114
|
+
@collapse_parents = Set.new
|
|
95
115
|
@eager_load_exclusions = Set.new
|
|
96
116
|
@reloading_enabled = false
|
|
97
117
|
@on_setup_callbacks = []
|
|
@@ -112,12 +132,12 @@ module Zeitwerk::Loader::Config
|
|
|
112
132
|
end
|
|
113
133
|
|
|
114
134
|
unless real_mod_name(namespace)
|
|
115
|
-
raise Zeitwerk::Error,
|
|
135
|
+
raise Zeitwerk::Error, 'root namespaces cannot be anonymous'
|
|
116
136
|
end
|
|
117
137
|
|
|
118
138
|
abspath = File.expand_path(path)
|
|
119
|
-
if dir?(abspath)
|
|
120
|
-
|
|
139
|
+
if @fs.dir?(abspath)
|
|
140
|
+
raise_if_conflicting_root_dir(abspath)
|
|
121
141
|
roots[abspath] = namespace
|
|
122
142
|
else
|
|
123
143
|
raise Zeitwerk::Error, "the root directory #{abspath} does not exist"
|
|
@@ -141,6 +161,18 @@ module Zeitwerk::Loader::Config
|
|
|
141
161
|
@tag = tag.to_s
|
|
142
162
|
end
|
|
143
163
|
|
|
164
|
+
#: (String?) -> void ! TypeError, ArgumentError
|
|
165
|
+
def nsfile=(nsfile)
|
|
166
|
+
unless nsfile.nil?
|
|
167
|
+
raise TypeError, 'nsfiles must be strings' unless nsfile.is_a?(String)
|
|
168
|
+
raise ArgumentError, 'nsfiles must have .rb extension' unless @fs.rb_extension?(nsfile)
|
|
169
|
+
raise ArgumentError, 'nsfiles must be basenames, not paths' unless File.basename(nsfile) == nsfile
|
|
170
|
+
raise ArgumentError, 'nsfiles cannot be hidden' if @fs.hidden?(nsfile)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
@nsfile = nsfile
|
|
174
|
+
end
|
|
175
|
+
|
|
144
176
|
# If `namespaces` is falsey (default), returns an array with the absolute
|
|
145
177
|
# paths of the root directories as strings. If truthy, returns a hash table
|
|
146
178
|
# instead. Keys are the absolute paths of the root directories as strings,
|
|
@@ -176,7 +208,7 @@ module Zeitwerk::Loader::Config
|
|
|
176
208
|
break if @reloading_enabled
|
|
177
209
|
|
|
178
210
|
if @setup
|
|
179
|
-
raise Zeitwerk::Error,
|
|
211
|
+
raise Zeitwerk::Error, 'cannot enable reloading after setup'
|
|
180
212
|
else
|
|
181
213
|
@reloading_enabled = true
|
|
182
214
|
end
|
|
@@ -214,7 +246,11 @@ module Zeitwerk::Loader::Config
|
|
|
214
246
|
glob_patterns = expand_paths(glob_patterns)
|
|
215
247
|
mutex.synchronize do
|
|
216
248
|
collapse_glob_patterns.merge(glob_patterns)
|
|
217
|
-
|
|
249
|
+
new_collapse_dirs = expand_glob_patterns(glob_patterns)
|
|
250
|
+
collapse_dirs.merge(new_collapse_dirs)
|
|
251
|
+
new_collapse_dirs.each do |dir|
|
|
252
|
+
collapse_parents << File.dirname(dir)
|
|
253
|
+
end
|
|
218
254
|
end
|
|
219
255
|
end
|
|
220
256
|
|
|
@@ -233,8 +269,8 @@ module Zeitwerk::Loader::Config
|
|
|
233
269
|
# Supports multiple callbacks, and if there are many, they are executed in
|
|
234
270
|
# the order in which they were defined.
|
|
235
271
|
#
|
|
236
|
-
# loader.on_load(
|
|
237
|
-
# klass.endpoint =
|
|
272
|
+
# loader.on_load('SomeApiClient') do |klass, _abspath|
|
|
273
|
+
# klass.endpoint = 'https://api.dev'
|
|
238
274
|
# end
|
|
239
275
|
#
|
|
240
276
|
# Can also be configured for any constant loaded:
|
|
@@ -243,12 +279,19 @@ module Zeitwerk::Loader::Config
|
|
|
243
279
|
# # ...
|
|
244
280
|
# end
|
|
245
281
|
#
|
|
246
|
-
#: (String
|
|
247
|
-
|
|
248
|
-
|
|
282
|
+
#: (String) { (top, String) -> void } -> void ! TypeError | NameError
|
|
283
|
+
#| { (String, top, String) -> void } -> void
|
|
284
|
+
def on_load(cpath = UNDEFINED, &block)
|
|
285
|
+
key = if cpath.equal?(UNDEFINED)
|
|
286
|
+
:ANY
|
|
287
|
+
elsif !cpath.is_a?(String)
|
|
288
|
+
raise TypeError, 'on_load only accepts strings'
|
|
289
|
+
else
|
|
290
|
+
@cpv.validate!(cpath)
|
|
291
|
+
end
|
|
249
292
|
|
|
250
293
|
mutex.synchronize do
|
|
251
|
-
(on_load_callbacks[
|
|
294
|
+
(on_load_callbacks[key] ||= []) << block
|
|
252
295
|
end
|
|
253
296
|
end
|
|
254
297
|
|
|
@@ -256,7 +299,7 @@ module Zeitwerk::Loader::Config
|
|
|
256
299
|
# Supports multiple callbacks, and if there are many, they are executed in the
|
|
257
300
|
# order in which they were defined.
|
|
258
301
|
#
|
|
259
|
-
# loader.on_unload(
|
|
302
|
+
# loader.on_unload('Country') do |klass, _abspath|
|
|
260
303
|
# klass.clear_cache
|
|
261
304
|
# end
|
|
262
305
|
#
|
|
@@ -266,12 +309,19 @@ module Zeitwerk::Loader::Config
|
|
|
266
309
|
# # ...
|
|
267
310
|
# end
|
|
268
311
|
#
|
|
269
|
-
#: (String
|
|
270
|
-
|
|
271
|
-
|
|
312
|
+
#: (String) { (top, String) -> void } -> void ! TypeError | NameError
|
|
313
|
+
#| { (String, top, String) -> void } -> void
|
|
314
|
+
def on_unload(cpath = UNDEFINED, &block)
|
|
315
|
+
key = if cpath.equal?(UNDEFINED)
|
|
316
|
+
:ANY
|
|
317
|
+
elsif !cpath.is_a?(String)
|
|
318
|
+
raise TypeError, 'on_unload only accepts strings'
|
|
319
|
+
else
|
|
320
|
+
@cpv.validate!(cpath)
|
|
321
|
+
end
|
|
272
322
|
|
|
273
323
|
mutex.synchronize do
|
|
274
|
-
(on_unload_callbacks[
|
|
324
|
+
(on_unload_callbacks[key] ||= []) << block
|
|
275
325
|
end
|
|
276
326
|
end
|
|
277
327
|
|
|
@@ -290,49 +340,54 @@ module Zeitwerk::Loader::Config
|
|
|
290
340
|
# Common use case.
|
|
291
341
|
return false if ignored_paths.empty?
|
|
292
342
|
|
|
293
|
-
walk_up(abspath) do |path|
|
|
343
|
+
@fs.walk_up(abspath) do |path|
|
|
294
344
|
return true if ignored_path?(path)
|
|
295
|
-
return false if
|
|
345
|
+
return false if root_dir?(path)
|
|
296
346
|
end
|
|
297
347
|
|
|
298
348
|
false
|
|
299
349
|
end
|
|
300
350
|
|
|
301
351
|
#: (String) -> bool
|
|
302
|
-
|
|
352
|
+
internal def ignored_path?(abspath)
|
|
303
353
|
ignored_paths.member?(abspath)
|
|
304
354
|
end
|
|
305
355
|
|
|
306
356
|
#: () -> Array[String]
|
|
307
357
|
private def actual_roots
|
|
308
358
|
roots.reject do |root_dir, _root_namespace|
|
|
309
|
-
|
|
359
|
+
!@fs.dir?(root_dir) || ignored_path?(root_dir)
|
|
310
360
|
end
|
|
311
361
|
end
|
|
312
362
|
|
|
313
363
|
#: (String) -> bool
|
|
314
|
-
|
|
364
|
+
internal def root_dir?(dir)
|
|
315
365
|
roots.key?(dir)
|
|
316
366
|
end
|
|
317
367
|
|
|
368
|
+
#: (String) -> bool
|
|
369
|
+
internal def collapse?(dir)
|
|
370
|
+
collapse_dirs.member?(dir)
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
#: (String) -> bool
|
|
374
|
+
internal def collapse_parent?(dir)
|
|
375
|
+
collapse_parents.member?(dir)
|
|
376
|
+
end
|
|
377
|
+
|
|
318
378
|
#: (String) -> bool
|
|
319
379
|
private def excluded_from_eager_load?(abspath)
|
|
320
380
|
# Optimize this common use case.
|
|
321
381
|
return false if eager_load_exclusions.empty?
|
|
322
382
|
|
|
323
|
-
walk_up(abspath) do |path|
|
|
383
|
+
@fs.walk_up(abspath) do |path|
|
|
324
384
|
return true if eager_load_exclusions.member?(path)
|
|
325
|
-
return false if
|
|
385
|
+
return false if root_dir?(path)
|
|
326
386
|
end
|
|
327
387
|
|
|
328
388
|
false
|
|
329
389
|
end
|
|
330
390
|
|
|
331
|
-
#: (String) -> bool
|
|
332
|
-
private def collapse?(dir)
|
|
333
|
-
collapse_dirs.member?(dir)
|
|
334
|
-
end
|
|
335
|
-
|
|
336
391
|
#: (String | Pathname | Array[String | Pathname]) -> Array[String]
|
|
337
392
|
private def expand_paths(paths)
|
|
338
393
|
paths.flatten.map! { |path| File.expand_path(path) }
|
|
@@ -354,4 +409,12 @@ module Zeitwerk::Loader::Config
|
|
|
354
409
|
private def recompute_collapse_dirs
|
|
355
410
|
collapse_dirs.replace(expand_glob_patterns(collapse_glob_patterns))
|
|
356
411
|
end
|
|
412
|
+
|
|
413
|
+
#: () -> void
|
|
414
|
+
private def recompute_collapse_parents
|
|
415
|
+
collapse_parents.clear
|
|
416
|
+
collapse_dirs.each do |dir|
|
|
417
|
+
collapse_parents << File.dirname(dir)
|
|
418
|
+
end
|
|
419
|
+
end
|
|
357
420
|
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @private
|
|
2
|
+
class Zeitwerk::Loader::ConstantPathValidator # :nodoc
|
|
3
|
+
CNAME_VALIDATOR = Module.new.freeze #: Module
|
|
4
|
+
private_constant :CNAME_VALIDATOR
|
|
5
|
+
|
|
6
|
+
# Technically, this validation works with symbols, but API boundary restricts
|
|
7
|
+
# input to strings, so we assume strings, and we test strings.
|
|
8
|
+
#
|
|
9
|
+
#: (String) -> String ! NameError
|
|
10
|
+
def validate!(possible_cpath)
|
|
11
|
+
# We do this before validating because as of this writing, TruffleRuby
|
|
12
|
+
# raises TypeError if the argument has leading colons.
|
|
13
|
+
possible_cpath = possible_cpath.delete_prefix('::')
|
|
14
|
+
CNAME_VALIDATOR.const_defined?(possible_cpath, false)
|
|
15
|
+
possible_cpath
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
module Zeitwerk::Loader::EagerLoad
|
|
2
2
|
# Eager loads all files in the root directories, recursively. Files do not
|
|
3
|
-
# need to be in `$LOAD_PATH`, absolute file names are used.
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
3
|
+
# need to be in `$LOAD_PATH`, absolute file names are used.
|
|
4
|
+
#
|
|
5
|
+
# Ignored files are not eager loaded. You can opt-out specifically in specific
|
|
6
|
+
# files and directories with `do_not_eager_load`, and that can be overridden
|
|
7
|
+
# passing `force: true`.
|
|
7
8
|
#
|
|
8
9
|
#: (?force: boolish) -> void
|
|
9
10
|
def eager_load(force: false)
|
|
@@ -11,7 +12,7 @@ module Zeitwerk::Loader::EagerLoad
|
|
|
11
12
|
break if @eager_loaded
|
|
12
13
|
raise Zeitwerk::SetupRequired unless @setup
|
|
13
14
|
|
|
14
|
-
log
|
|
15
|
+
log { 'eager load start' }
|
|
15
16
|
|
|
16
17
|
actual_roots.each do |root_dir, root_namespace|
|
|
17
18
|
actual_eager_load_dir(root_dir, root_namespace, force: force)
|
|
@@ -24,7 +25,7 @@ module Zeitwerk::Loader::EagerLoad
|
|
|
24
25
|
|
|
25
26
|
@eager_loaded = true
|
|
26
27
|
|
|
27
|
-
log
|
|
28
|
+
log { 'eager load end' }
|
|
28
29
|
end
|
|
29
30
|
end
|
|
30
31
|
|
|
@@ -34,23 +35,21 @@ module Zeitwerk::Loader::EagerLoad
|
|
|
34
35
|
|
|
35
36
|
abspath = File.expand_path(path)
|
|
36
37
|
|
|
37
|
-
raise Zeitwerk::Error.new("#{abspath} is not a directory") unless dir?(abspath)
|
|
38
|
+
raise Zeitwerk::Error.new("#{abspath} is not a directory") unless @fs.dir?(abspath)
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
paths = []
|
|
40
41
|
|
|
41
42
|
root_namespace = nil
|
|
42
|
-
walk_up(abspath) do |dir|
|
|
43
|
+
@fs.walk_up(abspath) do |dir|
|
|
43
44
|
return if ignored_path?(dir)
|
|
44
45
|
return if eager_load_exclusions.member?(dir)
|
|
45
46
|
|
|
46
47
|
break if root_namespace = roots[dir]
|
|
47
48
|
|
|
48
49
|
basename = File.basename(dir)
|
|
49
|
-
return if hidden?(basename)
|
|
50
|
+
return if @fs.hidden?(basename)
|
|
50
51
|
|
|
51
|
-
unless collapse?(dir)
|
|
52
|
-
cnames << inflector.camelize(basename, dir).to_sym
|
|
53
|
-
end
|
|
52
|
+
paths << [basename, dir] unless collapse?(dir)
|
|
54
53
|
end
|
|
55
54
|
|
|
56
55
|
raise Zeitwerk::Error.new("I do not manage #{abspath}") unless root_namespace
|
|
@@ -58,7 +57,8 @@ module Zeitwerk::Loader::EagerLoad
|
|
|
58
57
|
return if @eager_loaded
|
|
59
58
|
|
|
60
59
|
namespace = root_namespace
|
|
61
|
-
|
|
60
|
+
paths.reverse_each do |basename, dir|
|
|
61
|
+
cname = cname_for(basename, dir)
|
|
62
62
|
# Can happen if there are no Ruby files. This is not an error condition,
|
|
63
63
|
# the directory is actually managed. Could have Ruby files later.
|
|
64
64
|
return unless namespace.const_defined?(cname, false)
|
|
@@ -92,11 +92,11 @@ module Zeitwerk::Loader::EagerLoad
|
|
|
92
92
|
eager_load_child_namespace(mod, mod_name, root_dir, root_namespace)
|
|
93
93
|
else
|
|
94
94
|
root_namespace_name = real_mod_name(root_namespace)
|
|
95
|
-
if root_namespace_name.start_with?(mod_name +
|
|
95
|
+
if root_namespace_name.start_with?(mod_name + '::')
|
|
96
96
|
actual_eager_load_dir(root_dir, root_namespace)
|
|
97
97
|
elsif mod_name == root_namespace_name
|
|
98
98
|
actual_eager_load_dir(root_dir, root_namespace)
|
|
99
|
-
elsif mod_name.start_with?(root_namespace_name +
|
|
99
|
+
elsif mod_name.start_with?(root_namespace_name + '::')
|
|
100
100
|
eager_load_child_namespace(mod, mod_name, root_dir, root_namespace)
|
|
101
101
|
else
|
|
102
102
|
# Unrelated constant hierarchies, do nothing.
|
|
@@ -117,40 +117,42 @@ module Zeitwerk::Loader::EagerLoad
|
|
|
117
117
|
abspath = File.expand_path(path)
|
|
118
118
|
|
|
119
119
|
raise Zeitwerk::Error.new("#{abspath} does not exist") unless File.exist?(abspath)
|
|
120
|
-
raise Zeitwerk::Error.new("#{abspath} is not a Ruby file") if
|
|
120
|
+
raise Zeitwerk::Error.new("#{abspath} is not a Ruby file") if !@fs.rb_extension?(abspath)
|
|
121
121
|
raise Zeitwerk::Error.new("#{abspath} is ignored") if ignored_path?(abspath)
|
|
122
122
|
|
|
123
|
-
|
|
124
|
-
raise Zeitwerk::Error.new("#{abspath} is ignored") if hidden?(
|
|
125
|
-
|
|
126
|
-
base_cname = inflector.camelize(basename, abspath).to_sym
|
|
123
|
+
file_basename = File.basename(abspath)
|
|
124
|
+
raise Zeitwerk::Error.new("#{abspath} is ignored") if @fs.hidden?(file_basename)
|
|
127
125
|
|
|
128
126
|
root_namespace = nil
|
|
129
|
-
|
|
127
|
+
paths = []
|
|
130
128
|
|
|
131
|
-
walk_up(File.dirname(abspath)) do |dir|
|
|
129
|
+
@fs.walk_up(File.dirname(abspath)) do |dir|
|
|
132
130
|
raise Zeitwerk::Error.new("#{abspath} is ignored") if ignored_path?(dir)
|
|
133
131
|
|
|
134
132
|
break if root_namespace = roots[dir]
|
|
135
133
|
|
|
136
134
|
basename = File.basename(dir)
|
|
137
|
-
raise Zeitwerk::Error.new("#{abspath} is ignored") if hidden?(basename)
|
|
135
|
+
raise Zeitwerk::Error.new("#{abspath} is ignored") if @fs.hidden?(basename)
|
|
138
136
|
|
|
139
|
-
unless collapse?(dir)
|
|
140
|
-
cnames << inflector.camelize(basename, dir).to_sym
|
|
141
|
-
end
|
|
137
|
+
paths << [basename, dir] unless collapse?(dir)
|
|
142
138
|
end
|
|
143
139
|
|
|
144
140
|
raise Zeitwerk::Error.new("I do not manage #{abspath}") unless root_namespace
|
|
145
141
|
|
|
146
142
|
namespace = root_namespace
|
|
147
|
-
|
|
143
|
+
paths.reverse_each do |basename, dir|
|
|
144
|
+
cname = cname_for(basename, dir)
|
|
148
145
|
namespace = namespace.const_get(cname, false)
|
|
149
146
|
end
|
|
150
147
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
148
|
+
if file_basename == @nsfile
|
|
149
|
+
namespace
|
|
150
|
+
elsif shadowed_file?(abspath)
|
|
151
|
+
raise Zeitwerk::Error.new("#{abspath} is shadowed")
|
|
152
|
+
else
|
|
153
|
+
cname = cname_for(file_basename.delete_suffix('.rb'), abspath)
|
|
154
|
+
namespace.const_get(cname, false)
|
|
155
|
+
end
|
|
154
156
|
end
|
|
155
157
|
|
|
156
158
|
# The caller is responsible for making sure `namespace` is the namespace that
|
|
@@ -161,29 +163,27 @@ module Zeitwerk::Loader::EagerLoad
|
|
|
161
163
|
honour_exclusions = !force
|
|
162
164
|
return if honour_exclusions && excluded_from_eager_load?(dir)
|
|
163
165
|
|
|
164
|
-
log
|
|
166
|
+
log { "eager load directory #{dir} start" }
|
|
165
167
|
|
|
166
168
|
queue = [[dir, namespace]]
|
|
167
169
|
while (current_dir, namespace = queue.shift)
|
|
168
|
-
ls(current_dir) do |basename, abspath, ftype|
|
|
170
|
+
@fs.ls(current_dir, collapse: false) do |basename, abspath, ftype|
|
|
169
171
|
next if honour_exclusions && eager_load_exclusions.member?(abspath)
|
|
170
172
|
|
|
171
173
|
if ftype == :file
|
|
172
174
|
if (cref = autoloads[abspath])
|
|
173
175
|
cref.get
|
|
174
176
|
end
|
|
177
|
+
elsif collapse?(abspath)
|
|
178
|
+
queue << [abspath, namespace]
|
|
175
179
|
else
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
else
|
|
179
|
-
cname = inflector.camelize(basename, abspath).to_sym
|
|
180
|
-
queue << [abspath, namespace.const_get(cname, false)]
|
|
181
|
-
end
|
|
180
|
+
cname = cname_for(basename, abspath)
|
|
181
|
+
queue << [abspath, namespace.const_get(cname, false)]
|
|
182
182
|
end
|
|
183
183
|
end
|
|
184
184
|
end
|
|
185
185
|
|
|
186
|
-
log
|
|
186
|
+
log { "eager load directory #{dir} end" }
|
|
187
187
|
end
|
|
188
188
|
|
|
189
189
|
# In order to invoke this method, the caller has to ensure `child` is a
|
|
@@ -193,7 +193,7 @@ module Zeitwerk::Loader::EagerLoad
|
|
|
193
193
|
private def eager_load_child_namespace(child, child_name, root_dir, root_namespace)
|
|
194
194
|
suffix = child_name
|
|
195
195
|
unless root_namespace.equal?(Object)
|
|
196
|
-
suffix = suffix.delete_prefix(real_mod_name(root_namespace) +
|
|
196
|
+
suffix = suffix.delete_prefix(real_mod_name(root_namespace) + '::')
|
|
197
197
|
end
|
|
198
198
|
|
|
199
199
|
# These directories are at the same namespace level, there may be more if
|
|
@@ -206,14 +206,10 @@ module Zeitwerk::Loader::EagerLoad
|
|
|
206
206
|
dirs = [root_dir]
|
|
207
207
|
next_dirs = []
|
|
208
208
|
|
|
209
|
-
suffix.split(
|
|
209
|
+
suffix.split('::').each do |segment|
|
|
210
210
|
while (dir = dirs.shift)
|
|
211
|
-
ls(dir) do |basename, abspath, ftype|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
if collapse?(abspath)
|
|
215
|
-
dirs << abspath
|
|
216
|
-
elsif segment == inflector.camelize(basename, abspath)
|
|
211
|
+
@fs.ls(dir) do |basename, abspath, ftype|
|
|
212
|
+
if ftype == :directory && segment == cname_for(basename, abspath).to_s
|
|
217
213
|
next_dirs << abspath
|
|
218
214
|
end
|
|
219
215
|
end
|