From cfe37701b7af2aac6368820cd1a5c7516deb509b Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Fri, 28 Jun 2024 14:34:01 -0700 Subject: [PATCH 1/3] Add copyrightNotice to SwiftSettings & make it available to SwiftWriter. --- .../smithy/swift/codegen/SwiftDelegator.kt | 6 +++++- .../smithy/swift/codegen/SwiftSettings.kt | 10 +++++++++- .../amazon/smithy/swift/codegen/SwiftWriter.kt | 17 +++++++++++------ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftDelegator.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftDelegator.kt index 6480fb494..706d9c33b 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftDelegator.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftDelegator.kt @@ -27,7 +27,11 @@ class SwiftDelegator( private val fileManifest: FileManifest, private val symbolProvider: SymbolProvider, private val integrations: List = listOf() -) : WriterDelegator(fileManifest, symbolProvider, SwiftWriter.SwiftWriterFactory(integrations)) { +) : WriterDelegator( + fileManifest, + symbolProvider, + SwiftWriter.SwiftWriterFactory(integrations, settings) +) { /** * Gets a previously created writer or creates a new one if needed. diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftSettings.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftSettings.kt index abc85c766..4a8979d4e 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftSettings.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftSettings.kt @@ -28,6 +28,7 @@ private const val GIT_REPO = "gitRepo" private const val SWIFT_VERSION = "swiftVersion" private const val USE_INTERCEPTORS = "useInterceptors" private const val MERGE_MODELS = "mergeModels" +private const val COPYRIGHT_NOTICE = "copyrightNotice" class SwiftSettings( val service: ShapeId, @@ -41,6 +42,7 @@ class SwiftSettings( val swiftVersion: String, var useInterceptors: Boolean, val mergeModels: Boolean, + val copyrightNotice: String ) { companion object { @@ -69,6 +71,7 @@ class SwiftSettings( SWIFT_VERSION, USE_INTERCEPTORS, MERGE_MODELS, + COPYRIGHT_NOTICE ) ) @@ -86,6 +89,10 @@ class SwiftSettings( val sdkId = sanitizeSdkId(config.getStringMemberOrDefault(SDK_ID, serviceId.name)) val useInterceptors = config.getBooleanMemberOrDefault(USE_INTERCEPTORS) val mergeModels = config.getBooleanMemberOrDefault(MERGE_MODELS) + val copyrightNotice = config.getStringMemberOrDefault( + COPYRIGHT_NOTICE, + "// Code generated by smithy-swift-codegen. DO NOT EDIT!\n\n" + ) return SwiftSettings( serviceId, @@ -98,7 +105,8 @@ class SwiftSettings( gitRepo, swiftVersion, useInterceptors, - mergeModels + mergeModels, + copyrightNotice ) } diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftWriter.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftWriter.kt index ec923b9b5..8e9cf2c8d 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftWriter.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftWriter.kt @@ -65,15 +65,20 @@ fun SwiftWriter.customizeSection(id: SectionId, writer: SectionWriter): SwiftWri return this } -class SwiftWriter(private val fullPackageName: String, swiftImportContainer: SwiftImportContainer = SwiftImportContainer()) : - SymbolWriter(swiftImportContainer) { +class SwiftWriter( + private val fullPackageName: String, + private val copyrightNotice: String = "// Code generated by smithy-swift-codegen. DO NOT EDIT!\n\n", + swiftImportContainer: SwiftImportContainer = SwiftImportContainer() +) : SymbolWriter(swiftImportContainer) { companion object { - const val GENERATED_FILE_HEADER: String = "// Code generated by smithy-swift-codegen. DO NOT EDIT!\n\n" const val SWIFT_FILE_EXTENSION: String = ".swift" } - class SwiftWriterFactory(private val integrations: List = listOf()) : Factory { + class SwiftWriterFactory( + private val integrations: List = listOf(), + private val settings: SwiftSettings + ) : Factory { override fun apply(filename: String, namespace: String?): SwiftWriter { val moduleName = if (filename.endsWith(SWIFT_FILE_EXTENSION)) { @@ -82,7 +87,7 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi filename } - val swiftWriter = SwiftWriter(moduleName) + val swiftWriter = SwiftWriter(moduleName, settings.copyrightNotice) integrations.forEach { integration -> integration.sectionWriters.forEach { (sectionId, sectionWriter) -> @@ -149,7 +154,7 @@ class SwiftWriter(private val fullPackageName: String, swiftImportContainer: Swi // Package.swift requires a special comment at the top to specify Swift tools version, // and the package manifest generator manually writes its own dependency imports // (it only imports the PackageDescription module.) - return contents.takeIf { fullPackageName == "Package" } ?: (GENERATED_FILE_HEADER + imports + contents) + return contents.takeIf { fullPackageName == "Package" } ?: (copyrightNotice + imports + contents) } private class SwiftSymbolFormatter( From f444349ad37699ee0aef3c9d9af42fef4b131250 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Fri, 28 Jun 2024 14:34:22 -0700 Subject: [PATCH 2/3] Update codegen tests to source copyright notice from new place. --- smithy-swift-codegen/src/test/kotlin/EnumGeneratorTests.kt | 4 ++-- .../src/test/kotlin/StructureGeneratorTests.kt | 4 ++-- smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt | 4 ++-- smithy-swift-codegen/src/test/kotlin/UnionGeneratorTests.kt | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/smithy-swift-codegen/src/test/kotlin/EnumGeneratorTests.kt b/smithy-swift-codegen/src/test/kotlin/EnumGeneratorTests.kt index 7417edfa8..937e891d0 100644 --- a/smithy-swift-codegen/src/test/kotlin/EnumGeneratorTests.kt +++ b/smithy-swift-codegen/src/test/kotlin/EnumGeneratorTests.kt @@ -35,7 +35,7 @@ class EnumGeneratorTests { val contents = writer.toString() - contents.shouldContain(SwiftWriter.GENERATED_FILE_HEADER) + contents.shouldContain(settings.copyrightNotice) val expectedGeneratedEnum = """ /// Really long multi-line Documentation for the enum @@ -94,7 +94,7 @@ public enum MyEnum: Swift.Equatable, Swift.RawRepresentable, Swift.CaseIterable, val contents = writer.toString() - contents.shouldContain(SwiftWriter.GENERATED_FILE_HEADER) + contents.shouldContain(settings.copyrightNotice) val expectedGeneratedEnum = """ /// Really long multi-line Documentation for the enum diff --git a/smithy-swift-codegen/src/test/kotlin/StructureGeneratorTests.kt b/smithy-swift-codegen/src/test/kotlin/StructureGeneratorTests.kt index c4a06dda2..2c6620dd4 100644 --- a/smithy-swift-codegen/src/test/kotlin/StructureGeneratorTests.kt +++ b/smithy-swift-codegen/src/test/kotlin/StructureGeneratorTests.kt @@ -31,7 +31,7 @@ class StructureGeneratorTests { val contents = writer.toString() - contents.shouldContain(SwiftWriter.GENERATED_FILE_HEADER) + contents.shouldContain(swiftSettings.copyrightNotice) val expectedGeneratedStructure = """ /// This is documentation about the shape. @@ -253,7 +253,7 @@ public struct RecursiveShapesInputOutputLists { val contents = writer.toString() - contents.shouldContain(SwiftWriter.GENERATED_FILE_HEADER) + contents.shouldContain(swiftSettings.copyrightNotice) val expectedGeneratedStructure = """ public struct MyError: ClientRuntime.ModeledError, ClientRuntime.ServiceError, ClientRuntime.HTTPError, Swift.Error { diff --git a/smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt b/smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt index 13093dca5..cde01b84d 100644 --- a/smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt +++ b/smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt @@ -43,7 +43,7 @@ class SwiftDelegatorTests { delegator.useShapeWriter(getFooInputShape, { writer -> writer.write("Hello!") }) delegator.flushWriters() assertEquals( - SwiftWriter.GENERATED_FILE_HEADER + "\n\nHello!\n", + settings.copyrightNotice + "\n\nHello!\n", manifest.getFileString("Sources/example/models/GetFooInput.swift").get() ) } @@ -62,7 +62,7 @@ class SwiftDelegatorTests { delegator.useShapeWriter(getFooInputShape, { writer -> writer.write("Goodbye!") }) delegator.flushWriters() assertEquals( - SwiftWriter.GENERATED_FILE_HEADER + "\n\nHello!\n\nGoodbye!\n", + settings.copyrightNotice + "\n\nHello!\n\nGoodbye!\n", manifest.getFileString("Sources/example/models/GetFooInput.swift").get() ) } diff --git a/smithy-swift-codegen/src/test/kotlin/UnionGeneratorTests.kt b/smithy-swift-codegen/src/test/kotlin/UnionGeneratorTests.kt index bfd341b81..9e9585d5d 100644 --- a/smithy-swift-codegen/src/test/kotlin/UnionGeneratorTests.kt +++ b/smithy-swift-codegen/src/test/kotlin/UnionGeneratorTests.kt @@ -39,7 +39,7 @@ class UnionGeneratorTests { val contents = writer.toString() - contents.shouldContain(SwiftWriter.GENERATED_FILE_HEADER) + contents.shouldContain(settings.copyrightNotice) val expectedGeneratedEnum = """ @@ -83,7 +83,7 @@ class UnionGeneratorTests { val contents = writer.toString() - contents.shouldContain(SwiftWriter.GENERATED_FILE_HEADER) + contents.shouldContain(settings.copyrightNotice) val expectedGeneratedEnum = """ @@ -123,7 +123,7 @@ class UnionGeneratorTests { val contents = writer.toString() - contents.shouldContain(SwiftWriter.GENERATED_FILE_HEADER) + contents.shouldContain(settings.copyrightNotice) val expectedGeneratedEnum = """ From 75985b2a8ebbe294690efaeb27b047b5a3734f34 Mon Sep 17 00:00:00 2001 From: Sichan Yoo Date: Fri, 28 Jun 2024 14:41:16 -0700 Subject: [PATCH 3/3] ktlin --- smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt b/smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt index cde01b84d..30735f5df 100644 --- a/smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt +++ b/smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt @@ -12,7 +12,6 @@ import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.swift.codegen.SwiftCodegenPlugin import software.amazon.smithy.swift.codegen.SwiftDelegator import software.amazon.smithy.swift.codegen.SwiftSettings -import software.amazon.smithy.swift.codegen.SwiftWriter class SwiftDelegatorTests { @Test