forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.swift
More file actions
100 lines (95 loc) · 4.04 KB
/
Helpers.swift
File metadata and controls
100 lines (95 loc) · 4.04 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
import Foundation
import LanguageServerProtocol
extension FileManager {
func fileIsDirectory(atPath path: String) -> Bool {
var isDirectory: ObjCBool = false
let exists = fileExists(atPath: path, isDirectory: &isDirectory)
return isDirectory.boolValue && exists
}
}
@discardableResult
func runAppleScript(_ appleScript: String) async throws -> String {
let task = Process()
task.launchPath = "/usr/bin/osascript"
task.arguments = ["-e", appleScript]
let outpipe = Pipe()
task.standardOutput = outpipe
return try await withUnsafeThrowingContinuation { continuation in
do {
task.terminationHandler = { _ in
do {
if let data = try outpipe.fileHandleForReading.readToEnd(),
let content = String(data: data, encoding: .utf8)
{
continuation.resume(returning: content)
return
}
continuation.resume(returning: "")
} catch {
continuation.resume(throwing: error)
}
}
try task.run()
} catch {
continuation.resume(throwing: error)
}
}
}
extension XPCService {
@ServiceActor
func fetchOrCreateWorkspaceIfNeeded(fileURL: URL) async throws -> Workspace {
let projectURL = try await Environment.fetchCurrentProjectRootURL(fileURL)
let workspaceURL = projectURL ?? fileURL
let workspace = workspaces[workspaceURL] ?? Workspace(projectRootURL: workspaceURL)
workspaces[workspaceURL] = workspace
return workspace
}
}
extension NSError {
static func from(_ error: Error) -> NSError {
if let error = error as? ServerError {
var message = "Unknown"
switch error {
case let .handlerUnavailable(handler):
message = "Handler unavailable: \(handler)."
case let .unhandledMethod(method):
message = "Methond unhandled: \(method)."
case let .notificationDispatchFailed(error):
message = "Notification dispatch failed: \(error.localizedDescription)."
case let .requestDispatchFailed(error):
message = "Request dispatch failed: \(error.localizedDescription)."
case let .clientDataUnavailable(error):
message = "Client data unavalable: \(error.localizedDescription)."
case .serverUnavailable:
message = "Server unavailable, please make sure you have installed Node."
case .missingExpectedParameter:
message = "Missing expected parameter."
case .missingExpectedResult:
message = "Missing expected result."
case let .unableToDecodeRequest(error):
message = "Unable to decode request: \(error.localizedDescription)."
case let .unableToSendRequest(error):
message = "Unable to send request: \(error.localizedDescription)."
case let .unableToSendNotification(error):
message = "Unable to send notification: \(error.localizedDescription)."
case let .serverError(code, m, _):
message = "Server error: (\(code)) \(m)."
case let .invalidRequest(error):
message = "Invalid request: \(error?.localizedDescription ?? "Unknown")."
case .timeout:
message = "Timeout."
}
return NSError(domain: "com.intii.CopilotForXcode", code: -1, userInfo: [
NSLocalizedDescriptionKey: message,
])
}
if let error = error as? CancellationError {
return NSError(domain: "com.intii.CopilotForXcode", code: -100, userInfo: [
NSLocalizedDescriptionKey: error.localizedDescription,
])
}
return NSError(domain: "com.intii.CopilotForXcode", code: -1, userInfo: [
NSLocalizedDescriptionKey: error.localizedDescription,
])
}
}