Skip to content

Commit

Permalink
Refactor Formulary::loader_for.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Feb 8, 2024
1 parent a925911 commit 49c03d1
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 182 deletions.
4 changes: 2 additions & 2 deletions Library/Homebrew/api/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def download_and_cache_data!
end
private :download_and_cache_data!

sig { returns(Hash) }
sig { returns(T::Hash[String, Hash]) }
def all_formulae
unless cache.key?("formulae")
json_updated = download_and_cache_data!
Expand All @@ -69,7 +69,7 @@ def all_formulae
cache["formulae"]
end

sig { returns(Hash) }
sig { returns(T::Hash[String, String]) }
def all_aliases
unless cache.key?("aliases")
json_updated = download_and_cache_data!
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cli/named_args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def to_paths(only: parent&.only_formula_or_cask, recurse_tap: false)
paths = []

if formula_path.exist? ||
(!CoreTap.instance.installed? && Homebrew::API::Formula.all_formulae.key?(path.basename))
(!CoreTap.instance.installed? && Homebrew::API::Formula.all_formulae.key?(path.basename.to_s))
paths << formula_path
end
if cask_path.exist? ||
Expand Down
9 changes: 1 addition & 8 deletions Library/Homebrew/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -835,14 +835,7 @@ def check_deleted_formula
kegs = Keg.all

deleted_formulae = kegs.map do |keg|
next if Formulary.tap_paths(keg.name).any?

unless EnvConfig.no_install_from_api?
# Formulae installed from the API should not count as deleted formulae
# but may not have a tap listed in their tab
tap = Tab.for_keg(keg).tap
next if (tap.blank? || tap.core_tap?) && Homebrew::API::Formula.all_formulae.key?(keg.name)
end
next if Formulary::FromNameLoader.try_new(keg.name, warn: false).nil?

keg.name
end.compact.uniq
Expand Down
16 changes: 8 additions & 8 deletions Library/Homebrew/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,19 @@ def initialize(tap, name, error)

# Raised when a formula with the same name is found in multiple taps.
class TapFormulaAmbiguityError < RuntimeError
attr_reader :name, :paths, :formulae
attr_reader :name, :taps

def initialize(name, paths)
def initialize(name, taps)
@name = name
@paths = paths
@formulae = paths.map do |path|
"#{Tap.from_path(path).name}/#{path.basename(".rb")}"
end
@taps = taps

formulae = taps.map { |tap| "#{tap}/#{name}" }
formula_list = formulae.map { |f| "\n * #{f}" }.join

super <<~EOS
Formulae found in multiple taps: #{formulae.map { |f| "\n * #{f}" }.join}
Formulae found in multiple taps:#{formula_list}
Please use the fully-qualified name (e.g. #{formulae.first}) to refer to the formula.
Please use the fully-qualified name (e.g. #{formulae.first}) to refer to a specific formula.
EOS
end
end
Expand Down
19 changes: 11 additions & 8 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Formula

# The path to the alias that was used to identify this {Formula}.
# e.g. `/usr/local/Library/Taps/homebrew/homebrew-core/Aliases/another-name-for-this-formula`
sig { returns(T.any(NilClass, Pathname, String)) }
sig { returns(T.nilable(Pathname)) }
attr_reader :alias_path

# The name of the alias that was used to identify this {Formula}.
Expand Down Expand Up @@ -199,7 +199,7 @@ class Formula

# @private
sig {
params(name: String, path: Pathname, spec: Symbol, alias_path: T.any(NilClass, Pathname, String),
params(name: String, path: Pathname, spec: Symbol, alias_path: T.nilable(Pathname),
tap: T.nilable(Tap), force_bottle: T::Boolean).void
}
def initialize(name, path, spec, alias_path: nil, tap: nil, force_bottle: false)
Expand Down Expand Up @@ -326,18 +326,22 @@ def validate_attributes!
# The alias path that was used to install this formula, if it exists.
# Can differ from {#alias_path}, which is the alias used to find the formula,
# and is specified to this instance.
sig { returns(T.nilable(Pathname)) }
def installed_alias_path
build_tab = build
path = build_tab.source["path"] if build_tab.is_a?(Tab)

return unless path&.match?(%r{#{HOMEBREW_TAP_DIR_REGEX}/Aliases}o)
return unless File.symlink?(path)

path = Pathname(path)
return unless path.symlink?

path
end

sig { returns(T.nilable(String)) }
def installed_alias_name
File.basename(installed_alias_path) if installed_alias_path
installed_alias_path&.basename&.to_s
end

def full_installed_alias_name
Expand All @@ -346,14 +350,13 @@ def full_installed_alias_name

# The path that was specified to find this formula.
def specified_path
alias_pathname = Pathname(T.must(alias_path)) if alias_path.present?
return alias_pathname if alias_pathname&.exist?
return alias_path if alias_path&.exist?

return @unresolved_path if @unresolved_path.exist?

return local_bottle_path if local_bottle_path.presence&.exist?

alias_pathname || @unresolved_path
alias_path || @unresolved_path
end

# The name specified to find this formula.
Expand Down Expand Up @@ -2379,7 +2382,7 @@ def to_hash_with_variations(hash_method: :to_hash)

# Take from API, merging in local install status.
if loaded_from_api? && !Homebrew::EnvConfig.no_install_from_api?
json_formula = Homebrew::API::Formula.all_formulae[name].dup
json_formula = Homebrew::API::Formula.all_formulae.fetch(name).dup
return json_formula.merge(
hash.slice("name", "installed", "linked_keg", "pinned", "outdated"),
)
Expand Down
Loading

0 comments on commit 49c03d1

Please sign in to comment.