Skip to content

Commit

Permalink
fix tap migration renames with API
Browse files Browse the repository at this point in the history
We don't load casks and formulae that used to exist in an external
tap but have been moved and renamed to an API tap when using the
API. Loading works correctly when the core formula or cask tap
is tapped locally though.

This PR adds some logic to map tap migration renames to the API
and check that when loading formulae and casks.
  • Loading branch information
apainintheneck committed Jun 24, 2024
1 parent c53f600 commit fcaeae9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
18 changes: 14 additions & 4 deletions Library/Homebrew/cask/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,28 @@ class FromAPILoader
def self.try_new(ref, warn: false)
return if Homebrew::EnvConfig.no_install_from_api?
return unless ref.is_a?(String)
return unless (token = ref[HOMEBREW_DEFAULT_TAP_CASK_REGEX, :token])
if !Homebrew::API::Cask.all_casks.key?(token) &&
!Homebrew::API::Cask.all_renames.key?(token)
return

token = parse_token(ref)
token ||= begin
ref = CoreCaskTap.instance.tap_migration_renames[ref]
parse_token(ref) if ref
end
return unless token

ref = "#{CoreCaskTap.instance}/#{token}"

token, tap, = CaskLoader.tap_cask_token_type(ref, warn:)
new("#{tap}/#{token}")
end

sig { params(ref: String).returns(T.nilable(String)) }
private_class_method def self.parse_token(ref)
return unless (token = ref[HOMEBREW_DEFAULT_TAP_CASK_REGEX, :token])
return token if Homebrew::API::Cask.all_casks.key?(token)

token if Homebrew::API::Cask.all_renames.key?(token)
end

sig { params(token: String, from_json: Hash, path: T.nilable(Pathname)).void }
def initialize(token, from_json: T.unsafe(nil), path: nil)
@token = token.sub(%r{^homebrew/(?:homebrew-)?cask/}i, "")
Expand Down
22 changes: 16 additions & 6 deletions Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -917,12 +917,13 @@ class FromAPILoader < FormulaLoader
def self.try_new(ref, from: T.unsafe(nil), warn: false)
return if Homebrew::EnvConfig.no_install_from_api?
return unless ref.is_a?(String)
return unless (name = ref[HOMEBREW_DEFAULT_TAP_FORMULA_REGEX, :name])
if !Homebrew::API::Formula.all_formulae.key?(name) &&
!Homebrew::API::Formula.all_aliases.key?(name) &&
!Homebrew::API::Formula.all_renames.key?(name)
return

name = parse_name(ref)
name ||= begin
ref = CoreTap.instance.tap_migration_renames[ref]
parse_name(ref) if ref
end
return unless name

alias_name = name

Expand All @@ -932,7 +933,7 @@ def self.try_new(ref, from: T.unsafe(nil), warn: false)

name, tap, type = name_tap_type

options = if type == :alias
options = if type == :alias
{ alias_name: alias_name.downcase }
else
{}
Expand All @@ -941,6 +942,15 @@ def self.try_new(ref, from: T.unsafe(nil), warn: false)
new(name, tap:, **options)
end

sig { params(ref: String).returns(T.nilable(String)) }
private_class_method def self.parse_name(ref)
return unless (name = ref[HOMEBREW_DEFAULT_TAP_FORMULA_REGEX, :name])
return name if Homebrew::API::Formula.all_formulae.key?(name)
return name if Homebrew::API::Formula.all_aliases.key?(name)

name if Homebrew::API::Formula.all_renames.key?(name)
end

sig { params(name: String, tap: Tap, alias_name: String).void }
def initialize(name, tap: T.unsafe(nil), alias_name: T.unsafe(nil))
options = {
Expand Down
26 changes: 26 additions & 0 deletions Library/Homebrew/tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,19 @@ def tap_migrations
end
end

# External formula names to core formula renames
sig { returns(T::Hash[String, String]) }
def tap_migration_renames
@tap_migration_renames ||= Tap.each_with_object({}) do |tap, hash|
tap.tap_migrations.each do |old_name, new_name|
next unless new_name.start_with?("homebrew/core/")

hash[old_name] = new_name
hash["#{tap}/#{old_name}"] = new_name

Check warning on line 1295 in Library/Homebrew/tap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/tap.rb#L1294-L1295

Added lines #L1294 - L1295 were not covered by tests
end
end
end

sig { returns(T::Array[String]) }
def autobump
@autobump ||= begin
Expand Down Expand Up @@ -1456,6 +1469,19 @@ def tap_migrations
end
end

# External cask names to core cask renames
sig { returns(T::Hash[String, String]) }
def tap_migration_renames
@tap_migration_renames ||= Tap.each_with_object({}) do |tap, hash|
tap.tap_migrations.each do |old_name, new_name|
next unless new_name.start_with?("homebrew/cask/")

hash[old_name] = new_name
hash["#{tap}/#{old_name}"] = new_name

Check warning on line 1480 in Library/Homebrew/tap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/tap.rb#L1479-L1480

Added lines #L1479 - L1480 were not covered by tests
end
end
end

sig { returns(T::Hash[String, T.untyped]) }
def to_internal_api_hash
casks_api_hash = cask_tokens.to_h do |token|
Expand Down

0 comments on commit fcaeae9

Please sign in to comment.