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.

Putting settings back says why when the app is open

Țugui Dragoșd15a61b6570c
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift
index 7f19a7ae..a6627232 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PreferenceBackup.swift
@@ -93,6 +93,8 @@ public enum PreferenceBackup {
case incomplete(clearedSome: Bool)
/// The settings the app has now could not be saved, so nothing was cleared or put back.
case notSaved
+ /// The app was open, and would write its own settings over the copy, so nothing was cleared or put back.
+ case appIsOpen
public var isComplete: Bool {
if case .complete = self { true } else { false }
@@ -101,17 +103,19 @@ public enum PreferenceBackup {
/// 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. The app must not be running, or it overwrites them. `defaults import` merges rather than
+ /// otherwise be gone. `isOpen` tells whether the app runs, which would overwrite them: it is asked before
+ /// anything and again right before the settings in use are cleared. `defaults import` merges rather than
/// replaces, so each domain is deleted first.
@concurrent
public static func restore(
from folder: URL,
of bundleIdentifier: String,
in directory: URL = PreferenceBackup.defaultDirectory,
- exclusions: Exclusions = .none
+ exclusions: Exclusions = .none,
+ isOpen: @Sendable () async -> Bool
) async -> Restored {
let service = TrashService(exclusions: exclusions)
- return await restore(from: folder, of: bundleIdentifier, in: directory, through: service, run: run)
+ return await restore(from: folder, of: bundleIdentifier, in: directory, through: service, run: run, isOpen: isOpen)
}
static func restore(
@@ -119,8 +123,10 @@ public enum PreferenceBackup {
of bundleIdentifier: String,
in directory: URL,
through service: TrashService,
- run: Run
+ run: Run,
+ isOpen: @Sendable () async -> Bool
) async -> Restored {
+ 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 domain(from: file.lastPathComponent).map { ($0, file) } }
// A copy that is not a readable property list would put nothing back once the delete had cleared the
@@ -129,6 +135,7 @@ public enum PreferenceBackup {
guard !readable.isEmpty else { return .incomplete(clearedSome: false) }
let current = await save(readable.map(\.0), of: bundleIdentifier, in: directory, through: service, run: run)
guard current != .failed else { return .notSaved }
+ guard await !isOpen() else { return .appIsOpen }
var clearedSome = false
for (domain, file) in readable {
_ = await run(domain.command("delete"))
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
index f62e2f53..ff954bf2 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
@@ -246,7 +246,7 @@ struct RemovalHygieneTests {
return Self.exporting(arguments)
}
- let restored = await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run)
+ let restored = await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run, isOpen: { false })
#expect(restored == .incomplete(clearedSome: false), "a copy that could not be read was reported as put back, or as cleared")
let commands = asked.withLock { $0 }
@@ -255,7 +255,7 @@ struct RemovalHygieneTests {
// An import that fails after the delete has cleared that domain, and the result says so.
let failingImport: PreferenceBackup.Run = { arguments in arguments.first == "import" ? .no : Self.exporting(arguments) }
- #expect(await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: failingImport) == .incomplete(clearedSome: true))
+ #expect(await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: failingImport, isOpen: { false }) == .incomplete(clearedSome: true))
}
/// Put Back replaces what the app set since the reset, which may be a license entered again, so those settings
@@ -272,7 +272,7 @@ struct RemovalHygieneTests {
return Self.exporting(arguments)
}
- let restored = await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run)
+ let restored = await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run, isOpen: { false })
#expect(restored == .complete)
let copies = PreferenceBackup.copies(in: backups)
@@ -283,6 +283,37 @@ struct RemovalHygieneTests {
#expect(verbs == ["read", "export", "delete", "import"], "cleared before the settings in use were saved")
}
+ /// An app that runs writes its own settings over what is put back, so nothing is cleared or put back while it is
+ /// open. It is asked before anything and again once the settings in use are saved, since it can open meanwhile.
+ @Test func putsNothingBackWhileTheAppIsOpen() 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)
+ }
+
+ let open = await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run, isOpen: { true })
+ #expect(open == .appIsOpen)
+ #expect(asked.withLock { $0 }.isEmpty)
+
+ let checks = Mutex(0)
+ let opensMeanwhile = await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run) {
+ checks.withLock { count in
+ count += 1
+ return count > 1
+ }
+ }
+ #expect(opensMeanwhile == .appIsOpen)
+ let commands = asked.withLock { $0 }
+ #expect(commands.contains { $0.first == "export" }, "the settings in use were not saved first")
+ #expect(!commands.contains { $0.first == "delete" || $0.first == "import" }, "settings were cleared while the app was open")
+ }
+
/// When the settings in use cannot be saved, nothing is cleared, and the saved copy is not put back.
@Test func nothingIsClearedWhenTheSettingsInUseCannotBeSaved() async throws {
let directory = try TemporaryDirectory()
@@ -296,7 +327,7 @@ struct RemovalHygieneTests {
return arguments.first == "export" ? .no : .yes
}
- #expect(await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run) == .notSaved)
+ #expect(await PreferenceBackup.restore(from: folder, of: "com.example.app", in: backups, through: Self.service, run: run, isOpen: { false }) == .notSaved)
#expect(!asked.withLock { $0 }.contains { $0.first == "delete" || $0.first == "import" })
}
diff --git a/Peel/Model/ResetPlan.swift b/Peel/Model/ResetPlan.swift
index 9d11b503..7a0ebb34 100644
--- a/Peel/Model/ResetPlan.swift
+++ b/Peel/Model/ResetPlan.swift
@@ -114,14 +114,14 @@ final class ResetPlan {
return result
}
- /// Puts the saved settings back. Refused while the app is running, since it would write its own settings
- /// over them.
- func putSettingsBack() async -> Bool {
- refreshRunningState()
- guard let backup, !isAppRunning else { return false }
- return await QuitGuard.shared.run {
- await PreferenceBackup.restore(from: backup, of: app.bundleIdentifier, exclusions: ExclusionsStore.shared.exclusions)
- }.isComplete
+ /// 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 {
+ await QuitGuard.shared.run {
+ await PreferenceBackup.restore(from: backup, of: app.bundleIdentifier, exclusions: ExclusionsStore.shared.exclusions) { @MainActor in
+ self.refreshRunningState()
+ return self.isAppRunning
+ }
+ }
}
/// Quits the app and the helpers it ships, so nothing writes its settings again while they are cleared.
diff --git a/Peel/Views/ResetSheet.swift b/Peel/Views/ResetSheet.swift
index d73bd3d4..3bb6db89 100644
--- a/Peel/Views/ResetSheet.swift
+++ b/Peel/Views/ResetSheet.swift
@@ -17,7 +17,7 @@ struct ResetSheet: View {
@State private var isPuttingBack = false
/// The result of Put Settings Back, or nil before it is pressed. A result is shown either way, so a success
/// doesn't look like a click that did nothing.
- @State private var settingsWentBack: Bool?
+ @State private var putBack: PreferenceBackup.Restored?
init(app: InstalledApp, changedFiles: Binding<Bool>) {
_plan = State(initialValue: ResetPlan(app: app))
@@ -246,12 +246,24 @@ struct ResetSheet: View {
if let backup = plan.backup {
Section {
VStack(alignment: .leading, spacing: 10) {
+ if plan.isAppRunning {
+ HStack(spacing: 10) {
+ Text("Quit \(plan.app.name) first, or it can write its own settings over these.")
+ .font(.callout)
+ Spacer(minLength: 8)
+ Button("Quit \(plan.app.name)") { plan.quitApp() }
+ .buttonStyle(.bordered)
+ }
+ }
HStack(spacing: 10) {
Button("Put Settings Back") {
Task {
isPuttingBack = true
- settingsWentBack = await plan.putSettingsBack()
- changedFiles = true
+ let restored = await plan.putSettingsBack(from: backup)
+ putBack = restored
+ if restored != .appIsOpen, restored != .notSaved {
+ changedFiles = true
+ }
isPuttingBack = false
}
}
@@ -262,10 +274,8 @@ struct ResetSheet: View {
}
.buttonStyle(.bordered)
}
- if let settingsWentBack {
- Text(settingsWentBack ? "The settings are back as they were." : "Not all of the saved settings could be put back.")
- .font(.callout)
- .foregroundStyle(settingsWentBack ? AnyShapeStyle(.secondary) : AnyShapeStyle(.red))
+ if let putBack {
+ putBackResult(putBack)
}
}
.padding(.vertical, 2)
@@ -290,6 +300,35 @@ struct ResetSheet: View {
}
}
+ /// What Put Settings Back did, each way it can end.
+ @ViewBuilder
+ private func putBackResult(_ restored: PreferenceBackup.Restored) -> some View {
+ switch restored {
+ case .complete:
+ Text("The settings are back as they were.")
+ .font(.callout)
+ .foregroundStyle(.secondary)
+ case .incomplete(let clearedSome):
+ VStack(alignment: .leading, spacing: 2) {
+ Text("Not all of the saved settings could be put back.")
+ .foregroundStyle(.red)
+ if clearedSome {
+ Text("The settings that didn’t go back were cleared first, so the app starts them from scratch. The copy is still here to try again.")
+ .foregroundStyle(.secondary)
+ }
+ }
+ .font(.callout)
+ case .notSaved:
+ Text("Peel couldn’t save the settings \(plan.app.name) has now, so it put nothing back.")
+ .font(.callout)
+ .foregroundStyle(.red)
+ case .appIsOpen:
+ Text("Quit \(plan.app.name) first, or it can write its own settings over these.")
+ .font(.callout)
+ .foregroundStyle(.red)
+ }
+ }
+
/// The outcome's title, based on what really happened: reset, partly reset, or nothing reset.
private var outcomeTitle: Text {
let privacyWasReset = plan.privacy == .reset
diff --git a/Peel/Views/Settings/SavedSettingsSection.swift b/Peel/Views/Settings/SavedSettingsSection.swift
index 218f4edf..670260b0 100644
--- a/Peel/Views/Settings/SavedSettingsSection.swift
+++ b/Peel/Views/Settings/SavedSettingsSection.swift
@@ -106,17 +106,14 @@ struct SavedSettingsSection: View {
}
private func putBack(_ copy: PreferenceBackup.Copy, named name: String) async {
- // Asked again at the moment of Put Back: an app opened since the row was drawn would write its own settings
- // over these when it quits.
- refreshOpenApps()
- guard !openApps.contains(copy.bundleIdentifier) else {
- failure = Failure(title: String(localized: "Quit \(name) first, or it can write its own settings over these."), message: "")
- return
- }
workingOn = copy.id
defer { workingOn = nil }
+ // Asked again at the moment of Put Back and before the settings in use are cleared: an app opened since the
+ // row was drawn would write its own settings over these when it quits.
let restored = await QuitGuard.shared.run {
- await PreferenceBackup.restore(from: copy.folder, of: copy.bundleIdentifier, exclusions: ExclusionsStore.shared.exclusions)
+ await PreferenceBackup.restore(from: copy.folder, of: copy.bundleIdentifier, exclusions: ExclusionsStore.shared.exclusions) { @MainActor in
+ isRunning(copy.bundleIdentifier)
+ }
}
// The settings the app had until now may have become a copy of their own.
reload()
@@ -132,6 +129,8 @@ struct SavedSettingsSection: View {
)
case .notSaved:
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: "")
}
}