refactoring code

This commit is contained in:
thibhero 2025-06-09 00:14:16 -04:00
parent a8638270f2
commit aa47105d2a
6 changed files with 134 additions and 41 deletions

View File

@ -340,6 +340,7 @@ module Homebrew
dependants = Upgrade.get_dependants( dependants = Upgrade.get_dependants(
installed_formulae, installed_formulae,
flags: args.flags_only, flags: args.flags_only,
ask: args.ask?,
installed_on_request: !args.as_dependency?, installed_on_request: !args.as_dependency?,
force_bottle: args.force_bottle?, force_bottle: args.force_bottle?,
build_from_source_formulae: args.build_from_source_formulae, build_from_source_formulae: args.build_from_source_formulae,
@ -353,17 +354,12 @@ module Homebrew
dry_run: args.dry_run?, dry_run: args.dry_run?,
) )
formulae_dependencies = formulae_installer.flat_map do |f| formulae_dependencies = Install.get_hierarchy(formulae_installer, dependants)
[f.formula, f.compute_dependencies.flatten.filter do |c|
c.is_a? Dependency
end.flat_map(&:to_formula)]
end.flatten.uniq
formulae_dependencies.concat(dependants.upgradeable) if dependants
# Main block: if asking the user is enabled, show dependency and size information. # Main block: if asking the user is enabled, show dependency and size information.
Install.ask_formulae(formulae_dependencies, args: args) Install.ask_formulae(formulae_dependencies, args: args)
end end
Upgrade.upgrade_formulae(formulae_installer, Install.install_formulae(formulae_installer,
dry_run: args.dry_run?, dry_run: args.dry_run?,
verbose: args.verbose?) verbose: args.verbose?)
@ -372,7 +368,7 @@ module Homebrew
installed_formulae, installed_formulae,
flags: args.flags_only, flags: args.flags_only,
dry_run: args.dry_run?, dry_run: args.dry_run?,
ask: args.ask?, installed_on_request: !args.as_dependency?,
force_bottle: args.force_bottle?, force_bottle: args.force_bottle?,
build_from_source_formulae: args.build_from_source_formulae, build_from_source_formulae: args.build_from_source_formulae,
interactive: args.interactive?, interactive: args.interactive?,

View File

@ -130,16 +130,13 @@ module Homebrew
unless formulae.empty? unless formulae.empty?
Install.perform_preinstall_checks_once Install.perform_preinstall_checks_once
# If asking the user is enabled, show dependency and size information. formulae_keg = formulae.map do |formula|
Install.ask_formulae(formulae, args: args) if args.ask?
formulae.each do |formula|
if formula.pinned? if formula.pinned?
onoe "#{formula.full_name} is pinned. You must unpin it to reinstall." onoe "#{formula.full_name} is pinned. You must unpin it to reinstall."
next next
end end
Migrator.migrate_if_needed(formula, force: args.force?) Migrator.migrate_if_needed(formula, force: args.force?)
Homebrew::Reinstall.reinstall_formula( Homebrew::Reinstall.get_formula_to_reinstall(
formula, formula,
flags: args.flags_only, flags: args.flags_only,
force_bottle: args.force_bottle?, force_bottle: args.force_bottle?,
@ -153,10 +150,52 @@ module Homebrew
verbose: args.verbose?, verbose: args.verbose?,
git: args.git?, git: args.git?,
) )
Cleanup.install_formula_clean!(formula)
end end
Upgrade.check_installed_dependents( if args.ask?
dependants = Upgrade.get_dependants(
formulae,
flags: args.flags_only,
ask: args.ask?,
force_bottle: args.force_bottle?,
build_from_source_formulae: args.build_from_source_formulae,
interactive: args.interactive?,
keep_tmp: args.keep_tmp?,
debug_symbols: args.debug_symbols?,
force: args.force?,
debug: args.debug?,
quiet: args.quiet?,
verbose: args.verbose?,
)
formulae_dependencies = formulae_keg.map(&:formula_installer)
formulae_dependencies = Install.get_hierarchy(formulae_dependencies, dependants)
# Main block: if asking the user is enabled, show dependency and size information.
Install.ask_formulae(formulae_dependencies, args: args)
end
formulae_keg.each do |f|
Homebrew::Reinstall.reinstall_formula(
f,
flags: args.flags_only,
force_bottle: args.force_bottle?,
build_from_source_formulae: args.build_from_source_formulae,
interactive: args.interactive?,
keep_tmp: args.keep_tmp?,
debug_symbols: args.debug_symbols?,
force: args.force?,
debug: args.debug?,
quiet: args.quiet?,
verbose: args.verbose?,
git: args.git?,
)
Cleanup.install_formula_clean!(f.formula)
end
unless args.ask?
dependants = Upgrade.get_dependants(
formulae, formulae,
flags: args.flags_only, flags: args.flags_only,
force_bottle: args.force_bottle?, force_bottle: args.force_bottle?,
@ -171,6 +210,23 @@ module Homebrew
) )
end end
if dependants
Upgrade.upgrade_dependents(
dependants, formulae,
flags: args.flags_only,
force_bottle: args.force_bottle?,
build_from_source_formulae: args.build_from_source_formulae,
interactive: args.interactive?,
keep_tmp: args.keep_tmp?,
debug_symbols: args.debug_symbols?,
force: args.force?,
debug: args.debug?,
quiet: args.quiet?,
verbose: args.verbose?
)
end
end
if casks.any? if casks.any?
Install.ask_casks casks if args.ask? Install.ask_casks casks if args.ask?
Cask::Reinstall.reinstall_casks( Cask::Reinstall.reinstall_casks(

View File

@ -253,12 +253,7 @@ module Homebrew
verbose: args.verbose?, verbose: args.verbose?,
) )
formulae_dependencies = formulae_installer.flat_map do |f| formulae_dependencies = Install.get_hierarchy(formulae_installer, dependants)
[f.formula, f.compute_dependencies.flatten.filter do |c|
c.is_a? Dependency
end.flat_map(&:to_formula)]
end.flatten.uniq
formulae_dependencies.concat(dependants.upgradeable) if dependants
# Main block: if asking the user is enabled, show dependency and size information. # Main block: if asking the user is enabled, show dependency and size information.
Install.ask_formulae(formulae_dependencies, args: args) Install.ask_formulae(formulae_dependencies, args: args)
@ -273,7 +268,6 @@ module Homebrew
formulae_to_install, formulae_to_install,
flags: args.flags_only, flags: args.flags_only,
dry_run: args.dry_run?, dry_run: args.dry_run?,
ask: args.ask?,
force_bottle: args.force_bottle?, force_bottle: args.force_bottle?,
build_from_source_formulae: args.build_from_source_formulae, build_from_source_formulae: args.build_from_source_formulae,
interactive: args.interactive?, interactive: args.interactive?,

