From 677bb92ba93276a986a174ce05aa5d986534639c Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Thu, 25 Aug 2022 15:05:26 +0100 Subject: [PATCH 1/3] tap: add versioned_formula_files --- Library/Homebrew/tap.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index c41753c6e7..545e0537aa 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -491,6 +491,13 @@ class Tap end end + # An array of all versioned {Formula} files of this {Tap}. + def versioned_formula_files + @versioned_formula_files ||= formula_files.select do |file| + file.basename(".rb").to_s =~ /@[\d.]+$/ + end + end + # An array of all {Cask} files of this {Tap}. def cask_files @cask_files ||= if cask_dir.directory? From f09be9a5f75b44bfd16033af8b9ec456c24081c4 Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Thu, 25 Aug 2022 15:05:49 +0100 Subject: [PATCH 2/3] formula: try optimise `versioned_formulae_names` --- Library/Homebrew/formula.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index e3974cbfa6..d1325960ae 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -447,7 +447,17 @@ class Formula # Returns any `@`-versioned formulae names for any formula (including versioned formulae). sig { returns(T::Array[String]) } def versioned_formulae_names - Pathname.glob(path.to_s.gsub(/(@[\d.]+)?\.rb$/, "@*.rb")).map do |versioned_path| + versioned_paths = if tap + # Faster path, due to `tap.versioned_formula_files` caching. + name_prefix = "#{name.gsub(/(@[\d.]+)?$/, "")}@" + tap.versioned_formula_files.select do |file| + file.basename.to_s.start_with?(name_prefix) + end + else + Pathname.glob(path.to_s.gsub(/(@[\d.]+)?\.rb$/, "@*.rb")) + end + + versioned_paths.map do |versioned_path| next if versioned_path == path versioned_path.basename(".rb").to_s From 768eb1d51abeb93fbbf3557a7c169add40ac7a2d Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Thu, 25 Aug 2022 16:13:24 +0100 Subject: [PATCH 3/3] test/formula_spec: stub `Tap#versioned_formulae_names` --- Library/Homebrew/test/formula_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 7b2de44182..71f997c3df 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -149,11 +149,15 @@ describe Formula do end end + before do + allow(Formulary).to receive(:load_formula_from_path).with(f2.name, f2.path).and_return(f2) + allow(Formulary).to receive(:factory).with(f2.name).and_return(f2) + allow(f.tap).to receive(:versioned_formula_files).and_return([f2.path]) + end + it "returns array with versioned formulae" do FileUtils.touch f.path FileUtils.touch f2.path - allow(Formulary).to receive(:load_formula_from_path).with(f2.name, f2.path).and_return(f2) - allow(Formulary).to receive(:factory).with(f2.name).and_return(f2) expect(f.versioned_formulae).to eq [f2] end