-
Notifications
You must be signed in to change notification settings - Fork 135
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
WIP: [Tapioca Addon] Support gem RBI generation #2063
Changes from all commits
7e0772c
2c61cf4
cc95f0e
d322305
e7e8f96
1303433
e75b7c3
dfaa8ba
a58bec6
1837b6a
35eca20
2169eca
0b7ce68
b13cb58
f1aeeaa
9b1714c
7e68f5c
f976ea6
cc19ba0
800cac8
77d4f16
0f5138c
8cfe769
ff9ccec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,170 @@ | ||||||
# typed: strict | ||||||
# frozen_string_literal: true | ||||||
|
||||||
RubyLsp::Addon.depend_on_ruby_lsp!(">= 0.20", "< 0.22") | ||||||
|
||||||
begin | ||||||
# The Tapioca add-on depends on the Rails add-on to add a runtime component to the runtime server. We can allow the | ||||||
# add-on to work outside of a Rails context in the future, but that may require Tapioca spawning its own runtime | ||||||
# server | ||||||
require "ruby-lsp-rails" | ||||||
rescue LoadError | ||||||
return | ||||||
end | ||||||
|
||||||
require "zlib" | ||||||
|
||||||
module RubyLsp | ||||||
module Tapioca | ||||||
class Addon < ::RubyLsp::Addon | ||||||
extend T::Sig | ||||||
|
||||||
GEMFILE_LOCK_SNAPSHOT = "tmp/tapioca/.gemfile_lock_snapshot" | ||||||
GIT_OPERATION_THRESHOLD = 15.0 # seconds | ||||||
|
||||||
sig { void } | ||||||
def initialize | ||||||
super | ||||||
|
||||||
@global_state = T.let(nil, T.nilable(RubyLsp::GlobalState)) | ||||||
@rails_runner_client = T.let(nil, T.nilable(RubyLsp::Rails::RunnerClient)) | ||||||
@index = T.let(nil, T.nilable(RubyIndexer::Index)) | ||||||
@file_checksums = T.let({}, T::Hash[String, String]) | ||||||
end | ||||||
|
||||||
sig { override.params(global_state: RubyLsp::GlobalState, outgoing_queue: Thread::Queue).void } | ||||||
def activate(global_state, outgoing_queue) | ||||||
@global_state = global_state | ||||||
# TODO: Uncomment | ||||||
# return unless @global_state.experimental_features | ||||||
|
||||||
@index = @global_state.index | ||||||
Thread.new do | ||||||
# Get a handle to the Rails add-on's runtime client. The call to `rails_runner_client` will block this thread | ||||||
# until the server has finished booting, but it will not block the main LSP. This has to happen inside of a | ||||||
# thread | ||||||
addon = T.cast(::RubyLsp::Addon.get("Ruby LSP Rails", ">= 0.3.18", "< 0.4"), ::RubyLsp::Rails::Addon) | ||||||
@rails_runner_client = addon.rails_runner_client | ||||||
outgoing_queue << Notification.window_log_message("Activating Tapioca add-on v#{version}") | ||||||
@rails_runner_client.register_server_addon(File.expand_path("server_addon.rb", __dir__)) | ||||||
|
||||||
handle_gemfile_changes | ||||||
rescue IncompatibleApiError | ||||||
# The requested version for the Rails add-on no longer matches. We need to upgrade and fix the breaking | ||||||
# changes | ||||||
end | ||||||
end | ||||||
|
||||||
sig { override.void } | ||||||
def deactivate | ||||||
end | ||||||
|
||||||
sig { override.returns(String) } | ||||||
def name | ||||||
"Tapioca" | ||||||
end | ||||||
|
||||||
sig { override.returns(String) } | ||||||
def version | ||||||
"0.1.0" | ||||||
end | ||||||
|
||||||
sig { params(changes: T::Array[{ uri: String, type: Integer }]).void } | ||||||
def workspace_did_change_watched_files(changes) | ||||||
# TODO: Uncomment | ||||||
# return unless T.must(@global_state).experimental_features | ||||||
return unless @rails_runner_client # Client is not ready | ||||||
|
||||||
constants = changes.flat_map do |change| | ||||||
path = URI(change[:uri]).to_standardized_path | ||||||
next if path.end_with?("_test.rb", "_spec.rb") | ||||||
|
||||||
case change[:type] | ||||||
when Constant::FileChangeType::CREATED, Constant::FileChangeType::CHANGED | ||||||
content = File.read(path) | ||||||
current_checksum = Zlib.crc32(content).to_s | ||||||
|
||||||
if change[:type] == Constant::FileChangeType::CHANGED && @file_checksums[path] == current_checksum | ||||||
$stderr.puts "File has not changed. Skipping #{path}" | ||||||
next | ||||||
end | ||||||
|
||||||
@file_checksums[path] = current_checksum | ||||||
when Constant::FileChangeType::DELETED | ||||||
@file_checksums.delete(path) | ||||||
end | ||||||
|
||||||
entries = T.must(@index).entries_for(path) | ||||||
next unless entries | ||||||
|
||||||
entries.filter_map do |entry| | ||||||
entry.name if entry.class == RubyIndexer::Entry::Class || entry.class == RubyIndexer::Entry::Module | ||||||
end | ||||||
end.compact | ||||||
|
||||||
return if constants.empty? | ||||||
|
||||||
@rails_runner_client.trigger_reload | ||||||
@rails_runner_client.delegate_notification( | ||||||
server_addon_name: "Tapioca", | ||||||
request_name: "dsl", | ||||||
constants: constants, | ||||||
) | ||||||
end | ||||||
|
||||||
private | ||||||
|
||||||
sig { void } | ||||||
def handle_gemfile_changes | ||||||
return unless File.exist?(".git") && File.exist?(".ruby-lsp/shutdown-timestamp") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's extract this path to a constant since we refer to it multiple times. |
||||||
|
||||||
git_timestamp = fetch_last_git_operation_time | ||||||
return unless git_timestamp | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this isn't right. Let's say you're working on a new repo, where the reflog doesn't contain any |
||||||
|
||||||
ruby_lsp_stop_time = Time.iso8601(File.read(".ruby-lsp/shutdown-timestamp")) | ||||||
|
||||||
$stderr.puts("ruby_lsp_stop_time: #{ruby_lsp_stop_time}") # TODO: Remove | ||||||
$stderr.puts("git_timestamp: #{git_timestamp}") # TODO: Remove | ||||||
|
||||||
# If the Ruby LSP was stopped shortly after the last git checkout/pull operation, we don't need to regenerate | ||||||
# RBIs since any Gemfile.lock changes were likely from version control, not from running bundle install | ||||||
return if (ruby_lsp_stop_time - git_timestamp) <= GIT_OPERATION_THRESHOLD | ||||||
|
||||||
process_gemfile_changes | ||||||
end | ||||||
|
||||||
sig { returns(T.nilable(Time)) } | ||||||
def fetch_last_git_operation_time | ||||||
git_reflog_output = %x(git reflog --date=iso | grep -E "checkout|pull" | head -n 1).strip | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This approach has a couple of advantages:
|
||||||
return if git_reflog_output.empty? | ||||||
|
||||||
timestamp_string = T.must(git_reflog_output.match(/\{(.*)\}/))[1] | ||||||
Time.iso8601(T.must(timestamp_string).sub(" ", "T").delete(" ")) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could avoid the need for this parsing by setting the output so it contains only the timestamp:
( |
||||||
end | ||||||
|
||||||
sig { returns(T.nilable(Integer)) } | ||||||
def process_gemfile_changes | ||||||
current_lockfile = File.read("Gemfile.lock") | ||||||
snapshot_lockfile = File.read(GEMFILE_LOCK_SNAPSHOT) if File.exist?(GEMFILE_LOCK_SNAPSHOT) | ||||||
|
||||||
unless snapshot_lockfile | ||||||
$stdout.puts("Creating initial Gemfile.lock snapshot at #{GEMFILE_LOCK_SNAPSHOT}") | ||||||
FileUtils.mkdir_p(File.dirname(GEMFILE_LOCK_SNAPSHOT)) | ||||||
File.write(GEMFILE_LOCK_SNAPSHOT, current_lockfile) | ||||||
return | ||||||
end | ||||||
|
||||||
return if current_lockfile == snapshot_lockfile | ||||||
|
||||||
T.must(@rails_runner_client).delegate_notification( | ||||||
server_addon_name: "Tapioca", | ||||||
request_name: "gem", | ||||||
snapshot_lockfile: snapshot_lockfile, | ||||||
current_lockfile: current_lockfile, | ||||||
) | ||||||
|
||||||
File.write(GEMFILE_LOCK_SNAPSHOT, current_lockfile) | ||||||
end | ||||||
end | ||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require "tapioca/internal" | ||
|
||
module RubyLsp | ||
module Tapioca | ||
class ServerAddon < ::RubyLsp::Rails::ServerAddon | ||
def name | ||
"Tapioca" | ||
end | ||
|
||
def execute(request, params) | ||
case request | ||
when "dsl" | ||
dsl(params) | ||
when "gem" | ||
gem(params) | ||
end | ||
end | ||
|
||
private | ||
|
||
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) | ||
snapshot_specs = parse_lockfile(params[:snapshot_lockfile]) | ||
current_specs = parse_lockfile(params[:current_lockfile]) | ||
|
||
removed_gems = snapshot_specs.keys - current_specs.keys | ||
changed_gems = current_specs.select { |name, version| snapshot_specs[name] != version }.keys | ||
|
||
return $stdout.puts("No gem changes detected") if removed_gems.empty? && changed_gems.empty? | ||
|
||
if removed_gems.any? | ||
$stdout.puts("Removing RBIs for deleted gems: #{removed_gems.join(", ")}") | ||
FileUtils.rm_f(Dir.glob("sorbet/rbi/gems/{#{removed_gems.join(",")}}@*.rbi")) | ||
end | ||
|
||
if changed_gems.any? | ||
$stdout.puts("Generating RBIs for changed gems: #{changed_gems.join(", ")}") | ||
|
||
load("tapioca/cli.rb") # Reload the CLI to reset thor defaults between requests | ||
::Tapioca::Cli.start(["gem"] + changed_gems) | ||
end | ||
end | ||
|
||
def parse_lockfile(content) | ||
return {} if content.to_s.empty? | ||
|
||
Bundler::LockfileParser.new(content).specs.to_h { |spec| [spec.name, spec.version.to_s] } | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.