@@ -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
0 commit comments