From 211bf19e42e838d4546c02658ec071fb890535f0 Mon Sep 17 00:00:00 2001 From: Josh Elkins Date: Wed, 6 Nov 2024 14:37:51 -0600 Subject: [PATCH] fix: Add service client version to user-agent (#851) --- .../swift/codegen/PackageManifestGenerator.kt | 5 +- .../smithy/swift/codegen/SwiftDependency.kt | 232 +++--------------- .../integration/HttpProtocolServiceClient.kt | 1 + .../swiftmodules/SmithyTestUtilTypes.kt | 4 +- .../HttpProtocolClientGeneratorTests.kt | 1 + .../PackageManifestGeneratorTests.kt | 36 +-- 6 files changed, 60 insertions(+), 219 deletions(-) diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/PackageManifestGenerator.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/PackageManifestGenerator.kt index b022ea84d..390faa94e 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/PackageManifestGenerator.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/PackageManifestGenerator.kt @@ -52,12 +52,9 @@ class PackageManifestGenerator(val ctx: ProtocolGenerator.GenerationContext) { writer.openBlock("targets: [", "]") { writer.openBlock(".target(", "),") { writer.write("name: \$S,", ctx.settings.moduleName) - writer.openBlock("dependencies: [", "],") { + writer.openBlock("dependencies: [", "]") { dependenciesByTarget.forEach { writeTargetDependency(writer, it) } } - writer.openBlock("resources: [", "]") { - writer.write(".process(\"Resources\")") - } } writer.openBlock(".testTarget(", ")") { writer.write("name: \$S,", ctx.settings.testModuleName) diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftDependency.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftDependency.kt index 1c8a619cf..6e3e5f916 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftDependency.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftDependency.kt @@ -58,204 +58,40 @@ class SwiftDependency( "aws-crt-swift", DistributionMethod.GIT, ) - val CLIENT_RUNTIME = SwiftDependency( - "ClientRuntime", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY = SwiftDependency( - "Smithy", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_IDENTITY_API = SwiftDependency( - "SmithyIdentityAPI", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_IDENTITY = SwiftDependency( - "SmithyIdentity", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_RETRIES_API = SwiftDependency( - "SmithyRetriesAPI", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_RETRIES = SwiftDependency( - "SmithyRetries", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_HTTP_API = SwiftDependency( - "SmithyHTTPAPI", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_HTTP_AUTH_API = SwiftDependency( - "SmithyHTTPAuthAPI", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_HTTP_AUTH = SwiftDependency( - "SmithyHTTPAuth", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_CHECKSUMS_API = SwiftDependency( - "SmithyChecksumsAPI", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_CHECKSUMS = SwiftDependency( - "SmithyChecksums", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_STREAMS = SwiftDependency( - "SmithyStreams", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_EVENT_STREAMS_API = SwiftDependency( - "SmithyEventStreamsAPI", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_EVENT_STREAMS_AUTH_API = SwiftDependency( - "SmithyEventStreamsAuthAPI", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_EVENT_STREAMS = SwiftDependency( - "SmithyEventStreams", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_TEST_UTIL = SwiftDependency( - "SmithyTestUtil", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_READ_WRITE = SwiftDependency( - "SmithyReadWrite", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_TIMESTAMPS = SwiftDependency( - "SmithyTimestamps", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_XML = SwiftDependency( - "SmithyXML", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_JSON = SwiftDependency( - "SmithyJSON", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_FORM_URL = SwiftDependency( - "SmithyFormURL", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) - val SMITHY_WAITERS_API = SwiftDependency( - "SmithyWaitersAPI", - "main", - "0.0.1", - "aws-sdk-swift", - "../../../smithy-swift", - "smithy-swift", - DistributionMethod.SPR, - ) + val CLIENT_RUNTIME = smithySwiftDependency("ClientRuntime") + val SMITHY = smithySwiftDependency("Smithy") + val SMITHY_IDENTITY_API = smithySwiftDependency("SmithyIdentityAPI") + val SMITHY_IDENTITY = smithySwiftDependency("SmithyIdentity") + val SMITHY_RETRIES_API = smithySwiftDependency("SmithyRetriesAPI") + val SMITHY_RETRIES = smithySwiftDependency("SmithyRetries") + val SMITHY_HTTP_API = smithySwiftDependency("SmithyHTTPAPI") + val SMITHY_HTTP_AUTH_API = smithySwiftDependency("SmithyHTTPAuthAPI") + val SMITHY_HTTP_AUTH = smithySwiftDependency("SmithyHTTPAuth") + val SMITHY_CHECKSUMS_API = smithySwiftDependency("SmithyChecksumsAPI") + val SMITHY_CHECKSUMS = smithySwiftDependency("SmithyChecksums") + val SMITHY_STREAMS = smithySwiftDependency("SmithyStreams") + val SMITHY_EVENT_STREAMS_API = smithySwiftDependency("SmithyEventStreamsAPI") + val SMITHY_EVENT_STREAMS_AUTH_API = smithySwiftDependency("SmithyEventStreamsAuthAPI") + val SMITHY_EVENT_STREAMS = smithySwiftDependency("SmithyEventStreams") + val SMITHY_TEST_UTIL = smithySwiftDependency("SmithyTestUtil") + val SMITHY_READ_WRITE = smithySwiftDependency("SmithyReadWrite") + val SMITHY_TIMESTAMPS = smithySwiftDependency("SmithyTimestamps") + val SMITHY_XML = smithySwiftDependency("SmithyXML") + val SMITHY_JSON = smithySwiftDependency("SmithyJSON") + val SMITHY_FORM_URL = smithySwiftDependency("SmithyFormURL") + val SMITHY_WAITERS_API = smithySwiftDependency("SmithyWaitersAPI") + + fun smithySwiftDependency(name: String): SwiftDependency { + return SwiftDependency( + name, + "main", + "0.0.1", + "https://github.com/smithy-lang/smithy-swift", + "../../../smithy-swift", + "smithy-swift", + DistributionMethod.GIT, + ) + } } override fun getDependencies(): List { diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpProtocolServiceClient.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpProtocolServiceClient.kt index 3141bc2a5..d369620ec 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpProtocolServiceClient.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpProtocolServiceClient.kt @@ -30,6 +30,7 @@ open class HttpProtocolServiceClient( ClientRuntimeTypes.Core.Client, ) { writer.write("public static let clientName = \$S", serviceSymbol.name) + writer.write("public static let version = \$S", ctx.settings.moduleVersion) writer.write("let client: \$N", ClientRuntimeTypes.Http.SdkHttpClient) writer.write("let config: \$L", serviceConfig.typeName) writer.write("let serviceName = \$S", serviceName) diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/swiftmodules/SmithyTestUtilTypes.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/swiftmodules/SmithyTestUtilTypes.kt index dda1b7e9f..1f30bcbdf 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/swiftmodules/SmithyTestUtilTypes.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/swiftmodules/SmithyTestUtilTypes.kt @@ -5,8 +5,8 @@ import software.amazon.smithy.swift.codegen.SwiftDeclaration import software.amazon.smithy.swift.codegen.SwiftDependency object SmithyTestUtilTypes { - val TestInitializer = runtimeSymbol("TestInitializer") - val TestBaseError = runtimeSymbol("TestBaseError") + val TestInitializer = runtimeSymbol("TestInitializer", SwiftDeclaration.ENUM) + val TestBaseError = runtimeSymbol("TestBaseError", SwiftDeclaration.STRUCT) val dummyIdentityResolver = runtimeSymbol("dummyIdentityResolver", SwiftDeclaration.FUNC) } diff --git a/smithy-swift-codegen/src/test/kotlin/software/amazon/smithy/swift/codegen/HttpProtocolClientGeneratorTests.kt b/smithy-swift-codegen/src/test/kotlin/software/amazon/smithy/swift/codegen/HttpProtocolClientGeneratorTests.kt index 5a2972a58..fbfbbb498 100644 --- a/smithy-swift-codegen/src/test/kotlin/software/amazon/smithy/swift/codegen/HttpProtocolClientGeneratorTests.kt +++ b/smithy-swift-codegen/src/test/kotlin/software/amazon/smithy/swift/codegen/HttpProtocolClientGeneratorTests.kt @@ -19,6 +19,7 @@ class HttpProtocolClientGeneratorTests { val expected = """ public class RestJsonProtocolClient: ClientRuntime.Client { public static let clientName = "RestJsonProtocolClient" + public static let version = "2019-12-16" let client: ClientRuntime.SdkHttpClient let config: RestJsonProtocolClient.RestJsonProtocolClientConfiguration let serviceName = "Rest Json Protocol" diff --git a/smithy-swift-codegen/src/test/kotlin/software/amazon/smithy/swift/codegen/manifestanddocs/PackageManifestGeneratorTests.kt b/smithy-swift-codegen/src/test/kotlin/software/amazon/smithy/swift/codegen/manifestanddocs/PackageManifestGeneratorTests.kt index 935484b57..35729ba56 100644 --- a/smithy-swift-codegen/src/test/kotlin/software/amazon/smithy/swift/codegen/manifestanddocs/PackageManifestGeneratorTests.kt +++ b/smithy-swift-codegen/src/test/kotlin/software/amazon/smithy/swift/codegen/manifestanddocs/PackageManifestGeneratorTests.kt @@ -6,6 +6,7 @@ package software.amazon.smithy.swift.codegen.manifestanddocs */ import io.kotest.matchers.string.shouldContain +import io.kotest.matchers.string.shouldStartWith import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Test import software.amazon.smithy.swift.codegen.PackageManifestGenerator @@ -16,31 +17,39 @@ import software.amazon.smithy.swift.codegen.protocolgeneratormocks.MockHTTPAWSJs class PackageManifestGeneratorTests { private val testContext = setupTests("simple-service-with-operation-and-dependency.smithy", "smithy.example#Example") + @Test + fun `it starts with a swift-tools-version statement`() { + val packageManifest = testContext.manifest.getFileString("Package.swift.txt").get() + assertNotNull(packageManifest) + packageManifest.shouldStartWith("// swift-tools-version: 5.5.0") + } + @Test fun `it renders package manifest file with macOS and iOS platforms block`() { val packageManifest = testContext.manifest.getFileString("Package.swift.txt").get() assertNotNull(packageManifest) - packageManifest.shouldContain( - "platforms: [\n" + - " .macOS(.v10_15), .iOS(.v13)\n" + - " ]" - ) + val expected = """ + platforms: [ + .macOS(.v10_15), .iOS(.v13) + ], +""" + packageManifest.shouldContain(expected) } @Test fun `it renders package manifest file with single library in product block`() { val packageManifest = testContext.manifest.getFileString("Package.swift.txt").get() assertNotNull(packageManifest) - packageManifest.shouldContain( - "products: [\n" + - " .library(name: \"MockSDK\", targets: [\"MockSDK\"])\n" + - " ]" - ) + val expected = """ + products: [ + .library(name: "MockSDK", targets: ["MockSDK"]) + ], +""" + packageManifest.shouldContain(expected) } @Test fun `it renders package manifest file with target and test target`() { - println(testContext.manifest.files) val packageManifest = testContext.manifest.getFileString("Package.swift.txt").get() assertNotNull(packageManifest) val expected = """ @@ -48,9 +57,6 @@ class PackageManifestGeneratorTests { .target( name: "MockSDK", dependencies: [ - ], - resources: [ - .process("Resources") ] ), .testTarget( @@ -59,7 +65,7 @@ class PackageManifestGeneratorTests { "MockSDK", .product( name: "SmithyTestUtil", - package: "aws-sdk-swift.smithy-swift" + package: "smithy-swift" ), ] )