From 2a78eba1adec9c9dc351177dcbd6252ebc8d249c Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 23 Feb 2024 15:03:33 +0100 Subject: [PATCH] Simplify `Tap#formula_files_by_name`. --- Library/Homebrew/formulary.rb | 3 +-- Library/Homebrew/tap.rb | 14 ++++---------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 1a882e18d2..90fb41557b 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -1201,7 +1201,6 @@ module Formulary "#{name}.rb" end - Tap.formula_files_by_name(tap) - .fetch(name, tap.formula_dir/filename) + tap.formula_files_by_name.fetch(name, tap.formula_dir/filename) end end diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 1900976a27..d29b549c87 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -128,6 +128,7 @@ class Tap @command_dir = nil @formula_names = nil @formula_files = nil + @formula_files_by_name = nil @cask_files = nil @alias_dir = nil @alias_files = nil @@ -560,19 +561,12 @@ class Tap end end - # A cached hash of {Formula} basenames to {Formula} file pathnames for a {Tap} - sig { params(tap: Tap).returns(T::Hash[String, Pathname]) } - def self.formula_files_by_name(tap) - cache_key = "formula_files_by_name_#{tap}" - cache.fetch(cache_key) do |key| - cache[key] = tap.formula_files_by_name - end - end - + # A mapping of {Formula} names to {Formula} file paths. + # # @private sig { returns(T::Hash[String, Pathname]) } def formula_files_by_name - formula_files.each_with_object({}) do |file, hash| + @formula_files_by_name ||= formula_files.each_with_object({}) do |file, hash| # If there's more than one file with the same basename: use the longer one to prioritise more specific results. basename = file.basename(".rb").to_s existing_file = hash[basename]