Skip to content

Commit 3e0ff2c

Browse files
authored
feat: add Gradle codegen (#458)
1 parent 48cd3a9 commit 3e0ff2c

File tree

8 files changed

+93
-63
lines changed

8 files changed

+93
-63
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ package-lock.json
2525
build
2626

2727
codegen/build
28+
codegen/sdk-codegen/smithy-build.json
2829
.gradle

codegen/aws-typescript-models/README.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

codegen/aws-typescript-models/build.gradle.kts

Lines changed: 0 additions & 38 deletions
This file was deleted.

codegen/aws-typescript-models/smithy-build.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

codegen/sdk-codegen/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# AWS TypeScript models
2+
3+
This package is used to build each client in the SDK.
4+
5+
By running `./gradlew :sdk-codegen:build`, this package will:
6+
7+
1. Scan the `sdk-codegen` directory for JSON files. Each file is a standalone
8+
model that represents a service to generate.
9+
2. Generate a `smithy-build.json` file.
10+
11+
- For each model, a projection is created that uses the filename without
12+
".json" as the name of the projection.
13+
- An `imports` value is added that imports the file.
14+
- The `typescript-codegen` plugin is applied. The `package` value is set
15+
to `@aws-sdk/client-` + the first dot (.) segment of the filename
16+
converted to lowercase (this segment is also the AWS SDK service ID).
17+
3. Runs the `software.amazon.smithy.gradle.tasks.SmithyBuild` Gradle task
18+
to generate artifacts for each service model. Artifacts for each model
19+
are written to `codegen/sdk-codegen/build/smithyprojections/sdk-codegen/{filename}/typescript-codegen`
20+
where `{filename}` is the relative filename of a model without ".json".
21+
For example, `codegen/sdk-codegen/build/smithyprojections/sdk-codegen/RDS-Data.2018-08-01/typescript-codegen`
22+
would contain the code generated for `aws-models/RDS-Data.2018-08-01.json`.

codegen/sdk-codegen/build.gradle.kts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
import software.amazon.smithy.model.node.Node
17+
import software.amazon.smithy.gradle.tasks.SmithyBuild
18+
19+
plugins {
20+
id("software.amazon.smithy") version "0.4.1"
21+
}
22+
23+
dependencies {
24+
compile(project(":smithy-aws-typescript-codegen"))
25+
}
26+
27+
// This project doesn't produce a JAR.
28+
tasks["jar"].enabled = false
29+
30+
// Run the SmithyBuild task manually since this project needs the built JAR
31+
// from smithy-aws-typescript-codegen.
32+
tasks["smithyBuildJar"].enabled = false
33+
34+
tasks.create<SmithyBuild>("buildSdk") {
35+
addRuntimeClasspath = true
36+
}
37+
38+
// Generates a smithy-build.json file by creating a new projection for every
39+
// JSON file found in aws-models/. The generated smithy-build.json file is
40+
// not committed to git since it's rebuilt each time codegen is performed.
41+
tasks.register("generate-smithy-build") {
42+
doLast {
43+
val projectionsBuilder = Node.objectNodeBuilder()
44+
45+
fileTree("aws-models").filter { it.isFile }.files.forEach { file ->
46+
val (sdkId, version, remaining) = file.name.split(".")
47+
val projectionContents = Node.objectNodeBuilder()
48+
.withMember("imports", Node.fromStrings("aws-models/" + file.name))
49+
.withMember("plugins", Node.objectNode()
50+
.withMember("typescript-codegen", Node.objectNodeBuilder()
51+
.withMember("package", "@aws-sdk/client-" + sdkId.toLowerCase())
52+
// Note that this version is replaced by Lerna when publishing.
53+
.withMember("packageVersion", "1.0.0")
54+
.build()))
55+
.build()
56+
projectionsBuilder.withMember(sdkId + "." + version.toLowerCase(), projectionContents)
57+
}
58+
59+
file("smithy-build.json").writeText(Node.prettyPrintJson(Node.objectNodeBuilder()
60+
.withMember("version", "1.0")
61+
.withMember("projections", projectionsBuilder.build())
62+
.build()))
63+
}
64+
}
65+
66+
// Run the `buildSdk` automatically.
67+
tasks["build"]
68+
.dependsOn(tasks["generate-smithy-build"])
69+
.finalizedBy(tasks["buildSdk"])

codegen/settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
rootProject.name = "codegen"
1717
include(":smithy-aws-typescript-codegen")
18-
include(":aws-typescript-models")
18+
include(":sdk-codegen")
1919

2020
// TODO: remove once smithy-gradle-plugin:0.4.0 is published.
2121
pluginManagement {

0 commit comments

Comments
 (0)