Skip to content

Commit 86cdd0b

Browse files
committed
cask/upgrade: don't uninstall before upgrade
1 parent 9991345 commit 86cdd0b

File tree

10 files changed

+53
-4
lines changed

10 files changed

+53
-4
lines changed

Library/Homebrew/cask/cask.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ def to_h
423423
"disable_reason" => disable_reason,
424424
"disable_replacement_formula" => disable_replacement_formula,
425425
"disable_replacement_cask" => disable_replacement_cask,
426+
"uninstall_on_upgrade" => uninstall_on_upgrade?,
426427
"tap_git_head" => tap_git_head,
427428
"languages" => languages,
428429
"ruby_source_path" => ruby_source_path,

Library/Homebrew/cask/cask_loader.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ def load(config:)
434434
container(**container_hash)
435435
end
436436

437+
uninstall_on_upgrade! if json_cask[:uninstall_on_upgrade]
438+
437439
json_cask[:artifacts]&.each do |artifact|
438440
# convert generic string replacements into actual ones
439441
artifact = cask.loader.from_h_gsubs(artifact, appdir)

Library/Homebrew/cask/dsl.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class DSL
8888
:rename,
8989
:sha256,
9090
:staged_path,
91+
:uninstall_on_upgrade!,
92+
:uninstall_on_upgrade?,
9193
:url,
9294
:version,
9395
:appdir,
@@ -143,6 +145,7 @@ def initialize(cask)
143145
@container_set_in_block = T.let(false, T::Boolean)
144146
@depends_on = T.let(DSL::DependsOn.new, DSL::DependsOn)
145147
@depends_on_set_in_block = T.let(false, T::Boolean)
148+
@uninstall_on_upgrade = T.let(false, T::Boolean)
146149
@deprecated = T.let(false, T::Boolean)
147150
@deprecation_date = T.let(nil, T.nilable(Date))
148151
@deprecation_reason = T.let(nil, T.nilable(T.any(String, Symbol)))
@@ -559,6 +562,19 @@ def auto_updates(auto_updates = nil)
559562
set_unique_stanza(:auto_updates, auto_updates.nil?) { auto_updates }
560563
end
561564

565+
# Forces the uninstall to run during upgrades and reinstalls even when
566+
# `HOMEBREW_NO_UNINSTALL_ON_CASK_UPGRADE` is set.
567+
#
568+
# @api public
569+
def uninstall_on_upgrade!
570+
@uninstall_on_upgrade = true
571+
end
572+
573+
# Is uninstall_on_upgrade! method defined?
574+
def uninstall_on_upgrade?
575+
@uninstall_on_upgrade == true
576+
end
577+
562578
# Automatically fetch the latest version of a cask from changelogs.
563579
#
564580
# @api public

Library/Homebrew/cask/installer.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ def require_sha? = @require_sha
8383
sig { returns(T::Boolean) }
8484
def skip_cask_deps? = @skip_cask_deps
8585

86+
sig { returns(T::Boolean) }
87+
def skip_uninstall?
88+
return false unless Homebrew::EnvConfig.no_uninstall_on_cask_upgrade?
89+
return false if @cask.uninstall_on_upgrade?
90+
return false if !reinstall? && !upgrade?
91+
92+
true
93+
end
94+
8695
sig { returns(T::Boolean) }
8796
def upgrade? = @upgrade
8897

@@ -222,6 +231,9 @@ def check_conflicts
222231

223232
sig { void }
224233
def uninstall_existing_cask
234+
return if skip_uninstall?
235+
236+
odie "FAIL"
225237
return unless @cask.installed?
226238

227239
# Always force uninstallation, ignore method parameter
@@ -342,7 +354,7 @@ def install_artifacts(predecessor: nil)
342354

343355
artifact.install_phase(
344356
command: @command, verbose: verbose?, adopt: adopt?, auto_updates: @cask.auto_updates,
345-
force: force?, predecessor:
357+
force: force? || skip_uninstall?, predecessor:
346358
)
347359
already_installed_artifacts.unshift(artifact)
348360
end
@@ -552,7 +564,7 @@ def remove_download_sha
552564

553565
sig { params(successor: T.nilable(Cask)).void }
554566
def start_upgrade(successor:)
555-
uninstall_artifacts(successor:)
567+
uninstall_artifacts(successor:) unless skip_uninstall?
556568
backup
557569
end
558570

Library/Homebrew/env_config.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ module EnvConfig
444444
"outdated.",
445445
boolean: true,
446446
},
447+
HOMEBREW_NO_UNINSTALL_ON_CASK_UPGRADE: {
448+
description: "If set, `brew upgrade` will not run the uninstall script during cask upgrades and will " \
449+
"install over the top instead.",
450+
boolean: true,
451+
},
447452
HOMEBREW_NO_UPDATE_REPORT_NEW: {
448453
description: "If set, `brew update` will not show the list of newly added formulae/casks.",
449454
boolean: true,

Library/Homebrew/sorbet/rbi/dsl/cask/cask.rbi

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Library/Homebrew/sorbet/rbi/dsl/homebrew/env_config.rbi

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Library/Homebrew/test/support/fixtures/cask/Casks/everything.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
rename "Foobar.app", "Foo.app"
2727
rename "Foo.app", "Bar.app"
2828

29+
uninstall_on_upgrade!
30+
2931
app "Everything.app"
3032
installer script: {
3133
executable: "~/just/another/path/install.sh",

Library/Homebrew/test/support/fixtures/cask/everything-with-variations.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,15 @@
115115
"disable_reason": null,
116116
"disable_replacement_formula": null,
117117
"disable_replacement_cask": null,
118+
"uninstall_on_upgrade": true,
118119
"tap_git_head": "abcdef1234567890abcdef1234567890abcdef12",
119120
"languages": [
120121
"en",
121122
"eo"
122123
],
123124
"ruby_source_path": "Casks/everything-with-variations.rb",
124125
"ruby_source_checksum": {
125-
"sha256": "d3c19b564ee5a17f22191599ad795a6cc9c4758d0e1269f2d13207155b378dea"
126+
"sha256": "d4799fe58288f669de4688e57eb9f568b7c48d04abddfaaf8aa0a21beba0f2d3"
126127
},
127128
"variations": {
128129
"arm64_monterey": {

Library/Homebrew/test/support/fixtures/cask/everything.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,14 @@
115115
"disable_reason": null,
116116
"disable_replacement_formula": null,
117117
"disable_replacement_cask": null,
118+
"uninstall_on_upgrade": true,
118119
"tap_git_head": "abcdef1234567890abcdef1234567890abcdef12",
119120
"languages": [
120121
"en",
121122
"eo"
122123
],
123124
"ruby_source_path": "Casks/everything.rb",
124125
"ruby_source_checksum": {
125-
"sha256": "d3c19b564ee5a17f22191599ad795a6cc9c4758d0e1269f2d13207155b378dea"
126+
"sha256": "d4799fe58288f669de4688e57eb9f568b7c48d04abddfaaf8aa0a21beba0f2d3"
126127
}
127128
}

0 commit comments

Comments
 (0)