|
1 | 1 | import Foundation |
2 | | - |
3 | | -struct LaunchAgentManager { |
4 | | - var serviceIdentifier: String { |
5 | | - Bundle.main |
6 | | - .object(forInfoDictionaryKey: "BUNDLE_IDENTIFIER_BASE") as! String + |
7 | | - ".XPCService" |
8 | | - } |
9 | | - |
10 | | - var location: String { |
11 | | - Bundle.main.executableURL?.deletingLastPathComponent() |
12 | | - .appendingPathComponent("CopilotForXcodeXPCService").path ?? "" |
13 | | - } |
14 | | - |
15 | | - var launchAgentDirURL: URL { |
16 | | - FileManager.default.homeDirectoryForCurrentUser |
17 | | - .appendingPathComponent("Library/LaunchAgents") |
18 | | - } |
19 | | - |
20 | | - var launchAgentPath: String { |
21 | | - launchAgentDirURL.appendingPathComponent("\(serviceIdentifier).plist").path |
22 | | - } |
23 | | - |
24 | | - func setupLaunchAgent() throws { |
25 | | - let content = """ |
26 | | - <?xml version="1.0" encoding="UTF-8"?> |
27 | | - <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
28 | | - <plist version="1.0"> |
29 | | - <dict> |
30 | | - <key>RunAtLoad</key> |
31 | | - <true/> |
32 | | - <key>Label</key> |
33 | | - <string>\(serviceIdentifier)</string> |
34 | | - <key>Program</key> |
35 | | - <string>\(location)</string> |
36 | | - <key>MachServices</key> |
37 | | - <dict> |
38 | | - <key>\(serviceIdentifier)</key> |
39 | | - <true/> |
40 | | - </dict> |
41 | | - </dict> |
42 | | - </plist> |
43 | | - """ |
44 | | - if !FileManager.default.fileExists(atPath: launchAgentDirURL.path) { |
45 | | - try FileManager.default.createDirectory( |
46 | | - at: launchAgentDirURL, |
47 | | - withIntermediateDirectories: false |
48 | | - ) |
49 | | - } |
50 | | - FileManager.default.createFile( |
51 | | - atPath: launchAgentPath, |
52 | | - contents: content.data(using: .utf8) |
| 2 | +import LaunchAgentManager |
| 3 | + |
| 4 | +extension LaunchAgentManager { |
| 5 | + init() { |
| 6 | + self.init( |
| 7 | + serviceIdentifier: Bundle.main |
| 8 | + .object(forInfoDictionaryKey: "BUNDLE_IDENTIFIER_BASE") as! String + |
| 9 | + ".XPCService", |
| 10 | + executablePath: Bundle.main.executableURL?.deletingLastPathComponent() |
| 11 | + .appendingPathComponent("CopilotForXcodeXPCService").path ?? "" |
53 | 12 | ) |
54 | | - launchctl("load", launchAgentPath) |
55 | | - } |
56 | | - |
57 | | - func removeLaunchAgent() throws { |
58 | | - launchctl("unload", launchAgentPath) |
59 | | - try FileManager.default.removeItem(atPath: launchAgentPath) |
60 | | - } |
61 | | - |
62 | | - func restartLaunchAgent() { |
63 | | - launchctl("unload", launchAgentPath) |
64 | | - launchctl("load", launchAgentPath) |
65 | | - } |
66 | | -} |
67 | | - |
68 | | -private func launchctl(_ args: String...) { |
69 | | - let task = Process() |
70 | | - task.launchPath = "/bin/launchctl" |
71 | | - task.arguments = args |
72 | | - task.environment = [ |
73 | | - "PATH": "/usr/bin", |
74 | | - ] |
75 | | - let outpipe = Pipe() |
76 | | - task.standardOutput = outpipe |
77 | | - try? task.run() |
78 | | - task.waitUntilExit() |
79 | | - if let data = try? outpipe.fileHandleForReading.readToEnd(), |
80 | | - let text = String(data: data, encoding: .utf8) |
81 | | - { |
82 | | - print(text) |
83 | 13 | } |
84 | 14 | } |
0 commit comments