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 Homebrew again once it has checked for new versions

Țugui Dragoșcfc911fb7818
diff --git a/Peel/Model/HomebrewLibrary.swift b/Peel/Model/HomebrewLibrary.swift
index 82a14d53..dd951bfe 100644
--- a/Peel/Model/HomebrewLibrary.swift
+++ b/Peel/Model/HomebrewLibrary.swift
@@ -120,6 +120,8 @@ final class HomebrewLibrary {
/// Whether Homebrew answered the last reading, so its definitions are on this Mac.
private(set) var hasAnswered = false
+ /// Homebrew's last check for new versions as the last reading found it (`Homebrew.lastUpdateCheck()`).
+ private var checkedForUpdates: Date?
var selectedPackage: HomebrewPackage? {
switch selection {
@@ -178,6 +180,14 @@ final class HomebrewLibrary {
isInstalled = Homebrew.executableURL != nil
}
+ /// Reads the packages again when Homebrew checked for new versions since they were read, as `brew update` in
+ /// Terminal does: `checked` is its last check now (`Homebrew.lastUpdateCheck()`). Returns whether it read them.
+ func readAgain(ifHomebrewCheckedAt checked: Date?) async -> Bool {
+ guard packages != nil, runningCommand == nil, checked != checkedForUpdates else { return false }
+ await refresh()
+ return true
+ }
+
/// Reads the installed packages again. `includingReclaimable` also asks how much `brew cleanup` would
/// free, which only the Homebrew page shows. That is a Ruby command that walks the cache and the Cellar,
/// so it is asked for only while that page is open or after a command, never on every change in
@@ -188,9 +198,11 @@ final class HomebrewLibrary {
forgetHomebrew()
return
}
+ let checked = await Homebrew.lastUpdateCheck()
guard let reading = await scanRun.run({ await self.read(includingReclaimable: includingReclaimable) }),
isInstalled
else { return }
+ checkedForUpdates = checked
guard let (found, installed, preview, overridden) = reading else {
needsDefinitions = true
hasAnswered = false
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index 0532bbcb..c8a7e5ba 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -192,6 +192,7 @@ struct PeelApp: App {
history.checkReadability()
stats.reload()
homebrew.checkInstalled()
+ Task { await followHomebrewsChecks() }
// 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() }
@@ -244,10 +245,18 @@ struct PeelApp: App {
while !Task.isCancelled {
try? await Task.sleep(for: .seconds(Self.updateCheckInterval), tolerance: .seconds(60))
guard !Task.isCancelled, checksForAppUpdates else { continue }
+ await followHomebrewsChecks()
await checkForUpdates()
}
}
+ /// Reads Homebrew again once it checked for new versions since Peel last read it, as `brew update` in Terminal
+ /// does, so what it says now reaches Applications.
+ private func followHomebrewsChecks() async {
+ guard await homebrew.readAgain(ifHomebrewCheckedAt: Homebrew.lastUpdateCheck()) else { return }
+ library.loadHomebrewCasks(homebrew.caskEvidence, knowsItsOwnApps: homebrew.knowsItsOwnApps)
+ }
+
private func watchFreeSpace() async {
while !Task.isCancelled {
if warnsWhenDiskIsNearlyFull {
diff --git a/PeelTests/HomebrewLibraryTests.swift b/PeelTests/HomebrewLibraryTests.swift
new file mode 100644
index 00000000..d78556b5
--- /dev/null
+++ b/PeelTests/HomebrewLibraryTests.swift
@@ -0,0 +1,19 @@
+import Foundation
+@testable import Peel
+import PeelCore
+import Testing
+
+@Suite(.serialized) struct HomebrewLibraryTests {
+ @Test func readsHomebrewAgainOnceItCheckedForNewVersions() async {
+ guard Homebrew.executableURL != nil, await Homebrew.hasLocalDefinitions() else { return }
+ let homebrew = HomebrewLibrary()
+ await homebrew.refresh()
+ let read = homebrew.revision
+ let checked = await Homebrew.lastUpdateCheck()
+
+ #expect(await !homebrew.readAgain(ifHomebrewCheckedAt: checked))
+ #expect(homebrew.revision == read)
+ #expect(await homebrew.readAgain(ifHomebrewCheckedAt: (checked ?? .now) + 1))
+ #expect(homebrew.revision == read + 1)
+ }
+}