Skip to content

Commit

Permalink
Support filtering RBI files by version in annotations command
Browse files Browse the repository at this point in the history
  • Loading branch information
egiurleo committed Jan 31, 2024
1 parent c4fcdad commit 0635c79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
43 changes: 31 additions & 12 deletions lib/tapioca/commands/annotations.rb
Expand Up @@ -38,22 +38,27 @@ def initialize(
sig { override.void }
def execute
@indexes = fetch_indexes
project_gems = list_gemfile_gems
gem_names_and_versions = list_gemfile_gems

remove_expired_annotations(project_gems)
fetch_annotations(project_gems)
remove_expired_annotations(gem_names_and_versions.keys)
fetch_annotations(gem_names_and_versions)
ensure
GitAttributes.create_vendored_attribute_file(@outpath)
end

sig { returns(T::Array[String]) }
sig { returns(T::Hash[String, ::Gem::Version]) }
def list_gemfile_gems
say("Listing gems from Gemfile.lock... ", [:blue, :bold])
gemfile = Bundler.read_file("Gemfile.lock")
parser = Bundler::LockfileParser.new(gemfile)
gem_names = parser.specs.map(&:name)

gem_names_and_versions = T.let(Hash.new { |h, k| h[k] = [] }, T::Hash[String, ::Gem::Version])
parser.specs.each_with_object(gem_names_and_versions) do |spec, hash|
hash[spec.name] = spec.version
end

say("Done", :green)
gem_names
gem_names_and_versions
end

sig { params(project_gems: T::Array[String]).void }
Expand Down Expand Up @@ -109,12 +114,12 @@ def fetch_index(repo_uri, repo_number:)
index
end

sig { params(gem_names: T::Array[String]).returns(T::Array[String]) }
def fetch_annotations(gem_names)
sig { params(gem_names_and_versions: T::Hash[String, ::Gem::Version]).returns(T::Array[String]) }
def fetch_annotations(gem_names_and_versions)
say("Fetching gem annotations from central repository... ", [:blue, :bold])
fetchable_gems = T.let(Hash.new { |h, k| h[k] = [] }, T::Hash[String, T::Array[String]])

gem_names.each_with_object(fetchable_gems) do |gem_name, hash|
gem_names_and_versions.keys.each_with_object(fetchable_gems) do |gem_name, hash|
@indexes.each do |uri, index|
T.must(hash[gem_name]) << uri if index.has_gem?(gem_name)
end
Expand All @@ -127,20 +132,26 @@ def fetch_annotations(gem_names)
end

say("\n")
fetched_gems = fetchable_gems.select { |gem_name, repo_uris| fetch_annotation(repo_uris, gem_name) }
fetched_gems = fetchable_gems.select do |gem_name, repo_uris|
gem_version = gem_names_and_versions[gem_name]
next unless gem_version

fetch_annotation(repo_uris, gem_name, gem_version)
end
say("\nDone", :green)
fetched_gems.keys.sort
end

sig { params(repo_uris: T::Array[String], gem_name: String).void }
def fetch_annotation(repo_uris, gem_name)
sig { params(repo_uris: T::Array[String], gem_name: String, gem_version: ::Gem::Version).void }
def fetch_annotation(repo_uris, gem_name, gem_version)
contents = repo_uris.map do |repo_uri|
fetch_file(repo_uri, "#{CENTRAL_REPO_ANNOTATIONS_DIR}/#{gem_name}.rbi")
end

content = merge_files(gem_name, contents.compact)
return unless content

content = filter_versions(gem_version, content)
content = apply_typed_override(gem_name, content)
content = add_header(gem_name, content)

Expand Down Expand Up @@ -221,6 +232,14 @@ def apply_typed_override(name, content)
Spoom::Sorbet::Sigils.update_sigil(content, strictness)
end

sig { params(gem_version: ::Gem::Version, content: String).returns(String) }
def filter_versions(gem_version, content)
rbi = RBI::Parser.parse_string(content)
rbi.filter_versions!(gem_version)

rbi.string
end

sig { params(gem_name: String, contents: T::Array[String]).returns(T.nilable(String)) }
def merge_files(gem_name, contents)
return if contents.empty?
Expand Down
3 changes: 3 additions & 0 deletions spec/tapioca/cli/annotations_spec.rb
Expand Up @@ -372,6 +372,7 @@ class Foo; end
repo = create_repo({
foo: <<~RBI,
# typed: false
# @version > 0.3.5
class AnnotationForFoo; end
class Foo; end
Expand All @@ -384,9 +385,11 @@ class Foo; end

assert_project_annotation_equal("sorbet/rbi/annotations/foo.rbi", <<~RBI)
# typed: false
# DO NOT EDIT MANUALLY
# This file was pulled from a central RBI files repository.
# Please run `bin/tapioca annotations` to update it.
class Foo; end
RBI

Expand Down

0 comments on commit 0635c79

Please sign in to comment.