brew/Library/Homebrew/cmd/upgrade.rb

227 lines
8.4 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
2019-04-17 18:25:08 +09:00
require "cli/parser"
require "formula_installer"
require "install"
require "upgrade"
2020-07-06 12:49:20 -04:00
require "cask/cmd"
require "cask/utils"
require "cask/macos"
module Homebrew
2020-10-20 12:03:48 +02:00
extend T::Sig
2016-09-26 01:44:51 +02:00
module_function
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
def upgrade_args
Homebrew::CLI::Parser.new do
description <<~EOS
Upgrade outdated casks and outdated, unpinned formulae using the same options they were originally
installed with, plus any appended brew formula options. If <cask> or <formula> are specified,
upgrade only the given <cask> or <formula> kegs (unless they are pinned; see `pin`, `unpin`).
Unless `HOMEBREW_NO_INSTALL_CLEANUP` is set, `brew cleanup` will then be run for the
2019-08-06 13:23:19 -04:00
upgraded formulae or, every 30 days, for all formulae.
EOS
2020-07-30 18:40:10 +02:00
switch "-d", "--debug",
2019-04-30 08:44:35 +01:00
description: "If brewing fails, open an interactive debugging session with access to IRB "\
2019-08-06 14:22:24 -04:00
"or a shell inside the temporary build directory."
switch "-f", "--force",
description: "Install formulae without checking for previously installed keg-only or "\
"non-migrated versions. When installing casks, overwrite existing files "\
"(binaries and symlinks are excluded, unless originally from the same cask)."
2020-07-30 18:40:10 +02:00
switch "-v", "--verbose",
2019-04-30 08:44:35 +01:00
description: "Print the verification and postinstall steps."
switch "-n", "--dry-run",
2019-07-26 22:55:16 -07:00
description: "Show what would be upgraded, but do not actually upgrade anything."
[
[:switch, "--formula", "--formulae", {
2020-12-20 13:42:53 -05:00
description: "Treat all named arguments as formulae. If no named arguments " \
"are specified, upgrade only outdated formulae.",
}],
[:switch, "-s", "--build-from-source", {
description: "Compile <formula> from source even if a bottle is available.",
}],
[:switch, "-i", "--interactive", {
description: "Download and patch <formula>, then open a shell. This allows the user to "\
2020-11-12 10:40:41 -05:00
"run `./configure --help` and otherwise determine how to turn the software "\
"package into a Homebrew package.",
}],
[:switch, "--force-bottle", {
description: "Install from a bottle if it exists for the current or newest version of "\
"macOS, even if it would not normally be used for installation.",
}],
[:switch, "--fetch-HEAD", {
description: "Fetch the upstream repository to detect if the HEAD installation of the "\
"formula is outdated. Otherwise, the repository's HEAD will only be checked for "\
"updates when a new stable or development version has been released.",
}],
[:switch, "--ignore-pinned", {
description: "Set a successful exit status even if pinned formulae are not upgraded.",
}],
[:switch, "--keep-tmp", {
description: "Retain the temporary files created during installation.",
}],
[:switch, "--display-times", {
env: :display_install_times,
description: "Print install times for each formula at the end of the run.",
}],
].each do |options|
send(*options)
conflicts "--cask", options[-2]
end
formula_options
[
[:switch, "--cask", "--casks", {
description: "Treat all named arguments as casks. If no named arguments " \
"are specified, upgrade only outdated casks.",
}],
*Cask::Cmd::AbstractCommand::OPTIONS,
*Cask::Cmd::Upgrade::OPTIONS,
].each do |options|
send(*options)
conflicts "--formula", options[-2]
end
cask_options
conflicts "--build-from-source", "--force-bottle"
2021-01-10 14:26:40 -05:00
named_args [:outdated_formula, :outdated_cask]
end
end
sig { void }
def upgrade
args = upgrade_args.parse
2019-02-04 08:38:24 +01:00
2020-12-17 21:14:18 +09:00
formulae, casks = args.named.to_resolved_formulae_to_casks
# If one or more formulae are specified, but no casks were
# specified, we want to make note of that so we don't
# try to upgrade all outdated casks.
only_upgrade_formulae = formulae.present? && casks.blank?
only_upgrade_casks = casks.present? && formulae.blank?
display_messages = !only_upgrade_casks && upgrade_outdated_formulae(formulae, args: args)
force_caveats = !only_upgrade_formulae && upgrade_outdated_casks(casks, args: args)
return unless display_messages
Homebrew.messages.display_messages(force_caveats: force_caveats, display_times: args.display_times?)
end
sig { params(formulae: T::Array[Formula], args: CLI::Args).returns(T::Boolean) }
def upgrade_outdated_formulae(formulae, args:)
return false if args.cask?
if args.build_from_source? && !DevelopmentTools.installed?
raise BuildFlagsError.new(["--build-from-source"], bottled: formulae.all?(&:bottled?))
end
2020-07-27 11:37:07 +02:00
Install.perform_preinstall_checks
if formulae.blank?
outdated = Formula.installed.select do |f|
f.outdated?(fetch_head: args.fetch_HEAD?)
end
else
2020-07-06 12:49:20 -04:00
outdated, not_outdated = formulae.partition do |f|
f.outdated?(fetch_head: args.fetch_HEAD?)
end
2020-07-06 12:49:20 -04:00
not_outdated.each do |f|
2016-09-10 10:24:57 +01:00
versions = f.installed_kegs.map(&:version)
if versions.empty?
ofail "#{f.full_specified_name} not installed"
else
2015-11-29 15:37:06 +08:00
version = versions.max
opoo "#{f.full_specified_name} #{version} already installed"
end
end
end
return false if outdated.blank?
2020-07-06 12:49:20 -04:00
pinned = outdated.select(&:pinned?)
outdated -= pinned
formulae_to_install = outdated.map do |f|
f_latest = f.latest_formula
if f_latest.latest_version_installed?
f
else
f_latest
end
end
if !pinned.empty? && !args.ignore_pinned?
ofail "Not upgrading #{pinned.count} pinned #{"package".pluralize(pinned.count)}:"
puts pinned.map { |f| "#{f.full_specified_name} #{f.pkg_version}" } * ", "
end
if formulae_to_install.empty?
2016-09-10 10:24:57 +01:00
oh1 "No packages to upgrade"
else
2019-07-28 14:50:59 +01:00
verb = args.dry_run? ? "Would upgrade" : "Upgrading"
oh1 "#{verb} #{formulae_to_install.count} outdated #{"package".pluralize(formulae_to_install.count)}:"
formulae_upgrades = formulae_to_install.map do |f|
if f.optlinked?
"#{f.full_specified_name} #{Keg.new(f.opt_prefix).version} -> #{f.pkg_version}"
else
"#{f.full_specified_name} #{f.pkg_version}"
end
end
puts formulae_upgrades.join("\n")
end
2021-03-18 14:46:48 +00:00
Upgrade.upgrade_formulae(
formulae_to_install,
flags: args.flags_only,
installed_on_request: args.named.present?,
force_bottle: args.force_bottle?,
build_from_source_formulae: args.build_from_source_formulae,
interactive: args.interactive?,
keep_tmp: args.keep_tmp?,
force: args.force?,
debug: args.debug?,
quiet: args.quiet?,
verbose: args.verbose?,
)
2021-03-18 14:46:48 +00:00
Upgrade.check_installed_dependents(
formulae_to_install,
flags: args.flags_only,
dry_run: args.dry_run?,
installed_on_request: args.named.present?,
force_bottle: args.force_bottle?,
build_from_source_formulae: args.build_from_source_formulae,
interactive: args.interactive?,
keep_tmp: args.keep_tmp?,
force: args.force?,
debug: args.debug?,
quiet: args.quiet?,
verbose: args.verbose?,
)
true
end
2020-07-06 12:49:20 -04:00
sig { params(casks: T::Array[Cask::Cask], args: CLI::Args).returns(T::Boolean) }
2020-07-30 18:40:10 +02:00
def upgrade_outdated_casks(casks, args:)
return false if args.formula?
2020-08-05 11:50:01 +02:00
Cask::Cmd::Upgrade.upgrade_casks(
*casks,
force: args.force?,
greedy: args.greedy?,
dry_run: args.dry_run?,
binaries: args.binaries?,
quarantine: args.quarantine?,
require_sha: args.require_sha?,
2020-08-05 11:50:01 +02:00
skip_cask_deps: args.skip_cask_deps?,
verbose: args.verbose?,
args: args,
2020-08-05 11:50:01 +02:00
)
2020-07-06 12:49:20 -04:00
end
end