2025-07-24 09:42:11 +01:00
|
|
|
# typed: strict
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-12 19:28:02 +00:00
|
|
|
require "development_tools"
|
|
|
|
require "messages"
|
2025-08-20 19:20:19 +01:00
|
|
|
require "utils/output"
|
2018-09-12 19:28:02 +00:00
|
|
|
|
2025-07-24 09:42:11 +01:00
|
|
|
# Needed to handle circular require dependency.
|
|
|
|
# rubocop:disable Lint/EmptyClass
|
|
|
|
class FormulaInstaller; end
|
|
|
|
# rubocop:enable Lint/EmptyClass
|
|
|
|
|
2018-09-12 19:28:02 +00:00
|
|
|
module Homebrew
|
2024-08-20 03:58:01 +01:00
|
|
|
module Reinstall
|
2025-08-20 19:20:19 +01:00
|
|
|
extend Utils::Output::Mixin
|
|
|
|
|
2025-07-24 09:42:11 +01:00
|
|
|
class InstallationContext < T::Struct
|
|
|
|
const :formula_installer, ::FormulaInstaller
|
|
|
|
const :keg, T.nilable(Keg)
|
|
|
|
const :formula, Formula
|
|
|
|
const :options, Options
|
|
|
|
end
|
2024-08-20 03:58:01 +01:00
|
|
|
|
2025-07-24 09:42:11 +01:00
|
|
|
class << self
|
|
|
|
sig {
|
|
|
|
params(
|
|
|
|
formula: Formula, flags: T::Array[String], force_bottle: T::Boolean,
|
|
|
|
build_from_source_formulae: T::Array[String], interactive: T::Boolean, keep_tmp: T::Boolean,
|
|
|
|
debug_symbols: T::Boolean, force: T::Boolean, debug: T::Boolean, quiet: T::Boolean,
|
|
|
|
verbose: T::Boolean, git: T::Boolean
|
|
|
|
).returns(InstallationContext)
|
|
|
|
}
|
|
|
|
def build_install_context(
|
2024-08-20 03:58:01 +01:00
|
|
|
formula,
|
2025-07-24 09:42:11 +01:00
|
|
|
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
|
2024-08-20 03:58:01 +01:00
|
|
|
)
|
2025-07-24 09:42:11 +01:00
|
|
|
if formula.opt_prefix.directory?
|
|
|
|
keg = Keg.new(formula.opt_prefix.resolved_path)
|
|
|
|
tab = keg.tab
|
|
|
|
link_keg = keg.linked?
|
|
|
|
installed_as_dependency = tab.installed_as_dependency == true
|
|
|
|
installed_on_request = tab.installed_on_request == true
|
|
|
|
build_bottle = tab.built_bottle?
|
|
|
|
backup keg
|
|
|
|
else
|
|
|
|
link_keg = nil
|
|
|
|
installed_as_dependency = false
|
|
|
|
installed_on_request = true
|
|
|
|
build_bottle = false
|
|
|
|
end
|
|
|
|
|
|
|
|
build_options = BuildOptions.new(Options.create(flags), formula.options)
|
|
|
|
options = build_options.used_options
|
|
|
|
options |= formula.build.used_options
|
|
|
|
options &= formula.options
|
2025-06-09 00:14:16 -04:00
|
|
|
|
2025-07-24 09:42:11 +01:00
|
|
|
formula_installer = FormulaInstaller.new(
|
|
|
|
formula,
|
|
|
|
**{
|
|
|
|
options:,
|
|
|
|
link_keg:,
|
|
|
|
installed_as_dependency:,
|
|
|
|
installed_on_request:,
|
|
|
|
build_bottle:,
|
|
|
|
force_bottle:,
|
|
|
|
build_from_source_formulae:,
|
|
|
|
git:,
|
|
|
|
interactive:,
|
|
|
|
keep_tmp:,
|
|
|
|
debug_symbols:,
|
|
|
|
force:,
|
|
|
|
debug:,
|
|
|
|
quiet:,
|
|
|
|
verbose:,
|
|
|
|
}.compact,
|
|
|
|
)
|
|
|
|
InstallationContext.new(formula_installer:, keg:, formula:, options:)
|
2024-08-20 03:58:01 +01:00
|
|
|
end
|
2018-09-12 19:28:02 +00:00
|
|
|
|
2025-07-24 09:42:11 +01:00
|
|
|
sig { params(install_context: InstallationContext).void }
|
|
|
|
def reinstall_formula(install_context)
|
|
|
|
formula_installer = install_context.formula_installer
|
|
|
|
keg = install_context.keg
|
|
|
|
formula = install_context.formula
|
|
|
|
options = install_context.options
|
|
|
|
link_keg = keg&.linked?
|
|
|
|
verbose = formula_installer.verbose?
|
|
|
|
|
|
|
|
oh1 "Reinstalling #{Formatter.identifier(formula.full_name)} #{options.to_a.join " "}"
|
|
|
|
|
|
|
|
formula_installer.install
|
|
|
|
formula_installer.finish
|
|
|
|
rescue FormulaInstallationAlreadyAttemptedError
|
|
|
|
nil
|
|
|
|
# Any other exceptions we want to restore the previous keg and report the error.
|
|
|
|
rescue Exception # rubocop:disable Lint/RescueException
|
|
|
|
ignore_interrupts { restore_backup(keg, link_keg, verbose:) if keg }
|
|
|
|
raise
|
|
|
|
else
|
|
|
|
if keg
|
|
|
|
backup_keg = backup_path(keg)
|
|
|
|
begin
|
|
|
|
FileUtils.rm_r(backup_keg) if backup_keg.exist?
|
|
|
|
rescue Errno::EACCES, Errno::ENOTEMPTY
|
|
|
|
odie <<~EOS
|
|
|
|
Could not remove #{backup_keg.parent.basename} backup keg! Do so manually:
|
|
|
|
sudo rm -rf #{backup_keg}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
2024-08-20 03:58:01 +01:00
|
|
|
end
|
2018-09-12 19:28:02 +00:00
|
|
|
|
2025-07-24 09:42:11 +01:00
|
|
|
private
|
2018-09-12 19:28:02 +00:00
|
|
|
|
2025-07-24 09:42:11 +01:00
|
|
|
sig { params(keg: Keg).void }
|
|
|
|
def backup(keg)
|
|
|
|
keg.unlink
|
|
|
|
begin
|
|
|
|
keg.rename backup_path(keg)
|
|
|
|
rescue Errno::EACCES, Errno::ENOTEMPTY
|
|
|
|
odie <<~EOS
|
|
|
|
Could not rename #{keg.name} keg! Check/fix its permissions:
|
|
|
|
sudo chown -R #{ENV.fetch("USER", "$(whoami)")} #{keg}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
2018-09-12 19:28:02 +00:00
|
|
|
|
2025-07-24 09:42:11 +01:00
|
|
|
sig { params(keg: Keg, keg_was_linked: T::Boolean, verbose: T::Boolean).void }
|
|
|
|
def restore_backup(keg, keg_was_linked, verbose:)
|
|
|
|
path = backup_path(keg)
|
2018-09-12 19:28:02 +00:00
|
|
|
|
2025-07-24 09:42:11 +01:00
|
|
|
return unless path.directory?
|
2018-09-12 19:28:02 +00:00
|
|
|
|
2025-07-24 09:42:11 +01:00
|
|
|
FileUtils.rm_r(Pathname.new(keg)) if keg.exist?
|
|
|
|
|
|
|
|
path.rename keg.to_s
|
|
|
|
keg.link(verbose:) if keg_was_linked
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { params(keg: Keg).returns(Pathname) }
|
|
|
|
def backup_path(keg)
|
|
|
|
Pathname.new "#{keg}.reinstall"
|
|
|
|
end
|
2024-08-20 03:58:01 +01:00
|
|
|
end
|
2018-09-12 19:28:02 +00:00
|
|
|
end
|
|
|
|
end
|