Skip to content

Commit dfbb06b

Browse files
authored
chore: Create AWSSDKDynamic target with generated runtime code (#2032)
1 parent d471dd8 commit dfbb06b

File tree

23 files changed

+667
-7
lines changed

23 files changed

+667
-7
lines changed

AWSSDKSwiftCLI/Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ let package = Package(
3939
),
4040
.testTarget(
4141
name: "AWSSDKSwiftCLITests",
42-
dependencies: ["AWSSDKSwiftCLI"]
42+
dependencies: ["AWSSDKSwiftCLI"],
43+
resources: [.process("Resources")]
4344
)
4445
]
4546
)

AWSSDKSwiftCLI/Sources/AWSCLIUtils/FileManager+Utils.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public extension FileManager {
6969
.sorted()
7070
.filter { $0 != "AWSSDKForSwift" } // Ignore documentation module
7171
.filter { $0 != "SDKForSwift" } // Ignore new documentation module
72+
.filter { $0 != "AWSSDKDynamic" } // Internal module, do not document
7273
.filter { !$0.hasPrefix(".") }
7374
}
7475

AWSSDKSwiftCLI/Sources/AWSSDKSwiftCLI/Commands/AWSSDKSwiftCLI/AWSSDKSwiftCLI.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ struct AWSSDKSwiftCLI: ParsableCommand {
1717
PrepareReleaseCommand.self,
1818
SyncClientRuntimeVersionCommand.self,
1919
GenerateDocIndexCommand.self,
20-
GenerateSmokeTestsPackageManifestCommand.self
20+
GenerateSmokeTestsPackageManifestCommand.self,
21+
GeneratePartitionsCommand.self,
22+
GeneratePackageVersionCommand.self,
2123
]
2224
)
2325
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import ArgumentParser
9+
import Foundation
10+
import AWSCLIUtils
11+
12+
struct GeneratePackageVersionCommand: ParsableCommand {
13+
14+
static var configuration = CommandConfiguration(
15+
commandName: "generate-package-version",
16+
abstract: "Generates the PackageVersion.swift file for the AWSSDKDynamic target."
17+
)
18+
19+
@Argument(help: "The path to the aws-sdk-swift repository")
20+
var repoPath: String
21+
22+
func run() throws {
23+
try PackageVersionBuilder(
24+
repoPath: repoPath
25+
).generatePackageVersionFile()
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import ArgumentParser
9+
import Foundation
10+
import AWSCLIUtils
11+
12+
struct GeneratePartitionsCommand: ParsableCommand {
13+
14+
static var configuration = CommandConfiguration(
15+
commandName: "generate-partitions",
16+
abstract: "Generates the Partitions.swift file for the AWSSDKDynamic target."
17+
)
18+
19+
@Argument(help: "The path to the aws-sdk-swift repository")
20+
var repoPath: String
21+
22+
func run() throws {
23+
try PartitionsBuilder(
24+
repoPath: repoPath
25+
).generatePartitionsFile()
26+
}
27+
}

AWSSDKSwiftCLI/Sources/AWSSDKSwiftCLI/Commands/AWSSDKSwiftCLI/Subcommands/PrepareRelease.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ struct PrepareRelease {
211211
"Sources/Core/SDKForSwift/Documentation.docc/SDKForSwift.md",
212212
"Sources/Core/AWSSDKPartitions/Sources/AWSSDKPartitions/Partitions.swift",
213213
"Sources/Core/AWSSDKIdentity/",
214+
"Sources/Core/AWSSDKDynamic/Sources/AWSSDKDynamic/PackageVersion.swift",
215+
"Sources/Core/AWSSDKDynamic/Sources/AWSSDKDynamic/Partitions.swift",
214216
]
215217
case .smithySwift:
216218
files = ["Package.version", "Package.version.next"]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
import struct AWSCLIUtils.Error
10+
import struct AWSCLIUtils.Version
11+
12+
struct PackageVersionBuilder {
13+
let packageVersionFileURL: URL
14+
let packageVersionSwiftFileURL: URL
15+
16+
// MARK: - init
17+
18+
init(repoPath: String) {
19+
let repoFileURL = URL(fileURLWithPath: repoPath)
20+
self.init(
21+
packageVersionFileURL: repoFileURL.appendingPathComponent("Package.version.next"),
22+
packageVersionSwiftFileURL: repoFileURL.appendingPathComponent(
23+
"Sources/Core/AWSSDKDynamic/Sources/AWSSDKDynamic/PackageVersion.swift"
24+
)
25+
)
26+
}
27+
28+
init(
29+
packageVersionFileURL: URL,
30+
packageVersionSwiftFileURL: URL
31+
) {
32+
self.packageVersionFileURL = packageVersionFileURL
33+
self.packageVersionSwiftFileURL = packageVersionSwiftFileURL
34+
}
35+
36+
// MARK: - Code generation
37+
38+
func generatePackageVersionFile() throws {
39+
let currentVersionData = try Data(contentsOf: packageVersionFileURL)
40+
guard let packageVersion = String(data: currentVersionData, encoding: .utf8) else {
41+
throw Error("Package.version.next does not contain UTF-8 data.")
42+
}
43+
_ = try Version(packageVersion) // throws if currentVersion is not a valid version string
44+
let packageVersionFileContents = """
45+
//
46+
// Copyright Amazon.com Inc. or its affiliates.
47+
// All Rights Reserved.
48+
//
49+
// SPDX-License-Identifier: Apache-2.0
50+
//
51+
52+
// Code is auto-generated. DO NOT EDIT!
53+
54+
public let packageVersion = "\(packageVersion.trimmingCharacters(in: .whitespacesAndNewlines))"
55+
56+
"""
57+
try FileManager.default.createDirectory(
58+
at: packageVersionSwiftFileURL.deletingLastPathComponent(),
59+
withIntermediateDirectories: true
60+
)
61+
try Data(packageVersionFileContents.utf8).write(to: packageVersionSwiftFileURL)
62+
}
63+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
import struct AWSCLIUtils.Error
10+
11+
struct PartitionsBuilder {
12+
let partitionsFileURL: URL
13+
let partitionsSwiftFileURL: URL
14+
15+
// MARK: - init
16+
17+
init(repoPath: String) {
18+
let repoFileURL = URL(fileURLWithPath: repoPath)
19+
self.init(
20+
partitionsFileURL: repoFileURL.appendingPathComponent("codegen/sdk-codegen/sdk-partitions.json"),
21+
partitionsSwiftFileURL: repoFileURL.appendingPathComponent(
22+
"Sources/Core/AWSSDKDynamic/Sources/AWSSDKDynamic/Partitions.swift"
23+
)
24+
)
25+
}
26+
27+
init(
28+
partitionsFileURL: URL,
29+
partitionsSwiftFileURL: URL
30+
) {
31+
self.partitionsFileURL = partitionsFileURL
32+
self.partitionsSwiftFileURL = partitionsSwiftFileURL
33+
}
34+
35+
// MARK: - Code generation
36+
37+
func generatePartitionsFile() throws {
38+
let partitionsData = try Data(contentsOf: partitionsFileURL)
39+
guard let partitions = String(data: partitionsData, encoding: .utf8) else {
40+
throw Error("sdk-partitions.json does not contain UTF-8 data.")
41+
}
42+
_ = try JSONSerialization.jsonObject(with: partitionsData) // verifies partitions are valid JSON
43+
let partitionsSwiftFileContents = """
44+
//
45+
// Copyright Amazon.com Inc. or its affiliates.
46+
// All Rights Reserved.
47+
//
48+
// SPDX-License-Identifier: Apache-2.0
49+
//
50+
51+
// Code is auto-generated. DO NOT EDIT!
52+
53+
public let partitions = #\"\"\"
54+
\(partitions.trimmingCharacters(in: .whitespacesAndNewlines))
55+
\"\"\"#
56+
57+
"""
58+
try FileManager.default.createDirectory(
59+
at: partitionsSwiftFileURL.deletingLastPathComponent(),
60+
withIntermediateDirectories: true
61+
)
62+
try Data(partitionsSwiftFileContents.utf8).write(to: partitionsSwiftFileURL)
63+
}
64+
}

AWSSDKSwiftCLI/Sources/AWSSDKSwiftCLI/Resources/Package.Base.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extension Target.Dependency {
1212
static var AWSSDKIdentityAPI: Self { "AWSSDKIdentityAPI" }
1313
static var AWSSDKChecksums: Self { "AWSSDKChecksums" }
1414
static var AWSSDKPartitions: Self { "AWSSDKPartitions" }
15+
static var AWSSDKDynamic: Self { "AWSSDKDynamic" }
1516

1617
// CRT module
1718
static var CRT: Self { .product(name: "AwsCommonRuntimeKit", package: "aws-crt-swift") }
@@ -135,6 +136,7 @@ private var runtimeTargets: [Target] {
135136
.AWSSDKHTTPAuth,
136137
.AWSSDKChecksums,
137138
.AWSSDKPartitions,
139+
.AWSSDKDynamic,
138140
],
139141
path: "Sources/Core/AWSClientRuntime/Sources/AWSClientRuntime",
140142
resources: [
@@ -223,11 +225,15 @@ private var runtimeTargets: [Target] {
223225
.SmithyChecksums,
224226
.SmithyHTTPAPI,
225227
],
226-
path: "Sources/Core/AWSSDKChecksums/Sources"
228+
path: "Sources/Core/AWSSDKChecksums/Sources/AWSSDKChecksums"
227229
),
228230
.target(
229231
name: "AWSSDKPartitions",
230-
path: "Sources/Core/AWSSDKPartitions/Sources"
232+
path: "Sources/Core/AWSSDKPartitions/Sources/AWSSDKPartitions"
233+
),
234+
.target(
235+
name: "AWSSDKDynamic",
236+
path: "Sources/Core/AWSSDKDynamic/Sources/AWSSDKDynamic"
231237
),
232238
]
233239
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
@testable import AWSSDKSwiftCLI
10+
11+
class PackageVersionBuilderTests: XCTestCase {
12+
13+
func test_generatePackageVersion_suceedsWithExpectedContents() throws {
14+
let packageVersionFileURL = Bundle.module.url(forResource: "Package.version.next", withExtension: "test")!
15+
let packageVersionSwiftFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
16+
17+
try PackageVersionBuilder(
18+
packageVersionFileURL: packageVersionFileURL,
19+
packageVersionSwiftFileURL: packageVersionSwiftFileURL
20+
).generatePackageVersionFile()
21+
22+
let packageVersionSwiftContents = try XCTUnwrap(Data(contentsOf: packageVersionSwiftFileURL))
23+
let packageVersionSwift = try XCTUnwrap(String(data: packageVersionSwiftContents, encoding: .utf8))
24+
25+
let expected = """
26+
//
27+
// Copyright Amazon.com Inc. or its affiliates.
28+
// All Rights Reserved.
29+
//
30+
// SPDX-License-Identifier: Apache-2.0
31+
//
32+
33+
// Code is auto-generated. DO NOT EDIT!
34+
35+
public let packageVersion = "1.3.2"
36+
37+
"""
38+
39+
XCTAssertEqual(packageVersionSwift, expected)
40+
}
41+
42+
func test_generatePackageSwift_throwsOnInvalidPackageVersion() throws {
43+
let packageVersionFileURL = Bundle.module.url(forResource: "Package.version.next.invalid", withExtension: nil)!
44+
let packageVersionSwiftFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
45+
46+
XCTAssertThrowsError(
47+
try PackageVersionBuilder(
48+
packageVersionFileURL: packageVersionFileURL,
49+
packageVersionSwiftFileURL: packageVersionSwiftFileURL
50+
).generatePackageVersionFile()
51+
)
52+
}
53+
54+
func test_generateDefaultFileURLs() throws {
55+
let subject = PackageVersionBuilder(repoPath: "/path/to/sdk")
56+
57+
XCTAssertEqual(subject.packageVersionFileURL, URL(fileURLWithPath: "/path/to/sdk/Package.version.next"))
58+
XCTAssertEqual(subject.packageVersionSwiftFileURL,
59+
URL(fileURLWithPath: "/path/to/sdk/Sources/Core/AWSSDKDynamic/Sources/AWSSDKDynamic/PackageVersion.swift")
60+
)
61+
}
62+
}

0 commit comments

Comments
 (0)