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.

Peel forgets Homebrew once it is uninstalled

Țugui Dragoș6c63f2931c98
diff --git a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateFeed.swift b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateFeed.swift
index 00ed84a0..841a8cf9 100644
--- a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateFeed.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateFeed.swift
@@ -50,6 +50,12 @@ public enum UpdateStatus: Sendable, Hashable, Codable {
public var source: UpdateSource? {
if case .updateAvailable(_, let source, _) = self { source } else { nil }
}
+
+ /// Whether this answer still holds for an app, given whether Homebrew counts the app as its own now: an update
+ /// Homebrew reported is about its cask, and says nothing once the app is not Homebrew's.
+ public func holds(whileHomebrews isHomebrews: Bool) -> Bool {
+ source != .homebrew || isHomebrews
+ }
}
extension UpdateFeed {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
index f682c0c8..76ce01e5 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
@@ -471,6 +471,17 @@ struct UpdateSourceTests {
#expect(CaskEvidence.cask(for: firefox, in: casks)?.name == "firefox@esr")
}
+ @Test func anUpdateHomebrewReportedHoldsOnlyWhileTheAppIsHomebrews() {
+ let homebrew = UpdateStatus.updateAvailable(version: "2.0", source: .homebrew)
+ #expect(homebrew.holds(whileHomebrews: true))
+ #expect(!homebrew.holds(whileHomebrews: false))
+
+ #expect(UpdateStatus.updateAvailable(version: "2.0", source: .developer).holds(whileHomebrews: false))
+ #expect(UpdateStatus.updateAvailable(version: "2.0", source: .appStore).holds(whileHomebrews: false))
+ #expect(UpdateStatus.upToDate.holds(whileHomebrews: false))
+ #expect(UpdateStatus.failed.holds(whileHomebrews: false))
+ }
+
@Test func showsAHomebrewVersionWithoutWhatTheDownloadNeeds() {
let cask = UpdateStatus.updateAvailable(version: "26.10.22,802", source: .homebrew)
#expect(cask.displayVersion == "26.10.22")
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index e5923b96..ebe2dd8f 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -622,15 +622,33 @@ final class AppLibrary {
/// 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.
- func loadHomebrewCasks(_ packages: [HomebrewPackage]) {
+ /// `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 {
self.casks = casks
matchHomebrewApps()
}
+ if knowsItsOwnApps {
+ forgetUpdatesHomebrewNoLongerOffers()
+ }
applyHomebrewStatuses()
}
+ /// Forgets an update Homebrew reported for an app it no longer counts as its own, saved answer included, so the
+ /// app is due and its own feed answers at the next round.
+ private func forgetUpdatesHomebrewNoLongerOffers() {
+ let outlived = Set(updateStatuses.filter { !$0.value.holds(whileHomebrews: homebrewApps[$0.key] != nil) }.keys)
+ guard !outlived.isEmpty else { return }
+ updateStatuses = updateStatuses.filter { !outlived.contains($0.key) }
+ lastUpdateChecks = lastUpdateChecks.filter { !outlived.contains($0.key) }
+ for app in apps where outlived.contains(app.id) {
+ memory[app.bundleIdentifier] = nil
+ }
+ UpdateMemoryStore.save(memory)
+ }
+
/// Records an update for each app whose cask Homebrew marks outdated. Reading Homebrew costs no network
/// request, so this doesn't wait for the update schedule, and Applications agrees with the Homebrew page.
/// It applies only to an app with no answer of its own, or whose own check failed or had no feed to ask:
diff --git a/Peel/Model/FoundLastTimeStore.swift b/Peel/Model/FoundLastTimeStore.swift
index 516e2295..4dc4b0ff 100644
--- a/Peel/Model/FoundLastTimeStore.swift
+++ b/Peel/Model/FoundLastTimeStore.swift
@@ -55,7 +55,9 @@ extension IntelLibrary {
extension HomebrewLibrary {
var looked: Looked? {
- packages.map { Looked(count: $0.count(where: \.isOutdated), size: nil) }
+ // With no Homebrew on the Mac, none of its updates is waiting.
+ guard isInstalled else { return Looked(count: 0, size: nil) }
+ return packages.map { Looked(count: $0.count(where: \.isOutdated), size: nil) }
}
}
diff --git a/Peel/Model/HomebrewLibrary.swift b/Peel/Model/HomebrewLibrary.swift
index 9d8ee0e8..3905e1b2 100644
--- a/Peel/Model/HomebrewLibrary.swift
+++ b/Peel/Model/HomebrewLibrary.swift
@@ -73,9 +73,11 @@ final class HomebrewLibrary {
CaskEvidence.combined(installed: packages ?? [], known: knownCasks, receipts: receipts)
}
- /// Incremented only when the known casks or receipts change. An app's page scans again when it does, so a
- /// page opened before the evidence arrived picks it up, and a page the user is working on isn't scanned
- /// again for nothing.
+ var knowsItsOwnApps: Bool { !isInstalled || hasAnswered }
+
+ /// Incremented only when the known casks or receipts change, or Homebrew leaves the Mac. An app's page scans
+ /// again when it does, so a page opened before the evidence arrived picks it up, and a page the user is working
+ /// on isn't scanned again for nothing.
private(set) var evidenceRevision = 0
func loadKnownCasks(for apps: [InstalledApp]) async {
@@ -131,8 +133,13 @@ final class HomebrewLibrary {
/// `/Applications`.
func refresh(includingReclaimable: Bool = false) async {
isInstalled = Homebrew.executableURL != nil
- guard isInstalled else { return }
- guard let reading = await scanRun.run({ await self.read(includingReclaimable: includingReclaimable) }) else { return }
+ guard isInstalled else {
+ forgetHomebrew()
+ return
+ }
+ guard let reading = await scanRun.run({ await self.read(includingReclaimable: includingReclaimable) }),
+ isInstalled
+ else { return }
guard let (found, installed, preview, overridden) = reading else {
needsDefinitions = true
hasAnswered = false
@@ -164,6 +171,20 @@ final class HomebrewLibrary {
}
}
+ /// With no Homebrew on the Mac, nothing it said still holds, so the library becomes what it is when Peel starts
+ /// without one. The installer receipts stay: they are the Mac's, and an uninstall reads them on their own.
+ private func forgetHomebrew() {
+ guard packages != nil || installation != nil || !knownCasks.isEmpty else { return }
+ let hadEvidence = !caskEvidence.isEmpty
+ (packages, installation, hasAnswered, couldNotRead, needsDefinitions) = (nil, nil, false, nil, false)
+ (reclaimable, autoremovable, overrides, findings, advisories) = (nil, [], [], nil, nil)
+ (knownCasks, selection) = ([], nil)
+ revision += 1
+ if hadEvidence {
+ evidenceRevision += 1
+ }
+ }
+
/// Nil while Homebrew has no local definitions to read, as when `brew update` has never run.
private func read(
includingReclaimable: Bool
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index 26a80eb1..f5c00763 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -134,7 +134,7 @@ struct PeelApp: App {
await homebrew.refresh()
// A newly installed app may match a cask Homebrew knows about, so the known casks are read again.
await homebrew.loadKnownCasks(for: library.apps)
- library.loadHomebrewCasks(homebrew.caskEvidence)
+ library.loadHomebrewCasks(homebrew.caskEvidence, knowsItsOwnApps: homebrew.knowsItsOwnApps)
// A bundle that changed on disk is a different build, and what was cached about it describes the old
// one. Those apps are checked again at once, whoever updated them: Peel, the Homebrew page, or
// `brew upgrade` in Terminal.
@@ -339,7 +339,7 @@ struct PeelApp: App {
Task { await IconCache.warm(library.apps.map(\.url)) }
await homebrewPackages
await Marks.interval("Known casks") { await homebrew.loadKnownCasks(for: library.apps) }
- library.loadHomebrewCasks(homebrew.caskEvidence)
+ library.loadHomebrewCasks(homebrew.caskEvidence, knowsItsOwnApps: homebrew.knowsItsOwnApps)
await Marks.interval("Signing teams") { await library.checkSigningTeams() }
if checksForAppUpdates {
await Marks.interval("Update checks") { await checkForUpdates() }
diff --git a/Peel/Views/AppDetailView.swift b/Peel/Views/AppDetailView.swift
index b8bfdab1..ef1939e8 100644
--- a/Peel/Views/AppDetailView.swift
+++ b/Peel/Views/AppDetailView.swift
@@ -508,7 +508,7 @@ struct AppDetailView: View {
// Homebrew's own answer is read again first: it is what says whether a version is behind, and the
// copy from before the upgrade still said this one was.
await library.refresh()
- library.loadHomebrewCasks(homebrew.caskEvidence)
+ library.loadHomebrewCasks(homebrew.caskEvidence, knowsItsOwnApps: homebrew.knowsItsOwnApps)
if let installed = library.apps.first(where: { $0.id == plan.app.id }) {
plan = RemovalPlan(app: installed)
await plan.refresh(installedApps: library.apps, canUseHelper: helper.canAct, casks: homebrew.caskEvidence, receipts: homebrew.receipts)
diff --git a/Peel/Views/AppList.swift b/Peel/Views/AppList.swift
index 60b2b26e..16157ac2 100644
--- a/Peel/Views/AppList.swift
+++ b/Peel/Views/AppList.swift
@@ -86,7 +86,7 @@ struct AppList: View {
await library.loadSizes()
}
.task(id: homebrew.revision) {
- library.loadHomebrewCasks(homebrew.caskEvidence)
+ library.loadHomebrewCasks(homebrew.caskEvidence, knowsItsOwnApps: homebrew.knowsItsOwnApps)
}
}