Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Keep Homebrew from upgrading what you excluded
Țugui Dragoș35b57c544a5c
diff --git a/GUIDE.md b/GUIDE.mdindex ddd9feff..b91ecea3 100644--- a/GUIDE.md+++ b/GUIDE.md@@ -282,7 +282,8 @@ and how many items the others hold.every download), Repair Taps, Check Health, Scan for Vulnerabilities, Upgrade, and Uninstall, or the command to run inTerminal for a cask that asks for an administrator's password. Uninstall and the cleanups delete for good, as Homebrewdoes, and Peel says so before you confirm them. For a cask whose app Peel leaves alone on the app's own page, such as- one with a system extension, and for a package you excluded, Uninstall stays off, with the reason beside it. Homebrew+ one with a system extension, and for a package you excluded, Uninstall stays off, with the reason beside it, and+ Upgrade stays off for a package you excluded, since an upgrade deletes the version it replaces. Homebrewreplaces or deletes a cask's app even while it runs, so Peel asks the app to quit first, and opens it again after anupgrade. A cask Homebrew still lists though its app isn't where Homebrew put it anymore, or, for a cask with no app,its packages are gone, can't be upgraded. It is listed apart under Apps Already Gone, where Forget Cask, or Forgetdiff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swiftindex f7211a60..9a0cf875 100644--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift@@ -256,10 +256,10 @@ public struct Exclusions: Sendable, Codable, Hashable {app.bundleIdentifier.map(excludes(bundleIdentifier:)) == true || excludes(app.url)}- /// Whether `brew uninstall` would delete something excluded: an app the package installs (`apps` are the- /// installed apps known to be its), an app identifier it quits, or its own folder in the installation's Caskroom- /// or Cellar. Nil when one of these can't be looked into for an excluded app. An unreadable list, or one not read- /// yet, counts as excluding every package, since `brew uninstall` cannot be undone.+ /// Whether `brew uninstall`, or an upgrade, which deletes the version it replaces, would delete something excluded:+ /// an app the package installs (`apps` are the installed apps known to be its), an app identifier it quits, or its+ /// own folder in the installation's Caskroom or Cellar. Nil when one of these can't be looked into for an excluded+ /// app. An unreadable list, or one not read yet, counts as excluding every package, since neither can be undone.public func excludes(_ package: HomebrewPackage,apps: [InstalledApp],@@ -285,6 +285,14 @@ public struct Exclusions: Sendable, Codable, Hashable {return isKnownToHoldNothing ? false : nil}+ public func joiningUpgradeAll(+ _ outdated: [HomebrewPackage],+ appsOf: (HomebrewPackage) -> [InstalledApp],+ installation: HomebrewInstallation?+ ) -> [HomebrewPackage] {+ outdated.filter { $0.joinsUpgradeAll && excludes($0, apps: appsOf($0), installation: installation) == false }+ }+/// The installed formulae whose own folder in `cellar` is excluded or holds something excluded, which Homebrew/// is told to keep: `brew cleanup`, and the autoremove `brew uninstall` runs, delete formulae and their old/// versions for good. An unreadable list, or one not read yet, keeps every formula. Nil when the Cellar, or adiff --git a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swiftindex 87a08fd2..1e18221d 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift@@ -68,6 +68,27 @@ struct HomebrewTests {#expect(UpdateChecker.homebrewStatus(of: package(.disabled), appVersion: "1.0", at: .now) == nil)}+ @Test func anExcludedPackageIsLeftOutOfUpgradeAll() throws {+ let directory = try TemporaryDirectory()+ let app = try directory.directory("Applications/Sample.app")+ try directory.file("Applications/Other.app/Contents/Info.plist")+ func cask(_ name: String) -> HomebrewPackage {+ HomebrewPackage(name: name, kind: .cask, installedVersion: "1.0", latestVersion: "2.0", isOutdated: true)+ }+ let sample = InstalledApp(url: app, bundleIdentifier: "org.example.sample", name: "Sample")+ let casks = [cask("sample"), cask("other")]+ let appsOf = { (package: HomebrewPackage) in package.name == "sample" ? [sample] : [] }+ let names = { (exclusions: Exclusions) in+ exclusions.joiningUpgradeAll(casks, appsOf: appsOf, installation: nil).map(\.name)+ }++ #expect(names(Exclusions()) == ["sample", "other"])+ #expect(names(Exclusions(paths: [app])) == ["other"])+ #expect(names(Exclusions(bundleIdentifiers: ["org.example.sample"])) == ["other"])+ #expect(names(.unreadable).isEmpty)+ #expect(names(.notYetRead).isEmpty)+ }+@Test func peelsOwnCaskIsLeftToPeelsOwnUpdates() {func cask(quitting identifiers: [String] = [], apps: [String] = []) -> HomebrewPackage {HomebrewPackage(diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swiftindex 3249c704..024c7fcd 100644--- a/Peel/Model/AppLibrary.swift+++ b/Peel/Model/AppLibrary.swift@@ -497,6 +497,10 @@ final class AppLibrary {homebrewApps[app.id]}+ func apps(of cask: HomebrewPackage) -> [InstalledApp] {+ apps.filter { self.cask(for: $0)?.id == cask.id }+ }+func whyItCannotCheck(_ app: InstalledApp) -> CannotCheck {updateChecker.whyItCannotCheck(app, preference: updateSource, casks: casks)}diff --git a/Peel/Model/HomebrewLibrary.swift b/Peel/Model/HomebrewLibrary.swiftindex 4e9af50d..08593903 100644--- a/Peel/Model/HomebrewLibrary.swift+++ b/Peel/Model/HomebrewLibrary.swift@@ -15,6 +15,11 @@ final class HomebrewLibrary {/// Incremented every time the packages are read, so a view can key work to it. Their count stays the same/// through an upgrade, but their versions change.private(set) var revision = 0+ @ObservationIgnored private let appsOfCask: (HomebrewPackage) -> [InstalledApp]++ init(appsOfCask: @escaping (HomebrewPackage) -> [InstalledApp] = { _ in [] }) {+ self.appsOfCask = appsOfCask+ }enum Command: Equatable {case upgrade(HomebrewPackage.ID)@@ -253,10 +258,32 @@ final class HomebrewLibrary {+ retired.filter { $0.retirement?.stage == .deprecated }}- /// What Upgrade All upgrades: the rest are shown as out of date and left alone (`joinsUpgradeAll`), and all of- /// them while Homebrew would clean up after an upgrade, which deletes old versions for good.+ /// What Upgrade All upgrades: the rest are shown as out of date and left alone (`joinsUpgradeAll`, the exclusions),+ /// and all of them while Homebrew would clean up after an upgrade, which deletes old versions for good.var upgradable: [HomebrewPackage] {- overrides.contains(.cleansUp) ? [] : outdated.filter(\.joinsUpgradeAll)+ guard !overrides.contains(.cleansUp) else { return [] }+ return ExclusionsStore.shared.exclusions.joiningUpgradeAll(+ outdated, appsOf: appsOfCask, installation: installation+ )+ }++ /// Why the exclusions keep Homebrew from changing `package`, or nil when they don't: an upgrade deletes the version+ /// it replaces for good, as `brew uninstall` deletes the package.+ func exclusionRefusal(of package: HomebrewPackage) -> LocalizedStringResource? {+ let exclusions = ExclusionsStore.shared.exclusions+ if exclusions.isUnreadable { return "Exclusions can’t be read: see Settings" }+ if !exclusions.hasBeenRead { return "Checking…" }+ switch exclusions.excludes(package, apps: appsOfCask(package), installation: installation) {+ case true?: return "Excluded in Settings"+ case nil: return "Can’t be checked against your exclusions"+ case false?: return nil+ }+ }++ func isExcluded(_ package: HomebrewPackage) -> Bool {+ let exclusions = ExclusionsStore.shared.exclusions+ return exclusions.isKnown+ && exclusions.excludes(package, apps: appsOfCask(package), installation: installation) == true}/// How many packages of `kind` are installed, or nil while Homebrew hasn't said what is installed.diff --git a/Peel/Model/QuitBeforeHomebrew.swift b/Peel/Model/QuitBeforeHomebrew.swiftindex f9e3754e..db6c999c 100644--- a/Peel/Model/QuitBeforeHomebrew.swift+++ b/Peel/Model/QuitBeforeHomebrew.swift@@ -15,7 +15,7 @@ struct QuitBeforeHomebrew {var reopening: [URL] = []var processes: [RunningCopies.Process] = []for cask in packages where cask.kind == .cask {- let apps = library.apps.filter { library.cask(for: $0)?.id == cask.id }+ let apps = library.apps(of: cask)processes += RunningCopies.belonging(toCask: cask, apps: apps, among: running, installedApps: library.apps).filter { seen.insert($0.identifier).inserted }diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swiftindex 231e5b16..8962899a 100644--- a/Peel/PeelApp.swift+++ b/Peel/PeelApp.swift@@ -67,7 +67,7 @@ struct PeelApp: App {@State private var duplicates: DuplicateLibrary@State private var fileSearch: FileSearchLibrary@State private var plugins = PluginLibrary()- @State private var homebrew = HomebrewLibrary()+ @State private var homebrew: HomebrewLibrary@State private var helper: HelperModel@State private var updater = PeelUpdater()@State private var home: HomeModel@@ -100,6 +100,7 @@ struct PeelApp: App {let selfUninstall = SelfUninstall()_selfUninstall = State(initialValue: selfUninstall)let library = AppLibrary()+ _homebrew = State(initialValue: HomebrewLibrary(appsOfCask: { [library] in library.apps(of: $0) }))let history = RemovalHistoryStore(outcome: outcome, removesPeel: { selfUninstall.isRunning })let (orphans, space, developer, projects) = (OrphanLibrary(), SpaceLibrary(), DeveloperLibrary(), ProjectLibrary()diff --git a/Peel/Views/AppPageHeader.swift b/Peel/Views/AppPageHeader.swiftindex dc9e9659..ba435eac 100644--- a/Peel/Views/AppPageHeader.swift+++ b/Peel/Views/AppPageHeader.swift@@ -169,6 +169,8 @@ struct AppPageHeader: View {private func updateExplanation(for source: UpdateSource) -> LocalizedStringResource {switch source {case .appStore: "The App Store has it. Update from the App Store."+ case .homebrew where upgradeRefusal != nil:+ upgradeRefusal ?? .excludedAppcase .homebrew where library.cask(for: plan.app)?.upgradeNeedsAnAdministrator == true:"Homebrew needs an administrator’s password for this, and only Terminal can ask for it. Copy the command, then run it in Terminal."case .homebrew where homebrew.overrides.contains(.cleansUp):@@ -184,6 +186,10 @@ struct AppPageHeader: View {library.cask(for: plan.app)?.upgradeNeedsAnAdministrator == true || homebrew.overrides.contains(.cleansUp)}+ private var upgradeRefusal: LocalizedStringResource? {+ library.cask(for: plan.app).flatMap(homebrew.exclusionRefusal(of:))+ }+@ViewBuilderprivate func updateAction(for source: UpdateSource, version: String) -> some View {switch source {@@ -193,6 +199,8 @@ struct AppPageHeader: View {NSWorkspace.shared.open(page ?? URL(string: "macappstore://showUpdatesPage")!)}.buttonStyle(.borderedProminent)+ case .homebrew where upgradeRefusal != nil:+ EmptyView()case .homebrew where upgradeNeedsTerminal:if let cask = library.cask(for: plan.app) {CopyButton(@@ -235,6 +243,8 @@ struct AppPageHeader: View {guard let cask = library.cask(for: plan.app) else { return }let quit = QuitBeforeHomebrew([cask], library: library)quittingToUpgrade.check(quit.processes) {+ // Asked again once its apps have quit, since the exclusions can change meanwhile.+ guard upgradeRefusal == nil else { return }Task {await upgrade(cask)quit.reopen()diff --git a/Peel/Views/Homebrew/HomebrewDetailView.swift b/Peel/Views/Homebrew/HomebrewDetailView.swiftindex 075de639..5c1424a6 100644--- a/Peel/Views/Homebrew/HomebrewDetailView.swift+++ b/Peel/Views/Homebrew/HomebrewDetailView.swift@@ -140,7 +140,8 @@ struct HomebrewDetailView: View {}.buttonStyle(.borderedProminent).disabled(!updater.canCheckForUpdates)- } else if package.upgradeNeedsAnAdministrator || homebrew.overrides.contains(.cleansUp) {+ } else if upgradeRefusal == nil,+ package.upgradeNeedsAnAdministrator || homebrew.overrides.contains(.cleansUp) {let kind = package.kind == .cask ? "--cask" : "--formula"CopyButton(text: Homebrew.terminalCommand(@@ -154,7 +155,11 @@ struct HomebrewDetailView: View {}.buttonStyle(.borderedProminent)// A cask's apps, which have to quit first, are known only once they are matched.- .disabled(package.isPinned || package.kind == .cask && !library.knowsHomebrewsApps)+ .disabled(+ package.isPinned || package.kind == .cask && !library.knowsHomebrewsApps+ || upgradeRefusal != nil+ )+ .help(upgradeRefusal.map { Text($0) } ?? Text(verbatim: ""))}}if isRunning {@@ -189,13 +194,13 @@ struct HomebrewDetailView: View {Text("Peel updates itself, so it isn’t upgraded from here: Homebrew would have to quit Peel first.").font(.callout).foregroundStyle(.secondary)- } else if package.waitsForAnUpgrade, homebrew.overrides.contains(.cleansUp) {+ } else if package.waitsForAnUpgrade, homebrew.overrides.contains(.cleansUp), upgradeRefusal == nil {Text(HomebrewLibrary.cleansUp).font(.callout).foregroundStyle(.secondary)}- if (package.waitsForAnUpgrade && package.upgradeNeedsAnAdministrator && !package.isPeelItself)- || isUninstalledInTerminal {+ if (package.waitsForAnUpgrade && package.upgradeNeedsAnAdministrator && !package.isPeelItself+ && upgradeRefusal == nil) || isUninstalledInTerminal {Text(needsTerminal).font(.callout).foregroundStyle(.secondary)@@ -221,14 +226,18 @@ struct HomebrewDetailView: View {.toolbar(removing: .title)// Asked first, as Upgrade All is: an upgrade can run for a long time while Homebrew builds on this Mac..confirmationDialog("Upgrade \(package.name)?", isPresented: $isConfirmingUpgrade) {- Button("Upgrade") { upgrade() }+ Button("Upgrade") {+ // Checks again, since the exclusions can change while the dialog is open.+ guard upgradeRefusal == nil else { return }+ upgrade()+ }Button("Cancel", role: .cancel) {}} message: {Text("This can take a long time, since Homebrew builds some packages on this Mac. Its progress shows on this page as it works, and you can stop it there.")}.forgetCasksDialog(for: $casksToForget)- .task(id: caskApps.filter(\.carriesSystemExtensions).map(\.url)) {- let carrying = caskApps.filter(\.carriesSystemExtensions)+ .task(id: library.apps(of: package).filter(\.carriesSystemExtensions).map(\.url)) {+ let carrying = library.apps(of: package).filter(\.carriesSystemExtensions)guard !carrying.isEmpty else { return }let found = await RemovedElsewhere.first(among: carrying)extensionsAnswer = (carrying.map(\.url), found)@@ -250,6 +259,8 @@ struct HomebrewDetailView: View {private func upgrade() {let quit = QuitBeforeHomebrew([package], library: library)quittingToUpgrade.check(quit.processes) {+ // Asked again once its apps have quit, since the exclusions can change meanwhile.+ guard upgradeRefusal == nil else { return }Task {await homebrew.run(.upgrade(package.id))quit.reopen()@@ -296,7 +307,7 @@ struct HomebrewDetailView: View {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+ let apps = library.apps(of: package)// 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 }let carrying = apps.filter(\.carriesSystemExtensions).map(\.url)@@ -304,22 +315,18 @@ struct HomebrewDetailView: View {guard let extensionsAnswer, extensionsAnswer.apps == carrying else { return "Checking…" }if let removedElsewhere = extensionsAnswer.removedElsewhere { return removedElsewhere.explanation }}- switch exclusions.exclusions.excludes(package, apps: apps, installation: homebrew.installation) {- case true?: return "Excluded in Settings"- case nil: return "Can’t be checked against your exclusions"- case false?: return nil- }+ return upgradeRefusal}- private var caskApps: [InstalledApp] {- library.apps.filter { library.cask(for: $0)?.id == package.id }+ private var upgradeRefusal: LocalizedStringResource? {+ homebrew.exclusionRefusal(of: package)}private var isRunning: Bool {if homebrew.casksBeingForgotten.contains(package.id) { return true }return switch homebrew.commandHomebrewRuns {case .upgrade(let id), .uninstall(let id): id == package.id- case .upgradeAll: package.isOutdated && package.joinsUpgradeAll+ case .upgradeAll: homebrew.upgradable.contains { $0.id == package.id }case .update, .cleanup, .clearOlderDownloads, .clearEveryDownload, .repairTaps, .health, .vulnerabilities, nil:false}diff --git a/Peel/Views/Homebrew/HomebrewList.swift b/Peel/Views/Homebrew/HomebrewList.swiftindex be08d15e..21a3d1c8 100644--- a/Peel/Views/Homebrew/HomebrewList.swift+++ b/Peel/Views/Homebrew/HomebrewList.swift@@ -509,6 +509,8 @@ struct HomebrewList: View {Spacer(minLength: 4)if package.isPinned {Badge(title: Text("Pinned"), systemImage: "pin")+ } else if homebrew.isExcluded(package) {+ Badge(title: Text("Excluded in Settings"), systemImage: "hand.raised.fill")} else if package.upgradeNeedsAnAdministrator {Badge(title: Text("Needs Terminal"), systemImage: "terminal")}diff --git a/SAFETY.md b/SAFETY.mdindex d2f50927..8372915d 100644--- a/SAFETY.md+++ b/SAFETY.md@@ -567,10 +567,10 @@ there: that holds even for what only the helper could move, as the helper knowsreceipt stays too, on its page and on Package Receipts. Its other files, such as plug-ins or a folder named after theapp, are excluded only by their own paths, so to keep them, exclude them too.-Exclusions reach the Homebrew page too, since Homebrew deletes for good: Peel never runs `brew uninstall` for a package-whose app, or whose own folder, you excluded, nor for one with a folder Peel can't look into for an app you excluded,-nor for any package while your list isn't read or can't be, and Homebrew's cleanup keeps every formula whose folder you-excluded. While Peel can't read Homebrew's Cellar, or can't look into a formula's folder in it for an app you excluded,+Exclusions reach the Homebrew page too, since Homebrew deletes for good: Peel never runs `brew uninstall` or `brew+upgrade`, which deletes the version it replaces, for a package whose app, or whose own folder, you excluded, nor for one+with a folder Peel can't look into for an app you excluded, nor for any package while your list isn't read or can't be,+and Homebrew's cleanup keeps every formula whose folder you excluded. While Peel can't read Homebrew's Cellar, or can't look into a formula's folder in it for an app you excluded,nothing that deletes formulae runs at all. The Cellar and the prefix are the ones Homebrew names (`brew --cellar`, `brew--prefix`), never worked out from where `brew` sits.