-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathWorkspaceDirectory.swift
More file actions
104 lines (86 loc) · 3.63 KB
/
WorkspaceDirectory.swift
File metadata and controls
104 lines (86 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import Foundation
import Logger
import ConversationServiceProvider
/// Directory operations in workspace contexts
public struct WorkspaceDirectory {
/// Determines if a directory should be skipped based on its path
/// - Parameter url: The URL of the directory to check
/// - Returns: `true` if the directory should be skipped, `false` otherwise
public static func shouldSkipDirectory(_ url: URL) -> Bool {
let path = url.path
let normalizedPath = path.hasPrefix("/") ? path: "/" + path
for skipPattern in skipPatterns {
// Pattern: /skipPattern/ (directory anywhere in path)
if normalizedPath.contains("/\(skipPattern)/") {
return true
}
// Pattern: /skipPattern (directory at end of path)
if normalizedPath.hasSuffix("/\(skipPattern)") {
return true
}
// Pattern: skipPattern at root
if normalizedPath == "/\(skipPattern)" {
return true
}
}
return false
}
/// Validates if a URL represents a valid directory for workspace operations
/// - Parameter url: The URL to validate
/// - Returns: `true` if the directory is valid for processing, `false` otherwise
public static func isValidDirectory(_ url: URL) -> Bool {
guard !WorkspaceFile.shouldSkipURL(url) else {
return false
}
guard let resourceValues = try? url.resourceValues(forKeys: [.isDirectoryKey]),
resourceValues.isDirectory == true else {
return false
}
guard !shouldSkipDirectory(url) else {
return false
}
return true
}
/// Retrieves all valid directories within the active workspace
/// - Parameters:
/// - workspaceURL: The URL of the workspace
/// - workspaceRootURL: The root URL of the workspace
/// - Returns: An array of `ConversationDirectoryReference` objects representing valid directories
public static func getDirectoriesInActiveWorkspace(
workspaceURL: URL,
workspaceRootURL: URL
) -> [ConversationDirectoryReference] {
var directories: [ConversationDirectoryReference] = []
let fileManager = FileManager.default
var subprojects: [URL] = []
if WorkspaceFile.isXCWorkspace(workspaceURL) {
subprojects = WorkspaceFile.getSubprojectURLs(in: workspaceURL)
} else {
subprojects.append(workspaceRootURL)
}
for subproject in subprojects {
guard FileManager.default.fileExists(atPath: subproject.path) else {
continue
}
let enumerator = fileManager.enumerator(
at: subproject,
includingPropertiesForKeys: [.isDirectoryKey],
options: [.skipsHiddenFiles]
)
while let directoryURL = enumerator?.nextObject() as? URL {
// Skip items matching the specified pattern
if WorkspaceFile.shouldSkipURL(directoryURL) {
enumerator?.skipDescendants()
continue
}
guard isValidDirectory(directoryURL) else { continue }
let directory = ConversationDirectoryReference(
url: directoryURL,
projectURL: workspaceRootURL
)
directories.append(directory)
}
}
return directories
}
}