-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathSystemUtils.swift
More file actions
175 lines (138 loc) · 5.68 KB
/
SystemUtils.swift
File metadata and controls
175 lines (138 loc) · 5.68 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import Foundation
import IOKit
import CryptoKit
public class SystemUtils {
public static let shared = SystemUtils()
// Static properties for constant values
public static let machineId: String = {
return shared.computeMachineId()
}()
public static let osVersion: String = {
return "\(ProcessInfo.processInfo.operatingSystemVersion.majorVersion).\(ProcessInfo.processInfo.operatingSystemVersion.minorVersion).\(ProcessInfo.processInfo.operatingSystemVersion.patchVersion)"
}()
public static let xcodeVersion: String? = {
return shared.computeXcodeVersion()
}()
public static let editorVersionString: String = {
return "Xcode/\(xcodeVersion ?? "0.0.0")"
}()
public static let editorPluginVersion: String? = {
return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
}()
public static let editorPluginVersionString: String = {
return "\(editorPluginVersion ?? "0.0.0")"
}()
public static let build: String = {
return shared.isDeveloperMode() ? "dev" : ""
}()
public static let buildType: String = {
return shared.isDeveloperMode() ? "true" : "false"
}()
private init() {}
// Renamed to computeMachineId since it's now an internal implementation detail
private func computeMachineId() -> String {
// Original getMachineId implementation
let matchingDict = IOServiceMatching("IOEthernetInterface") as NSMutableDictionary
var iterator: io_iterator_t = 0
let result = IOServiceGetMatchingServices(kIOMainPortDefault, matchingDict, &iterator)
if result != KERN_SUCCESS {
return UUID().uuidString
}
var macAddress: String = ""
var service = IOIteratorNext(iterator)
while service != 0 {
var parentService: io_object_t = 0
let kernResult = IORegistryEntryGetParentEntry(service, "IOService", &parentService)
if kernResult == KERN_SUCCESS {
let propertyPtr = UnsafeMutablePointer<Unmanaged<CFMutableDictionary>?>.allocate(capacity: 1)
_ = IORegistryEntryCreateCFProperties(
parentService,
propertyPtr,
kCFAllocatorDefault,
0
)
if let properties = propertyPtr.pointee?.takeUnretainedValue() as? [String: Any],
let data = properties["IOMACAddress"] as? Data {
macAddress = data.map { String(format: "%02x", $0) }.joined()
IOObjectRelease(parentService)
break
}
IOObjectRelease(parentService)
}
IOObjectRelease(service)
service = IOIteratorNext(iterator)
}
IOObjectRelease(iterator)
// Hash the MAC address using SHA256
if !macAddress.isEmpty, let macData = macAddress.data(using: .utf8) {
let hashedData = SHA256.hash(data: macData)
return hashedData.compactMap { String(format: "%02x", $0) }.joined()
}
return "unknown"
}
public func getXcodeBinaryPath() -> String {
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
let path: String
if identifier == "x86_64" {
path = Bundle.main.bundleURL.appendingPathComponent("Contents/Resources/copilot-language-server").path
} else if identifier == "arm64" {
path = Bundle.main.bundleURL.appendingPathComponent("Contents/Resources/copilot-language-server-arm64").path
} else {
fatalError("Unsupported architecture")
}
return path
}
private func computeXcodeVersion() -> String? {
let process = Process()
let pipe = Pipe()
defer {
pipe.fileHandleForReading.closeFile()
if process.isRunning {
process.terminate()
}
}
process.executableURL = URL(fileURLWithPath: "/usr/bin/xcrun")
process.arguments = ["xcodebuild", "-version"]
process.standardOutput = pipe
do {
try process.run()
} catch {
print("Error running xcrun xcodebuild: \(error)")
return nil
}
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard let output = String(data: data, encoding: .utf8) else {
return nil
}
let lines = output.split(separator: "\n")
return lines.first?.split(separator: " ").last.map(String.init)
}
public func getEditorVersionString() -> String {
return "Xcode/\(computeXcodeVersion() ?? "0.0.0")"
}
public func getEditorPluginVersion() -> String? {
return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
}
public func getEditorPluginVersionString() -> String {
return "copilot-xcode/\(getEditorPluginVersion() ?? "0.0.0")"
}
public func getBuild() -> String {
return isDeveloperMode() ? "dev" : ""
}
public func getBuildType() -> String {
return isDeveloperMode() ? "true" : "false"
}
func isDeveloperMode() -> Bool {
#if DEBUG
return true
#else
return false
#endif
}
}