View File

@ -354,6 +354,16 @@ module Homebrew
puts formula_names.join(" ") puts formula_names.join(" ")
end end
def get_hierarchy(formulae_installer, dependants)
formulae_dependencies = formulae_installer.flat_map do |f|
[f.formula, f.compute_dependencies.flatten.filter do |c|
c.is_a? Dependency
end.flat_map(&:to_formula)]
end.flatten.uniq
formulae_dependencies.concat(dependants.upgradeable) if dependants&.upgradeable
formulae_dependencies
end
# If asking the user is enabled, show dependency and size information. # If asking the user is enabled, show dependency and size information.
def ask_formulae(formulae, args:) def ask_formulae(formulae, args:)
return if formulae.empty? return if formulae.empty?

View File

@ -7,7 +7,8 @@ require "messages"
module Homebrew module Homebrew
module Reinstall module Reinstall
def self.reinstall_formula( Formula_keg = Struct.new(:formula_installer, :keg, :formula, :options)
def self.get_formula_to_reinstall(
formula, formula,
flags:, flags:,
force_bottle: false, force_bottle: false,
@ -61,13 +62,35 @@ module Homebrew
verbose:, verbose:,
}.compact, }.compact,
) )
fi.prelude Formula_keg.new(fi, keg, formula, options)
fi.fetch end
def self.reinstall_formula(
formula_keg,
flags:,
force_bottle: false,
build_from_source_formulae: [],
interactive: false,
keep_tmp: false,
debug_symbols: false,
force: false,
debug: false,
quiet: false,
verbose: false,
git: false
)
formula_installer = formula_keg.formula_installer
keg = formula_keg.keg
formula = formula_keg.formula
options = formula_keg.options
link_keg = keg.linked?
formula_installer.prelude
formula_installer.fetch
oh1 "Reinstalling #{Formatter.identifier(formula.full_name)} #{options.to_a.join " "}" oh1 "Reinstalling #{Formatter.identifier(formula.full_name)} #{options.to_a.join " "}"
fi.install formula_installer.install
fi.finish formula_installer.finish
rescue FormulaInstallationAlreadyAttemptedError rescue FormulaInstallationAlreadyAttemptedError
nil nil
# Any other exceptions we want to restore the previous keg and report the error. # Any other exceptions we want to restore the previous keg and report the error.

View File

@ -439,7 +439,7 @@ module Homebrew
return if dry_run return if dry_run
reinstallable_broken_dependents.each do |formula| reinstallable_broken_dependents.each do |formula|
Reinstall.reinstall_formula( formula_installer = Reinstall.get_formula_to_reinstall(
formula, formula,
flags:, flags:,
force_bottle:, force_bottle:,
@ -452,6 +452,20 @@ module Homebrew
quiet:, quiet:,
verbose:, verbose:,
) )
Reinstall.reinstall_formula(
formula_installer,
flags: args.flags_only,
force_bottle: args.force_bottle?,
build_from_source_formulae: args.build_from_source_formulae,
interactive: args.interactive?,
keep_tmp: args.keep_tmp?,
debug_symbols: args.debug_symbols?,
force: args.force?,
debug: args.debug?,
quiet: args.quiet?,
verbose: args.verbose?,
git: args.git?,
)
rescue FormulaInstallationAlreadyAttemptedError rescue FormulaInstallationAlreadyAttemptedError
# We already attempted to reinstall f as part of the dependency tree of # We already attempted to reinstall f as part of the dependency tree of
# another formula. In that case, don't generate an error, just move on. # another formula. In that case, don't generate an error, just move on.