-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathSwiftSDKBundleStore.swift
390 lines (337 loc) · 16.1 KB
/
SwiftSDKBundleStore.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// FIXME: can't write `import actor Basics.HTTPClient`, importing the whole module because of that :(
import Basics
import struct Foundation.URL
import protocol TSCBasic.FileSystem
import struct TSCBasic.RegEx
import protocol TSCUtility.ProgressAnimationProtocol
public final class SwiftSDKBundleStore {
public enum Output: Equatable, CustomStringConvertible {
case downloadStarted(URL)
case downloadFinishedSuccessfully(URL)
case unpackingArchive(bundlePathOrURL: String)
case installationSuccessful(bundlePathOrURL: String, bundleName: String)
public var description: String {
switch self {
case let .downloadStarted(url):
return "Downloading a Swift SDK bundle archive from `\(url)`..."
case let .downloadFinishedSuccessfully(url):
return "Swift SDK bundle archive successfully downloaded from `\(url)`."
case let .installationSuccessful(bundlePathOrURL, bundleName):
return "Swift SDK bundle at `\(bundlePathOrURL)` successfully installed as \(bundleName)."
case let .unpackingArchive(bundlePathOrURL):
return "Swift SDK bundle at `\(bundlePathOrURL)` is assumed to be an archive, unpacking..."
}
}
}
enum Error: Swift.Error, CustomStringConvertible {
case noMatchingSwiftSDK(selector: String, hostTriple: Triple)
var description: String {
switch self {
case let .noMatchingSwiftSDK(selector, hostTriple):
return """
No Swift SDK found matching query `\(selector)` and host triple \
`\(hostTriple.tripleString)`. Use `swift sdk list` command to see \
available Swift SDKs.
"""
}
}
}
/// Directory in which Swift SDKs bundles are stored.
let swiftSDKsDirectory: AbsolutePath
/// File system instance used for reading from and writing to SDK bundles stored on it.
let fileSystem: any FileSystem
/// Observability scope used for logging.
private let observabilityScope: ObservabilityScope
/// Closure invoked for output produced by this store during its operation.
private let outputHandler: (Output) -> Void
/// Progress animation used for downloading SDK bundles.
private let downloadProgressAnimation: ProgressAnimationProtocol?
public init(
swiftSDKsDirectory: AbsolutePath,
fileSystem: any FileSystem,
observabilityScope: ObservabilityScope,
outputHandler: @escaping (Output) -> Void,
downloadProgressAnimation: ProgressAnimationProtocol? = nil
) {
self.swiftSDKsDirectory = swiftSDKsDirectory
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope
self.outputHandler = outputHandler
self.downloadProgressAnimation = downloadProgressAnimation
}
/// An array of valid Swift SDK bundles stored in ``SwiftSDKBundleStore//swiftSDKsDirectory``.
public var allValidBundles: [SwiftSDKBundle] {
get throws {
// Get absolute paths to available Swift SDK bundles.
try self.fileSystem.getDirectoryContents(swiftSDKsDirectory).filter {
$0.hasSuffix(BinaryTarget.Kind.artifactsArchive.fileExtension)
}.map {
self.swiftSDKsDirectory.appending(components: [$0])
}.compactMap {
do {
// Enumerate available bundles and parse manifests for each of them, then validate supplied
// Swift SDKs.
return try self.parseAndValidate(bundlePath: $0)
} catch {
observabilityScope.emit(
warning: "Couldn't parse `info.json` manifest of a Swift SDK bundle at \($0)",
underlyingError: error
)
return nil
}
}
}
}
/// Select a Swift SDK matching a given query and host triple from all Swift SDKs available in
/// ``SwiftSDKBundleStore//swiftSDKsDirectory``.
/// - Parameters:
/// - query: either an artifact ID or target triple to filter with.
/// - hostTriple: triple of the host building with these Swift SDKs.
/// - Returns: ``SwiftSDK`` value matching `query` either by artifact ID or target triple, `nil` if none found.
public func selectBundle(
matching selector: String,
hostTriple: Triple
) throws -> SwiftSDK {
let validBundles = try self.allValidBundles
guard !validBundles.isEmpty else {
throw StringError(
"No valid Swift SDK bundles found at \(self.swiftSDKsDirectory)."
)
}
guard var selectedSwiftSDKs = validBundles.selectSwiftSDK(
matching: selector,
hostTriple: hostTriple,
observabilityScope: self.observabilityScope
) else {
throw Error.noMatchingSwiftSDK(selector: selector, hostTriple: hostTriple)
}
selectedSwiftSDKs.applyPathCLIOptions()
return selectedSwiftSDKs
}
/// Installs a Swift SDK bundle from a given path or URL to ``SwiftSDKBundleStore//swiftSDKsDirectory``.
/// - Parameters:
/// - bundlePathOrURL: A string passed on the command line, which is either an absolute or relative to a current
/// working directory path, or a URL to a Swift SDK artifact bundle.
/// - archiver: Archiver instance to use for extracting bundle archives.
public func install(
bundlePathOrURL: String,
_ archiver: any Archiver,
_ httpClient: HTTPClient = .init()
) async throws {
let bundleName = try await withTemporaryDirectory(fileSystem: self.fileSystem, removeTreeOnDeinit: true) { temporaryDirectory in
let bundlePath: AbsolutePath
if
let bundleURL = URL(string: bundlePathOrURL),
let scheme = bundleURL.scheme,
scheme == "http" || scheme == "https"
{
let bundleName: String
let fileNameComponent = bundleURL.lastPathComponent
if archiver.supportedExtensions.contains(where: { fileNameComponent.hasSuffix($0) }) {
bundleName = fileNameComponent
} else {
// Assume that the bundle is a tarball if it doesn't have a recognized extension.
bundleName = "bundle.tar.gz"
}
let downloadedBundlePath = temporaryDirectory.appending(component: bundleName)
var request = HTTPClientRequest.download(
url: bundleURL,
fileSystem: self.fileSystem,
destination: downloadedBundlePath
)
request.options.validResponseCodes = [200]
self.outputHandler(.downloadStarted(bundleURL))
_ = try await httpClient.execute(
request,
observabilityScope: self.observabilityScope,
progress: { step, total in
guard let progressAnimation = self.downloadProgressAnimation else {
return
}
let step = step > Int.max ? Int.max : Int(step)
let total = total.map { $0 > Int.max ? Int.max : Int($0) } ?? step
progressAnimation.update(
step: step,
total: total,
text: "Downloading \(bundleURL.lastPathComponent)"
)
}
)
self.downloadProgressAnimation?.complete(success: true)
bundlePath = downloadedBundlePath
self.outputHandler(.downloadFinishedSuccessfully(bundleURL))
} else if
let cwd: AbsolutePath = self.fileSystem.currentWorkingDirectory,
let originalBundlePath = try? AbsolutePath(validating: bundlePathOrURL, relativeTo: cwd)
{
bundlePath = originalBundlePath
} else {
throw SwiftSDKError.invalidPathOrURL(bundlePathOrURL)
}
return try await self.installIfValid(
bundlePathOrURL: bundlePathOrURL,
validatedBundlePath: bundlePath,
temporaryDirectory: temporaryDirectory,
archiver: archiver
)
}.value
self.outputHandler(.installationSuccessful(bundlePathOrURL: bundlePathOrURL, bundleName: bundleName))
}
/// Unpacks a Swift SDK bundle if it has an archive extension in its filename.
/// - Parameters:
/// - bundlePath: Absolute path to a Swift SDK bundle to unpack if needed.
/// - temporaryDirectory: Absolute path to a temporary directory in which the bundle can be unpacked if needed.
/// - archiver: Archiver instance to use for extracting bundle archives.
/// - Returns: Path to an unpacked Swift SDK bundle if unpacking is needed, value of `bundlePath` is returned
/// otherwise.
private func unpackIfNeeded(
bundlePathOrURL: String,
validatedBundlePath bundlePath: AbsolutePath,
temporaryDirectory: AbsolutePath,
_ archiver: any Archiver
) async throws -> AbsolutePath {
// If there's no archive extension on the bundle name, assuming it's not archived and returning the same path.
guard !bundlePath.pathString.hasSuffix(".\(artifactBundleExtension)") else {
return bundlePath
}
self.outputHandler(.unpackingArchive(bundlePathOrURL: bundlePathOrURL))
let extractionResultsDirectory = temporaryDirectory.appending("extraction-results")
try self.fileSystem.createDirectory(extractionResultsDirectory)
try await archiver.extract(from: bundlePath, to: extractionResultsDirectory)
guard let bundleName = try fileSystem.getDirectoryContents(extractionResultsDirectory).first,
bundleName.hasSuffix(".\(artifactBundleExtension)")
else {
throw SwiftSDKError.invalidBundleArchive(bundlePath)
}
let installedBundlePath = swiftSDKsDirectory.appending(component: bundleName)
guard !self.fileSystem.exists(installedBundlePath) else {
throw SwiftSDKError.swiftSDKBundleAlreadyInstalled(bundleName: bundleName)
}
return extractionResultsDirectory.appending(component: bundleName)
}
/// Installs an unpacked Swift SDK bundle to a Swift SDK installation directory.
/// - Parameters:
/// - bundlePath: absolute path to an unpacked Swift SDK bundle directory.
/// - temporaryDirectory: Temporary directory to use if the bundle is an archive that needs extracting.
/// - archiver: Archiver instance to use for extracting bundle archives.
/// - Returns: Name of the bundle installed.
private func installIfValid(
bundlePathOrURL: String,
validatedBundlePath: AbsolutePath,
temporaryDirectory: AbsolutePath,
archiver: any Archiver
) async throws -> String {
#if os(macOS)
// Check the quarantine attribute on bundles downloaded manually in the browser.
guard !self.fileSystem.hasAttribute(.quarantine, validatedBundlePath) else {
throw SwiftSDKError.quarantineAttributePresent(bundlePath: validatedBundlePath)
}
#endif
let unpackedBundlePath = try await self.unpackIfNeeded(
bundlePathOrURL: bundlePathOrURL,
validatedBundlePath: validatedBundlePath,
temporaryDirectory: temporaryDirectory,
archiver
)
guard
self.fileSystem.isDirectory(unpackedBundlePath),
let bundleName = unpackedBundlePath.components.last
else {
throw SwiftSDKError.pathIsNotDirectory(validatedBundlePath)
}
let installedBundlePath = self.swiftSDKsDirectory.appending(component: bundleName)
let validatedBundle = try self.parseAndValidate(bundlePath: unpackedBundlePath)
let newArtifactIDs = validatedBundle.artifacts.keys
let installedBundles = try self.allValidBundles
for installedBundle in installedBundles {
for artifactID in installedBundle.artifacts.keys {
guard !newArtifactIDs.contains(artifactID) else {
throw SwiftSDKError.swiftSDKArtifactAlreadyInstalled(
installedBundleName: installedBundle.name,
newBundleName: validatedBundle.name,
artifactID: artifactID
)
}
}
}
try self.fileSystem.copy(from: unpackedBundlePath, to: installedBundlePath)
return bundleName
}
/// Parses metadata of an `.artifactbundle` and validates it as a bundle containing
/// cross-compilation Swift SDKs.
/// - Parameters:
/// - bundlePath: path to the bundle root directory.
/// - Returns: Validated ``SwiftSDKBundle`` containing validated ``SwiftSDK`` values for
/// each artifact and its variants.
private func parseAndValidate(bundlePath: AbsolutePath) throws -> SwiftSDKBundle {
let parsedManifest = try ArtifactsArchiveMetadata.parse(
fileSystem: self.fileSystem,
rootPath: bundlePath
)
return try self.validateSwiftSDKBundle(
bundlePath: bundlePath,
bundleManifest: parsedManifest
)
}
private func validateSwiftSDKBundle(
bundlePath: AbsolutePath,
bundleManifest: ArtifactsArchiveMetadata
) throws -> SwiftSDKBundle {
var result = SwiftSDKBundle(path: bundlePath)
for (artifactID, artifactMetadata) in bundleManifest.artifacts {
if artifactMetadata.type == .crossCompilationDestination {
self.observabilityScope.emit(
warning: """
`crossCompilationDestination` bundle metadata value used for `\(artifactID)` is deprecated, \
use `swiftSDK` instead.
"""
)
} else {
guard artifactMetadata.type == .swiftSDK else { continue }
}
var variants = [SwiftSDKBundle.Variant]()
for variantMetadata in artifactMetadata.variants {
let variantConfigurationPath = bundlePath
.appending(variantMetadata.path)
.appending("swift-sdk.json")
guard self.fileSystem.exists(variantConfigurationPath) else {
self.observabilityScope.emit(
.warning(
"""
Swift SDK metadata file not found at \(
variantConfigurationPath
) for a variant of artifact \(artifactID)
"""
)
)
continue
}
do {
let swiftSDKs = try SwiftSDK.decode(
fromFile: variantConfigurationPath, fileSystem: fileSystem,
observabilityScope: observabilityScope
)
variants.append(.init(metadata: variantMetadata, swiftSDKs: swiftSDKs))
} catch {
observabilityScope.emit(
warning: "Couldn't parse Swift SDK artifact metadata at \(variantConfigurationPath)",
underlyingError: error
)
}
}
result.artifacts[artifactID] = variants
}
return result
}
}