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.

Stop reading the disk for every outdated package as the Homebrew list is drawn

Țugui Dragoș79c343ebab2b
diff --git a/Packages/PeelCore/Sources/PeelCore/Homebrew/ExclusionAnswers.swift b/Packages/PeelCore/Sources/PeelCore/Homebrew/ExclusionAnswers.swift
new file mode 100644
index 00000000..73da831a
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Homebrew/ExclusionAnswers.swift
@@ -0,0 +1,24 @@
+/// What the exclusions say of each package, kept while nothing they go by changes: asking reads the disk while an
+/// identifier is excluded, and a list asks of every outdated package each time it is drawn. What runs asks anew.
+public struct ExclusionAnswers: Sendable {
+ private struct Question: Sendable, Equatable {
+ let exclusions: Exclusions
+ let package: HomebrewPackage
+ let apps: [InstalledApp]
+ let installation: HomebrewInstallation?
+ }
+
+ private var kept: [HomebrewPackage.ID: (question: Question, answer: Bool?)] = [:]
+
+ public init() {}
+
+ public mutating func answer(
+ _ exclusions: Exclusions, of package: HomebrewPackage, apps: [InstalledApp], installation: HomebrewInstallation?
+ ) -> Bool? {
+ let question = Question(exclusions: exclusions, package: package, apps: apps, installation: installation)
+ if let known = kept[package.id], known.question == question { return known.answer }
+ let answer = exclusions.excludes(package, apps: apps, installation: installation)
+ kept[package.id] = (question, answer)
+ return answer
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
index 2dc7fc26..3af00dce 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
@@ -120,6 +120,26 @@ struct HomebrewTests {
#expect(names(.notYetRead).isEmpty)
}
+ @Test func anAnswerForTheListStandsUntilWhatItGoesByChanges() throws {
+ let directory = try TemporaryDirectory()
+ let installation = HomebrewInstallation(version: "7.0.0", prefix: directory.url, repository: directory.url)
+ let package = HomebrewPackage(name: "sample", kind: .cask)
+ let excluded = Exclusions(bundleIdentifiers: ["org.example.kept"])
+ var answers = ExclusionAnswers()
+ func answer(_ exclusions: Exclusions, apps: [InstalledApp] = []) -> Bool? {
+ answers.answer(exclusions, of: package, apps: apps, installation: installation)
+ }
+
+ #expect(answer(excluded) == false)
+ try directory.directory("Caskroom/sample/1.0/org.example.kept")
+ #expect(answer(excluded) == false, "the disk was read again")
+ #expect(excluded.excludes(package, apps: [], installation: installation) == true)
+
+ let app = InstalledApp(url: try directory.directory("Sample.app"), bundleIdentifier: "org.example.s", name: "Sample")
+ #expect(answer(excluded, apps: [app]) == true, "another app was answered as the one before")
+ #expect(answer(Exclusions()) == false, "other exclusions were answered as the ones before")
+ }
+
@Test func peelsOwnCaskIsLeftToPeelsOwnUpdates() {
func cask(quitting identifiers: [String] = [], apps: [String] = []) -> HomebrewPackage {
HomebrewPackage(
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index 6fed58c2..c5b057c1 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -141,6 +141,8 @@ final class AppLibrary {
/// The cask each Homebrew-installed app came from. Matched with the same evidence everything else uses,
/// rather than by guessing a token from the app's name.
private(set) var homebrewApps: [InstalledApp.ID: HomebrewPackage] = [:]
+ /// The same, by cask, since the Homebrew page asks about every outdated cask each time it is drawn.
+ private var appsOfCasks: [HomebrewPackage.ID: [InstalledApp]] = [:]
/// The rows chosen in the list, which the page beside it shows.
var selection: Set<InstalledApp.ID> = []
/// An app something asked to show that Peel couldn't read, which the window says.
@@ -332,6 +334,7 @@ final class AppLibrary {
guard listed != apps else { return [] }
guard !AppCatalog.listsTheSameApps(listed, as: apps) else {
apps = listed
+ groupHomebrewApps()
lastOpenedRevision += 1
return []
}
@@ -522,7 +525,7 @@ final class AppLibrary {
}
func apps(of cask: HomebrewPackage) -> [InstalledApp] {
- apps.filter { self.cask(for: $0)?.id == cask.id }
+ appsOfCasks[cask.id] ?? []
}
func whyItCannotCheck(_ app: InstalledApp) -> CannotCheck {
@@ -1006,6 +1009,11 @@ final class AppLibrary {
homebrewApps = Dictionary(uniqueKeysWithValues: apps.compactMap { app in
CaskEvidence.installedCask(for: app, in: casks).map { (app.id, $0) }
})
+ groupHomebrewApps()
+ }
+
+ private func groupHomebrewApps() {
+ appsOfCasks = Dictionary(grouping: apps.filter { homebrewApps[$0.id] != nil }) { homebrewApps[$0.id]?.id ?? "" }
}
/// Records the team that signs each app, and keeps the apps whose team changed. The registry reports each
diff --git a/Peel/Model/HomebrewLibrary.swift b/Peel/Model/HomebrewLibrary.swift
index 278ab30f..d495e83d 100644
--- a/Peel/Model/HomebrewLibrary.swift
+++ b/Peel/Model/HomebrewLibrary.swift
@@ -16,6 +16,7 @@ final class HomebrewLibrary {
/// through an upgrade, but their versions change.
private(set) var revision = 0
@ObservationIgnored private let appsOfCask: (HomebrewPackage) -> [InstalledApp]
+ @ObservationIgnored private var exclusionAnswers = ExclusionAnswers()
/// Waiting for a cask's apps to quit before Homebrew changes them. These are the library's, not a page's: choosing
/// another row builds another page, and the command the person asked for still has to run.
let quittingToUpgrade = QuitBeforeRemoving()
@@ -300,15 +301,28 @@ final class HomebrewLibrary {
+ retired.filter { $0.retirement?.stage == .deprecated }
}
- /// What Upgrade All upgrades: the rest are shown as out of date and left alone (`joinsUpgradeAll`, the exclusions),
- /// and all of them while Homebrew would clean up after an upgrade, which deletes old versions for good.
+ /// What Upgrade All upgrades, as the page shows it: the rest are shown as out of date and left alone
+ /// (`joinsUpgradeAll`, the exclusions), and all of them while Homebrew would clean up after an upgrade, which
+ /// deletes old versions for good.
var upgradable: [HomebrewPackage] {
+ guard !overrides.contains(.cleansUp) else { return [] }
+ return outdated.filter { $0.joinsUpgradeAll && exclusionAnswer(of: $0) == false }
+ }
+
+ /// `upgradable`, asked anew of the exclusions and the disk, for what is about to run.
+ var upgradableNow: [HomebrewPackage] {
guard !overrides.contains(.cleansUp) else { return [] }
return ExclusionsStore.shared.exclusions.joiningUpgradeAll(
outdated, appsOf: appsOfCask, installation: installation
)
}
+ private func exclusionAnswer(of package: HomebrewPackage) -> Bool? {
+ exclusionAnswers.answer(
+ ExclusionsStore.shared.exclusions, of: package, apps: appsOfCask(package), installation: installation
+ )
+ }
+
/// Why the exclusions keep Homebrew from changing `package`, or nil when they don't: an upgrade deletes the version
/// it replaces for good, as `brew uninstall` deletes the package.
func exclusionRefusal(of package: HomebrewPackage) -> LocalizedStringResource? {
@@ -323,9 +337,7 @@ final class HomebrewLibrary {
}
func isExcluded(_ package: HomebrewPackage) -> Bool {
- let exclusions = ExclusionsStore.shared.exclusions
- return exclusions.isKnown
- && exclusions.excludes(package, apps: appsOfCask(package), installation: installation) == true
+ ExclusionsStore.shared.exclusions.isKnown && exclusionAnswer(of: package) == true
}
/// How many packages of `kind` are installed, or nil while Homebrew hasn't said what is installed.
@@ -574,7 +586,7 @@ final class HomebrewLibrary {
try await Homebrew.upgrade(package, onOutput: onOutput)
}
case .upgradeAll:
- let packages = upgradable
+ let packages = upgradableNow
return await following { onOutput throws(Homebrew.CommandFailure) in
try await Homebrew.upgrade(packages, onOutput: onOutput)
}
diff --git a/Peel/Views/Homebrew/HomebrewList.swift b/Peel/Views/Homebrew/HomebrewList.swift
index bdc991a5..faf9452c 100644
--- a/Peel/Views/Homebrew/HomebrewList.swift
+++ b/Peel/Views/Homebrew/HomebrewList.swift
@@ -699,7 +699,7 @@ struct HomebrewList: View {
}
private func upgradeAll() {
- let quit = QuitBeforeHomebrew(homebrew.upgradable, library: library)
+ let quit = QuitBeforeHomebrew(homebrew.upgradableNow, library: library)
homebrew.quittingToUpgrade.check(quit.processes) {
Task { await homebrew.run(.upgradeAll, reopening: quit.reopen) }
}