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.

Tell projects in folders of the same name apart

Țugui Dragoș13ea50a9deb6
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index 5645cda5..d0916d4b 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -621,11 +621,11 @@ struct ProjectsCommand: AsyncParsableCommand {
Output.line("Nothing here is safe to suggest. See `peel projects`.")
return
}
- let projects = Set(suggested.map(\.project.lastPathComponent)).sorted()
+ let (source, sourceKey) = Self.source(of: suggested)
let cleanup = Cleanup.of(
suggested.map { (url: $0.url, size: $0.size) },
- source: projects.count == 1 ? projects[0] : "Build artifacts",
- sourceKey: projects.count == 1 ? nil : "tool",
+ source: source,
+ sourceKey: sourceKey,
tool: "projects",
service: service
)
@@ -639,6 +639,13 @@ struct ProjectsCommand: AsyncParsableCommand {
)
}
+ /// How History names a removal: by its project when it moves from one, otherwise as build artifacts.
+ static func source(of artifacts: [ProjectArtifact]) -> (source: String, sourceKey: String?) {
+ let projects = Set(artifacts.map { PathPattern.comparablePath(of: $0.project) })
+ guard projects.count == 1, let project = artifacts.first?.project else { return ("Build artifacts", "tool") }
+ return (project.lastPathComponent, nil)
+ }
+
static func rows(for artifacts: [ProjectArtifact]) -> [[String]] {
artifacts.map { [Output.size($0.size), Output.path($0.url), $0.tool ?? "tagged as a cache", note(for: $0)] }
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
index c685b89c..f8aff277 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
@@ -804,6 +804,24 @@ public enum ProjectArtifacts {
definitions.flatMap(\.markers).filter { $0.hasPrefix("*") }.map { String($0.dropFirst()) }
)
+ /// Each project's name as a list shows it: its folder's name, or, while another of `projects` has a folder of the
+ /// same name, as many folders above it as tell them apart, as in `acme/frontend`.
+ public static func names(of projects: [URL]) -> [String] {
+ let paths = projects.map { PathComponents.of(key(of: $0)) }
+ var lengths = Array(repeating: 1, count: paths.count)
+ while true {
+ let names = paths.indices.map { paths[$0].suffix(lengths[$0]).joined(separator: "/") }
+ var grew = false
+ for clash in Dictionary(grouping: paths.indices, by: { names[$0] }).values where clash.count > 1 {
+ for index in clash where lengths[index] < paths[index].count {
+ lengths[index] += 1
+ grew = true
+ }
+ }
+ if !grew { return names }
+ }
+ }
+
static let maximumFolders = 40_000
/// Past this many entries read, a project's last change counts as unknown.
static let maximumActivitySamples = 10_000
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
index 5459500f..95e25412 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
@@ -674,6 +674,24 @@ struct FileCommandTests {
#expect(found["skippedLocations"] as? [String] == ["/Users/me/Projects/app"])
}
+ @Test func aRemovalFromTwoProjectsOfTheSameNameIsNotNamedForOne() {
+ let artifacts = ["/Users/me/Developer/acme/web", "/Users/me/Developer/globex/web"].map { project in
+ ProjectArtifact(
+ url: URL(filePath: project + "/node_modules", directoryHint: .isDirectory),
+ project: URL(filePath: project, directoryHint: .isDirectory),
+ name: "node_modules",
+ tool: "npm",
+ size: 10,
+ lastActivity: nil,
+ hasGenericName: false,
+ isEnvironment: false
+ )
+ }
+
+ #expect(ProjectsCommand.source(of: artifacts) == ("Build artifacts", "tool"))
+ #expect(ProjectsCommand.source(of: [artifacts[0], artifacts[0]]) == ("web", nil))
+ }
+
/// A chosen folder macOS kept Peel out of is said whatever the output, so an empty answer is not read as none.
@Test func saysWhenItCouldNotLookInAChosenFolder() throws {
let locked = URL(filePath: "/Users/me/Library/Safari", directoryHint: .isDirectory)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
index 3f775d89..4e41c535 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
@@ -306,6 +306,17 @@ struct ProjectArtifactsTests {
#expect(!scan.wasCutShort)
}
+ @Test func namesProjectsInFoldersOfTheSameNameApart() {
+ let projects = [
+ "/Users/me/Developer/acme/frontend", "/Users/me/Developer/globex/frontend", "/Users/me/Code/apps/web",
+ "/Users/me/Code/examples/apps/web", "/Users/me/Code/site",
+ ].map { URL(filePath: $0, directoryHint: .isDirectory) }
+
+ #expect(ProjectArtifacts.names(of: projects) == [
+ "acme/frontend", "globex/frontend", "Code/apps/web", "examples/apps/web", "site",
+ ])
+ }
+
@Test func findsWhatJavaScriptToolsBuild() async throws {
try await expectFound([
Kind(artifact: ".nuxt", marker: "nuxt.config.ts", tool: "Nuxt", isGeneric: false),
diff --git a/Peel/Model/ProjectLibrary.swift b/Peel/Model/ProjectLibrary.swift
index 85a5c056..a52e01af 100644
--- a/Peel/Model/ProjectLibrary.swift
+++ b/Peel/Model/ProjectLibrary.swift
@@ -6,6 +6,8 @@ import PeelCore
/// One project folder and everything a build left inside it.
struct ProjectGroup: Identifiable, Hashable {
let project: URL
+ /// The folder's name, with the folders above it while another listed project has a folder of the same name.
+ let name: String
let artifacts: [ProjectArtifact]
var id: URL { project }
@@ -65,11 +67,13 @@ final class ProjectLibrary {
needsFullDiskAccess = scan.needsFullDiskAccess
unreadable = scan.unreadableLocations
unsearchable = scan.refusedRoots
- let result = Dictionary(grouping: artifacts, by: \.project)
- .map {
+ let byProject = Array(Dictionary(grouping: artifacts, by: \.project))
+ let result = zip(byProject, ProjectArtifacts.names(of: byProject.map(\.key)))
+ .map { entry, name in
ProjectGroup(
- project: $0.key,
- artifacts: $0.value.sorted { SizeTotal([$0.size]) > SizeTotal([$1.size]) }
+ project: entry.key,
+ name: name,
+ artifacts: entry.value.sorted { SizeTotal([$0.size]) > SizeTotal([$1.size]) }
)
}
.sorted { $0.total > $1.total }
@@ -174,8 +178,8 @@ extension ProjectLibrary: CarriesSelection {
guard !selected.isEmpty else { return nil }
return CarriedSelection.Part(
page: group.page,
- title: group.project.lastPathComponent,
- source: group.project.lastPathComponent,
+ title: group.name,
+ source: group.name,
sourceKey: nil,
sizes: Dictionary(selected.map { ($0.url, $0.size) }, uniquingKeysWith: { first, _ in first })
)
diff --git a/Peel/Views/Projects/ProjectDetailView.swift b/Peel/Views/Projects/ProjectDetailView.swift
index 01b11970..112559d7 100644
--- a/Peel/Views/Projects/ProjectDetailView.swift
+++ b/Peel/Views/Projects/ProjectDetailView.swift
@@ -34,7 +34,7 @@ struct ProjectDetailView: View {
} actions: {
SelectMenu(
list: group.artifacts.selectableRows,
- place: Text(verbatim: group.project.lastPathComponent),
+ place: Text(verbatim: group.name),
selection: projects
)
}
@@ -69,7 +69,7 @@ struct ProjectDetailView: View {
)
}
.fadesInColumn(whenRowsChange: group.artifacts.map(\.url))
- .navigationTitle(group.project.lastPathComponent)
+ .navigationTitle(group.name)
.toolbar(removing: .title)
}
@@ -110,9 +110,9 @@ struct ProjectDetailView: View {
private var header: some View {
PageHeader(systemImage: "folder") {
- Text(verbatim: group.project.lastPathComponent)
+ Text(verbatim: group.name)
.pageTitle()
- .help(Text(verbatim: group.project.lastPathComponent))
+ .help(Text(verbatim: group.name))
} details: {
Text(group.project.abbreviatedPath)
.font(.subheadline.monospaced())
diff --git a/Peel/Views/Projects/ProjectList.swift b/Peel/Views/Projects/ProjectList.swift
index 79991d18..935508c2 100644
--- a/Peel/Views/Projects/ProjectList.swift
+++ b/Peel/Views/Projects/ProjectList.swift
@@ -17,7 +17,7 @@ struct ProjectList: View {
let all = projects.groups ?? []
guard !searchText.isEmpty else { return all }
return all.filter { group in
- SearchText.matches(group.project.lastPathComponent, searchText)
+ SearchText.matches(group.name, searchText)
|| group.artifacts.contains { $0.matches(searchText) }
}
}
@@ -196,7 +196,7 @@ private struct ProjectRow: View {
var body: some View {
ToolRow(systemImage: "folder") {
- Text(verbatim: group.project.lastPathComponent)
+ Text(verbatim: group.name)
.lineLimit(1)
} details: {
switch group.lastChange {