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.

Several apps' page keeps what was chosen when it scans again

Țugui Dragoș7bcc7d7f0cf3
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift b/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift
index 78fdb9dc..a1c394fa 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/BulkUninstallation.swift
@@ -79,6 +79,16 @@ public struct BulkUninstallation: Sendable {
}.map(\.url))
}
+ /// What a checkbox can select, by the rule of an app's own page: nothing Peel leaves alone or with something
+ /// excluded inside, nothing that needs the helper while it cannot act, no app macOS keeps or the helper may not
+ /// move, and nothing of Peel.
+ public func selectable(canUseHelper: Bool) -> Set<URL> {
+ Set(items.filter { item in
+ !item.isExcluded && !item.isPeels && !item.isBeyondTheHelper && !(item.isApplication && item.isKeptByMacOS)
+ && item.match?.heldBack?.cannotBeMoved != true && (canUseHelper || !item.requiresPrivileges)
+ }.map(\.url))
+ }
+
public var privilegedURLs: Set<URL> {
Set(items.filter { $0.requiresPrivileges && !$0.isExcluded && !$0.isPeels && !$0.isBeyondTheHelper && !($0.isApplication && $0.isKeptByMacOS) }.map(\.url))
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/KeptSelection.swift b/Packages/PeelCore/Sources/PeelCore/Removal/KeptSelection.swift
index ce2500f8..26ba9298 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/KeptSelection.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/KeptSelection.swift
@@ -24,8 +24,10 @@ public struct KeptSelection: Sendable {
return made
}
- /// Forgets every choice, so the next update is Peel's suggestion alone.
- public mutating func startOver() {
- offered = [:]
+ /// Forgets what was chosen for `urls`, so the next update gives them Peel's suggestion as if they were new.
+ public mutating func forget(_ urls: Set<URL>) {
+ for url in urls {
+ offered[url] = nil
+ }
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/UninstallSelection.swift b/Packages/PeelCore/Sources/PeelCore/Removal/UninstallSelection.swift
index 81986264..23762ea5 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/UninstallSelection.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/UninstallSelection.swift
@@ -1,30 +1,64 @@
public import Foundation
-/// What is selected on an app's page, kept across its scans (`KeptSelection`) and as the helper comes and goes.
+/// What is selected on an app's page, or on several apps' page, kept across its scans (`KeptSelection`) and as the
+/// helper comes and goes.
///
-/// When the app comes to stay, everything selected was chosen for it going, so nothing stays selected: its leftovers
-/// would go and it would stay without them. When it can go again, Peel's suggestion comes back only if the person
-/// left the selection as Peel made it.
+/// When an app comes to stay, what it holds was chosen for it going, so none of it stays selected: its files would
+/// go and leave it without them. When it can go again, Peel's suggestion comes back for it only if the person left
+/// the selection as Peel made it.
public struct UninstallSelection: Sendable {
private var kept = KeptSelection()
- private var appStays: Bool?
+ /// Whether each app stayed at the last update, by bundle identifier.
+ private var stayed: [String: Bool] = [:]
/// What a checkbox can select as of the last update.
public private(set) var selectable: Set<URL> = []
public init() {}
public mutating func update(_ selected: Set<URL>, in uninstallation: Uninstallation, canUseHelper: Bool) -> Set<URL> {
- let stays = uninstallation.appStays(canUseHelper: canUseHelper)
- var suggested = uninstallation.suggestedSelection(canUseHelper: canUseHelper)
- if let stayed = appStays, stayed != stays {
- if stays || selected == kept.made {
- kept.startOver()
+ let holds = Set(uninstallation.scan.leftovers.map(\.url)).union([uninstallation.app.url])
+ return update(
+ selected,
+ apps: [uninstallation.app.bundleIdentifier: (uninstallation.appStays(canUseHelper: canUseHelper), holds)],
+ selectable: uninstallation.selectable(canUseHelper: canUseHelper),
+ suggested: uninstallation.suggestedSelection(canUseHelper: canUseHelper)
+ )
+ }
+
+ public mutating func update(_ selected: Set<URL>, in bulk: BulkUninstallation, canUseHelper: Bool) -> Set<URL> {
+ var apps: [String: (stays: Bool, holds: Set<URL>)] = [:]
+ for uninstallation in bulk.uninstallations {
+ let identifier = uninstallation.app.bundleIdentifier
+ let stays = uninstallation.appStays(canUseHelper: canUseHelper) || apps[identifier]?.stays == true
+ apps[identifier] = (stays, Set(bulk.items.filter { $0.apps.contains(identifier) }.map(\.url)))
+ }
+ return update(
+ selected,
+ apps: apps,
+ selectable: bulk.selectable(canUseHelper: canUseHelper),
+ suggested: bulk.suggestedSelection(canUseHelper: canUseHelper)
+ )
+ }
+
+ /// `apps` says, for each app by bundle identifier, whether it stays now and every item it holds.
+ private mutating func update(
+ _ selected: Set<URL>,
+ apps: [String: (stays: Bool, holds: Set<URL>)],
+ selectable: Set<URL>,
+ suggested: Set<URL>
+ ) -> Set<URL> {
+ var suggested = suggested
+ let isAsMade = selected == kept.made
+ for (identifier, app) in apps {
+ guard let before = stayed[identifier], before != app.stays else { continue }
+ if app.stays || isAsMade {
+ kept.forget(app.holds)
} else {
- suggested = []
+ suggested.subtract(app.holds)
}
}
- appStays = stays
- selectable = uninstallation.selectable(canUseHelper: canUseHelper)
+ stayed = apps.mapValues(\.stays)
+ self.selectable = selectable
return kept.update(selected, selectable: selectable, suggested: suggested)
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift
index 67075f69..f3904072 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BulkUninstallationTests.swift
@@ -27,6 +27,35 @@ struct BulkUninstallationTests {
)
}
+ /// A checkbox can select what a row lets the person choose, by the rule of an app's own page: nothing Peel
+ /// leaves alone or with something excluded inside, nothing that needs the helper while it cannot act, no app
+ /// macOS keeps or the helper may not move, and nothing of an excluded app or of Peel. Everything Peel suggests
+ /// is among it.
+ @Test func selectsOnlyWhatARowLetsThePersonChoose() {
+ let notes = app("com.example.notes", "Notes")
+ let own = leftover("/Users/x/Library/Caches/com.example.notes", size: 100)
+ let keys = leftover("/Users/x/Library/Application Support/Notes Keys", size: 100).heldBack(.holdsKeys)
+ let excluded = leftover("/Users/x/Library/Application Support/Notes Excluded", size: 100).heldBack(.holdsAnExclusion)
+ let needsHelper = Uninstallation(
+ app: notes, appSize: 1000, appRequiresPrivileges: true,
+ scan: LeftoverScan(leftovers: [own, keys, excluded], unreadableLocations: [])
+ )
+ let chess = InstalledApp(url: URL(filePath: "/System/Applications/Chess.app"), bundleIdentifier: "com.apple.Chess", name: "Chess", isSystemProtected: true)
+ let chessData = leftover("/Users/x/Library/Containers/com.apple.Chess", size: 100)
+ var beyond = uninstallation(app("com.example.scribbler", "Scribbler"), [])
+ beyond.isAppBeyondTheHelper = true
+ var excludedApp = uninstallation(app("com.example.excluded", "Excluded"), [])
+ excludedApp.isExcluded = true
+ let peel = uninstallation(app("com.tuguidragos.Peel", "Peel"), [leftover("/Users/x/Library/Caches/com.tuguidragos.Peel", size: 100)])
+ let bulk = BulkUninstallation(uninstallations: [needsHelper, uninstallation(chess, [chessData]), beyond, excludedApp, peel])
+
+ #expect(bulk.selectable(canUseHelper: false) == [own.url, chessData.url])
+ #expect(bulk.selectable(canUseHelper: true) == [notes.url, own.url, chessData.url])
+ for canUseHelper in [false, true] {
+ #expect(bulk.suggestedSelection(canUseHelper: canUseHelper).isSubset(of: bulk.selectable(canUseHelper: canUseHelper)))
+ }
+ }
+
/// A batch keeps the order an app's own page keeps: what could not be measured first among the leftovers, since
/// it is most likely the biggest, then the largest. The apps come after their leftovers.
@Test func listsWhatCouldNotBeMeasuredFirst() {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/KeptSelectionTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/KeptSelectionTests.swift
index 02161392..c0935a23 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/KeptSelectionTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/KeptSelectionTests.swift
@@ -57,12 +57,13 @@ struct KeptSelectionTests {
#expect(kept.update(selected, selectable: [url("suggested"), url("picked")], suggested: []) == [url("picked")])
}
- @Test func startingOverGivesPeelsSuggestionAgain() {
+ /// A forgotten item takes Peel's suggestion at the next update, as if it were new, and only that item.
+ @Test func aForgottenItemTakesPeelsSuggestionAgain() {
var kept = KeptSelection()
- _ = kept.update([], selectable: [url("a"), url("b")], suggested: [url("a")])
+ _ = kept.update([], selectable: [url("a"), url("b")], suggested: [url("a"), url("b")])
- kept.startOver()
- #expect(kept.update([], selectable: [url("a"), url("b")], suggested: [url("a")]) == [url("a")])
+ kept.forget([url("a")])
+ #expect(kept.update([], selectable: [url("a"), url("b")], suggested: [url("a"), url("b")]) == [url("a")])
}
@Test func remembersTheSelectionItMade() {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UninstallSelectionTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UninstallSelectionTests.swift
index 813845c1..4d75269f 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UninstallSelectionTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UninstallSelectionTests.swift
@@ -16,9 +16,11 @@ struct UninstallSelectionTests {
)
}
- private func uninstallation(appRequiresPrivileges: Bool = false, _ leftovers: [Leftover]) -> Uninstallation {
+ private let other = InstalledApp(url: URL(filePath: "/Applications/Other.app"), bundleIdentifier: "com.example.other", name: "Other")
+
+ private func uninstallation(of app: InstalledApp? = nil, appRequiresPrivileges: Bool = false, _ leftovers: [Leftover]) -> Uninstallation {
Uninstallation(
- app: app,
+ app: app ?? self.app,
appSize: 10_000,
appRequiresPrivileges: appRequiresPrivileges,
scan: LeftoverScan(leftovers: leftovers, unreadableLocations: [])
@@ -87,4 +89,58 @@ struct UninstallSelectionTests {
_ = changed.update([], in: plan, canUseHelper: false)
#expect(changed.update([possible.url], in: plan, canUseHelper: true) == [possible.url])
}
+
+ /// Several apps' page keeps what the person deselected through its scans, as an app's own page does.
+ @Test func severalAppsKeepWhatThePersonDeselectedWhenTheyScanAgain() {
+ let own = leftover("com.example.app")
+ let others = leftover("com.example.other")
+ let bulk = BulkUninstallation(uninstallations: [uninstallation([own]), uninstallation(of: other, [others])])
+ var choices = UninstallSelection()
+
+ var selected = choices.update([], in: bulk, canUseHelper: true)
+ #expect(selected == [app.url, own.url, other.url, others.url])
+ selected.remove(own.url)
+
+ #expect(choices.update(selected, in: bulk, canUseHelper: true) == [app.url, other.url, others.url])
+ }
+
+ /// When one of several apps comes to stay, what it holds leaves the selection, the files it shares with the
+ /// other chosen apps included, and the others keep theirs.
+ @Test func whenOneOfSeveralAppsComesToStayOnlyItsFilesLeaveTheSelection() {
+ let own = leftover("com.example.app")
+ let possible = leftover("Example", confidence: .possible)
+ let shared = leftover("com.example.shared")
+ let others = leftover("com.example.other")
+ let bulk = BulkUninstallation(uninstallations: [
+ uninstallation(appRequiresPrivileges: true, [own, possible, shared]),
+ uninstallation(of: other, [others, shared]),
+ ])
+ var choices = UninstallSelection()
+
+ var selected = choices.update([], in: bulk, canUseHelper: true)
+ #expect(selected == [app.url, own.url, shared.url, other.url, others.url])
+ selected.insert(possible.url)
+
+ #expect(choices.update(selected, in: bulk, canUseHelper: false) == [other.url, others.url])
+ }
+
+ /// When one of several apps can go again, Peel's suggestion comes back for it only if nothing on the page was
+ /// changed meanwhile.
+ @Test func oneOfSeveralAppsThatCanGoAgainGetsTheSuggestionOnlyIfNothingWasChanged() {
+ let own = leftover("com.example.app")
+ let others = leftover("com.example.other")
+ let bulk = BulkUninstallation(uninstallations: [
+ uninstallation(appRequiresPrivileges: true, [own]),
+ uninstallation(of: other, [others]),
+ ])
+
+ var untouched = UninstallSelection()
+ let first = untouched.update([], in: bulk, canUseHelper: false)
+ #expect(first == [other.url, others.url])
+ #expect(untouched.update(first, in: bulk, canUseHelper: true) == [app.url, own.url, other.url, others.url])
+
+ var changed = UninstallSelection()
+ _ = changed.update([], in: bulk, canUseHelper: false)
+ #expect(changed.update([other.url], in: bulk, canUseHelper: true) == [other.url])
+ }
}
diff --git a/Peel/Model/BulkRemovalPlan.swift b/Peel/Model/BulkRemovalPlan.swift
index c3fea17e..11f8a200 100644
--- a/Peel/Model/BulkRemovalPlan.swift
+++ b/Peel/Model/BulkRemovalPlan.swift
@@ -15,6 +15,9 @@ final class BulkRemovalPlan {
/// Every installed app as of the last scan, to tell a chosen app's processes from another installed app's.
private var installedApps: [InstalledApp] = []
var selectedURLs: Set<URL> = []
+ private var choices = UninstallSelection()
+ /// Whether the helper can act, as last heard, so a scan that lands after it changed selects by what is true now.
+ private var canUseHelper = false
init(apps: [InstalledApp]) {
self.apps = apps
@@ -56,6 +59,7 @@ final class BulkRemovalPlan {
}
func refresh(installedApps: [InstalledApp], canUseHelper: Bool, casks: [HomebrewPackage] = [], receipts: Set<String> = []) async {
+ self.canUseHelper = canUseHelper
let apps = apps
guard let result = await scanRun.run({
await BulkUninstallation.prepare(
@@ -68,7 +72,14 @@ final class BulkRemovalPlan {
}) else { return }
bulk = result
self.installedApps = installedApps
- selectedURLs = result.suggestedSelection(canUseHelper: canUseHelper)
+ selectedURLs = choices.update(selectedURLs, in: result, canUseHelper: self.canUseHelper)
+ }
+
+ /// Brings the selection in line with the helper as it is now, without scanning again.
+ func follow(canUseHelper: Bool) {
+ self.canUseHelper = canUseHelper
+ guard let bulk else { return }
+ selectedURLs = choices.update(selectedURLs, in: bulk, canUseHelper: canUseHelper)
}
func removeSelected() async -> TrashResult {
diff --git a/Peel/Views/MultipleAppsView.swift b/Peel/Views/MultipleAppsView.swift
index cf32d94f..3c9c1f2d 100644
--- a/Peel/Views/MultipleAppsView.swift
+++ b/Peel/Views/MultipleAppsView.swift
@@ -110,6 +110,9 @@ struct MultipleAppsView: View {
.rescanOnExclusionChange("MultipleAppsView") {
await plan.refresh(installedApps: library.apps, canUseHelper: helper.canAct, casks: homebrew.caskEvidence, receipts: homebrew.receipts)
}
+ .onChange(of: helper.canAct) { _, canAct in
+ plan.follow(canUseHelper: canAct)
+ }
}
private var phase: ScanPhase {