Skip to content

Commit

Permalink
Refactor generating gem rbis out of ServerAddon
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrocha committed Dec 17, 2024
1 parent ba6b9e6 commit e2697ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
37 changes: 32 additions & 5 deletions lib/ruby_lsp/tapioca/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

require "zlib"
require "open3"
require_relative "lockfile_diff_parser"

module RubyLsp
module Tapioca
Expand Down Expand Up @@ -153,11 +154,37 @@ def fetch_lockfile_diff

sig { void }
def generate_gem_rbis
T.must(@rails_runner_client).delegate_notification(
server_addon_name: "Tapioca",
request_name: "gem",
diff: T.must(@lockfile_diff),
)
gem_changes = LockfileDiffParser.new(@lockfile_diff)

removed_gems = gem_changes.removed_gems
added_or_modified_gems = gem_changes.added_or_modified_gems

if added_or_modified_gems.any?
# Resetting BUNDLE_GEMFILE to root folder to use the project's Gemfile instead of Ruby LSP's composed Gemfile
stdout, stderr, status = T.unsafe(Open3).capture3(
{ "BUNDLE_GEMFILE" => "Gemfile" },
"bin/tapioca",
"gem",
*added_or_modified_gems,
)
T.must(@outgoing_queue) << if status.success?
Notification.window_log_message(
stdout,
type: Constant::MessageType::INFO,
)
else
Notification.window_log_message(
stderr,
type: Constant::MessageType::ERROR,
)
end
elsif removed_gems.any?
FileUtils.rm_f(Dir.glob("sorbet/rbi/gems/{#{removed_gems.join(",")}}@*.rbi"))
T.must(@outgoing_queue) << Notification.window_log_message(
"Removed RBIs for: #{removed_gems.join(", ")}",
type: Constant::MessageType::INFO,
)
end
end

sig { void }
Expand Down
17 changes: 0 additions & 17 deletions lib/ruby_lsp/tapioca/server_addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# frozen_string_literal: true

require "tapioca/internal"
require_relative "lockfile_diff_parser"

module RubyLsp
module Tapioca
Expand All @@ -17,8 +16,6 @@ def execute(request, params)
fork do
dsl(params)
end
when "gem"
gem(params)
end
end

Expand All @@ -28,20 +25,6 @@ def dsl(params)
load("tapioca/cli.rb") # Reload the CLI to reset thor defaults between requests
::Tapioca::Cli.start(["dsl", "--lsp_addon", "--workers=1"] + params[:constants])
end

def gem(params)
gem_changes = LockfileDiffParser.new(params[:diff])

removed_gems = gem_changes.removed_gems
added_or_modified_gems = gem_changes.added_or_modified_gems

if added_or_modified_gems.any?
load("tapioca/cli.rb") # Reload the CLI to reset thor defaults between requests
::Tapioca::Cli.start(["gem"] + added_or_modified_gems)
elsif removed_gems.any?
FileUtils.rm_f(Dir.glob("sorbet/rbi/gems/{#{removed_gems.join(",")}}@*.rbi"))
end
end
end
end
end

0 comments on commit e2697ce

Please sign in to comment.