Include core formulae from API in Formula.all.

This is needed to allow `Formula.all` to behave as expected when using
the API. Let's also (immediately, because it's broken) deprecate the
`CoreTap#formula_files` method.

Fixes #15122
This commit is contained in:
Mike McQuaid 2023-06-28 15:27:20 +01:00
parent 043b9512e4
commit cd414bd95f
No known key found for this signature in database
GPG Key ID: 3338A31AFDB1D829
2 changed files with 43 additions and 20 deletions

View File

@ -1827,12 +1827,6 @@ class Formula
CoreTap.instance.formula_names CoreTap.instance.formula_names
end end
# an array of all core {Formula} files
# @private
def self.core_files
CoreTap.instance.formula_files
end
# an array of all tap {Formula} names # an array of all tap {Formula} names
# @private # @private
def self.tap_names def self.tap_names
@ -1851,12 +1845,6 @@ class Formula
@names ||= (core_names + tap_names.map { |name| name.split("/").last }).uniq.sort @names ||= (core_names + tap_names.map { |name| name.split("/").last }).uniq.sort
end end
# an array of all {Formula} files
# @private
def self.files
@files ||= core_files + tap_files
end
# an array of all {Formula} names, which the tap formulae have the fully-qualified name # an array of all {Formula} names, which the tap formulae have the fully-qualified name
# @private # @private
def self.full_names def self.full_names
@ -1872,11 +1860,11 @@ class Formula
odeprecated "Formula#all without --all or HOMEBREW_EVAL_ALL" odeprecated "Formula#all without --all or HOMEBREW_EVAL_ALL"
end end
files.map do |file| (core_names + tap_files).map do |name_or_file|
Formulary.factory(file) Formulary.factory(name_or_file)
rescue FormulaUnavailableError, FormulaUnreadableError => e rescue FormulaUnavailableError, FormulaUnreadableError => e
# Don't let one broken formula break commands. But do complain. # Don't let one broken formula break commands. But do complain.
onoe "Failed to import: #{file}" onoe "Failed to import: #{name_or_file}"
$stderr.puts e $stderr.puts e
nil nil

View File

@ -514,11 +514,17 @@ class Tap
def self.formula_files_by_name(tap) def self.formula_files_by_name(tap)
cache_key = "formula_files_by_name_#{tap}" cache_key = "formula_files_by_name_#{tap}"
cache.fetch(cache_key) do |key| cache.fetch(cache_key) do |key|
cache[key] = tap.formula_files.each_with_object({}) do |file, hash| cache[key] = tap.formula_files_by_name
# If there's more than one file with the same basename: intentionally end
# ignore the later ones here. end
hash[file.basename.to_s] ||= file
end # @private
sig { returns(T::Hash[String, Pathname]) }
def formula_files_by_name
formula_files.each_with_object({}) do |file, hash|
# If there's more than one file with the same basename: intentionally
# ignore the later ones here.
hash[file.basename.to_s] ||= file
end end
end end
@ -1011,6 +1017,14 @@ class CoreTap < Tap
Homebrew::API::Formula.all_aliases.keys Homebrew::API::Formula.all_aliases.keys
end end
# @private
sig { returns(T::Array[Pathname]) }
def formula_files
return super if Homebrew::EnvConfig.no_install_from_api? || installed?
raise TapUnavailableError, name
end
# @private # @private
sig { returns(T::Array[String]) } sig { returns(T::Array[String]) }
def formula_names def formula_names
@ -1018,6 +1032,27 @@ class CoreTap < Tap
Homebrew::API::Formula.all_formulae.keys Homebrew::API::Formula.all_formulae.keys
end end
# @private
sig { returns(T::Hash[String, Pathname]) }
def formula_files_by_name
return super if Homebrew::EnvConfig.no_install_from_api?
formula_names.each_with_object({}) do |name, hash|
# If there's more than one file with the same basename: intentionally
# ignore the later ones here.
hash[name] ||= sharded_formula_path(name)
end
end
private
# @private
sig { params(name: String).returns(Pathname) }
def sharded_formula_path(name)
# TODO: add sharding logic.
formula_dir/"#{name}.rb"
end
end end
# Permanent configuration per {Tap} using `git-config(1)`. # Permanent configuration per {Tap} using `git-config(1)`.