Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ruby_lsp/tapioca/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def activate(global_state, outgoing_queue)
)

send_usage_telemetry("activated")
run_gem_rbi_check
# run_gem_rbi_check
rescue IncompatibleApiError
send_usage_telemetry("incompatible_api_error")

Expand Down
41 changes: 41 additions & 0 deletions lib/rubygems_plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# typed: true
# frozen_string_literal: true

# If we're not running a bundler command, skip registering the plugin
return unless $PROGRAM_NAME.end_with?("bundle", "bundler")

# If the command is not update or lock, skip registering the plugin
return unless ["update", "lock"].include?(ARGV.first)

# Use a RubyGems plugin to run gem RBI synchronization when gems are updated
# This file gets required before actually installing gems, which gives us a chance to compare the lockfile and know
# if gems have actually changed

# We use this global variable to ensure that the plugin is only registered once since RubyGems / Bundler will actually
# load this multiple times
# rubocop:disable Style/GlobalVars
return if $registered_tapioca_rubygems_plugin

$registered_tapioca_rubygems_plugin = true
# rubocop:enable Style/GlobalVars

lockfile = begin
Bundler.default_lockfile
rescue Bundler::GemfileNotFound
nil
end
return unless lockfile

# Get the state of the lockfile before any changes are committed
current_lockfile = File.read(lockfile)

# This is a RubyGems and not a Bundler plugin, so it would get invoked multiple times during a bundle update. We workaround
# that by registering an at_exit hook to generate the RBIs once Bundler is fully done
at_exit do
new_lockfile = File.read(lockfile)

if current_lockfile != new_lockfile
puts "Detected gem updates, regenerating gem RBIs"
system("bundle exec tapioca gem")
end
end
Loading