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.

Follow a change in the list of apps whichever reading found it

Țugui Dragoșd135f08b6d5a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 87d71622..4f4a3b2e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -460,6 +460,8 @@ its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.
started again, and the question came back. Moving it stops the job, and Put Back starts it again.
- A cask definition Homebrew can't read costs only its own app: before, one in a tap took away what every other cask
says about the apps it names, and the files only a cask names left their pages.
+- Rescan pressed right after an app is installed no longer leaves it unknown to Homebrew's lists: its page said it
+ couldn't check for updates and left out Homebrew's receipt until the next app changed.
- Without Full Disk Access, an app's page no longer makes macOS ask about your photos, since `~/Library/Photos` isn't
looked into.
- On macOS 27, the list keeps its place when the first update arrives or the last one leaves.
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index 6166d526..5f840e1a 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -184,6 +184,10 @@ final class AppLibrary {
let scanRun = ScanRun()
private var lastRead: ContinuousClock.Instant?
@ObservationIgnored private var firstReading: Task<Void, Never>?
+ /// The revision `followChanges` last ran its work for.
+ @ObservationIgnored private var followedRevision = 0
+ /// The apps readings found at another build since that work last ran.
+ @ObservationIgnored private var changedBuilds: [InstalledApp.ID: InstalledApp] = [:]
private let updateChecker: UpdateChecker
private let teamRegistry = TeamRegistry()
@@ -261,6 +265,8 @@ final class AppLibrary {
showFolders(of: found, readSince: changes)
keepUnreadable(of: found)
adopt(found.apps)
+ // Launch does for this reading what `followChanges` does for the others.
+ followedRevision = revision
}
recall()
isLoading = false
@@ -272,9 +278,9 @@ final class AppLibrary {
/// Calls `refresh()` unless a reading of the folders started in the last 30 seconds, even one still running. For
/// Peel coming forward: the folder watcher sees every real change, so a recent read is enough.
- func refreshUnlessRecent() async -> [InstalledApp] {
- if let lastRead, ContinuousClock.now - lastRead < .seconds(30) { return [] }
- return await refresh()
+ func refreshUnlessRecent() async {
+ if let lastRead, ContinuousClock.now - lastRead < .seconds(30) { return }
+ await refresh()
}
func addFolders(_ urls: [URL]) {
@@ -314,36 +320,44 @@ final class AppLibrary {
}
/// Reads the folders again without emptying the list first, so an app installed while Peel is open
- /// appears in its place instead of the whole list blinking.
- ///
- /// Returns the apps whose build changed, so the caller can check them again. An upgrade keeps the path,
- /// and with it the app's identity, so what was cached about the old build (its size, its update status,
- /// when it was last checked) is dropped rather than shown for a version it doesn't describe.
- @discardableResult
- func refresh() async -> [InstalledApp] {
+ /// appears in its place instead of the whole list blinking. An upgrade keeps the path, and with it the app's
+ /// identity, so what was cached about the old build (its size, its update status, when it was last checked)
+ /// is dropped rather than shown for a version it doesn't describe.
+ func refresh() async {
await firstReading?.value
lastRead = .now
let changes = folderChanges
let read = await scanRun.run { [appFolders] in await AppCatalog.scan(choices: appFolders) }
if !scanRun.isRunning { askedForReading = false }
- guard let scan = read else { return [] }
+ guard let scan = read else { return }
showFolders(of: scan, readSince: changes)
keepUnreadable(of: scan)
let found = scan.apps
let listed = AppCatalog.sorted(found + stillThere(revealed, beside: found))
- guard listed != apps else { return [] }
+ guard listed != apps else { return }
guard !AppCatalog.listsTheSameApps(listed, as: apps) else {
apps = listed
groupHomebrewApps()
lastOpenedRevision += 1
- return []
+ return
}
let gone = Set(apps.compactMap(\.bundleIdentifier)).subtracting(listed.compactMap(\.bundleIdentifier))
- let changed = adopt(found)
+ for app in adopt(found) { changedBuilds[app.id] = app }
// 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)
- return changed
+ }
+
+ /// Runs `work` for each change of the list, with the apps found at another build. Changes that arrive while it
+ /// runs are one more run. Whichever reading comes first, the folder watch, Rescan, or Peel coming forward, the
+ /// others find nothing new, so what depends on the list follows it here and not after a reading.
+ func followChanges(_ work: ([InstalledApp]) async -> Void) async {
+ for await revision in Observations({ self.revision }) where revision != followedRevision {
+ followedRevision = revision
+ let changed = Array(changedBuilds.values)
+ changedBuilds = [:]
+ await work(changed)
+ }
}
/// Checks again what a reading found at another build: whether it has an update and who signs it, since what
@@ -357,8 +371,7 @@ final class AppLibrary {
/// Reads the folders again and asks about every app's updates, in a round of its own so the list isn't held.
func rescan() async {
askedForReading = true
- let changed = await refresh()
- if !changed.isEmpty { await checkSigningTeams() }
+ await refresh()
checkEveryAppAgain()
}
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index 3b5b5d80..eba0eb60 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -184,33 +184,32 @@ struct PeelApp: App {
private func followApps(in folders: [URL], readingNow: Bool) async {
if readingNow {
- await appsMayHaveChanged()
+ await library.refresh()
}
for await _ in FolderWatch.changes(in: folders) {
try? await Task.sleep(for: .seconds(1), tolerance: .milliseconds(250))
guard !Task.isCancelled else { return }
- await appsMayHaveChanged()
+ await library.refresh()
}
}
- private func appsMayHaveChanged() async {
- let before = library.revision
- let changed = await library.refresh()
- // Any write inside a bundle wakes the watch, so Homebrew and the rest run only when the app list changed.
- guard library.revision != before else { return }
- Task { await IconCache.warm(library.apps.map(\.url)) }
- await homebrew.refresh()
- // A newly installed app may match a cask Homebrew knows about, so the known casks are read again.
- await homebrew.loadKnownCasks(for: library.apps)
- library.loadHomebrewCasks(homebrew.caskEvidence, knowsItsOwnApps: homebrew.knowsItsOwnApps)
- // A bundle that changed on disk is a different build, and what was cached about it describes the old
- // one. Those apps are checked again at once, whoever updated them: Peel, the Homebrew page, or
- // `brew upgrade` in Terminal.
- await library.checkAgain(changed)
- // A newly installed app has never been checked, so it is due. The round checks only the apps that
- // are due.
- if checksForAppUpdates {
- await library.checkForUpdates(library.apps)
+ /// Any write inside a bundle wakes the watch, so Homebrew and the rest run only when the list of apps changed.
+ private func followAppList() async {
+ await library.followChanges { changed in
+ Task { await IconCache.warm(library.apps.map(\.url)) }
+ await homebrew.refresh()
+ // A newly installed app may match a cask Homebrew knows about, so the known casks are read again.
+ await homebrew.loadKnownCasks(for: library.apps)
+ library.loadHomebrewCasks(homebrew.caskEvidence, knowsItsOwnApps: homebrew.knowsItsOwnApps)
+ // A bundle that changed on disk is a different build, and what was cached about it describes the old
+ // one. Those apps are checked again at once, whoever updated them: Peel, the Homebrew page, or
+ // `brew upgrade` in Terminal.
+ await library.checkAgain(changed)
+ // A newly installed app has never been checked, so it is due. The round checks only the apps that
+ // are due.
+ if checksForAppUpdates {
+ await library.checkForUpdates(library.apps)
+ }
}
}
@@ -230,11 +229,7 @@ struct PeelApp: App {
// Home's checks run again whatever page is showing: permissions change in System Settings, and the
// badge on Home in the sidebar shows a missing one. They read the helper's status again too.
Task { await home.refresh() }
- Task {
- // Whichever read finds a changed bundle checks it again. When two reads overlap, only the newer
- // one reports what changed, and it may be this one rather than the folder watcher's.
- await library.checkAgain(await library.refreshUnlessRecent())
- }
+ Task { await library.refreshUnlessRecent() }
// A watch that could not start, for want of Full Disk Access or for any other reason, is tried again.
if watchesTrash, trashMonitor.status != .watching {
trashMonitor.start()
@@ -333,8 +328,8 @@ struct PeelApp: App {
Navigator.shared.openWindow = openWindow
background.start(
[
- followFolders, followAppsForTheTrash, askWhenDue, followFindings, followActivations, watchFreeSpace,
- followHelper, followHomebrew, followUpdates,
+ followFolders, followAppList, followAppsForTheTrash, askWhenDue, followFindings, followActivations,
+ watchFreeSpace, followHelper, followHomebrew, followUpdates,
],
stoppingWithIt: [duplicates.stop, library.stopCheckingEveryApp]
)
diff --git a/Peel/Views/AppDetailView.swift b/Peel/Views/AppDetailView.swift
index b0e00d3a..0d420dae 100644
--- a/Peel/Views/AppDetailView.swift
+++ b/Peel/Views/AppDetailView.swift
@@ -559,7 +559,7 @@ struct AppDetailView: View {
}
if result.trashed.contains(where: { $0.originalURL == plan.app.url }) {
- await library.checkAgain(await library.refresh())
+ await library.refresh()
} else {
await plan.refresh(
installedApps: library.apps,
diff --git a/Peel/Views/MultipleAppsView.swift b/Peel/Views/MultipleAppsView.swift
index 7cc4c8be..79a9ae62 100644
--- a/Peel/Views/MultipleAppsView.swift
+++ b/Peel/Views/MultipleAppsView.swift
@@ -431,7 +431,7 @@ struct MultipleAppsView: View {
// would fail for each one.
let bundles = Set(plan.apps.map(\.url))
if result.trashed.contains(where: { bundles.contains($0.originalURL) }) {
- await library.checkAgain(await library.refresh())
+ await library.refresh()
} else {
await plan.refresh(
installedApps: library.apps,
diff --git a/Peel/Views/Orphans/OrphanList.swift b/Peel/Views/Orphans/OrphanList.swift
index 4c7552f8..d0fe9122 100644
--- a/Peel/Views/Orphans/OrphanList.swift
+++ b/Peel/Views/Orphans/OrphanList.swift
@@ -127,7 +127,7 @@ struct OrphanList: View {
) {
// What could not be read may be readable now, so the apps are read again first.
if !library.readEverything {
- await library.checkAgain(await library.refresh())
+ await library.refresh()
}
await orphans.refresh(from: library)
}
diff --git a/PeelTests/AppLibraryTests.swift b/PeelTests/AppLibraryTests.swift
index f6744cfe..df154b28 100644
--- a/PeelTests/AppLibraryTests.swift
+++ b/PeelTests/AppLibraryTests.swift
@@ -1109,6 +1109,53 @@ private nonisolated final class TestClock: Sendable {
#expect(!library.isReadingAsAsked)
}
+ @Test(.timeLimit(.minutes(1))) func whatDependsOnTheListFollowsEachChangeWhicheverReadingFoundIt() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ let chosen = folder.appending(path: "Chosen", directoryHint: .isDirectory)
+ try FileManager.default.createDirectory(at: chosen, withIntermediateDirectories: true)
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let made = try MadeUpDefaults()
+ defer { made.forget() }
+ made.defaults.set(false, forKey: SettingsKey.checksForAppUpdates)
+ let library = AppLibrary(
+ defaults: made.defaults,
+ releaseNotesStore: ReleaseNotesStore(url: folder.appending(path: "release-notes.json")),
+ appFolders: AppFolders(url: folder.appending(path: "app-folders.json")),
+ appMemory: AppMemory(url: folder.appending(path: "apps.json"))
+ )
+ library.addFolders([chosen])
+ let (heard, tell) = AsyncStream.makeStream(of: [String].self)
+ let following = Task { await library.followChanges { tell.yield($0.map(\.name)) } }
+ defer { following.cancel() }
+ var next = heard.makeAsyncIterator()
+ func install(version: String) throws {
+ let bundle = chosen.appending(path: "Followed.app", directoryHint: .isDirectory)
+ try FileManager.default.createDirectory(
+ at: bundle.appending(path: "Contents"), withIntermediateDirectories: true
+ )
+ let info: [String: Any] = [
+ "CFBundleIdentifier": "org.example.followed", "CFBundleName": "Followed",
+ "CFBundlePackageType": "APPL", "CFBundleShortVersionString": version, "CFBundleVersion": version,
+ ]
+ try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0)
+ .write(to: bundle.appending(path: "Contents/Info.plist"))
+ }
+
+ await library.load()
+ try install(version: "1")
+ await library.refresh()
+ #expect(await next.next() == [], "the first reading is launch's, and a new app is at no other build")
+
+ try install(version: "2")
+ await library.rescan()
+ #expect(await next.next() == ["Followed"], "Rescan's reading is followed like the folder watch's")
+
+ await library.refresh()
+ try install(version: "3")
+ await library.refresh()
+ #expect(await next.next() == ["Followed"], "a reading that found nothing new ran nothing")
+ }
+
@Test func aReadingWaitsForTheListOfFoldersAwayFromTheMainActor() async throws {
let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)