forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
74 lines (66 loc) · 2.34 KB
/
main.swift
File metadata and controls
74 lines (66 loc) · 2.34 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
import AppKit
import FileChangeChecker
import Foundation
import LaunchAgentManager
import os.log
import Service
let bundleIdentifierBase = Bundle.main
.object(forInfoDictionaryKey: "BUNDLE_IDENTIFIER_BASE") as! String
let serviceIdentifier = bundleIdentifierBase + ".XPCService"
func setupXPCListener() -> (NSXPCListener, ServiceDelegate) {
let listener = NSXPCListener(machServiceName: serviceIdentifier)
let delegate = ServiceDelegate()
listener.delegate = delegate
listener.resume()
return (listener, delegate)
}
func setupAutoTrigger() {
_ = AutoTrigger.shared
}
func setupRestartOnUpdate() {
Task {
guard let url = Bundle.main.executableURL else { return }
let checker = await FileChangeChecker(fileURL: url)
// If Xcode or Copilot for Xcode is made active, check if the executable of this program is
// changed.
// If changed, restart the launch agent.
let sequence = NSWorkspace.shared.notificationCenter
.notifications(named: NSWorkspace.didActivateApplicationNotification)
for await notification in sequence {
try Task.checkCancellation()
guard let app = notification
.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication,
[
"com.apple.dt.Xcode",
bundleIdentifierBase,
].contains(app.bundleIdentifier)
else { continue }
guard await checker.checkIfChanged() else {
os_log(.info, "XPC Service is not updated, no need to restart.")
continue
}
os_log(.info, "XPC Service will be restarted.")
#if DEBUG
#else
let manager = LaunchAgentManager(
serviceIdentifier: serviceIdentifier,
executablePath: Bundle.main.executablePath ?? ""
)
do {
try await manager.restartLaunchAgent()
} catch {
os_log(
.error,
"XPC Service failed to restart. %{public}s",
error.localizedDescription
)
}
#endif
}
}
}
let xpcListener = setupXPCListener()
setupAutoTrigger()
setupRestartOnUpdate()
os_log(.info, "XPC Service started.")
RunLoop.main.run()