Skip to content

Commit

Permalink
Move custom compiler reloading to Loaders::Dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Jan 31, 2025
1 parent 2552314 commit 795582d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
30 changes: 4 additions & 26 deletions lib/ruby_lsp/tapioca/server_addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,18 @@ def execute(request, params)
case request
when "reload_workspace_compilers"
with_notification_wrapper("reload_workspace_compilers", "Reloading DSL compilers") do
compiler_paths = Dir.glob(
"#{params[:workspace_path]}/**/tapioca/**/compilers/**/*.rb",
File::FNM_PATHNAME | File::Constants::FNM_EXTGLOB,
)

# Remove all loaded compilers that are inside the workspace
::Tapioca::Dsl::Compiler.descendants.each do |compiler|
name = compiler.name
next unless name && compiler_paths.include?(Module.const_source_location(name).first)

*parts, unqualified_name = name.split("::")

if parts.empty?
Object.send(:remove_const, unqualified_name)
else
parts.join("::").safe_constantize.send(:remove_const, unqualified_name)
end
end

# Remove from $LOADED_FEATURES each workspace compiler file and then re-required it to reload
compiler_paths.each do |path|
$LOADED_FEATURES.delete(path)
require File.expand_path(path)
end
@loader&.reload_custom_compilers
end
when "load_compilers_and_extensions"
# Load DSL extensions and compilers ahead of time, so that we don't have to pay the price of invoking
# `Gem.find_files` on every execution, which is quite expensive
::Tapioca::Loaders::Dsl.new(
@loader = ::Tapioca::Loaders::Dsl.new(
tapioca_path: ::Tapioca::TAPIOCA_DIR,
eager_load: false,
app_root: params[:workspace_path],
halt_upon_load_error: false,
).load_dsl_extensions_and_compilers
)
@loader.load_dsl_extensions_and_compilers
when "dsl"
fork do
with_notification_wrapper("dsl", "Generating DSL RBIs") do
Expand Down
40 changes: 34 additions & 6 deletions lib/tapioca/loaders/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ def load_dsl_extensions_and_compilers
load_dsl_compilers
end

sig { void }
def reload_custom_compilers
# Remove all loaded custom compilers
::Tapioca::Dsl::Compiler.descendants.each do |compiler|
name = compiler.name
next unless name && @custom_compiler_paths.include?(Module.const_source_location(name)&.first)

*parts, unqualified_name = name.split("::")

if parts.empty?
Object.send(:remove_const, unqualified_name)
else
parts.join("::").safe_constantize.send(:remove_const, unqualified_name)
end
end

# Remove from $LOADED_FEATURES each workspace compiler file and then re-load
@custom_compiler_paths.each { |path| $LOADED_FEATURES.delete(path) }
load_custom_dsl_compilers
end

protected

sig do
Expand All @@ -57,6 +78,7 @@ def initialize(tapioca_path:, eager_load: true, app_root: ".", halt_upon_load_er
@eager_load = eager_load
@app_root = app_root
@halt_upon_load_error = halt_upon_load_error
@custom_compiler_paths = T.let([], T::Array[String])
end

sig { void }
Expand Down Expand Up @@ -89,12 +111,7 @@ def load_dsl_compilers
end

# Load all custom compilers from the project
Dir.glob([
"#{@tapioca_path}/generators/**/*.rb", # TODO: Here for backcompat, remove later
"#{@tapioca_path}/compilers/**/*.rb",
]).each do |compiler|
require File.expand_path(compiler)
end
load_custom_dsl_compilers

say("Done", :green)
end
Expand All @@ -112,6 +129,17 @@ def load_application

say("Done", :green)
end

private

sig { void }
def load_custom_dsl_compilers
@custom_compiler_paths = Dir.glob([
"#{@tapioca_path}/generators/**/*.rb", # TODO: Here for backcompat, remove later
"#{@tapioca_path}/compilers/**/*.rb",
])
@custom_compiler_paths.each { |compiler| require File.expand_path(compiler) }
end
end
end
end

0 comments on commit 795582d

Please sign in to comment.