Merge pull request #13755 from Bo98/versioned_formulae_names-optimise

formula: try optimise `versioned_formulae_names`
This commit is contained in:
Mike McQuaid 2022-08-25 16:54:22 +01:00 committed by GitHub
commit b2ddb341a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View File

@ -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

View File

@ -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?

View File

@ -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