Skip to content

Commit

Permalink
Merge pull request #16010 from apainintheneck/improve-versioned-formu…
Browse files Browse the repository at this point in the history
…la-names-performance

cmd/audit: improve performance of versioned formula names
  • Loading branch information
apainintheneck authored Sep 17, 2023
2 parents 184efd9 + 7e05a9b commit aa2a77b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
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 @@ def formula_names
@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.]+)?$/, "") }
.transform_values(&:sort)
.freeze
end

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

2 comments on commit aa2a77b

@CyberFlameGO
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no .pkg file released on the release associated with this tagged commit - just FYI :)

@MikeMcQuaid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed now, thanks!

Please sign in to comment.