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
61 lines (53 loc) · 1.92 KB
/
main.swift
File metadata and controls
61 lines (53 loc) · 1.92 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
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 launched, check if the executable of this program is changed.
// If changed, restart the launch agent.
let sequence = NSWorkspace.shared.notificationCenter
.notifications(named: NSWorkspace.didLaunchApplicationNotification)
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
manager.restartLaunchAgent()
#endif
}
}
}
let xpcListener = setupXPCListener()
setupAutoTrigger()
setupRestartOnUpdate()
os_log(.info, "XPC Service started.")
RunLoop.main.run()