Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add copyright notice extension point #765

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class SwiftDelegator(
private val fileManifest: FileManifest,
private val symbolProvider: SymbolProvider,
private val integrations: List<SwiftIntegration> = listOf()
) : WriterDelegator<SwiftWriter>(fileManifest, symbolProvider, SwiftWriter.SwiftWriterFactory(integrations)) {
) : WriterDelegator<SwiftWriter>(
fileManifest,
symbolProvider,
SwiftWriter.SwiftWriterFactory(integrations, settings)
) {

/**
* Gets a previously created writer or creates a new one if needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -41,6 +42,7 @@ class SwiftSettings(
val swiftVersion: String,
var useInterceptors: Boolean,
val mergeModels: Boolean,
val copyrightNotice: String
) {

companion object {
Expand Down Expand Up @@ -69,6 +71,7 @@ class SwiftSettings(
SWIFT_VERSION,
USE_INTERCEPTORS,
MERGE_MODELS,
COPYRIGHT_NOTICE
)
)

Expand All @@ -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,
Expand All @@ -98,7 +105,8 @@ class SwiftSettings(
gitRepo,
swiftVersion,
useInterceptors,
mergeModels
mergeModels,
copyrightNotice
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ fun SwiftWriter.customizeSection(id: SectionId, writer: SectionWriter): SwiftWri
return this
}

class SwiftWriter(private val fullPackageName: String, swiftImportContainer: SwiftImportContainer = SwiftImportContainer()) :
SymbolWriter<SwiftWriter, SwiftImportContainer>(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<SwiftWriter, SwiftImportContainer>(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<SwiftIntegration> = listOf()) : Factory<SwiftWriter> {
class SwiftWriterFactory(
private val integrations: List<SwiftIntegration> = listOf(),
private val settings: SwiftSettings
) : Factory<SwiftWriter> {
override fun apply(filename: String, namespace: String?): SwiftWriter {

val moduleName = if (filename.endsWith(SWIFT_FILE_EXTENSION)) {
Expand All @@ -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) ->
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions smithy-swift-codegen/src/test/kotlin/EnumGeneratorTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {

Expand Down
5 changes: 2 additions & 3 deletions smithy-swift-codegen/src/test/kotlin/SwiftDelegatorTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,7 +42,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()
)
}
Expand All @@ -62,7 +61,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()
)
}
Expand Down
6 changes: 3 additions & 3 deletions smithy-swift-codegen/src/test/kotlin/UnionGeneratorTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class UnionGeneratorTests {

val contents = writer.toString()

contents.shouldContain(SwiftWriter.GENERATED_FILE_HEADER)
contents.shouldContain(settings.copyrightNotice)

val expectedGeneratedEnum =
"""
Expand Down Expand Up @@ -83,7 +83,7 @@ class UnionGeneratorTests {

val contents = writer.toString()

contents.shouldContain(SwiftWriter.GENERATED_FILE_HEADER)
contents.shouldContain(settings.copyrightNotice)

val expectedGeneratedEnum =
"""
Expand Down Expand Up @@ -123,7 +123,7 @@ class UnionGeneratorTests {

val contents = writer.toString()

contents.shouldContain(SwiftWriter.GENERATED_FILE_HEADER)
contents.shouldContain(settings.copyrightNotice)

val expectedGeneratedEnum =
"""
Expand Down
Loading