Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prototype runtime] Add --autoload option #1561

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 39 additions & 6 deletions lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ def run_prototype(args, options)
todo = false
owners_included = []
outline = false
autoload = false

OptionParser.new do |opts|
opts.banner = <<EOU
Expand Down Expand Up @@ -683,17 +684,49 @@ def run_prototype(args, options)
opts.on("--outline", "Generates only module/class/constant declaration (no method definition)") do
outline = true
end
opts.on("--autoload", "Load all autoload path") do
autoload = true
end
end.parse!(args)

loader = options.loader()
env = Environment.from_loader(loader).resolve_type_names

require_libs.each do |lib|
require(lib)
end

relative_libs.each do |lib|
eval("require_relative(lib)", binding, "rbs")
# @type var autoloader: ^() { () -> void } -> void
autoloader = ->(&block) {
if autoload
hook = Module.new do
def autoload(name, path)
super
end
end
::Module.prepend(hook)
::Kernel.prepend(hook)

arguments = []
TracePoint.new(:call) do |tp|
base = tp.self.kind_of?(Module) ? tp.self : Kernel
name = (tp.binding or raise).local_variable_get(:name)
arguments << [base, name]
end.enable(target: hook.instance_method(:autoload), &block)

arguments.each do |(base, name)|
begin
base.const_get(name)
rescue LoadError, StandardError
end
end
else
block.call
end
}
autoloader.call do
require_libs.each do |lib|
require(lib)
end
relative_libs.each do |lib|
eval("require_relative(lib)", binding, "rbs")
end
end

runtime = Prototype::Runtime.new(patterns: args, env: env, merge: merge, todo: todo, owners_included: owners_included)
Expand Down