From 1623c94d31689598ab3cef220956fcd1bf1ee1fc Mon Sep 17 00:00:00 2001 From: Jeff Carbonella Date: Fri, 18 Jun 2021 23:05:03 -0600 Subject: [PATCH] Add support for `tapioca sync --verify` --- lib/tapioca/cli/main.rb | 6 ++++- lib/tapioca/generator.rb | 47 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/tapioca/cli/main.rb b/lib/tapioca/cli/main.rb index bd452fce5..00ee6a958 100644 --- a/lib/tapioca/cli/main.rb +++ b/lib/tapioca/cli/main.rb @@ -84,9 +84,13 @@ def generate(*gems) end desc "sync", "sync RBIs to Gemfile" + option :verify, + type: :boolean, + default: false, + desc: "Verifies RBIs are up-to-date" def sync Tapioca.silence_warnings do - generator.sync_rbis_with_gemfile + generator.sync_rbis_with_gemfile(should_verify: options[:verify]) end end diff --git a/lib/tapioca/generator.rb b/lib/tapioca/generator.rb index 073650fe8..a56dadf33 100644 --- a/lib/tapioca/generator.rb +++ b/lib/tapioca/generator.rb @@ -171,8 +171,15 @@ def build_dsl(requested_constants, should_verify: false, quiet: false) end end - sig { void } - def sync_rbis_with_gemfile + sig { params(should_verify: T::Boolean).void } + def sync_rbis_with_gemfile(should_verify: false) + if should_verify + say("Checking for out-of-date RBIs...") + say("") + perform_sync_verification + return + end + anything_done = [ perform_removals, perform_additions, @@ -624,5 +631,41 @@ def purge_stale_dsl_rbi_files(files) say("") end end + + sig { void } + def perform_sync_verification + diff = {} + + removed_rbis.each do |gem_name| + filename = existing_rbi(gem_name) + diff[filename] = :removed + end + + added_rbis.each do |gem_name| + filename = expected_rbi(gem_name) + if gem_rbi_exists?(gem_name) + diff[filename] = :changed + else + diff[filename] = :added + end + end + + if diff.empty? + say("Nothing to do, all RBIs are up-to-date.") + else + say("RBI files are out-of-date. In your development environment, please run:", :green) + say(" `#{Config::DEFAULT_COMMAND} sync`", [:green, :bold]) + say("Once it is complete, be sure to commit and push any changes", :green) + + say("") + + say("Reason:", [:red]) + diff.group_by(&:last).sort.each do |cause, diff_for_cause| + say(build_error_for_files(cause, diff_for_cause.map(&:first))) + end + + exit(1) + end + end end end