|
| 1 | +import Foundation |
| 2 | +import Terminal |
| 3 | + |
| 4 | +public struct CodeiumInstallationManager { |
| 5 | + private static var isInstalling = false |
| 6 | + let latestSupportedVersion: String = "1.2.9" |
| 7 | + |
| 8 | + public init() {} |
| 9 | + |
| 10 | + public enum InstallationStatus { |
| 11 | + case notInstalled |
| 12 | + case installed(String) |
| 13 | + case outdated(current: String, latest: String) |
| 14 | + case unsupported(current: String, latest: String) |
| 15 | + } |
| 16 | + |
| 17 | + public func checkInstallation() -> InstallationStatus { |
| 18 | + guard let urls = try? CodeiumSuggestionService.createFoldersIfNeeded() |
| 19 | + else { return .notInstalled } |
| 20 | + let executableFolderURL = urls.executableURL |
| 21 | + let binaryURL = executableFolderURL.appendingPathComponent("language_server") |
| 22 | + let versionFileURL = executableFolderURL.appendingPathComponent("version") |
| 23 | + |
| 24 | + if !FileManager.default.fileExists(atPath: binaryURL.path) { |
| 25 | + return .notInstalled |
| 26 | + } |
| 27 | + |
| 28 | + if FileManager.default.fileExists(atPath: versionFileURL.path), |
| 29 | + let versionData = try? Data(contentsOf: versionFileURL), |
| 30 | + let version = String(data: versionData, encoding: .utf8) |
| 31 | + { |
| 32 | + switch version.compare(latestSupportedVersion) { |
| 33 | + case .orderedAscending: |
| 34 | + return .outdated(current: version, latest: latestSupportedVersion) |
| 35 | + case .orderedSame: |
| 36 | + return .installed(version) |
| 37 | + case .orderedDescending: |
| 38 | + return .unsupported(current: version, latest: latestSupportedVersion) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return .outdated(current: "Unknown", latest: latestSupportedVersion) |
| 43 | + } |
| 44 | + |
| 45 | + public enum InstallationStep { |
| 46 | + case downloading |
| 47 | + case uninstalling |
| 48 | + case decompressing |
| 49 | + case done |
| 50 | + } |
| 51 | + |
| 52 | + public func installLatestVersion() -> AsyncThrowingStream<InstallationStep, Error> { |
| 53 | + AsyncThrowingStream<InstallationStep, Error> { continuation in |
| 54 | + Task { |
| 55 | + guard !CodeiumInstallationManager.isInstalling else { |
| 56 | + continuation.finish(throwing: CodeiumError.languageServiceIsInstalling) |
| 57 | + return |
| 58 | + } |
| 59 | + CodeiumInstallationManager.isInstalling = true |
| 60 | + defer { CodeiumInstallationManager.isInstalling = false } |
| 61 | + do { |
| 62 | + continuation.yield(.downloading) |
| 63 | + let urls = try CodeiumSuggestionService.createFoldersIfNeeded() |
| 64 | + let urlString = |
| 65 | + "https://github.com/Exafunction/codeium/releases/download/language-server-v\(latestSupportedVersion)/language_server_macos_\(isAppleSilicon() ? "arm" : "x64").gz" |
| 66 | + guard let url = URL(string: urlString) else { return } |
| 67 | + |
| 68 | + // download |
| 69 | + let (fileURL, _) = try await URLSession.shared.download(from: url) |
| 70 | + let targetURL = urls.executableURL.appendingPathComponent("language_server") |
| 71 | + .appendingPathExtension("gz") |
| 72 | + try FileManager.default.copyItem(at: fileURL, to: targetURL) |
| 73 | + defer { try? FileManager.default.removeItem(at: targetURL) } |
| 74 | + |
| 75 | + continuation.yield(.uninstalling) |
| 76 | + try await uninstall() |
| 77 | + |
| 78 | + continuation.yield(.decompressing) |
| 79 | + // extract file |
| 80 | + let terminal = Terminal() |
| 81 | + _ = try await terminal.runCommand( |
| 82 | + "/usr/bin/gunzip", |
| 83 | + arguments: [targetURL.path], |
| 84 | + environment: [:] |
| 85 | + ) |
| 86 | + |
| 87 | + // update permission 755 |
| 88 | + try FileManager.default.setAttributes( |
| 89 | + [.posixPermissions: 0o755], |
| 90 | + ofItemAtPath: targetURL.deletingPathExtension().path |
| 91 | + ) |
| 92 | + |
| 93 | + // create version file |
| 94 | + let data = latestSupportedVersion.data(using: .utf8) |
| 95 | + FileManager.default.createFile( |
| 96 | + atPath: urls.executableURL.appendingPathComponent("version").path, |
| 97 | + contents: data |
| 98 | + ) |
| 99 | + |
| 100 | + continuation.yield(.done) |
| 101 | + continuation.finish() |
| 102 | + } catch { |
| 103 | + continuation.finish(throwing: error) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public func uninstall() async throws { |
| 110 | + guard let urls = try? CodeiumSuggestionService.createFoldersIfNeeded() |
| 111 | + else { return } |
| 112 | + let executableFolderURL = urls.executableURL |
| 113 | + let binaryURL = executableFolderURL.appendingPathComponent("language_server") |
| 114 | + let versionFileURL = executableFolderURL.appendingPathComponent("version") |
| 115 | + if FileManager.default.fileExists(atPath: binaryURL.path) { |
| 116 | + try FileManager.default.removeItem(at: binaryURL) |
| 117 | + } |
| 118 | + if FileManager.default.fileExists(atPath: versionFileURL.path) { |
| 119 | + try FileManager.default.removeItem(at: versionFileURL) |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func isAppleSilicon() -> Bool { |
| 125 | + var result = false |
| 126 | + #if arch(arm64) |
| 127 | + result = true |
| 128 | + #endif |
| 129 | + return result |
| 130 | +} |
| 131 | + |
0 commit comments