Skip to content

Commit d157fa6

Browse files
committed
Get Xcode version from the Xcode bundle version.plist
1 parent f73af0e commit d157fa6

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

Core/Sources/CodeiumService/CodeiumService.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import LanguageClient
33
import LanguageServerProtocol
44
import Logger
55
import SuggestionModel
6+
import XcodeInspector
67

78
public protocol CodeiumSuggestionServiceType {
89
func getCompletions(
@@ -54,9 +55,9 @@ public class CodeiumSuggestionService {
5455

5556
let authService = CodeiumAuthService()
5657

57-
var xcodeVersion = "14.0.0"
58+
var fallbackXcodeVersion = "14.0.0"
5859
var languageServerVersion = CodeiumInstallationManager.latestSupportedVersion
59-
60+
6061
private var ongoingTasks = Set<Task<[CodeSuggestion], Error>>()
6162

6263
init(designatedServer: CodeiumLSP) {
@@ -95,13 +96,6 @@ public class CodeiumSuggestionService {
9596
}
9697

9798
let metadata = try getMetadata()
98-
xcodeVersion = (try? await getXcodeVersion()) ?? xcodeVersion
99-
let versionNumberSegmentCount = xcodeVersion.split(separator: ".").count
100-
if versionNumberSegmentCount == 2 {
101-
xcodeVersion += ".0"
102-
} else if versionNumberSegmentCount == 1 {
103-
xcodeVersion += ".0.0"
104-
}
10599
let tempFolderURL = FileManager.default.temporaryDirectory
106100
let managerDirectoryURL = tempFolderURL
107101
.appendingPathComponent("com.intii.CopilotForXcode")
@@ -192,9 +186,16 @@ extension CodeiumSuggestionService {
192186
}
193187
throw E()
194188
}
189+
var ideVersion = XcodeInspector.shared.latestActiveXcode?.version ?? fallbackXcodeVersion
190+
let versionNumberSegmentCount = ideVersion.split(separator: ".").count
191+
if versionNumberSegmentCount == 2 {
192+
ideVersion += ".0"
193+
} else if versionNumberSegmentCount == 1 {
194+
ideVersion += ".0.0"
195+
}
195196
return Metadata(
196197
ide_name: "xcode",
197-
ide_version: xcodeVersion,
198+
ide_version: ideVersion,
198199
extension_version: languageServerVersion,
199200
api_key: key,
200201
session_id: CodeiumSuggestionService.sessionId,
@@ -231,11 +232,11 @@ extension CodeiumSuggestionService: CodeiumSuggestionServiceType {
231232
ongoingTasks.forEach { $0.cancel() }
232233
ongoingTasks.removeAll()
233234
await cancelRequest()
234-
235+
235236
requestCounter += 1
236237
let languageId = languageIdentifierFromFileURL(fileURL)
237238
let relativePath = getRelativePath(of: fileURL)
238-
239+
239240
let task = Task {
240241
let request = await CodeiumRequest.GetCompletion(requestBody: .init(
241242
metadata: try getMetadata(),
@@ -263,11 +264,11 @@ extension CodeiumSuggestionService: CodeiumSuggestionServiceType {
263264
)
264265
}
265266
))
266-
267+
267268
try Task.checkCancellation()
268269

269270
let result = try await (try await setupServerIfNeeded()).sendRequest(request)
270-
271+
271272
try Task.checkCancellation()
272273

273274
return result.completionItems?.filter { item in
@@ -294,7 +295,7 @@ extension CodeiumSuggestionService: CodeiumSuggestionServiceType {
294295
)
295296
} ?? []
296297
}
297-
298+
298299
ongoingTasks.insert(task)
299300

300301
return try await task.value

Core/Sources/XcodeInspector/XcodeInspector.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
167167
@Published public var documentURL: URL = .init(fileURLWithPath: "/")
168168
@Published public var projectURL: URL = .init(fileURLWithPath: "/")
169169
@Published public var workspaces = [WorkspaceIdentifier: WorkspaceInfo]()
170+
var _version: String?
171+
public var version: String? {
172+
if let _version { return _version }
173+
guard let plistPath = runningApplication.bundleURL?
174+
.appendingPathComponent("Contents")
175+
.appendingPathComponent("version.plist")
176+
.path
177+
else { return nil }
178+
guard let plistData = FileManager.default.contents(atPath: plistPath) else { return nil }
179+
var format = PropertyListSerialization.PropertyListFormat.xml
180+
guard let plistDict = try? PropertyListSerialization.propertyList(
181+
from: plistData,
182+
options: .mutableContainersAndLeaves,
183+
format: &format
184+
) as? [String: AnyObject] else { return nil }
185+
let result = plistDict["CFBundleShortVersionString"] as? String
186+
_version = result
187+
return result
188+
}
189+
170190
private var longRunningTasks = Set<Task<Void, Error>>()
171191
private var focusedWindowObservations = Set<AnyCancellable>()
172192

0 commit comments

Comments
 (0)