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.

Read the list of app folders once for each reading of the apps

Țugui Dragoș338cd82d45b4
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
index f4338e77..52b66d45 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
@@ -90,7 +90,13 @@ public enum AppCatalog {
/// folders cannot be read, the reading says so (`listOfFoldersUnreadable`).
@concurrent
public static func scan(choices: AppFolders = AppFolders()) async -> AppScan {
- guard let chosen = choices.load() else {
+ await scan(chosen: choices.load())
+ }
+
+ /// The same reading, from a list of chosen folders already read: nil when it could not be read.
+ @concurrent
+ public static func scan(chosen: [URL]?) async -> AppScan {
+ guard let chosen else {
let standard = await scan(in: standardDirectories)
return AppScan(
apps: standard.apps,
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index 527d7d62..a16c0c14 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -69,7 +69,7 @@ final class AppLibrary {
private(set) var hasLoaded = false
/// The folders the person chose for apps, beside the Applications folders, or nil when their list cannot be read.
/// `PeelApp` watches them and reads the apps again when they change.
- private(set) var folders = AppFolders().load()
+ private(set) var folders: [URL]?
private(set) var couldNotSaveFolders = false
/// What the last reading of the folders could not read, so the apps in it are missing from `apps`
/// (`AppScan.unreadable`).
@@ -156,6 +156,8 @@ final class AppLibrary {
private(set) var releaseNotes = ReleaseNotesMemory()
private let releaseNotesStore: ReleaseNotesStore
private let defaults: UserDefaults
+ private let appFolders: AppFolders
+ private let appMemory: AppMemory
/// Apps shown from outside the scanned folders, such as one in Downloads or in the Trash. Kept apart,
/// because the next reading of the folders doesn't find them and would drop their row and their page.
private var revealed: [InstalledApp] = []
@@ -172,11 +174,16 @@ final class AppLibrary {
init(
defaults: UserDefaults = .standard,
updateChecker: UpdateChecker = UpdateChecker(),
- releaseNotesStore: ReleaseNotesStore = ReleaseNotesStore()
+ releaseNotesStore: ReleaseNotesStore = ReleaseNotesStore(),
+ appFolders: AppFolders = AppFolders(),
+ appMemory: AppMemory = AppMemory()
) {
self.defaults = defaults
self.updateChecker = updateChecker
self.releaseNotesStore = releaseNotesStore
+ self.appFolders = appFolders
+ self.appMemory = appMemory
+ folders = appFolders.load()
ignoredApps = Set(defaults.stringArray(forKey: SettingsKey.ignoredUpdateApps) ?? [])
skippedVersions = defaults.dictionary(forKey: SettingsKey.skippedUpdateVersions) as? [String: String] ?? [:]
announced = AnnouncedUpdates(
@@ -223,7 +230,8 @@ final class AppLibrary {
if !hasLoaded {
releaseNotes = await releaseNotesStore.load()
}
- let found = await scanRun.run { await AppCatalog.scan() }
+ let chosen = readFolders()
+ let found = await scanRun.run { await AppCatalog.scan(chosen: chosen) }
if let found {
keepUnreadable(of: found)
adopt(found.apps)
@@ -232,7 +240,7 @@ final class AppLibrary {
isLoading = false
hasLoaded = true
if let found {
- await AppMemory().remember(found.apps)
+ await appMemory.remember(found.apps)
}
}
@@ -254,17 +262,25 @@ final class AppLibrary {
/// Sets the list of folders aside when it cannot be read, so the folders can be chosen again.
@discardableResult
func startFoldersOver() -> StartOver {
- let store = AppFolders()
- let answer = store.startOver()
+ let answer = appFolders.startOver()
if answer == .started { couldNotSaveFolders = false }
- folders = store.load()
+ folders = appFolders.load()
return answer
}
+ var foldersFile: URL { appFolders.url }
+
private func changeFolders(_ change: (AppFolders) -> Bool) {
- let store = AppFolders()
- couldNotSaveFolders = !change(store)
- folders = store.load()
+ couldNotSaveFolders = !change(appFolders)
+ folders = appFolders.load()
+ }
+
+ /// Reads the list of chosen folders for a reading of the apps, which looks in exactly these, so Settings and the
+ /// pages say the same of it. Set before the reading, so a change made in Settings meanwhile is never undone.
+ private func readFolders() -> [URL]? {
+ let read = appFolders.load()
+ if read != folders { folders = read }
+ return read
}
/// Reads the folders again without emptying the list first, so an app installed while Peel is open
@@ -276,7 +292,8 @@ final class AppLibrary {
@discardableResult
func refresh() async -> [InstalledApp] {
lastRead = .now
- guard let scan = await scanRun.run({ await AppCatalog.scan() }) else { return [] }
+ let chosen = readFolders()
+ guard let scan = await scanRun.run({ await AppCatalog.scan(chosen: chosen) }) else { return [] }
keepUnreadable(of: scan)
let found = scan.apps
let listed = AppCatalog.sorted(found + stillThere(revealed, beside: found))
@@ -290,7 +307,7 @@ final class AppLibrary {
let changed = adopt(found)
// Remembered whenever the list changes, so an app installed and removed between two visits to Orphaned
// Files is still known. Done last, so nothing above is left half done while this waits.
- await AppMemory().remember(found, gone: gone)
+ await appMemory.remember(found, gone: gone)
return changed
}
diff --git a/Peel/Views/AppList.swift b/Peel/Views/AppList.swift
index e60156f9..d56c2e0f 100644
--- a/Peel/Views/AppList.swift
+++ b/Peel/Views/AppList.swift
@@ -24,7 +24,7 @@ struct AppList: View {
detail: Text("Peel can’t read its list of app folders, so Orphaned Files lists nothing. Starting over keeps the old list beside a new one."),
kind: .note
) {
- StartOverButton(file: AppFolders.defaultURL, startedOver: "Started the list of app folders over.") {
+ StartOverButton(file: library.foldersFile, startedOver: "Started the list of app folders over.") {
library.startFoldersOver()
}
}
diff --git a/Peel/Views/Intel/IntelList.swift b/Peel/Views/Intel/IntelList.swift
index 17a659c7..5aa6e541 100644
--- a/Peel/Views/Intel/IntelList.swift
+++ b/Peel/Views/Intel/IntelList.swift
@@ -148,7 +148,7 @@ struct IntelList: View {
@ViewBuilder
private var unreadActions: some View {
if library.listOfFoldersUnreadable {
- StartOverButton(file: AppFolders.defaultURL, startedOver: "Started the list of app folders over.") {
+ StartOverButton(file: library.foldersFile, startedOver: "Started the list of app folders over.") {
library.startFoldersOver()
}
}
diff --git a/Peel/Views/Orphans/OrphanList.swift b/Peel/Views/Orphans/OrphanList.swift
index 9cb291ea..7374f420 100644
--- a/Peel/Views/Orphans/OrphanList.swift
+++ b/Peel/Views/Orphans/OrphanList.swift
@@ -55,7 +55,7 @@ struct OrphanList: View {
parts.dropFirst().reduce(parts.first ?? Text(verbatim: "")) { Text("\($0)\n\n\($1)") }
} actions: {
if library.listOfFoldersUnreadable {
- StartOverButton(file: AppFolders.defaultURL, startedOver: "Started the list of app folders over.") {
+ StartOverButton(file: library.foldersFile, startedOver: "Started the list of app folders over.") {
library.startFoldersOver()
}
}
diff --git a/Peel/Views/Settings/AppFoldersSection.swift b/Peel/Views/Settings/AppFoldersSection.swift
index 84cbed1c..91bf5c3a 100644
--- a/Peel/Views/Settings/AppFoldersSection.swift
+++ b/Peel/Views/Settings/AppFoldersSection.swift
@@ -49,7 +49,7 @@ struct AppFoldersSection: View {
HStack {
StatusLabel(title: Text("Peel can’t read its list of app folders, so Orphaned Files lists nothing. Starting over keeps the old list beside a new one."))
Spacer()
- StartOverButton(file: AppFolders.defaultURL, startedOver: "Started the list of app folders over.") {
+ StartOverButton(file: library.foldersFile, startedOver: "Started the list of app folders over.") {
library.startFoldersOver()
}
}
diff --git a/PeelTests/AppLibraryTests.swift b/PeelTests/AppLibraryTests.swift
index 504fa77b..d4a7ba3d 100644
--- a/PeelTests/AppLibraryTests.swift
+++ b/PeelTests/AppLibraryTests.swift
@@ -898,4 +898,30 @@ private nonisolated final class TestClock: Sendable {
library.loadHomebrewCasks([cask("Fed", checked: homebrewChecked)], knowsItsOwnApps: true)
#expect(library.shownUpdateStatus(of: app) == .upToDate)
}
+
+ @Test func aListOfFoldersDamagedWhilePeelRunsIsSaidAlikeEverywhereAndStartedOver() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let made = try MadeUpDefaults()
+ defer { made.forget() }
+ let list = AppFolders(url: folder.appending(path: "app-folders.json"))
+ let library = AppLibrary(
+ defaults: made.defaults,
+ releaseNotesStore: ReleaseNotesStore(url: folder.appending(path: "release-notes.json")),
+ appFolders: list,
+ appMemory: AppMemory(url: folder.appending(path: "apps.json"))
+ )
+ await library.load()
+ #expect(library.folders == [] && !library.listOfFoldersUnreadable)
+
+ try Data("not a list".utf8).write(to: list.url)
+ await library.refresh()
+ #expect(library.listOfFoldersUnreadable && library.folders == nil)
+
+ #expect(library.startFoldersOver() == .started)
+ #expect(library.folders == [])
+ await library.refresh()
+ #expect(library.folders == [] && !library.listOfFoldersUnreadable)
+ }
}