forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenSettingsCommand.swift
More file actions
68 lines (61 loc) · 2.09 KB
/
OpenSettingsCommand.swift
File metadata and controls
68 lines (61 loc) · 2.09 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
//
// OpenSettingsCommand.swift
// EditorExtension
//
// Opens the settings app
//
import Foundation
import XcodeKit
enum GitHubCopilotForXcodeSettingsLaunchError: Error, LocalizedError {
case appNotFound
case openFailed(exitCode: Int32)
var errorDescription: String? {
switch self {
case .appNotFound:
return "\(hostAppName()) settings application not found"
case let .openFailed(exitCode):
return "Failed to launch \(hostAppName()) settings (exit code \(exitCode))"
}
}
}
class OpenSettingsCommand: NSObject, XCSourceEditorCommand, CommandType {
var name: String { "Open \(hostAppName()) Settings" }
func perform(
with invocation: XCSourceEditorCommandInvocation,
completionHandler: @escaping (Error?) -> Void
) {
Task {
if let appPath = locateHostBundleURL(url: Bundle.main.bundleURL)?.absoluteString {
let task = Process()
task.launchPath = "/usr/bin/open"
task.arguments = [appPath]
task.launch()
task.waitUntilExit()
if task.terminationStatus == 0 {
completionHandler(nil)
} else {
completionHandler(GitHubCopilotForXcodeSettingsLaunchError.openFailed(exitCode: task.terminationStatus))
}
} else {
completionHandler(GitHubCopilotForXcodeSettingsLaunchError.appNotFound)
}
}
}
func locateHostBundleURL(url: URL) -> URL? {
var nextURL = url
while nextURL.path != "/" {
nextURL = nextURL.deletingLastPathComponent()
if nextURL.lastPathComponent.hasSuffix(".app") {
return nextURL
}
}
let devAppURL = url
.deletingLastPathComponent()
.appendingPathComponent("GitHub Copilot for Xcode Dev.app")
return devAppURL
}
}
func hostAppName() -> String {
return Bundle.main.object(forInfoDictionaryKey: "HOST_APP_NAME") as? String
?? "GitHub Copilot for Xcode"
}