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 Build Artifacts' suggested folders and judge chosen ones off the main actor

Țugui Dragoșea7103275d49
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
index 9d35d9ce..f0b3cebc 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
@@ -980,13 +980,27 @@ public enum ProjectArtifacts {
/// The folders in the home folder where people usually keep projects, suggested until one is chosen: each is
/// offered only when it is a real folder there, never a link, and the person still confirms it.
- public static func suggestedRoots(home: URL = .homeDirectory) -> [URL] {
+ @concurrent
+ public static func suggestedRoots(home: URL = .homeDirectory) async -> [URL] {
["Developer", "Projects", "Code", "src"].compactMap { name in
let url = home.appending(path: name, directoryHint: .isDirectory)
return url.isRealFolder && refusal(for: url, home: home) == nil ? url : nil
}
}
+ /// Why each of `roots` cannot be searched, in their order. Each is judged within `FileSize.budget`, since a
+ /// folder on a network volume that stopped answering would hold the thread, and one that doesn't answer in time
+ /// can't be reached.
+ @concurrent
+ public static func refusals(of roots: [URL], home: URL = .homeDirectory) async -> [Refusal?] {
+ var reasons: [Refusal?] = []
+ for root in roots {
+ let reason = await SlowRead.answer(within: FileSize.budget) { _ in refusal(for: root, home: home) }
+ reasons.append(reason ?? .notAFolder)
+ }
+ return reasons
+ }
+
/// Why `root` cannot be searched, or nil. Every spelling of the path is checked: `/users/me` and
/// `/System/Volumes/Data/Users/me` are the same home folder, and a link into a cloud folder is that cloud folder.
public static func refusal(for root: URL, home: URL = .homeDirectory) -> Refusal? {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
index 71513043..82c4601b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
@@ -49,7 +49,7 @@ struct ProjectArtifactsTests {
#expect(returned - stopped < .seconds(1))
}
- @Test func suggestsTheProjectFoldersThatExistInTheHomeFolder() throws {
+ @Test func suggestsTheProjectFoldersThatExistInTheHomeFolder() async throws {
let directory = try TemporaryDirectory()
try directory.directory("Developer")
try directory.directory("Code")
@@ -60,11 +60,22 @@ struct ProjectArtifactsTests {
withDestinationPath: directory.url.appending(path: "Elsewhere/src").path(percentEncoded: false)
)
- let suggested = ProjectArtifacts.suggestedRoots(home: directory.url)
+ let suggested = await ProjectArtifacts.suggestedRoots(home: directory.url)
#expect(suggested.map(\.lastPathComponent) == ["Developer", "Code"])
}
+ @Test func judgesEachChosenFolderInItsOrder() async throws {
+ let directory = try TemporaryDirectory()
+ let home = try directory.directory("Users/someone")
+ let code = try directory.directory("Users/someone/Code")
+ let roots = [home, URL(filePath: "/Applications"), code, directory.url.appending(path: "gone")]
+
+ let reasons = await ProjectArtifacts.refusals(of: roots, home: home)
+
+ #expect(reasons == [.tooBroad, .systemFolder, nil, .notAFolder])
+ }
+
/// Full Disk Access does not open a folder closed by ordinary file permissions, so the scan must not ask for it.
/// The Time Machine checkbox marks only what a build certainly makes again, and never an artifact Time
/// Machine already leaves out through a folder above it, whose exclusion is not its own to change. A project
diff --git a/Peel/Model/ProjectLibrary.swift b/Peel/Model/ProjectLibrary.swift
index c4b6ad51..8dbb0879 100644
--- a/Peel/Model/ProjectLibrary.swift
+++ b/Peel/Model/ProjectLibrary.swift
@@ -42,6 +42,8 @@ final class ProjectLibrary {
private(set) var unsearchable: [(url: URL, reason: ProjectArtifacts.Refusal)] = []
/// Folders the last Add refused, with the reason, until the next one.
private(set) var refused: [(url: URL, reason: ProjectArtifacts.Refusal)] = []
+ /// The usual project folders that are in the home folder, read off the main actor (`readSuggestions()`).
+ private var suggestedRoots: [URL] = []
var selection: URL?
var selectedURLs: Set<URL> = []
@@ -55,6 +57,13 @@ final class ProjectLibrary {
self.defaults = defaults
folders = (defaults.array(forKey: Self.foldersKey) as? [String] ?? [])
.map { URL(filePath: $0, directoryHint: .isDirectory) }
+ Task { await readSuggestions() }
+ }
+
+ /// Reads which of the usual project folders are in the home folder, so the page offers them without reading
+ /// the disk while it draws.
+ func readSuggestions() async {
+ suggestedRoots = await ProjectArtifacts.suggestedRoots()
}
/// Scans unless the last scan ended less than `age` ago, one or a removal runs, or the last scan was stopped: a
@@ -115,22 +124,26 @@ final class ProjectLibrary {
/// The usual project folders in the home folder that are not on the list yet.
var suggestedFolders: [URL] {
- ProjectArtifacts.suggestedRoots().filter { url in
- !folders.contains { $0.standardizedFileURL == url.standardizedFileURL }
- }
+ suggestedRoots.filter { !isListed($0) }
}
func add(_ urls: [URL]) async {
- let chosen = urls.filter { url in !folders.contains { $0.standardizedFileURL == url.standardizedFileURL } }
+ let chosen = urls.filter { !isListed($0) }
+ let reasons = await ProjectArtifacts.refusals(of: chosen)
// A folder that cannot be scanned is refused here, rather than saved and then silently skipped.
- refused = chosen.compactMap { url in ProjectArtifacts.refusal(for: url).map { (url, $0) } }
- let keeping = chosen.filter { url in !refused.contains { $0.url == url } }
+ refused = zip(chosen, reasons).compactMap { url, reason in reason.map { (url, $0) } }
+ // Another Add may have listed one of them while these were judged.
+ let keeping = zip(chosen, reasons).filter { url, reason in reason == nil && !isListed(url) }.map(\.0)
guard !keeping.isEmpty else { return }
folders += keeping
save()
await refresh()
}
+ private func isListed(_ url: URL) -> Bool {
+ folders.contains { $0.standardizedFileURL == url.standardizedFileURL }
+ }
+
func clearRefusals() {
refused = []
}
diff --git a/Peel/Views/Projects/ProjectList.swift b/Peel/Views/Projects/ProjectList.swift
index 0c4e7ecf..5112a160 100644
--- a/Peel/Views/Projects/ProjectList.swift
+++ b/Peel/Views/Projects/ProjectList.swift
@@ -164,10 +164,14 @@ struct ProjectList: View {
}
}
.task {
+ await projects.readSuggestions()
await projects.refreshUnlessScanned(within: ProjectLibrary.scanAgainAfter)
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
- Task { await projects.refreshUnlessScanned(within: ProjectLibrary.scanAgainAfter) }
+ Task {
+ await projects.readSuggestions()
+ await projects.refreshUnlessScanned(within: ProjectLibrary.scanAgainAfter)
+ }
}
.rescanOnExclusionChange("ProjectList") { await projects.refresh() }
.rescanAfterPutBack(.projects) { await projects.refresh() }
diff --git a/PeelTests/ProjectLibraryTests.swift b/PeelTests/ProjectLibraryTests.swift
index fb4880f9..619904ae 100644
--- a/PeelTests/ProjectLibraryTests.swift
+++ b/PeelTests/ProjectLibraryTests.swift
@@ -25,6 +25,23 @@ struct ProjectLibraryTests {
#expect(library.groups?.map(\.name).sorted() == ["app", "tool"])
}
+ @Test func addsAFolderOnceWhenItIsAddedTwiceAtOnce() 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)
+ let library = ProjectLibrary(defaults: made.defaults)
+
+ async let first: Void = library.add([root])
+ async let second: Void = library.add([root])
+ _ = await (first, second)
+
+ #expect(library.folders == [root])
+ }
+
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)