Skip to content

Commit d68bb67

Browse files
authored
chore: refactor build to use custom smithy build plugin (#1168)
1 parent 21f4779 commit d68bb67

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

codegen/protocol-tests/build.gradle.kts

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
import aws.sdk.kotlin.gradle.codegen.dsl.generateSmithyProjections
56
import aws.sdk.kotlin.gradle.codegen.dsl.smithyKotlinPlugin
6-
import software.amazon.smithy.gradle.tasks.SmithyBuild
7+
import aws.sdk.kotlin.gradle.codegen.smithyKotlinProjectionPath
8+
import aws.sdk.kotlin.gradle.codegen.smithyKotlinProjectionSrcDir
79

810
plugins {
9-
id("aws.sdk.kotlin.codegen")
11+
kotlin("jvm") // FIXME - configuration doesn't resolve without this
12+
id("aws.sdk.kotlin.gradle.smithybuild")
1013
}
1114

1215
description = "Smithy protocol test suite"
1316

14-
dependencies {
15-
implementation(libs.smithy.aws.protocol.tests)
16-
}
17-
1817
data class ProtocolTest(val projectionName: String, val serviceShapeId: String, val sdkId: String? = null) {
19-
val packageName: String = projectionName.toLowerCase().filter { it.isLetterOrDigit() }
18+
val packageName: String = projectionName.lowercase().filter { it.isLetterOrDigit() }
2019
}
2120

2221
// The following section exposes Smithy protocol test suites as gradle test targets
@@ -40,9 +39,11 @@ val enabledProtocols = listOf(
4039
ProtocolTest("error-correction-xml", "aws.protocoltests.errorcorrection#RequiredValueXml"),
4140
)
4241

43-
codegen {
42+
smithyBuild {
4443
enabledProtocols.forEach { test ->
4544
projections.register(test.projectionName) {
45+
imports = listOf(file("model").absolutePath)
46+
4647
transforms = listOf(
4748
"""
4849
{
@@ -74,64 +75,79 @@ codegen {
7475
}
7576
}
7677

77-
tasks.named<SmithyBuild>("generateSmithyProjections") {
78+
val codegen by configurations.getting
79+
dependencies {
80+
codegen(project(":codegen:aws-sdk-codegen"))
81+
codegen(libs.smithy.cli)
82+
codegen(libs.smithy.model)
83+
7884
// NOTE: The protocol tests are published to maven as a jar, this ensures that
7985
// the aws-protocol-tests dependency is found when generating code such that the `includeServices` transform
8086
// actually works
81-
addCompileClasspath = true
87+
codegen(libs.smithy.aws.protocol.tests)
88+
}
8289

90+
tasks.generateSmithyProjections {
8391
// ensure the generated clients use the same version of the runtime as the aws aws-runtime
8492
val smithyKotlinRuntimeVersion = libs.versions.smithy.kotlin.runtime.version.get()
8593
doFirst {
8694
System.setProperty("smithy.kotlin.codegen.clientRuntimeVersion", smithyKotlinRuntimeVersion)
8795
}
8896
}
8997

90-
open class ProtocolTestTask : DefaultTask() {
98+
abstract class ProtocolTestTask @Inject constructor(private val project: Project) : DefaultTask() {
9199
/**
92100
* The projection
93101
*/
94102
@get:Input
95-
var projection: aws.sdk.kotlin.gradle.codegen.dsl.SmithyProjection? = null
103+
abstract val projectionName: Property<String>
104+
105+
/**
106+
* The projection root directory
107+
*/
108+
@get:Input
109+
abstract val projectionRootDirectory: Property<String>
96110

97111
@TaskAction
98112
fun runTests() {
99-
val projection = requireNotNull(projection) { "projection is required task input" }
100-
println("[${projection.name}] buildDir: ${projection.projectionRootDir}")
101-
if (!projection.projectionRootDir.exists()) {
102-
throw GradleException("${projection.projectionRootDir} does not exist")
113+
val projectionRootDir = project.file(projectionRootDirectory.get())
114+
println("[$projectionName] buildDir: $projectionRootDir")
115+
if (!projectionRootDir.exists()) {
116+
throw GradleException("$projectionRootDir does not exist")
103117
}
104-
val wrapper = if (System.getProperty("os.name").toLowerCase().contains("windows")) "gradlew.bat" else "gradlew"
118+
val wrapper = if (System.getProperty("os.name").lowercase().contains("windows")) "gradlew.bat" else "gradlew"
105119
val gradlew = project.rootProject.file(wrapper).absolutePath
106120

107121
// NOTE - this still requires us to publish to maven local.
108122
project.exec {
109-
workingDir = projection.projectionRootDir
123+
workingDir = projectionRootDir
110124
executable = gradlew
111125
args = listOf("test")
112126
}
113127
}
114128
}
115129

116-
val codegenTask = tasks.getByName("generateSmithyProjections")
117-
codegen.projections.forEach {
130+
smithyBuild.projections.forEach {
118131
val protocolName = it.name
119132

120133
tasks.register<ProtocolTestTask>("testProtocol-$protocolName") {
121-
dependsOn(codegenTask)
134+
dependsOn(tasks.generateSmithyProjections)
122135
group = "Verification"
123-
projection = it
136+
projectionName.set(it.name)
137+
projectionRootDirectory.set(smithyBuild.smithyKotlinProjectionPath(it.name).map { it.toString() })
124138
}
125139

126140
// FIXME This is a hack to work around how protocol tests aren't in the actual service model and thus codegen
127141
// separately from service customizations.
128142
val copyStaticFiles = tasks.register<Copy>("copyStaticFiles-$protocolName") {
129143
group = "codegen"
130144
from(rootProject.projectDir.resolve("services/$protocolName/common/src"))
131-
into(it.projectionRootDir.resolve("src/main/kotlin/"))
145+
into(smithyBuild.smithyKotlinProjectionSrcDir(it.name))
132146
}
133147

134-
codegenTask.finalizedBy(copyStaticFiles)
148+
tasks.generateSmithyProjections.configure {
149+
finalizedBy(copyStaticFiles)
150+
}
135151
}
136152

137153
tasks.register("testAllProtocols") {

0 commit comments

Comments
 (0)