Skip to content

Commit 74e9c7d

Browse files
RoshanNagaram-engintitni
authored andcommitted
Added Support for Automatic and Custom Enterprise Language Server Updates as well as fixing versioning issues
1 parent 7232f23 commit 74e9c7d

File tree

6 files changed

+88
-14
lines changed

6 files changed

+88
-14
lines changed

Core/Sources/HostApp/AccountSettings/CodeiumView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ struct CodeiumView: View {
1717

1818
init() {
1919
isSignedIn = codeiumAuthService.isSignedIn
20-
installationStatus = installationManager.checkInstallation()
20+
installationStatus = .notInstalled
21+
Task { @MainActor in
22+
installationStatus = await installationManager.checkInstallation()
23+
}
2124
}
2225

2326
init(
@@ -56,7 +59,7 @@ struct CodeiumView: View {
5659

5760
func refreshInstallationStatus() {
5861
Task { @MainActor in
59-
installationStatus = installationManager.checkInstallation()
62+
installationStatus = await installationManager.checkInstallation()
6063
}
6164
}
6265

Core/Sources/Service/DependencyUpdater.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import CodeiumService
2+
import Foundation
23
import GitHubCopilotService
34
import Logger
45

@@ -39,8 +40,10 @@ struct DependencyUpdater {
3940
}
4041
}
4142
}
43+
4244
let codeium = CodeiumInstallationManager()
43-
switch codeium.checkInstallation() {
45+
46+
switch await codeium.checkInstallation() {
4447
case .notInstalled: break
4548
case .installed: break
4649
case .unsupported: break

Tool/Sources/CodeiumService/LanguageServer/CodeiumInstallationManager.swift

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,56 @@ public struct CodeiumInstallationManager {
77

88
public init() {}
99

10+
enum CodeiumInstallationError: Error, LocalizedError {
11+
case badURL(String)
12+
case invalidResponse
13+
case invalidData
14+
15+
var errorDescription: String? {
16+
switch self {
17+
case .badURL: return "URL is invalid"
18+
case .invalidResponse: return "Invalid response"
19+
case .invalidData: return "Invalid data"
20+
}
21+
}
22+
}
23+
24+
func getEnterprisePortalVersion() async throws -> String {
25+
let enterprisePortalUrl = UserDefaults.shared.value(for: \.codeiumPortalUrl)
26+
let enterprisePortalVersionUrl = "\(enterprisePortalUrl)/api/version"
27+
28+
guard let url = URL(string: enterprisePortalVersionUrl)
29+
else { throw CodeiumInstallationError.badURL(enterprisePortalVersionUrl) }
30+
31+
let (data, response) = try await URLSession.shared.data(from: url)
32+
33+
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
34+
throw CodeiumInstallationError.invalidResponse
35+
}
36+
37+
if let version = String(data: data, encoding: .utf8)?
38+
.trimmingCharacters(in: .whitespacesAndNewlines)
39+
{
40+
UserDefaults.shared.set(version, for: \.codeiumEnterpriseVersion)
41+
return version
42+
} else {
43+
return UserDefaults.shared.value(for: \.codeiumEnterpriseVersion)
44+
}
45+
}
46+
47+
var isEnterprise: Bool {
48+
return UserDefaults.shared.value(for: \.codeiumEnterpriseMode)
49+
&& !UserDefaults.shared.value(for: \.codeiumPortalUrl).isEmpty
50+
}
51+
1052
public enum InstallationStatus {
1153
case notInstalled
1254
case installed(String)
1355
case outdated(current: String, latest: String)
1456
case unsupported(current: String, latest: String)
1557
}
1658

17-
public func checkInstallation() -> InstallationStatus {
59+
public func checkInstallation() async -> InstallationStatus {
1860
guard let urls = try? CodeiumService.createFoldersIfNeeded()
1961
else { return .notInstalled }
2062
let executableFolderURL = urls.executableURL
@@ -25,20 +67,25 @@ public struct CodeiumInstallationManager {
2567
return .notInstalled
2668
}
2769

70+
let targetVersion = await {
71+
if !isEnterprise { return Self.latestSupportedVersion }
72+
return (try? await getEnterprisePortalVersion())
73+
?? UserDefaults.shared.value(for: \.codeiumEnterpriseVersion)
74+
}()
75+
2876
if FileManager.default.fileExists(atPath: versionFileURL.path),
2977
let versionData = try? Data(contentsOf: versionFileURL),
3078
let version = String(data: versionData, encoding: .utf8)
3179
{
32-
switch version.compare(Self.latestSupportedVersion) {
80+
switch version.compare(targetVersion, options: .numeric) {
3381
case .orderedAscending:
34-
return .outdated(current: version, latest: Self.latestSupportedVersion)
82+
return .outdated(current: version, latest: targetVersion)
3583
case .orderedSame:
3684
return .installed(version)
3785
case .orderedDescending:
38-
return .unsupported(current: version, latest: Self.latestSupportedVersion)
86+
return .unsupported(current: version, latest: targetVersion)
3987
}
4088
}
41-
4289
return .outdated(current: "Unknown", latest: Self.latestSupportedVersion)
4390
}
4491

@@ -61,9 +108,23 @@ public struct CodeiumInstallationManager {
61108
do {
62109
continuation.yield(.downloading)
63110
let urls = try CodeiumService.createFoldersIfNeeded()
64-
let urlString =
65-
"https://github.com/Exafunction/codeium/releases/download/language-server-v\(Self.latestSupportedVersion)/language_server_macos_\(isAppleSilicon() ? "arm" : "x64").gz"
66-
guard let url = URL(string: urlString) else { return }
111+
let urlString: String
112+
let version: String
113+
if !isEnterprise {
114+
version = CodeiumInstallationManager.latestSupportedVersion
115+
urlString =
116+
"https://github.com/Exafunction/codeium/releases/download/language-server-v\(Self.latestSupportedVersion)/language_server_macos_\(isAppleSilicon() ? "arm" : "x64").gz"
117+
} else {
118+
version = try await getEnterprisePortalVersion()
119+
let enterprisePortalUrl = UserDefaults.shared.value(for: \.codeiumPortalUrl)
120+
urlString =
121+
"\(enterprisePortalUrl)/language-server-v\(version)/language_server_macos_\(isAppleSilicon() ? "arm" : "x64").gz"
122+
}
123+
124+
guard let url = URL(string: urlString) else {
125+
continuation.finish(throwing: CodeiumInstallationError.badURL(urlString))
126+
return
127+
}
67128

68129
// download
69130
let (fileURL, _) = try await URLSession.shared.download(from: url)
@@ -90,9 +151,11 @@ public struct CodeiumInstallationManager {
90151
[.posixPermissions: 0o755],
91152
ofItemAtPath: targetURL.deletingPathExtension().path
92153
)
154+
var data: Data?
93155

94156
// create version file
95-
let data = Self.latestSupportedVersion.data(using: .utf8)
157+
data = version.data(using: .utf8)
158+
96159
FileManager.default.createFile(
97160
atPath: urls.executableURL.appendingPathComponent("version").path,
98161
contents: data

Tool/Sources/CodeiumService/Services/CodeiumService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public class CodeiumService {
9595
if let server { return server }
9696

9797
let binaryManager = CodeiumInstallationManager()
98-
let installationStatus = binaryManager.checkInstallation()
98+
let installationStatus = await binaryManager.checkInstallation()
9999
switch installationStatus {
100100
case let .installed(version), let .unsupported(version, _):
101101
languageServerVersion = version

Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotInstallationManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public struct GitHubCopilotInstallationManager {
3838
let versionData = try? Data(contentsOf: versionFileURL),
3939
let version = String(data: versionData, encoding: .utf8)
4040
{
41-
switch version.compare(Self.latestSupportedVersion) {
41+
switch version.compare(Self.latestSupportedVersion, options: .numeric) {
4242
case .orderedAscending:
4343
switch version.compare(Self.minimumSupportedVersion) {
4444
case .orderedAscending:

Tool/Sources/Preferences/Keys.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ public extension UserDefaultPreferenceKeys {
211211
var codeiumApiUrl: PreferenceKey<String> {
212212
.init(defaultValue: "", key: "CodeiumApiUrl")
213213
}
214+
215+
var codeiumEnterpriseVersion: PreferenceKey<String> {
216+
.init(defaultValue: "", key: "CodeiumEnterpriseVersion")
217+
}
218+
214219
}
215220

216221
// MARK: - Chat Models

0 commit comments

Comments
 (0)