Skip to content

Commit b361651

Browse files
authored
Merge pull request #585 from XcodesOrg/matt/iOS18Runtimes
Fix runtime downloading
2 parents 8a6f5a7 + a587d53 commit b361651

3 files changed

Lines changed: 47 additions & 14 deletions

File tree

Xcodes/Backend/AppState+Runtimes.swift

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ extension AppState {
5757
self.setInstallationStep(of: runtime, to: .installing)
5858
}
5959
switch runtime.contentType {
60+
case .cryptexDiskImage:
61+
// not supported yet (do we need to for old packages?)
62+
throw "Installing via cryptexDiskImage not support - please install manually from \(downloadedURL.description)"
6063
case .package:
6164
// not supported yet (do we need to for old packages?)
6265
throw "Installing via package not support - please install manually from \(downloadedURL.description)"
@@ -80,19 +83,31 @@ extension AppState {
8083
Logger.appState.error("Error downloading runtime: \(error.localizedDescription)")
8184
DispatchQueue.main.async {
8285
self.error = error
83-
self.presentedAlert = .generic(title: localizeString("Alert.Install.Error.Title"), message: error.legibleLocalizedDescription)
86+
if let error = error as? String {
87+
self.presentedAlert = .generic(title: localizeString("Alert.Install.Error.Title"), message: error)
88+
} else {
89+
self.presentedAlert = .generic(title: localizeString("Alert.Install.Error.Title"), message: error.legibleLocalizedDescription)
90+
}
8491
}
8592
}
8693
}
8794
}
8895

8996
func downloadRunTimeFull(runtime: DownloadableRuntime) async throws -> URL {
97+
guard let source = runtime.source else {
98+
throw "Invalid runtime source"
99+
}
100+
101+
guard let downloadPath = runtime.downloadPath else {
102+
throw "Invalid runtime downloadPath"
103+
}
104+
90105
// sets a proper cookie for runtimes
91-
try await validateADCSession(path: runtime.downloadPath)
106+
try await validateADCSession(path: downloadPath)
92107

93108
let downloader = Downloader(rawValue: UserDefaults.standard.string(forKey: "downloader") ?? "aria2") ?? .aria2
94109

95-
let url = URL(string: runtime.source)!
110+
let url = URL(string: source)!
96111
let expectedRuntimePath = Path.xcodesApplicationSupport/"\(url.lastPathComponent)"
97112
// aria2 downloads directly to the destination (instead of into /tmp first) so we need to make sure that the download isn't incomplete
98113
let aria2DownloadMetadataPath = expectedRuntimePath.parent/(expectedRuntimePath.basename() + ".aria2")
@@ -123,9 +138,15 @@ extension AppState {
123138
}
124139

125140
public func downloadRuntimeWithAria2(_ runtime: DownloadableRuntime, to destination: Path, aria2Path: Path) -> AsyncThrowingStream<Progress, Error> {
126-
let cookies = AppleAPI.Current.network.session.configuration.httpCookieStorage?.cookies(for: runtime.url) ?? []
141+
guard let url = runtime.url else {
142+
return AsyncThrowingStream<Progress, Error> { continuation in
143+
continuation.finish(throwing: "Invalid or non existant runtime url")
144+
}
145+
}
146+
147+
let cookies = AppleAPI.Current.network.session.configuration.httpCookieStorage?.cookies(for: url) ?? []
127148

128-
return Current.shell.downloadWithAria2Async(aria2Path, runtime.url, destination, cookies)
149+
return Current.shell.downloadWithAria2Async(aria2Path, url, destination, cookies)
129150
}
130151

131152

@@ -140,7 +161,10 @@ extension AppState {
140161
runtimePublishers[runtime.identifier] = nil
141162

142163
// If the download is cancelled by the user, clean up the download files that aria2 creates.
143-
let url = URL(string: runtime.source)!
164+
guard let source = runtime.source else {
165+
return
166+
}
167+
let url = URL(string: source)!
144168
let expectedRuntimePath = Path.xcodesApplicationSupport/"\(url.lastPathComponent)"
145169
let aria2DownloadMetadataPath = expectedRuntimePath.parent/(expectedRuntimePath.basename() + ".aria2")
146170

Xcodes/XcodesKit/Sources/XcodesKit/Models/Runtimes/Runtimes.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public struct DownloadableRuntimesResponse: Codable {
1111
public struct DownloadableRuntime: Codable, Identifiable, Hashable {
1212
public let category: Category
1313
public let simulatorVersion: SimulatorVersion
14-
public let source: String
14+
public let source: String?
1515
public let dictionaryVersion: Int
1616
public let contentType: ContentType
1717
public let platform: Platform
@@ -21,11 +21,14 @@ public struct DownloadableRuntime: Codable, Identifiable, Hashable {
2121
public let hostRequirements: HostRequirements?
2222
public let name: String
2323
public let authentication: Authentication?
24-
public var url: URL {
25-
return URL(string: source)!
24+
public var url: URL? {
25+
if let source {
26+
return URL(string: source)!
27+
}
28+
return nil
2629
}
27-
public var downloadPath: String {
28-
url.path
30+
public var downloadPath: String? {
31+
url?.path
2932
}
3033

3134
// dynamically updated - not decoded
@@ -117,6 +120,7 @@ extension DownloadableRuntime {
117120
public enum ContentType: String, Codable {
118121
case diskImage = "diskImage"
119122
case package = "package"
123+
case cryptexDiskImage = "cryptexDiskImage"
120124
}
121125

122126
public enum Platform: String, Codable {

Xcodes/XcodesKit/Sources/XcodesKit/Services/RuntimeService.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ public struct RuntimeService {
2222

2323
// Apple gives a plist for download
2424
let (data, _) = try await networkService.requestData(urlRequest, validators: [])
25-
let decodedResponse = try PropertyListDecoder().decode(DownloadableRuntimesResponse.self, from: data)
26-
27-
return decodedResponse
25+
do {
26+
let decodedResponse = try PropertyListDecoder().decode(DownloadableRuntimesResponse.self, from: data)
27+
return decodedResponse
28+
} catch {
29+
print("error: \(error)")
30+
throw error
31+
}
32+
2833
}
2934

3035
public func installedRuntimes() async throws -> [InstalledRuntime] {

0 commit comments

Comments
 (0)