Skip to content

Commit

Permalink
Start generating gitattributes file for generated and vendored RBI files
Browse files Browse the repository at this point in the history
This commit adds a `.gitattributes` file to the output folder that marks all RBI files as `generated` for `dsl` and `gem` RBI generation commands. It also add a `.gitattributes` file to the output folder that marks all RBI files as `vendored` for `annotations` command.
  • Loading branch information
paracycle committed Aug 14, 2023
1 parent fcbde0d commit 91391fb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/tapioca/commands/annotations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def initialize(
typed_overrides: {}
)
super()
@outpath = T.let(Pathname.new(DEFAULT_ANNOTATIONS_DIR), Pathname)
@central_repo_root_uris = central_repo_root_uris
@auth = auth
@netrc_file = netrc_file
Expand All @@ -38,8 +39,11 @@ def initialize(
def execute
@indexes = fetch_indexes
project_gems = list_gemfile_gems

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

sig { returns(T::Array[String]) }
Expand All @@ -56,7 +60,7 @@ def list_gemfile_gems
def remove_expired_annotations(project_gems)
say("Removing annotations for gems that have been removed... ", [:blue, :bold])

annotations = Pathname.glob("#{DEFAULT_ANNOTATIONS_DIR}/*.rbi").map { |f| f.basename(".*").to_s }
annotations = Pathname.glob(@outpath.join("*.rbi")).map { |f| f.basename(".*").to_s }
expired = annotations - project_gems

if expired.empty?
Expand All @@ -67,7 +71,7 @@ def remove_expired_annotations(project_gems)
say("\n")
expired.each do |gem_name|
say("\n")
path = "#{DEFAULT_ANNOTATIONS_DIR}/#{gem_name}.rbi"
path = @outpath.join("#{gem_name}.rbi")
remove_file(path)
end
say("\nDone\n\n", :green)
Expand Down Expand Up @@ -140,10 +144,8 @@ def fetch_annotation(repo_uris, gem_name)
content = apply_typed_override(gem_name, content)
content = add_header(gem_name, content)

dir = DEFAULT_ANNOTATIONS_DIR
FileUtils.mkdir_p(dir)
say("\n Fetched #{set_color(gem_name, :yellow, :bold)}", :green)
create_file("#{dir}/#{gem_name}.rbi", content)
create_file(@outpath.join("#{gem_name}.rbi"), content)
end

sig { params(repo_uri: String, path: String).returns(T.nilable(String)) }
Expand Down
2 changes: 2 additions & 0 deletions lib/tapioca/commands/dsl_generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def execute

say("All operations performed in working directory.", [:green, :bold])
say("Please review changes and commit them.", [:green, :bold])
ensure
GitAttributes.create_generated_attribute_file(@outpath)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/tapioca/commands/gem_generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def execute
else
say("No operations performed, all RBIs are up-to-date.", [:green, :bold])
end
ensure
GitAttributes.create_generated_attribute_file(@outpath)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/tapioca/commands/gem_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def execute
end

puts
ensure
GitAttributes.create_generated_attribute_file(@outpath)
end
end
end
Expand Down
34 changes: 34 additions & 0 deletions lib/tapioca/helpers/git_attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# typed: strict
# frozen_string_literal: true

class GitAttributes
class << self
extend T::Sig

sig { params(path: Pathname).void }
def create_generated_attribute_file(path)
create_gitattributes_file(path, <<~CONTENT)
**/*.rbi linguist-generated=true
CONTENT
end

sig { params(path: Pathname).void }
def create_vendored_attribute_file(path)
create_gitattributes_file(path, <<~CONTENT)
**/*.rbi linguist-vendored=true
CONTENT
end

private

sig { params(path: Pathname, content: String).void }
def create_gitattributes_file(path, content)
# We don't want to start creating folders, just to write
# the `.gitattributes` file. So, if the folder doesn't
# exist, we just return.
return unless path.exist?

File.write(path.join(".gitattributes"), content)
end
end
end
1 change: 1 addition & 0 deletions lib/tapioca/internal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require "tapioca/runtime/dynamic_mixin_compiler"
require "tapioca/helpers/gem_helper"

require "tapioca/helpers/git_attributes"
require "tapioca/helpers/sorbet_helper"
require "tapioca/helpers/rbi_helper"
require "tapioca/sorbet_ext/backcompat_patches"
Expand Down

0 comments on commit 91391fb

Please sign in to comment.