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.

Remember when Homebrew answers in place of an app's own source

Țugui Dragoș9f55e5301b7c
diff --git a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateChecker.swift b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateChecker.swift
index f0f1ed0f..cacd46ca 100644
--- a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateChecker.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateChecker.swift
@@ -81,15 +81,15 @@ public struct UpdateChecker: Sendable {
? await appStoreAnswer(for: app, earlier: earlier) : await feedAnswer(for: app, earlier: earlier)
// A feed can be down, and the App Store may not sell the app here: what Homebrew knows is used then.
if let homebrew, own.status == .unsupported || own.status == .failed {
- return UpdateAnswer(status: homebrew, developer: own.developer)
+ return UpdateAnswer(status: homebrew, developer: own.developer, standsInFor: own.status)
}
return own
}
/// The status `answer(for:preference:casks:earlier:)` gives `app` without asking any server, from Homebrew's
/// casks and the app's own files alone, or nil when the app's own feed or the App Store has to be asked.
- /// `known` is the answer shown now: when it says the app's own source had no answer, what Homebrew knows now
- /// answers in its place, as a check would.
+ /// `known` is what that source answered last: where it had no answer, what Homebrew knows now answers in its
+ /// place, as a check would, and once Homebrew knows nothing that counts, the source's own answer stands again.
public func statusWithoutAsking(
for app: InstalledApp, preference: UpdateSource, casks: [HomebrewPackage], known: UpdateStatus? = nil
) -> UpdateStatus? {
@@ -97,7 +97,7 @@ public struct UpdateChecker: Sendable {
if let status = Self.statusWithoutAsking(for: app, preference: preference, homebrew: homebrew) {
return status
}
- return known == .unsupported || known == .failed ? homebrew : nil
+ return known == .unsupported || known == .failed ? homebrew ?? known : nil
}
private static func statusWithoutAsking(
@@ -361,12 +361,22 @@ public struct UpdateAnswer: Sendable, Hashable {
public let notes: ReleaseNotes?
/// The page the feed names for what is new in the update found, when it does not write it in.
public let notesPage: URL?
+ /// What the app's own feed or the App Store answered, `.failed` or `.unsupported`, when `status` is Homebrew's
+ /// answer in its place.
+ public let standsInFor: UpdateStatus?
- public init(status: UpdateStatus, developer: String? = nil, notes: ReleaseNotes? = nil, notesPage: URL? = nil) {
+ public init(
+ status: UpdateStatus,
+ developer: String? = nil,
+ notes: ReleaseNotes? = nil,
+ notesPage: URL? = nil,
+ standsInFor: UpdateStatus? = nil
+ ) {
self.status = status
self.developer = developer
self.notes = notes
self.notesPage = notesPage
+ self.standsInFor = standsInFor
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateMemory.swift b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateMemory.swift
index b9ea369a..5c631831 100644
--- a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateMemory.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateMemory.swift
@@ -16,6 +16,10 @@ public struct UpdateMemory: Codable, Equatable, Sendable {
/// The developer's name from the App Store. It is saved because an App Store app's signature is Apple's and
/// does not name the developer, and the next check may be a week away.
public let developer: String?
+ /// What the app's own feed or the App Store answered when `status` is Homebrew's answer in its place
+ /// (`UpdateAnswer.standsInFor`). Homebrew's answer counts only while Homebrew gives it, and the app falls back to
+ /// this once it doesn't.
+ public let standsInFor: UpdateStatus?
/// The `UpdateChecker.revision` that gave the answer; nil for one saved before revisions were kept.
public let revision: Int?
@@ -24,12 +28,14 @@ public struct UpdateMemory: Codable, Equatable, Sendable {
schedule: UpdateSchedule,
checked: Date?,
describing app: InstalledApp,
- developer: String? = nil
+ developer: String? = nil,
+ standsInFor: UpdateStatus? = nil
) {
self.status = status
self.schedule = schedule
self.checked = checked
self.developer = developer
+ self.standsInFor = standsInFor
version = app.version
buildVersion = app.buildVersion
revision = UpdateChecker.revision
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UpdateCheckerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UpdateCheckerTests.swift
index c4551dee..8e947670 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UpdateCheckerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UpdateCheckerTests.swift
@@ -283,6 +283,8 @@ private final class CannedProtocol: URLProtocol, @unchecked Sendable {
reply("feed.example.com", status: 500, "")
#expect(await recent.status(for: fedApp, preference: .developer, casks: current) == .upToDate)
+ #expect(await recent.answer(for: fedApp, preference: .developer, casks: current).standsInFor == .failed)
+ #expect(await recent.answer(for: fedApp, preference: .developer, casks: []).standsInFor == nil)
reply("feed.example.com", appcast("<item><enclosure url=\"https://feed.example.com/a.zip\" sparkle:version=\"100\" sparkle:shortVersionString=\"1.0\" /></item>"))
#expect(await recent.status(for: fedApp, preference: .developer, casks: outdated) == .upToDate, "its feed first")
@@ -304,7 +306,8 @@ private final class CannedProtocol: URLProtocol, @unchecked Sendable {
#expect(decided(recent, .developer, .upToDate) == nil)
#expect(decided(recent, .developer, found) == nil)
#expect(decided(recent, .developer, nil) == nil)
- #expect(decided(later, .developer, .failed) == nil)
+ #expect(decided(later, .developer, .failed) == .failed, "Homebrew can no longer tell")
+ #expect(decided(later, .appStore, .unsupported) == .unsupported)
}
@Test func aMacAppReadsNoIPhoneRecord() async {
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index c01c9ff1..34240989 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -584,27 +584,31 @@ final class AppLibrary {
guard let app = asked[id], apps.contains(where: { $0.id == id && $0.isTheSameBuild(as: app) }) else {
continue
}
+ let kept = answer.status.following(updateStatuses[id])
// Homebrew may have been read again while this answer was on its way: its casks decide where they can.
- let status = casks == self.casks
- ? answer.status
+ let shown = casks == self.casks
+ ? kept
: checker.statusWithoutAsking(
- for: app, preference: preference, casks: self.casks, known: answer.status
- ) ?? answer.status
- let kept = status.following(updateStatuses[id])
+ for: app, preference: preference, casks: self.casks, known: answer.standsInFor ?? kept
+ ) ?? kept
// Any write redraws the whole Applications list, so an answer that changes nothing is not written.
- if updateStatuses[id] != kept {
- updateStatuses[id] = kept
+ if updateStatuses[id] != shown {
+ updateStatuses[id] = shown
}
- if status != .failed {
+ if answer.status != .failed {
lastUpdateChecks[id] = .now
}
guard let identifier = app.bundleIdentifier else { continue }
memory[identifier] = UpdateMemory(
status: kept,
- schedule: UpdateSchedule.next(after: status, following: memory[identifier]?.schedule),
- checked: status == .failed ? memory[identifier]?.checked : .now,
+ // A feed that failed is asked again on its own count, even where Homebrew answered in its place.
+ schedule: UpdateSchedule.next(
+ after: answer.standsInFor ?? answer.status, following: memory[identifier]?.schedule
+ ),
+ checked: answer.status == .failed ? memory[identifier]?.checked : .now,
describing: app,
- developer: answer.developer ?? memory[identifier]?.developer
+ developer: answer.developer ?? memory[identifier]?.developer,
+ standsInFor: answer.standsInFor
)
// Only an answer that found this update can say what is new in it; a failed one keeps what was known.
if case .updateAvailable(let version, _, _) = kept, answer.status == kept {
@@ -800,18 +804,20 @@ final class AppLibrary {
UpdateMemoryStore.save(memory, to: defaults)
}
- /// Shows for each app Homebrew installed what a check would answer now without asking any server: what
- /// Homebrew's casks and the app's own files decide, and what Homebrew knows where the app's own source had no
- /// answer. Reading Homebrew costs no network request, so this doesn't wait for the update schedule, and
- /// Applications agrees with the Homebrew page.
+ /// Shows what a check would answer now without asking any server, for each app Homebrew installed and each app
+ /// whose last answer is Homebrew's in place of its own source: what Homebrew's casks and the app's own files
+ /// decide, and what Homebrew knows where that source had no answer, for as long as it counts. Reading Homebrew
+ /// costs no network request, so this doesn't wait for the update schedule, and Applications agrees with the
+ /// Homebrew page.
private func applyHomebrewStatuses() {
let preference = updateSource
- for app in apps where homebrewApps[app.id] != nil && isAskedAboutUpdates(app) {
- let known = updateStatuses[app.id]
+ for app in apps where isAskedAboutUpdates(app) {
+ let remembered = app.bundleIdentifier.flatMap { memory[$0] }.flatMap { $0.describes(app) ? $0 : nil }
+ guard homebrewApps[app.id] != nil || remembered?.standsInFor != nil else { continue }
let decided = updateChecker.statusWithoutAsking(
- for: app, preference: preference, casks: casks, known: known
+ for: app, preference: preference, casks: casks, known: remembered.map { $0.standsInFor ?? $0.status }
)
- guard let decided, decided != known else { continue }
+ guard let decided, decided != updateStatuses[app.id] else { continue }
updateStatuses[app.id] = decided
}
}
diff --git a/PeelTests/AppLibraryTests.swift b/PeelTests/AppLibraryTests.swift
index db0363fe..45a5f328 100644
--- a/PeelTests/AppLibraryTests.swift
+++ b/PeelTests/AppLibraryTests.swift
@@ -269,6 +269,49 @@ private nonisolated final class TestClock: Sendable {
#expect(library.shownUpdateStatus(of: app) == .unsupported, "not due, yet Homebrew can no longer tell")
}
+ @Test func homebrewsUpToDateInPlaceOfAFailedFeedEndsADayAfterHomebrewsCheck() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ let bundle = try caskApp("Fed", in: folder, feed: "https://feed.example.com/appcast.xml")
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let made = try MadeUpDefaults()
+ defer { made.forget() }
+ AppcastProtocol.body.withLock { $0 = Data() }
+ let clock = TestClock(homebrewChecked + 60 * 60)
+ let library = library(with: made.defaults, notesIn: folder, clock: clock)
+
+ #expect(await library.reveal([bundle]))
+ let app = try #require(library.apps.first { $0.bundleIdentifier == "org.example.fed" })
+ library.loadHomebrewCasks([cask("Fed", checked: homebrewChecked)], knowsItsOwnApps: true)
+ await library.checkForUpdates([app], force: true)
+ #expect(library.shownUpdateStatus(of: app) == .upToDate)
+ let saved = try #require(made.defaults.data(forKey: SettingsKey.updateMemory))
+ let memory = try JSONDecoder().decode([String: UpdateMemory].self, from: saved)
+ #expect(memory["org.example.fed"]?.schedule.isRetrying == true, "the feed failed")
+
+ clock.set(homebrewChecked + 2 * Homebrew.autoUpdateInterval)
+ library.loadHomebrewCasks([cask("Fed", checked: homebrewChecked)], knowsItsOwnApps: true)
+ #expect(library.shownUpdateStatus(of: app) == .failed)
+ }
+
+ @Test func homebrewsUpToDateInPlaceOfAFailedFeedEndsWhenHomebrewNoLongerHasTheApp() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ let bundle = try caskApp("Fed", in: folder, feed: "https://feed.example.com/appcast.xml")
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let made = try MadeUpDefaults()
+ defer { made.forget() }
+ AppcastProtocol.body.withLock { $0 = Data() }
+ let library = library(with: made.defaults, notesIn: folder, clock: TestClock(homebrewChecked + 60 * 60))
+
+ #expect(await library.reveal([bundle]))
+ let app = try #require(library.apps.first { $0.bundleIdentifier == "org.example.fed" })
+ library.loadHomebrewCasks([cask("Fed", checked: homebrewChecked)], knowsItsOwnApps: true)
+ await library.checkForUpdates([app], force: true)
+ #expect(library.shownUpdateStatus(of: app) == .upToDate)
+
+ library.loadHomebrewCasks([], knowsItsOwnApps: true)
+ #expect(library.shownUpdateStatus(of: app) == .failed)
+ }
+
@Test func anAppPeelNeverChecksGetsNothingFromHomebrew() async throws {
let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
let bundle = try caskApp("Tool", in: folder)