Skip to content

Commit 60dd23c

Browse files
authored
chore: refactor build to use custom smithy build plugin (#1168)
1 parent de5368a commit 60dd23c

File tree

16 files changed

+126
-623
lines changed

16 files changed

+126
-623
lines changed

aws-runtime/aws-config/build.gradle.kts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
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
7+
import aws.sdk.kotlin.gradle.codegen.smithyKotlinProjectionSrcDir
68
import org.jetbrains.dokka.gradle.DokkaTaskPartial
79

810
plugins {
9-
id("aws.sdk.kotlin.codegen")
11+
id("aws.sdk.kotlin.gradle.smithybuild")
1012
}
1113

1214
description = "Support for AWS configuration"
@@ -69,7 +71,14 @@ kotlin {
6971
fun awsModelFile(name: String): String =
7072
rootProject.file("codegen/sdk/aws-models/$name").relativeTo(project.layout.buildDirectory.get().asFile).toString()
7173

72-
codegen {
74+
val codegen by configurations.getting
75+
dependencies {
76+
codegen(project(":codegen:aws-sdk-codegen"))
77+
codegen(libs.smithy.cli)
78+
codegen(libs.smithy.model)
79+
}
80+
81+
smithyBuild {
7382
val basePackage = "aws.sdk.kotlin.runtime.auth.credentials.internal"
7483

7584
projections {
@@ -193,9 +202,8 @@ NOTE: We need the following tasks to depend on codegen for gradle caching/up-to-
193202
* For Kotlin/Native, an additional dependency is introduced:
194203
* `compileKotlin<Platform>` (Type=KotlinNativeCompile) (e.g. compileKotlinLinuxX64)
195204
*/
196-
val codegenTask = tasks.named("generateSmithyProjections")
197205
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
198-
dependsOn(codegenTask)
206+
dependsOn(tasks.generateSmithyProjections)
199207

200208
compilerOptions {
201209
// generated sts/sso credential providers have quite a few warnings
@@ -204,47 +212,37 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
204212
}
205213

206214
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile> {
207-
dependsOn(codegenTask)
215+
dependsOn(tasks.generateSmithyProjections)
208216
compilerOptions {
209217
allWarningsAsErrors.set(false)
210218
}
211219
}
212220

213221
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompileCommon> {
214-
dependsOn(codegenTask)
222+
dependsOn(tasks.generateSmithyProjections)
215223
}
216224

217225
tasks.withType<org.gradle.jvm.tasks.Jar> {
218226
if (name == "jar") {
219227
println("Disabling $project task '$name' because it conflicts with Kotlin JAR tasks")
220228
enabled = false
221229
} else {
222-
dependsOn(codegenTask)
230+
dependsOn(tasks.generateSmithyProjections)
223231
}
224232
}
225233

226-
codegen.projections.all {
234+
smithyBuild.projections.all {
227235
// add this projected source dir to the common sourceSet
228-
// NOTE - build.gradle.kts is still being generated, it's NOT used though
229-
// TODO - we should probably either have a postProcessing spec or a plugin setting to not generate it to avoid confusion
230-
val projectedSrcDir = projectionRootDir.resolve("src/main/kotlin")
236+
val projectionSrcDir = smithyBuild.smithyKotlinProjectionSrcDir(name)
231237
kotlin.sourceSets.commonMain {
232-
println("added $projectedSrcDir to common sourceSet")
233-
kotlin.srcDir(projectedSrcDir)
234-
}
235-
}
236-
237-
// Necessary to avoid Gradle problems identifying correct variant of aws-config. This stems from the smithy-gradle
238-
// plugin (used by codegen plugin) applying the Java plugin which creates these configurations.
239-
listOf("apiElements", "runtimeElements").forEach {
240-
configurations.named(it) {
241-
isCanBeConsumed = false
238+
logger.info("added $projectionSrcDir to common sourceSet")
239+
kotlin.srcDir(projectionSrcDir)
242240
}
243241
}
244242

245243
// suppress internal generated clients
246244
tasks.named<DokkaTaskPartial>("dokkaHtmlPartial") {
247-
dependsOn(codegenTask)
245+
dependsOn(tasks.generateSmithyProjections)
248246
dokkaSourceSets.configureEach {
249247
perPackageOption {
250248
matchingRegex.set(""".*\.internal.*""")

build.gradle.kts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ buildscript {
1414
classpath(libs.kotlinx.atomicfu.plugin)
1515
classpath("aws.sdk.kotlin:build-plugins") {
1616
version {
17-
require("0.3.1")
17+
require("0.3.2")
1818
}
1919
}
20+
21+
// FIXME - we need the ClassLoader used for Model and any traits to be the same. Unfortunately our
22+
// build plugin has a transitive dependency on `smithy-model` which means our :codegen:sdk project
23+
// that uses both in it's build logic won't work correctly. We "fix" this by placing them both
24+
// into the root buildscript classpath and force them to share a class loader.
25+
classpath(libs.smithy.model)
26+
classpath(libs.smithy.aws.traits)
2027
}
2128
}
2229

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)