-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragonborn.rb
333 lines (275 loc) · 8.43 KB
/
dragonborn.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
module Dragonborn
class Config
attr_accessor :ignored, :roots, :debug
def initialize
@ignored = ["app/main"]
@roots = []
@debug = false
end
def ignore(file)
@ignored << file
@ignored.uniq!
end
def root(dir)
@roots << dir
end
def debug!
@debug = true
end
def inflection(inflection)
Inflector.customize(inflection)
end
end
class Loader
LOADABLE_EXTENSIONS = [ "mrb", "rb" ]
HollowNamespace = Module
class LoadError < StandardError
def initialize(file, cpath)
super "Expected #{file} to define #{cpath}!"
end
end
def self.debug(method_name)
aliased_method = :"__#{method_name}"
alias_method aliased_method, method_name
define_method method_name do |*args|
next send(aliased_method, *args) unless @debug
call_args = args.map(&:inspect).join(", ")
puts_immediate (" " * @indent) + "#{method_name}(#{call_args})"
@indent += 1
send(aliased_method, *args).tap do |result|
puts_immediate (" " * (@indent - 1)) + "=> " + result.inspect
puts_immediate (" " * (@indent - 1)) + "=> (#{result.object_id})"
end
ensure
@indent -= 1
end
end
def initialize(loadables)
@loadables = loadables
@bases = {}
@root = Object
@debug = Dragonborn.config.debug
@indent = 0
end
def undefine_all_constants!
required = [
:BasicObject,
# :Kernel,
# :Hash,
# :Range,
# :Integer,
# :Struct,
# :String,
# :NilClass,
# :IOError,
# :GC,
# :Enumerable,
# :Numeric,
# :ValueType,
# :File,
# :Fixnum,
# :Time,
# :Exception,
# :Array,
# :Symbol,
# :ArgumentError,
# :Enumerator,
# :StandardError,
# :NoMethodError,
# :NameError,
# :RuntimeError,
# :NIL,
# :GTK,
]
(@root.constants - required).each do |cname|
@bases[cname] = @root.remove_const(cname)
end
end
def restore_constants!
@bases.each do |cname, const|
@root.const_set(cname, const)
end
end
debug def find_in_namespace(ns, cname)
ns == @root ? search_top_level(cname) : search_namespace(ns, cname)
end
debug def search_top_level(cname)
return @bases[cname] if @bases.key?(cname)
if autoload?("", cname)
load("", cname)
elsif namespace?("", cname)
autovivify("", cname)
else
@root.__const_missing(cname)
end
@bases[cname] = @root.remove_const(cname)
end
debug def search_namespace(ns, cname)
namespace_parts = ns.name.split("::")
namespace = ns.name
until namespace_parts.empty?
return lookup(namespace, cname) if is_defined?(namespace, cname)
return load(namespace, cname) if autoload?(namespace, cname)
return autovivify(namespace, cname) if namespace?(namespace, cname)
namespace_parts.pop
namespace = namespace_parts.join("::")
end
search_top_level(cname)
end
debug def is_defined?(namespace, cname)
ns = lookup(namespace)
ns.const_defined?(cname, false)
end
debug def autoload?(namespace, cname)
@loadables.dig(namespace, cname)
end
debug def namespace?(namespace, cname)
# @NOTE `namespace?` is only valid after `autoload?` fails!
@loadables[namespace]&.key?(cname)
end
debug def load(namespace, cname)
ns = lookup(namespace)
file = @loadables.dig(namespace, cname)
if namespace.empty?
require(file)
else
base_cname = namespace.split("::", 2).first
@root.const_set(base_cname, lookup(base_cname))
require(file)
@root.remove_const(base_cname)
end
if ns.const_defined?(cname, false)
ns.const_get(cname)
else
path = $gtk.required_files.last
raise LoadError.new(path, "#{ns == @root ? "" : "#{ns}::"}#{cname}")
end
end
debug def autovivify(namespace, cname)
ns = lookup(namespace)
ns.const_set(cname, HollowNamespace.new)
end
debug def lookup(cpath)
return @root if cpath == ""
unless cpath.include?("::")
cname = cpath.to_sym
return @bases[cname] if @bases.key?(cname)
end
cpath.split("::").inject(@root) do |ns, cname|
ns.const_get(cname.to_sym)
end
end
end
module Inflector
@customizations = {}
class << self
def inflect(name)
@customizations[name] || begin
parts = name.split("_").collect! do |part|
@customizations[part] || part.capitalize! || part
end
parts.join.to_sym
end
end
def customize(mappings)
mappings.transform_values!(&:to_sym)
@customizations.merge!(mappings)
end
end
end
@config = Config.new
class << self
attr_reader :config
def require_dir(dir)
files = $gtk.list_files(dir)
files.each do |file|
path = "#{dir}/#{file}"
next if $gtk.required_files.include?(path)
stat = $gtk.stat_file(path)
require path if stat[:file_type] == :regular
end
end
def configure(eager: true, &block)
@errors = []
@config.instance_eval(&block)
reload!
end
def reload!
@config.roots.sort! { |a, b| b.count("/") <=> a.count("/") }
stats = @config.roots.flat_map { |root| stat_recursively(root) }
# @TODO Cache require order based on `stats`. Subsequent loads can avoid
# the hoop jumping altogether.
@to_be_loaded = []
@loadables = stats.group_by { |x| x[:ns] }
@loadables.transform_values! do |ns_stats|
actions_by_name = ns_stats.group_by { |x| x[:cname] }
actions_by_name.transform_values! do |cname_stats|
if cname_stats.first[:ns].empty? && Object.const_defined?(cname_stats.first[:cpath])
cname_stats.each do |stat|
type = stat[:file_type] == :directory ? "directory" : "file"
@errors << "The #{type} #{stat[:path]} masks #{stat[:cname]}"
end
end
virtual, loadable = cname_stats.partition do |stat|
stat[:file_type] == :directory
end
load_paths = loadable.map { |x| x[:file] }
load_paths.uniq!
if load_paths.size == 1
@to_be_loaded << loadable.first.values_at(:cpath, :file)
next load_paths.first
end
next if loadable.empty? && !virtual.empty?
cpath = cname_stats.first[:cpath]
paths = load_paths.map { |path| " * #{path}" }.join("\n")
@errors << "Multiple files mapped to #{cpath}\n#{paths}"
nil
end
end
unless @errors.empty?
@errors.sort!
errors = @errors.join("\n").indent(1)
raise "[Dragonborn] Encountered issues:\n#{errors}"
end
finalize!
end
private
def finalize!
loader = Loader.new(@loadables)
Module.instance_eval do
alias_method :__const_missing, :const_missing
define_method(:const_missing) { |cname| loader.find_in_namespace(self, cname) }
end
@to_be_loaded.sort! { |a, b| a.last <=> b.last }
loader.undefine_all_constants!
@to_be_loaded.each do |cpath, path|
next if path == "app/main"
loader.lookup(cpath)
end
ensure
loader.restore_constants!
Module.instance_eval do
alias_method :const_missing, :__const_missing
remove_method :__const_missing
end
end
def stat_recursively(dir, ns = "")
$gtk.list_files(dir).sort!.flat_map do |file|
stat = $gtk.stat_file("#{dir}/#{file}")
is_dir = stat[:file_type] == :directory
next [] if is_dir && @config.roots.include?(stat[:path])
name, _, ext = (is_dir ? [file] : file.rpartition("."))
next [] unless is_dir || Loader::LOADABLE_EXTENSIONS.include?(ext)
load_path = "#{dir}/#{name}"
next [] if @config.ignored.include?(load_path)
stat[:file] = load_path
stat[:ext] = ext
stat[:ns] = ns
stat[:cname] = Inflector.inflect(name)
stat[:cpath] = stat.values_at(:ns, :cname).reject(&:empty?).join("::")
children = stat_recursively(stat[:path], stat[:cpath]) if is_dir
[ stat, *children ]
end
end
end
end