Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Wait for Homebrew's apps to be known before uninstalling or upgrading a cask

Țugui Dragoș82c141197a0c
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index 31543e28..e5afe2b4 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -823,11 +823,6 @@ final class AppLibrary {
defaults.set(skippedVersions, forKey: SettingsKey.skippedUpdateVersions)
}
- /// Called often, including every time the Applications list appears. Apps are matched to casks again only
- /// when the casks changed, since `adopt` already does it when the apps change, and matching holds the
- /// main thread.
- /// `knowsItsOwnApps` is true when `packages` says of every app whether Homebrew installed it, and not while
- /// Homebrew could not be read.
struct Upgrade {
/// The app as the upgrade left it.
let app: InstalledApp
@@ -843,6 +838,15 @@ final class AppLibrary {
upgrades[app.id] = upgrade
}
+ /// True once casks came from a reading of Homebrew that says of every app whether it installed it.
+ private(set) var hasMatchedHomebrew = false
+ /// Whether `cask(for:)` answers for every app listed: the apps were read, and matched to such a reading. Until
+ /// then an app Homebrew installed may still look like one it didn't.
+ var knowsHomebrewsApps: Bool { hasLoaded && hasMatchedHomebrew }
+
+ /// Called whenever Homebrew is read again. Apps are matched to casks again only when the casks changed, since
+ /// `adopt` already does it when the apps change, and matching holds the main thread. `knowsItsOwnApps` is true
+ /// when `packages` says of every app whether Homebrew installed it, and not while Homebrew could not be read.
func loadHomebrewCasks(_ packages: [HomebrewPackage], knowsItsOwnApps: Bool) {
let casks = packages.filter { $0.kind == .cask }
if casks != self.casks {
@@ -851,6 +855,7 @@ final class AppLibrary {
}
if knowsItsOwnApps {
forgetUpdatesHomebrewNoLongerOffers()
+ hasMatchedHomebrew = true
}
applyHomebrewStatuses()
}
diff --git a/Peel/Views/Homebrew/HomebrewDetailView.swift b/Peel/Views/Homebrew/HomebrewDetailView.swift
index 7b11a250..dd28b44c 100644
--- a/Peel/Views/Homebrew/HomebrewDetailView.swift
+++ b/Peel/Views/Homebrew/HomebrewDetailView.swift
@@ -133,7 +133,8 @@ struct HomebrewDetailView: View {
isConfirmingUpgrade = true
}
.buttonStyle(.borderedProminent)
- .disabled(package.isPinned)
+ // A cask's apps, which have to quit first, are known only once they are matched.
+ .disabled(package.isPinned || package.kind == .cask && !library.knowsHomebrewsApps)
}
}
if isRunning {
@@ -261,6 +262,8 @@ struct HomebrewDetailView: View {
private var uninstallRefusal: LocalizedStringResource? {
if exclusions.exclusions.isUnreadable { return "Exclusions can’t be read: see Settings" }
if !exclusions.exclusions.hasBeenRead { return "Checking…" }
+ // Until then the cask's apps aren't known, and with them neither are the rules their own pages follow.
+ if !library.knowsHomebrewsApps { return "Checking…" }
let apps = caskApps
// Homebrew deletes the app, so an app its own page never removes is never uninstalled from here either.
if let removedElsewhere = apps.lazy.compactMap(\.removedElsewhere).first { return removedElsewhere.explanation }
diff --git a/Peel/Views/Homebrew/HomebrewList.swift b/Peel/Views/Homebrew/HomebrewList.swift
index 9d7bff23..8eb75ad4 100644
--- a/Peel/Views/Homebrew/HomebrewList.swift
+++ b/Peel/Views/Homebrew/HomebrewList.swift
@@ -426,7 +426,10 @@ struct HomebrewList: View {
.minimumTarget()
}
.buttonStyle(.borderless)
- .disabled(homebrew.upgradable.isEmpty || isBusy || quittingToUpgrade.isWaiting)
+ .disabled(
+ homebrew.upgradable.isEmpty || isBusy || quittingToUpgrade.isWaiting
+ || homebrew.upgradable.contains { $0.kind == .cask } && !library.knowsHomebrewsApps
+ )
}
}
}
diff --git a/PeelTests/AppLibraryTests.swift b/PeelTests/AppLibraryTests.swift
index fd212607..504fa77b 100644
--- a/PeelTests/AppLibraryTests.swift
+++ b/PeelTests/AppLibraryTests.swift
@@ -301,6 +301,21 @@ private nonisolated final class TestClock: Sendable {
#expect(library.shownUpdateStatus(of: app)?.version == "1.1")
}
+ @Test func knowsHomebrewsAppsOnlyOnceAReadingKnowsThemAll() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let made = try MadeUpDefaults()
+ defer { made.forget() }
+ let library = library(with: made.defaults, notesIn: folder)
+
+ #expect(!library.hasMatchedHomebrew)
+ library.loadHomebrewCasks([cask("Tool", checked: homebrewChecked)], knowsItsOwnApps: false)
+ #expect(!library.hasMatchedHomebrew, "a reading that couldn't say of every app whether Homebrew installed it")
+ library.loadHomebrewCasks([cask("Tool", checked: homebrewChecked)], knowsItsOwnApps: true)
+ #expect(library.hasMatchedHomebrew)
+ #expect(!library.knowsHomebrewsApps, "the apps themselves aren't read yet")
+ }
+
@Test func showsWhatHomebrewSaysOfAnAppWithNoFeedAsSoonAsHomebrewIsRead() async throws {
let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
let bundle = try caskApp("Tool", in: folder)