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.

Leave an excluded app's saved settings alone

Țugui Dragoș9f6baec060cd
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift
index e1514be0..6d30da22 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift
@@ -102,12 +102,23 @@ public enum PreferenceBackup {
case notSaved
/// The app was open, and would write its own settings over the copy, so nothing was cleared or put back.
case appIsOpen
+ /// The exclusions aren't read yet or couldn't be read, so nothing was cleared or put back.
+ case exclusionsNotKnown
+ /// The app is excluded, so its settings were left alone.
+ case excluded
public var isComplete: Bool {
if case .complete = self { true } else { false }
}
}
+ /// Why a copy of `bundleIdentifier`'s settings can't be put back now, or nil when it can: nothing goes back while
+ /// the exclusions aren't known, and an excluded app's settings are left alone.
+ public static func refusal(ofPuttingBack bundleIdentifier: String, exclusions: Exclusions) -> Restored? {
+ guard exclusions.isKnown else { return .exclusionsNotKnown }
+ return exclusions.excludes(bundleIdentifier: bundleIdentifier) ? .excluded : nil
+ }
+
/// Puts the domains saved in `folder` back with `defaults import`, once the settings the app has now are saved
/// in `directory` as a copy of their own: what it set since, a license entered again or an account, would
/// otherwise be gone. `isOpen` tells whether the app runs, which would overwrite them: it is asked before
@@ -126,6 +137,7 @@ public enum PreferenceBackup {
from: folder,
of: bundleIdentifier,
in: directory,
+ exclusions: exclusions,
through: service,
run: run,
isOpen: isOpen
@@ -136,10 +148,12 @@ public enum PreferenceBackup {
from folder: URL,
of bundleIdentifier: String,
in directory: URL,
+ exclusions: Exclusions = .none,
through service: TrashService,
run: Run,
isOpen: @Sendable () async -> Bool
) async -> Restored {
+ if let refusal = refusal(ofPuttingBack: bundleIdentifier, exclusions: exclusions) { return refusal }
guard await !isOpen() else { return .appIsOpen }
let files = (try? FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)) ?? []
let saved = files.filter { $0.pathExtension == "plist" }.compactMap { file in
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
index 1dcbab1f..906de6b6 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
@@ -486,6 +486,75 @@ struct RemovalHygieneTests {
#expect(!asked.withLock { $0 }.contains { $0.first == "delete" || $0.first == "import" })
}
+ @Test func putsNothingBackWhileTheExclusionsAreNotKnown() async throws {
+ let directory = try TemporaryDirectory()
+ let backups = try directory.directory("Backups")
+ let folder = try directory.directory("Backups/com.example.app 2026-09-24 101500")
+ let good = try PropertyListSerialization.data(fromPropertyList: ["key": "value"], format: .xml, options: 0)
+ try good.write(to: folder.appending(path: "com.example.app.plist"))
+ let asked = Mutex<[[String]]>([])
+ let run: PreferenceBackup.Run = { arguments in
+ asked.withLock { $0.append(arguments) }
+ return Self.exporting(arguments)
+ }
+
+ for exclusions in [Exclusions.notYetRead, .unreadable] {
+ let restored = await PreferenceBackup.restore(
+ from: folder,
+ of: "com.example.app",
+ in: backups,
+ exclusions: exclusions,
+ through: Self.service,
+ run: run,
+ isOpen: { false }
+ )
+ #expect(restored == .exclusionsNotKnown)
+ }
+ #expect(asked.withLock { $0 }.isEmpty, "settings were saved, cleared or put back while the exclusions were not known")
+ #expect(PreferenceBackup.copies(in: backups).count == 1)
+ }
+
+ @Test func leavesAnExcludedAppsSettingsAlone() async throws {
+ let directory = try TemporaryDirectory()
+ let backups = try directory.directory("Backups")
+ let good = try PropertyListSerialization.data(fromPropertyList: ["key": "value"], format: .xml, options: 0)
+ let excludedCopy = try directory.directory("Backups/com.example.app 2026-09-24 101500")
+ try good.write(to: excludedCopy.appending(path: "com.example.app.plist"))
+ let otherCopy = try directory.directory("Backups/com.example.other 2026-09-24 101500")
+ try good.write(to: otherCopy.appending(path: "com.example.other.plist"))
+ var exclusions = Exclusions.none
+ exclusions.bundleIdentifiers.insert("com.example.app")
+ let asked = Mutex<[[String]]>([])
+ let run: PreferenceBackup.Run = { arguments in
+ asked.withLock { $0.append(arguments) }
+ return Self.exporting(arguments)
+ }
+
+ let excluded = await PreferenceBackup.restore(
+ from: excludedCopy,
+ of: "com.example.app",
+ in: backups,
+ exclusions: exclusions,
+ through: Self.service,
+ run: run,
+ isOpen: { false }
+ )
+ #expect(excluded == .excluded)
+ #expect(asked.withLock { $0 }.isEmpty, "an excluded app's settings were saved, cleared or put back")
+
+ let other = await PreferenceBackup.restore(
+ from: otherCopy,
+ of: "com.example.other",
+ in: backups,
+ exclusions: exclusions,
+ through: Self.service,
+ run: run,
+ isOpen: { false }
+ )
+ #expect(other == .complete)
+ #expect(asked.withLock { $0 }.map(\.[0]) == ["read", "export", "delete", "import"])
+ }
+
/// A stand-in for `defaults` that answers yes, and writes the file an `export` names, as the real one does.
private static func exporting(_ arguments: [String]) -> PreferenceBackup.Answer {
if arguments.first == "export", let file = arguments.last {
diff --git a/Peel/Model/ResetPlan.swift b/Peel/Model/ResetPlan.swift
index faa27a86..c22a9c2d 100644
--- a/Peel/Model/ResetPlan.swift
+++ b/Peel/Model/ResetPlan.swift
@@ -147,6 +147,13 @@ final class ResetPlan {
return outcome.result
}
+ /// Why the saved settings can't be put back now, or nil when they can, read from the exclusions as they stand.
+ var putBackRefusal: PreferenceBackup.Restored? {
+ app.bundleIdentifier.flatMap {
+ PreferenceBackup.refusal(ofPuttingBack: $0, exclusions: ExclusionsStore.shared.exclusions)
+ }
+ }
+
/// Puts the settings saved in `backup` back, unless the app is open, which would write its own over them.
func putSettingsBack(from backup: URL) async -> PreferenceBackup.Restored {
// A copy is saved under the app's identifier, so an app with none has no copy to put back.
diff --git a/Peel/Views/ResetSheet.swift b/Peel/Views/ResetSheet.swift
index 5546041b..9bd2bdb5 100644
--- a/Peel/Views/ResetSheet.swift
+++ b/Peel/Views/ResetSheet.swift
@@ -309,20 +309,23 @@ struct ResetSheet: View {
Task {
let restored = await plan.putSettingsBack(from: backup)
putBack = restored
- if restored != .appIsOpen, restored != .notSaved {
- changedFiles = true
+ switch restored {
+ case .complete, .incomplete: changedFiles = true
+ case .notSaved, .appIsOpen, .exclusionsNotKnown, .excluded: break
}
isPuttingBack = false
}
}
.buttonStyle(.borderedProminent)
- .disabled(isPuttingBack || plan.isAppRunning)
+ .disabled(isPuttingBack || plan.isAppRunning || plan.putBackRefusal != nil)
Button("Show in Finder", systemImage: "folder") {
NSWorkspace.shared.activateFileViewerSelecting([backup])
}
.buttonStyle(.bordered)
}
- if let putBack {
+ if let refusal = plan.putBackRefusal {
+ putBackResult(refusal)
+ } else if let putBack {
putBackResult(putBack)
}
}
@@ -376,6 +379,20 @@ struct ResetSheet: View {
StatusLabel(title: Text("Quit \(plan.app.name) first, or it can write its own settings over these."), tint: .red)
.font(.callout)
.foregroundStyle(.secondary)
+ case .exclusionsNotKnown:
+ VStack(alignment: .leading, spacing: 2) {
+ StatusLabel(title: Text("Peel couldn’t read your exclusions"), tint: .red)
+ Text("Peel removes nothing and puts nothing back until you start the list over in Settings.")
+ }
+ .font(.callout)
+ .foregroundStyle(.secondary)
+ case .excluded:
+ VStack(alignment: .leading, spacing: 2) {
+ StatusLabel(title: Text("This app is excluded"), tint: .red)
+ Text("Peel leaves it and its files alone. Change that in Settings.")
+ }
+ .font(.callout)
+ .foregroundStyle(.secondary)
}
}
diff --git a/Peel/Views/Settings/SavedSettingsSection.swift b/Peel/Views/Settings/SavedSettingsSection.swift
index 5b9686ff..95011148 100644
--- a/Peel/Views/Settings/SavedSettingsSection.swift
+++ b/Peel/Views/Settings/SavedSettingsSection.swift
@@ -7,6 +7,7 @@ import SwiftUI
struct SavedSettingsSection: View {
@Environment(AppLibrary.self) private var library
@Environment(RemovalHistoryStore.self) private var history
+ @Environment(ExclusionsStore.self) private var exclusions
@State private var workingOn: PreferenceBackup.Copy.ID?
@State private var failure: Failure?
@State private var copyToTrash: PreferenceBackup.Copy?
@@ -64,13 +65,14 @@ struct SavedSettingsSection: View {
private func row(for copy: PreferenceBackup.Copy) -> some View {
let name = library.name(forReference: copy.bundleIdentifier)
let isOpen = openApps.contains(copy.bundleIdentifier)
+ let refusal = PreferenceBackup.refusal(ofPuttingBack: copy.bundleIdentifier, exclusions: exclusions.exclusions)
return LabeledContent {
HStack(spacing: 8) {
Button("Put Back") {
copyToPutBack = copy
}
- .disabled(isOpen)
- .help(isOpen ? Text("Quit \(name) first, or it can write its own settings over these.") : Text("Puts these settings back, after saving the ones the app has now."))
+ .disabled(isOpen || refusal != nil)
+ .help(putBackHelp(refusal: refusal, isOpen: isOpen, name: name))
Button("Show in Finder", systemImage: "folder") {
NSWorkspace.shared.activateFileViewerSelecting([copy.folder])
}
@@ -91,6 +93,21 @@ struct SavedSettingsSection: View {
}
}
+ private func putBackHelp(refusal: PreferenceBackup.Restored?, isOpen: Bool, name: String) -> Text {
+ switch refusal {
+ case .exclusionsNotKnown?:
+ exclusions.exclusions.isUnreadable
+ ? Text("Peel removes nothing and puts nothing back until you start the list over in Settings.")
+ : Text("Checking…")
+ case .excluded?:
+ Text("Peel leaves it and its files alone. Change that in Settings.")
+ default:
+ isOpen
+ ? Text("Quit \(name) first, or it can write its own settings over these.")
+ : Text("Puts these settings back, after saving the ones the app has now.")
+ }
+ }
+
/// Whether the app a copy belongs to is running, by the rule a reset uses: the app, its helpers, and another
/// copy of it that writes the same settings.
private func isRunning(_ bundleIdentifier: String) -> Bool {
@@ -139,6 +156,16 @@ struct SavedSettingsSection: View {
failure = Failure(title: String(localized: "Peel couldn’t save the settings \(name) has now, so it put nothing back."), message: "")
case .appIsOpen:
failure = Failure(title: String(localized: "Quit \(name) first, or it can write its own settings over these."), message: "")
+ case .exclusionsNotKnown:
+ failure = Failure(
+ title: String(localized: "Peel couldn’t read your exclusions"),
+ message: String(localized: "Peel removes nothing and puts nothing back until you start the list over in Settings.")
+ )
+ case .excluded:
+ failure = Failure(
+ title: String(localized: "This app is excluded"),
+ message: String(localized: "Peel leaves it and its files alone. Change that in Settings.")
+ )
}
}