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
Ask the helper once before Remove Peel hands it the link and its ledger
Țugui Dragoș4d165579c5a3
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.mdindex c1ed103d..99cf46ab 100644--- a/ARCHITECTURE.md+++ b/ARCHITECTURE.md@@ -301,7 +301,9 @@ through the user's own lines (`SelfRemoval`), gathered first into one folder namand leaves what it changed outside them as it is. The helper moves its own ledger there, since nothing else can,just before Peel unregisters it, and before that the link to the `peel` command when only an administrator can moveit (`Uninstallation.unreviewedLinks`): it leads into Peel until Peel has moved, and the helper takes such a link-only when it leads to the tool of the Peel it runs from (`PrivilegedPathPolicy`'s `ownTool`). It is the only way+only when it leads to the tool of the Peel it runs from (`PrivilegedPathPolicy`'s `ownTool`). Both are asked of the+helper only once it has answered (`SelfRemoval.beforeTheHelperGoes`): one that cannot start would keep each request+waiting, so then both stay, and Remove Peel names them with what stayed. It is the only wayPeel removes itself: Peel's own page in Applications, and Peel among several chosen apps, list it and select nothingof it (`Uninstallation.isPeel`), and `peel uninstall` refuses it. For a copy Homebrew installed, Settings shows`brew uninstall --zap` in its place (`SettingsView`).diff --git a/GUIDE.md b/GUIDE.mdindex 7188e44a..5fc295b0 100644--- a/GUIDE.md+++ b/GUIDE.md@@ -290,8 +290,9 @@ brew uninstall --zap --cask peel```When only an administrator can remove the link to the `peel` command you made in Settings, and Peel's helper isn't-installed, Remove Peel leaves the link, and Settings says so beforehand. `brew uninstall` removes only the link-Homebrew made. Remove a link that stays yourself:+installed, Remove Peel leaves the link, and Settings says so beforehand. When the helper is installed but doesn't+answer, Remove Peel leaves the link and the helper's record of what it moved, and names both once it is done.+`brew uninstall` removes only the link Homebrew made. Remove a link that stays yourself:```bashsudo rm -f /usr/local/bin/peeldiff --git a/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swiftindex c3d3dcc2..3bd2d6a6 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift@@ -44,6 +44,27 @@ public enum SelfRemoval {return result}+ /// Has the helper take Peel's own links in the root folders and its ledger before it is unregistered, asking it+ /// once first: a helper that cannot start would keep each request waiting, so then nothing is sent and both stay.+ public static func beforeTheHelperGoes(+ links: [URL],+ helperIsEnabled: Bool,+ using service: TrashService,+ answers: () async -> Bool = { await PrivilegedHelper.isResponding() },+ moveLedger: () async -> PrivilegedHelper.LedgerMove = { await PrivilegedHelper.moveLedgerToTrash() },+ ledger: URL = PrivilegedHelper.ledgerFolder+ ) async -> (links: TrashResult, ledger: PrivilegedHelper.LedgerMove) {+ guard helperIsEnabled, await answers() else {+ let unanswered = TrashFailure.Reason.failed(PrivilegedHelper.unavailable)+ let left = helperIsEnabled ? links.map { TrashFailure(url: $0, reason: unanswered) } : []+ return (+ TrashResult(failures: left),+ ledger.isThere ? .stayed(TrashFailure(url: ledger, reason: unanswered)) : .none+ )+ }+ return (await service.trash(links, usingHelperFor: Set(links)), await moveLedger())+ }+/// Gathers everything in `folder` but `kept` into a folder named Peel inside it, so the Trash shows Peel's files/// as one item. An entry that cannot be gathered is answered on its own.private static func gathered(in folder: URL, leaving kept: URL) throws -> [URL] {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swiftindex 4a1d09d2..0e3cf813 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift@@ -3,6 +3,16 @@ import Foundationimport PeelPrivilegedimport Testing+private actor HelperRequests {+ private(set) var sent: [URL] = []+ private(set) var ledgerMoves = 0+ private(set) var questions = 0++ func send(_ urls: [URL]) { sent += urls }+ func moveLedger() { ledgerMoves += 1 }+ func ask() { questions += 1 }+}+struct SelfRemovalTests {private func service(in directory: borrowing TemporaryDirectory,@@ -24,6 +34,109 @@ struct SelfRemovalTests {Set(urls.map(PathPattern.comparablePath))}+ private func service(+ in directory: borrowing TemporaryDirectory,+ sendingTo requests: HelperRequests+ ) throws -> TrashService {+ let trash = try directory.directory("Trash")+ let environment = SearchEnvironment(+ homeDirectory: directory.url.appending(path: "home", directoryHint: .isDirectory),+ rootDirectory: directory.url.appending(path: "root", directoryHint: .isDirectory)+ )+ return TrashService(environment: environment, moveThroughHelper: { urls in+ await requests.send(urls)+ return TrashResult(trashed: urls.map {+ TrashedItem(originalURL: $0, trashedURL: trash.appending(path: $0.lastPathComponent), date: .now)+ })+ }) { url in+ let destination = trash.appending(path: UUID().uuidString)+ try FileManager.default.moveItem(at: url, to: destination)+ return destination+ }+ }++ @Test func aHelperThatDoesNotAnswerIsSentNothingAndItsLinkAndLedgerStay() async throws {+ let directory = try TemporaryDirectory()+ let requests = HelperRequests()+ let link = try directory.file("home/bin/peel")+ let ledger = try directory.directory("root/var/db/org.example.helper")++ let (links, moved) = await SelfRemoval.beforeTheHelperGoes(+ links: [link],+ helperIsEnabled: true,+ using: try service(in: directory, sendingTo: requests),+ answers: { false },+ moveLedger: { await requests.moveLedger(); return .moved },+ ledger: ledger+ )++ #expect(await requests.sent.isEmpty)+ #expect(await requests.ledgerMoves == 0)+ #expect(links.trashed.isEmpty)+ #expect(links.failures == [TrashFailure(url: link, reason: .failed(PrivilegedHelper.unavailable))])+ #expect(moved == .stayed(TrashFailure(url: ledger, reason: .failed(PrivilegedHelper.unavailable))))+ }++ @Test func aHelperWaitingForApprovalIsNotAskedAndItsLedgerStays() async throws {+ let directory = try TemporaryDirectory()+ let requests = HelperRequests()+ let link = try directory.file("home/bin/peel")+ let ledger = try directory.directory("root/var/db/org.example.helper")++ let (links, moved) = await SelfRemoval.beforeTheHelperGoes(+ links: [link],+ helperIsEnabled: false,+ using: try service(in: directory, sendingTo: requests),+ answers: { await requests.ask(); return true },+ moveLedger: { await requests.moveLedger(); return .moved },+ ledger: ledger+ )++ #expect(await requests.questions == 0)+ #expect(await requests.sent.isEmpty)+ #expect(await requests.ledgerMoves == 0)+ #expect(links.trashed.isEmpty && links.failures.isEmpty)+ #expect(moved == .stayed(TrashFailure(url: ledger, reason: .failed(PrivilegedHelper.unavailable))))+ }++ @Test func withNoLedgerNoneStaysWhenTheHelperDoesNotAnswer() async throws {+ let directory = try TemporaryDirectory()+ let requests = HelperRequests()++ let (_, moved) = await SelfRemoval.beforeTheHelperGoes(+ links: [],+ helperIsEnabled: true,+ using: try service(in: directory, sendingTo: requests),+ answers: { false },+ moveLedger: { await requests.moveLedger(); return .moved },+ ledger: directory.url.appending(path: "root/var/db/org.example.helper", directoryHint: .isDirectory)+ )++ #expect(moved == .none)+ #expect(await requests.ledgerMoves == 0)+ }++ @Test func aHelperThatAnswersMovesTheLinkAndItsLedger() async throws {+ let directory = try TemporaryDirectory()+ let requests = HelperRequests()+ let link = try directory.file("home/bin/peel")+ let ledger = try directory.directory("root/var/db/org.example.helper")++ let (links, moved) = await SelfRemoval.beforeTheHelperGoes(+ links: [link],+ helperIsEnabled: true,+ using: try service(in: directory, sendingTo: requests),+ answers: { true },+ moveLedger: { await requests.moveLedger(); return .moved },+ ledger: ledger+ )++ #expect(paths(await requests.sent) == paths([link]))+ #expect(paths(links.trashed.map(\.originalURL)) == paths([link]))+ #expect(moved == .moved)+ #expect(await requests.ledgerMoves == 1)+ }+/// History lives in the folder, so it is written first and goes with it.@Test func movesTheFolderLastWithTheRecordOfTheRestInside() async throws {let directory = try TemporaryDirectory()diff --git a/Peel/Model/SelfUninstall.swift b/Peel/Model/SelfUninstall.swiftindex 87c2b1da..f441d989 100644--- a/Peel/Model/SelfUninstall.swift+++ b/Peel/Model/SelfUninstall.swift@@ -67,11 +67,11 @@ final class SelfUninstall {var ledger = PrivilegedHelper.LedgerMove.nonevar links = TrashResult()if isRegistered() {- if helper.status == .enabled {- let own = plan.unreviewedLinks- links = await TrashService(exclusions: exclusions).trash(own, usingHelperFor: Set(own))- }- ledger = await PrivilegedHelper.moveLedgerToTrash()+ (links, ledger) = await SelfRemoval.beforeTheHelperGoes(+ links: plan.unreviewedLinks,+ helperIsEnabled: helper.status == .enabled,+ using: TrashService(exclusions: exclusions)+ )let helperFailure = await helper.uninstall()// A helper still registered would point into the Trash, so Peel moves only once the helper is gone.guard !isRegistered() else {