Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/audit: improve performance of versioned formula names #16010

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -489,20 +489,19 @@ def versioned_formula?
name.include?("@")
end

# Returns any `@`-versioned formulae names for any formula (including versioned formulae).
# Returns any other `@`-versioned formulae names for any formula (including versioned formulae).
sig { returns(T::Array[String]) }
def versioned_formulae_names
versioned_names = if tap
name_prefix = "#{name.gsub(/(@[\d.]+)?$/, "")}@"
T.must(tap).formula_names.select do |name|
name.start_with?(name_prefix)
end
name_prefix = name.gsub(/(@[\d.]+)?$/, "")
T.must(tap).prefix_to_versioned_formulae_names.fetch(name_prefix, [])
elsif path.exist?
Pathname.glob(path.to_s.gsub(/(@[\d.]+)?\.rb$/, "@*.rb"))
.map { |path| path.basename(".rb").to_s }
.sort
else
raise "Either tap or path is required to list versioned formulae"
end.sort
end

versioned_names.reject do |versioned_name|
versioned_name == name
Expand Down
11 changes: 11 additions & 0 deletions Library/Homebrew/tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,17 @@
@formula_names ||= formula_files.map(&method(:formula_file_to_name))
end

# A hash of all {Formula} name prefixes to versioned {Formula} in this {Tap}.
# @private
sig { returns(T::Hash[String, T::Array[String]]) }
def prefix_to_versioned_formulae_names
@prefix_to_versioned_formulae_names ||= formula_names
.select { |name| name.include?("@") }
.group_by { |name| name.gsub(/(@[\d.]+)?$/, "") }

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/tap.rb#L653

Added line #L653 was not covered by tests
.transform_values(&:sort)
.freeze
end

# An array of all {Cask} tokens of this {Tap}.
sig { returns(T::Array[String]) }
def cask_tokens
Expand Down