Skip to content

Commit 4fdd3ad

Browse files
committed
Introduce DestinationsBundle to SPMBuildCore
1 parent acfd9eb commit 4fdd3ad

File tree

4 files changed

+92
-51
lines changed

4 files changed

+92
-51
lines changed

Sources/Commands/DestinationTools/ListDestinations.swift

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -65,64 +65,21 @@ extension DestinationCommand {
6565
rootPath: bundlePath
6666
)
6767

68-
try parsedManifest.printValidatedArtifactIDs(
68+
let destinationsBundle = try parsedManifest.validateDestinationsBundle(
6969
bundlePath: bundlePath,
7070
fileSystem: fileSystem,
7171
observabilityScope: observabilityScope
7272
)
73-
} catch {
74-
observabilityScope.emit(
75-
.warning(
76-
"Couldn't parse `info.json` manifest of a destination bundle at \(bundlePath): \(error)"
77-
)
78-
)
79-
}
80-
}
81-
}
82-
}
83-
}
84-
85-
private extension ArtifactsArchiveMetadata {
86-
func printValidatedArtifactIDs(
87-
bundlePath: AbsolutePath,
88-
fileSystem: FileSystem,
89-
observabilityScope: ObservabilityScope
90-
) throws {
91-
for (artifactID, artifactMetadata) in artifacts
92-
where artifactMetadata.type == .crossCompilationDestination {
93-
for variant in artifactMetadata.variants {
94-
let destinationJSONPath = try bundlePath
95-
.appending(RelativePath(validating: variant.path))
96-
.appending(component: "destination.json")
9773

98-
guard fileSystem.exists(destinationJSONPath) else {
99-
observabilityScope.emit(
100-
.warning(
101-
"""
102-
Destination metadata file not found at \(
103-
destinationJSONPath
104-
) for a variant of artifact \(artifactID)
105-
"""
106-
)
107-
)
108-
109-
continue
110-
}
111-
112-
do {
113-
_ = try Destination(
114-
fromFile: destinationJSONPath, fileSystem: fileSystem
115-
)
74+
destinationsBundle.artifacts.keys.forEach { print($0) }
11675
} catch {
11776
observabilityScope.emit(
11877
.warning(
119-
"Couldn't parse destination metadata at \(destinationJSONPath): \(error)"
78+
"Couldn't parse `info.json` manifest of a destination bundle at \(bundlePath): \(error)"
12079
)
12180
)
12281
}
12382
}
124-
125-
print(artifactID)
12683
}
12784
}
12885
}

Sources/SPMBuildCore/ArtifactsArchiveMetadata.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public struct ArtifactsArchiveMetadata: Equatable {
2727
}
2828

2929
public struct Artifact: Equatable {
30-
public let type: ArtifactType
30+
let type: ArtifactType
3131
let version: String
32-
public let variants: [Variant]
32+
let variants: [Variant]
3333

3434
public init(type: ArtifactsArchiveMetadata.ArtifactType, version: String, variants: [Variant]) {
3535
self.type = type
@@ -47,7 +47,7 @@ public struct ArtifactsArchiveMetadata: Equatable {
4747
}
4848

4949
public struct Variant: Equatable {
50-
public let path: String
50+
let path: String
5151
let supportedTriples: [Triple]
5252

5353
public init(path: String, supportedTriples: [Triple]) {
@@ -73,11 +73,13 @@ extension ArtifactsArchiveMetadata {
7373
usesLenientParsing: true
7474
)
7575

76-
guard version <= Version(1, 1, 0) else {
76+
switch (version.major, version.minor) {
77+
case (1, 1), (1, 0):
78+
return decodedMetadata
79+
default:
7780
throw StringError("invalid `schemaVersion` of bundle manifest at `\(path)`: \(decodedMetadata.schemaVersion)")
7881
}
7982

80-
return decodedMetadata
8183
} catch {
8284
throw StringError("failed parsing ArtifactsArchive info.json at '\(path)': \(error)")
8385
}

Sources/SPMBuildCore/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_library(SPMBuildCore
1414
BuildSystemCommand.swift
1515
BuildSystemDelegate.swift
1616
BuiltTestProduct.swift
17+
CrossCompilationDestinations.swift
1718
PluginContextSerializer.swift
1819
PluginInvocation.swift
1920
PluginMessages.swift
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Basics
14+
import PackageModel
15+
import TSCBasic
16+
17+
/// Represents an `.artifactbundle` on the filesystem that contains cross-compilation destinations.
18+
public struct DestinationsBundle {
19+
public struct Variant {
20+
let metadata: ArtifactsArchiveMetadata.Variant
21+
let destination: Destination
22+
}
23+
24+
let path: AbsolutePath
25+
26+
/// Mapping of artifact IDs to variants available for a corresponding artifact.
27+
public fileprivate(set) var artifacts = [String: [Variant]]()
28+
}
29+
30+
extension ArtifactsArchiveMetadata {
31+
public func validateDestinationsBundle(
32+
bundlePath: AbsolutePath,
33+
fileSystem: FileSystem,
34+
observabilityScope: ObservabilityScope
35+
) throws -> DestinationsBundle {
36+
var result = DestinationsBundle(path: bundlePath)
37+
38+
for (artifactID, artifactMetadata) in artifacts
39+
where artifactMetadata.type == .crossCompilationDestination {
40+
var variants = [DestinationsBundle.Variant]()
41+
42+
for variant in artifactMetadata.variants {
43+
let destinationJSONPath = try bundlePath
44+
.appending(RelativePath(validating: variant.path))
45+
.appending(component: "destination.json")
46+
47+
guard fileSystem.exists(destinationJSONPath) else {
48+
observabilityScope.emit(
49+
.warning(
50+
"""
51+
Destination metadata file not found at \(
52+
destinationJSONPath
53+
) for a variant of artifact \(artifactID)
54+
"""
55+
)
56+
)
57+
58+
continue
59+
}
60+
61+
do {
62+
let destination = try Destination(
63+
fromFile: destinationJSONPath, fileSystem: fileSystem
64+
)
65+
66+
variants.append(.init(metadata: variant, destination: destination))
67+
} catch {
68+
observabilityScope.emit(
69+
.warning(
70+
"Couldn't parse destination metadata at \(destinationJSONPath): \(error)"
71+
)
72+
)
73+
}
74+
}
75+
76+
result.artifacts[artifactID] = variants
77+
}
78+
79+
return result
80+
}
81+
}

0 commit comments

Comments
 (0)