Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Write Time Machine's marks away from the main actor
Țugui Dragoș09d321cac803
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/TimeMachineExclusion.swift b/Packages/PeelCore/Sources/PeelCore/Developer/TimeMachineExclusion.swiftindex 874cbbdd..dbd90f07 100644--- a/Packages/PeelCore/Sources/PeelCore/Developer/TimeMachineExclusion.swift+++ b/Packages/PeelCore/Sources/PeelCore/Developer/TimeMachineExclusion.swift@@ -51,6 +51,12 @@ public enum TimeMachineExclusion {Set(urls.filter(isExcluded))}+ /// `setExcluded(_:_:)` away from the caller's thread, since on a network volume a mark can take its time.+ @concurrent+ public static func marking(_ isExcluded: Bool, _ urls: [URL]) async -> Set<URL> {+ setExcluded(isExcluded, urls)+ }+/// Sets or clears the mark on each folder and returns the folders that could not be changed. A folder/// that refuses the mark, such as one owned by root, does not stop the others.public static func setExcluded(_ isExcluded: Bool, _ urls: [URL]) -> Set<URL> {diff --git a/Peel/Model/ProjectLibrary.swift b/Peel/Model/ProjectLibrary.swiftindex 7c4d06a3..c83a0040 100644--- a/Peel/Model/ProjectLibrary.swift+++ b/Peel/Model/ProjectLibrary.swift@@ -49,13 +49,20 @@ final class ProjectLibrary {var selectedURLs: Set<URL> = []private let defaults: UserDefaults+ private let mark: @Sendable (Bool, [URL]) async -> Set<URL>+ /// What was clicked for each project whose marks are being written.+ private var backupsBeingChanged: [URL: Bool] = [:]@ObservationIgnored private var scannedAt: ContinuousClock.Instant?/// How long what a scan saw is shown before the page, appearing or with Peel coming forward, looks again.static let scanAgainAfter: Duration = .seconds(60 * 60)- init(defaults: UserDefaults = .standard) {+ init(+ defaults: UserDefaults = .standard,+ markingForBackups mark: @escaping @Sendable (Bool, [URL]) async -> Set<URL> = TimeMachineExclusion.marking+ ) {self.defaults = defaults+ self.mark = markfolders = (defaults.array(forKey: Self.foldersKey) as? [String] ?? []).map { URL(filePath: $0, directoryHint: .isDirectory) }Task { await readSuggestions() }@@ -164,7 +171,11 @@ final class ProjectLibrary {}func isExcludedFromBackups(_ group: ProjectGroup) -> Bool {- backupMarks(group).isOn+ backupsBeingChanged[group.project] ?? backupMarks(group).isOn+ }++ func isChangingBackups(of group: ProjectGroup) -> Bool {+ backupsBeingChanged[group.project] != nil}/// The group's folders that would not take the mark the last time Peel set it.@@ -173,7 +184,7 @@ final class ProjectLibrary {}func canMarkForBackups(_ group: ProjectGroup) -> Bool {- backupMarks(group).canChange+ !isChangingBackups(of: group) && backupMarks(group).canChange}func markedByOthers(in group: ProjectGroup) -> [URL] {@@ -182,9 +193,12 @@ final class ProjectLibrary {/// Excludes the group's artifacts from Time Machine backups, or includes them again, so a backup holds the/// project and not what a build can make again.- func setExcludedFromBackups(_ isExcluded: Bool, in group: ProjectGroup) {+ func setExcludedFromBackups(_ isExcluded: Bool, in group: ProjectGroup) async {+ guard !isChangingBackups(of: group) else { return }+ backupsBeingChanged[group.project] = isExcluded+ defer { backupsBeingChanged[group.project] = nil }let urls = backupMarks(group).folders(turning: isExcluded)- let failed = TimeMachineExclusion.setExcluded(isExcluded, urls)+ let failed = await mark(isExcluded, urls)backupMarkFailures = backupMarkFailures.subtracting(urls).union(failed)let changed = Set(urls).subtracting(failed)if isExcluded {diff --git a/Peel/Views/Projects/ProjectDetailView.swift b/Peel/Views/Projects/ProjectDetailView.swiftindex 5e7e968f..4dc12b09 100644--- a/Peel/Views/Projects/ProjectDetailView.swift+++ b/Peel/Views/Projects/ProjectDetailView.swift@@ -77,7 +77,7 @@ struct ProjectDetailView: View {private var backupsCheckbox: some View {let isOn = Binding(get: { projects.isExcludedFromBackups(group) },- set: { projects.setExcludedFromBackups($0, in: group) }+ set: { isExcluded in Task { await projects.setExcludedFromBackups(isExcluded, in: group) } })let explanation = String(localized: "Backups keep the project and skip the folders listed here, node_modules and Pods included. Installed packages such as .venv, and the .terraform folder, stay in backups, as do folders whose name could mean anything and folders Peel doesn’t select because of what is inside. The mark sits on the folder itself: it moves with the folder, and a folder that a build deletes and makes again comes back without it.")let others = projects.markedByOthers(in: group).map(\.lastPathComponent)diff --git a/PeelTests/ProjectLibraryTests.swift b/PeelTests/ProjectLibraryTests.swiftindex 619904ae..91ffaf41 100644--- a/PeelTests/ProjectLibraryTests.swift+++ b/PeelTests/ProjectLibraryTests.swift@@ -1,5 +1,6 @@import Foundation@testable import Peel+@testable import PeelCoreimport Testing@MainActor@@ -42,6 +43,39 @@ struct ProjectLibraryTests {#expect(library.folders == [root])}+ @Test func aMarkBeingWrittenShowsWhatWasClickedAndTakesNoOtherClick() async throws {+ let made = try MadeUpDefaults()+ defer { made.forget() }+ let root = FileManager.default.temporaryDirectory.appending(+ path: "ProjectLibraryTests-\(UUID().uuidString)", directoryHint: .isDirectory+ )+ defer { try? FileManager.default.removeItem(at: root) }+ try project("app", in: root)+ made.defaults.set([root.path(percentEncoded: false)], forKey: "projectFolders")+ let (held, release) = AsyncStream.makeStream(of: Void.self)+ let library = ProjectLibrary(defaults: made.defaults) { _, _ in+ for await _ in held { break }+ return []+ }+ await library.refresh()+ let group = try #require(library.groups?.first)+ #expect(!library.isExcludedFromBackups(group))++ let change = Task { await library.setExcludedFromBackups(true, in: group) }+ let clock = ContinuousClock()+ let start = clock.now+ while !library.isChangingBackups(of: group), clock.now - start < .seconds(5) { await Task.yield() }++ #expect(library.isExcludedFromBackups(group))+ #expect(!library.canMarkForBackups(group))+ release.yield()+ release.finish()+ await change.value+ #expect(library.isExcludedFromBackups(group))+ #expect(!library.isChangingBackups(of: group))+ #expect(library.canMarkForBackups(group))+ }+private func project(_ name: String, in root: URL) throws {let folder = root.appending(path: "\(name)/node_modules/left-pad", directoryHint: .isDirectory)try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)