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
Move Peel's files to the Trash as one folder when Terminal's stay
Țugui Dragoșead6429664bd
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swiftindex 5462965c..af682e9c 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift@@ -33,8 +33,7 @@ public enum SelfRemoval {moved = await service.trash([folder])} else {do {- let entries = try FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)- let rest = entries.filter { $0.lastPathComponent != terminal.lastPathComponent }+ let rest = try gathered(in: folder, leaving: terminal)moved = await service.trashOwnFiles(rest)} catch {moved = TrashResult(failures: [TrashFailure(url: folder, reason: .failed(error.localizedDescription))])@@ -44,4 +43,20 @@ public enum SelfRemoval {result.failures += moved.failuresreturn result}++ /// 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] {+ let manager = FileManager.default+ let entries = try manager.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)+ .filter { $0.lastPathComponent != kept.lastPathComponent }+ let together = folder.appending(path: "Peel", directoryHint: .isDirectory)+ guard (try? manager.createDirectory(at: together, withIntermediateDirectories: false)) != nil else {+ return entries+ }+ let left = entries.filter { entry in+ (try? manager.moveItem(at: entry, to: together.appending(path: entry.lastPathComponent))) == nil+ }+ return [together] + left+ }}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swiftindex 28e52893..792b61e4 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift@@ -75,7 +75,8 @@ struct SelfRemovalTests {}/// The Terminal settings the person's `.zshrc` and `~/.ssh/config` read stay where those lines look for them, so- /// the shell and ssh keep them once Peel is gone. Everything else in the folder goes, History included.+ /// the shell and ssh keep them once Peel is gone. Everything else in the folder goes, History included, together+ /// in one folder named Peel, as the Trash shows the whole folder when nothing stays.@Test func leavesTheTerminalSettingsTheShellAndSSHRead() async throws {let directory = try TemporaryDirectory()let app = try directory.directory("home/Applications/Peel.app")@@ -93,6 +94,27 @@ struct SelfRemovalTests {#expect(!ShellFile.url(in: folder).isMissing)#expect(!SSHFile.url(in: folder).isMissing)#expect(try FileManager.default.contentsOfDirectory(atPath: folder.path(percentEncoded: false)) == ["Terminal"])+ let rest = result.trashed.filter {+ PathPattern.comparablePath(of: $0.originalURL) != PathPattern.comparablePath(of: app)+ }+ #expect(rest.map(\.originalURL.lastPathComponent) == ["Peel"])+ let trashed = try #require(rest.first).trashedURL.path(percentEncoded: false)+ #expect(Set(try FileManager.default.contentsOfDirectory(atPath: trashed)) == [".hidden", "exclusions.json", "removals.json"])+ }++ @Test func movesEachFileOnItsOwnWhenTheFolderCannotBeGathered() async throws {+ let directory = try TemporaryDirectory()+ let app = try directory.directory("home/Applications/Peel.app")+ let folder = try directory.directory("home/Library/Application Support/Peel")+ try directory.file("home/Library/Application Support/Peel/Terminal/zshrc")+ try directory.file("home/Library/Application Support/Peel/Peel")+ try directory.file("home/Library/Application Support/Peel/exclusions.json")++ let result = await SelfRemoval.move([app], app: app, folder: folder, using: try service(in: directory)) { _ in }++ #expect(result.failures.isEmpty)+ #expect(try FileManager.default.contentsOfDirectory(atPath: folder.path(percentEncoded: false)) == ["Terminal"])+ #expect(Set(result.trashed.map(\.originalURL.lastPathComponent)) == ["Peel.app", "Peel", "exclusions.json"])}/// When the app stays, its files and its folder stay too: Peel goes on using them.