|
| 1 | +import Foundation |
| 2 | +import XcodesKit |
| 3 | +import OSLog |
| 4 | +import Combine |
| 5 | +import Path |
| 6 | +import AppleAPI |
| 7 | + |
| 8 | +extension AppState { |
| 9 | + func updateDownloadableRuntimes() { |
| 10 | + Task { |
| 11 | + do { |
| 12 | + |
| 13 | + let downloadableRuntimes = try await self.runtimeService.downloadableRuntimes() |
| 14 | + let runtimes = downloadableRuntimes.downloadables.map { runtime in |
| 15 | + var updatedRuntime = runtime |
| 16 | + |
| 17 | + // This loops through and matches up the simulatorVersion to the mappings |
| 18 | + let simulatorBuildUpdate = downloadableRuntimes.sdkToSimulatorMappings.first { SDKToSimulatorMapping in |
| 19 | + SDKToSimulatorMapping.simulatorBuildUpdate == runtime.simulatorVersion.buildUpdate |
| 20 | + } |
| 21 | + updatedRuntime.sdkBuildUpdate = simulatorBuildUpdate?.sdkBuildUpdate |
| 22 | + return updatedRuntime |
| 23 | + } |
| 24 | + |
| 25 | + DispatchQueue.main.async { |
| 26 | + self.downloadableRuntimes = runtimes |
| 27 | + } |
| 28 | + try? cacheDownloadableRuntimes(runtimes) |
| 29 | + } catch { |
| 30 | + Logger.appState.error("Error downloading runtimes: \(error.localizedDescription)") |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + func updateInstalledRuntimes() { |
| 36 | + Task { |
| 37 | + do { |
| 38 | + let runtimes = try await self.runtimeService.localInstalledRuntimes() |
| 39 | + DispatchQueue.main.async { |
| 40 | + self.installedRuntimes = runtimes |
| 41 | + } |
| 42 | + } catch { |
| 43 | + Logger.appState.error("Error loading installed runtimes: \(error.localizedDescription)") |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + func downloadRuntime(runtime: DownloadableRuntime) { |
| 49 | + Task { |
| 50 | + do { |
| 51 | + try await downloadRunTimeFull(runtime: runtime) |
| 52 | + |
| 53 | + DispatchQueue.main.async { |
| 54 | + guard let index = self.downloadableRuntimes.firstIndex(where: { $0.identifier == runtime.identifier }) else { return } |
| 55 | + self.downloadableRuntimes[index].installState = .installed |
| 56 | + } |
| 57 | + |
| 58 | + updateInstalledRuntimes() |
| 59 | + } |
| 60 | + catch { |
| 61 | + Logger.appState.error("Error downloading runtime: \(error.localizedDescription)") |
| 62 | + DispatchQueue.main.async { |
| 63 | + self.error = error |
| 64 | + self.presentedAlert = .generic(title: localizeString("Alert.Install.Error.Title"), message: error.legibleLocalizedDescription) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + func downloadRunTimeFull(runtime: DownloadableRuntime) async throws { |
| 71 | + // sets a proper cookie for runtimes |
| 72 | + try await validateADCSession(path: runtime.downloadPath) |
| 73 | + |
| 74 | + let downloader = Downloader(rawValue: UserDefaults.standard.string(forKey: "downloader") ?? "aria2") ?? .aria2 |
| 75 | + Logger.appState.info("Downloading \(runtime.visibleIdentifier) with \(downloader)") |
| 76 | + |
| 77 | + |
| 78 | + let url = try await self.downloadRuntime(for: runtime, downloader: downloader, progressChanged: { [unowned self] progress in |
| 79 | + DispatchQueue.main.async { |
| 80 | + self.setInstallationStep(of: runtime, to: .downloading(progress: progress)) |
| 81 | + } |
| 82 | + }).async() |
| 83 | + |
| 84 | + Logger.appState.debug("Done downloading: \(url)") |
| 85 | + DispatchQueue.main.async { |
| 86 | + self.setInstallationStep(of: runtime, to: .installing) |
| 87 | + } |
| 88 | + switch runtime.contentType { |
| 89 | + case .package: |
| 90 | + // not supported yet (do we need to for old packages?) |
| 91 | + throw "Installing via package not support - please install manually from \(url.description)" |
| 92 | + case .diskImage: |
| 93 | + try await self.installFromImage(dmgURL: url) |
| 94 | + DispatchQueue.main.async { |
| 95 | + self.setInstallationStep(of: runtime, to: .trashingArchive) |
| 96 | + } |
| 97 | + try Current.files.removeItem(at: url) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @MainActor |
| 102 | + func downloadRuntime(for runtime: DownloadableRuntime, downloader: Downloader, progressChanged: @escaping (Progress) -> Void) -> AnyPublisher<URL, Error> { |
| 103 | + // Check to see if the dmg is in the expected path in case it was downloaded but failed to install |
| 104 | + |
| 105 | + // call https://developerservices2.apple.com/services/download?path=/Developer_Tools/watchOS_10_beta/watchOS_10_beta_Simulator_Runtime.dmg 1st to get cookie |
| 106 | + // use runtime.url for final with cookies |
| 107 | + |
| 108 | + // Check to see if the archive is in the expected path in case it was downloaded but failed to install |
| 109 | + let url = URL(string: runtime.source)! |
| 110 | + let expectedRuntimePath = Path.xcodesApplicationSupport/"\(url.lastPathComponent)" |
| 111 | + // aria2 downloads directly to the destination (instead of into /tmp first) so we need to make sure that the download isn't incomplete |
| 112 | + let aria2DownloadMetadataPath = expectedRuntimePath.parent/(expectedRuntimePath.basename() + ".aria2") |
| 113 | + var aria2DownloadIsIncomplete = false |
| 114 | + if case .aria2 = downloader, aria2DownloadMetadataPath.exists { |
| 115 | + aria2DownloadIsIncomplete = true |
| 116 | + } |
| 117 | + if Current.files.fileExistsAtPath(expectedRuntimePath.string), aria2DownloadIsIncomplete == false { |
| 118 | + Logger.appState.info("Found existing runtime that will be used for installation at \(expectedRuntimePath).") |
| 119 | + return Just(expectedRuntimePath.url) |
| 120 | + .setFailureType(to: Error.self) |
| 121 | + .eraseToAnyPublisher() |
| 122 | + } |
| 123 | + else { |
| 124 | + |
| 125 | + Logger.appState.info("Downloading runtime: \(url.lastPathComponent)") |
| 126 | + switch downloader { |
| 127 | + case .aria2: |
| 128 | + let aria2Path = Path(url: Bundle.main.url(forAuxiliaryExecutable: "aria2c")!)! |
| 129 | + return downloadRuntimeWithAria2( |
| 130 | + runtime, |
| 131 | + to: expectedRuntimePath, |
| 132 | + aria2Path: aria2Path, |
| 133 | + progressChanged: progressChanged) |
| 134 | + |
| 135 | + case .urlSession: |
| 136 | + // TODO: Support runtime download via URL Session |
| 137 | + return Just(runtime.url) |
| 138 | + .setFailureType(to: Error.self) |
| 139 | + .eraseToAnyPublisher() |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public func downloadRuntimeWithAria2(_ runtime: DownloadableRuntime, to destination: Path, aria2Path: Path, progressChanged: @escaping (Progress) -> Void) -> AnyPublisher<URL, Error> { |
| 145 | + let cookies = AppleAPI.Current.network.session.configuration.httpCookieStorage?.cookies(for: runtime.url) ?? [] |
| 146 | + |
| 147 | + let (progress, publisher) = Current.shell.downloadWithAria2( |
| 148 | + aria2Path, |
| 149 | + runtime.url, |
| 150 | + destination, |
| 151 | + cookies |
| 152 | + ) |
| 153 | + progressChanged(progress) |
| 154 | + return publisher |
| 155 | + .map { _ in destination.url } |
| 156 | + .eraseToAnyPublisher() |
| 157 | + } |
| 158 | + |
| 159 | + public func installFromImage(dmgURL: URL) async throws { |
| 160 | + try await self.runtimeService.installRuntimeImage(dmgURL: dmgURL) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +extension AnyPublisher { |
| 165 | + func async() async throws -> Output { |
| 166 | + try await withCheckedThrowingContinuation { continuation in |
| 167 | + var cancellable: AnyCancellable? |
| 168 | + |
| 169 | + cancellable = first() |
| 170 | + .sink { result in |
| 171 | + switch result { |
| 172 | + case .finished: |
| 173 | + break |
| 174 | + case let .failure(error): |
| 175 | + continuation.resume(throwing: error) |
| 176 | + } |
| 177 | + cancellable?.cancel() |
| 178 | + } receiveValue: { value in |
| 179 | + continuation.resume(with: .success(value)) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments