Skip to content

Commit

Permalink
Extract update_strictnesses into RBIHelper
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
  • Loading branch information
Morriar committed Mar 1, 2022
1 parent 19dc932 commit 2ab2cb3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 56 deletions.
60 changes: 4 additions & 56 deletions lib/tapioca/commands/gem.rb
Expand Up @@ -5,6 +5,7 @@ module Tapioca
module Commands
class Gem < Command
include SorbetHelper
include RBIHelper

sig do
params(
Expand Down Expand Up @@ -78,7 +79,8 @@ def execute
end

if anything_done
update_strictnesses(gem_queue.map(&:name), gem_dir: @outpath.to_s, dsl_dir: @dsl_dir) if @auto_strictness
gem_names = gem_queue.map(&:name)
update_gem_rbis_strictnesses(gem_names, gem_dir: @outpath.to_s, dsl_dir: @dsl_dir) if @auto_strictness

say("All operations performed in working directory.", [:green, :bold])
say("Please review changes and commit them.", [:green, :bold])
Expand All @@ -102,7 +104,7 @@ def sync(should_verify: false)
].any?

if anything_done
update_strictnesses([], gem_dir: @outpath.to_s, dsl_dir: @dsl_dir) if @auto_strictness
update_gem_rbis_strictnesses([], gem_dir: @outpath.to_s, dsl_dir: @dsl_dir) if @auto_strictness

say("All operations performed in working directory.", [:green, :bold])
say("Please review changes and commit them.", [:green, :bold])
Expand Down Expand Up @@ -379,60 +381,6 @@ def merge_with_exported_rbi(gem, file)
say_error("\n\n RBIs exported by `#{gem.name}` contain errors and can't be used:", :yellow)
say_error("Cause: #{e.message} (#{e.location})")
end

sig { params(gem_names: T::Array[String], gem_dir: String, dsl_dir: String).void }
def update_strictnesses(gem_names, gem_dir: DEFAULT_GEM_DIR, dsl_dir: DEFAULT_DSL_DIR)
return unless File.directory?(dsl_dir)

error_url_base = Spoom::Sorbet::Errors::DEFAULT_ERROR_URL_BASE

say("Typechecking RBI files... ")
res = sorbet(
"--no-config",
"--error-url-base=#{error_url_base}",
"--isolate-error-code 4010",
dsl_dir,
gem_dir
)
say(" Done", :green)

errors = Spoom::Sorbet::Errors::Parser.parse_string(res.err)

if errors.empty?
say("No error found", [:green, :bold])
return
end

files = []

errors.each do |error|
# Collect the file with error
files << error.file
error.more.each do |line|
# Also collect the conflicting definition file paths
next unless line.include?("Previous definition")
files << line.split(":").first&.strip
end
end

files
.uniq
.sort
.select do |file|
name = gem_name_from_rbi_path(file)
file.start_with?(gem_dir) && (gem_names.empty? || gem_names.include?(name))
end.each do |file|
Spoom::Sorbet::Sigils.change_sigil_in_file(file, "false")
say("\n Changed strictness of #{file} to `typed: false` (conflicting with DSL files)", [:yellow, :bold])
end

say("\n")
end

sig { params(path: String).returns(String) }
def gem_name_from_rbi_path(path)
T.must(File.basename(path, ".rbi").split("@").first)
end
end
end
end
66 changes: 66 additions & 0 deletions lib/tapioca/helpers/rbi_helper.rb
@@ -0,0 +1,66 @@
# typed: strict
# frozen_string_literal: true

module Tapioca
module RBIHelper
extend T::Sig
extend T::Helpers

requires_ancestor { Thor::Shell }
requires_ancestor { SorbetHelper }

sig { params(gem_names: T::Array[String], gem_dir: String, dsl_dir: String).void }
def update_gem_rbis_strictnesses(gem_names, gem_dir: DEFAULT_GEM_DIR, dsl_dir: DEFAULT_DSL_DIR)
return unless File.directory?(dsl_dir)

error_url_base = Spoom::Sorbet::Errors::DEFAULT_ERROR_URL_BASE

say("Typechecking RBI files... ")
res = sorbet(
"--no-config",
"--error-url-base=#{error_url_base}",
"--isolate-error-code 4010",
dsl_dir,
gem_dir
)
say(" Done", :green)

errors = Spoom::Sorbet::Errors::Parser.parse_string(res.err)

if errors.empty?
say("No error found", [:green, :bold])
return
end

files = []

errors.each do |error|
# Collect the file with error
files << error.file
error.more.each do |line|
# Also collect the conflicting definition file paths
next unless line.include?("Previous definition")
files << line.split(":").first&.strip
end
end

files
.uniq
.sort
.select do |file|
name = gem_name_from_rbi_path(file)
file.start_with?(gem_dir) && (gem_names.empty? || gem_names.include?(name))
end.each do |file|
Spoom::Sorbet::Sigils.change_sigil_in_file(file, "false")
say("\n Changed strictness of #{file} to `typed: false` (conflicting with DSL files)", [:yellow, :bold])
end

say("\n")
end

sig { params(path: String).returns(String) }
def gem_name_from_rbi_path(path)
T.must(File.basename(path, ".rbi").split("@").first)
end
end
end
1 change: 1 addition & 0 deletions lib/tapioca/internal.rb
Expand Up @@ -12,6 +12,7 @@
require "tapioca/helpers/cli_helper"
require "tapioca/helpers/config_helper"
require "tapioca/helpers/signatures_helper"
require "tapioca/helpers/rbi_helper"
require "tapioca/helpers/shims_helper"
require "tapioca/helpers/sorbet_helper"
require "tapioca/commands"
Expand Down

0 comments on commit 2ab2cb3

Please sign in to comment.