Skip to content

feat: add Gradle codegen #458

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

Merged
merged 1 commit into from
Nov 20, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ package-lock.json
build

codegen/build
codegen/sdk-codegen/smithy-build.json
.gradle
14 changes: 0 additions & 14 deletions codegen/aws-typescript-models/README.md

This file was deleted.

38 changes: 0 additions & 38 deletions codegen/aws-typescript-models/build.gradle.kts

This file was deleted.

10 changes: 0 additions & 10 deletions codegen/aws-typescript-models/smithy-build.json

This file was deleted.

22 changes: 22 additions & 0 deletions codegen/sdk-codegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# AWS TypeScript models

This package is used to build each client in the SDK.

By running `./gradlew :sdk-codegen:build`, this package will:

1. Scan the `sdk-codegen` directory for JSON files. Each file is a standalone
model that represents a service to generate.
2. Generate a `smithy-build.json` file.

- For each model, a projection is created that uses the filename without
".json" as the name of the projection.
- An `imports` value is added that imports the file.
- The `typescript-codegen` plugin is applied. The `package` value is set
to `@aws-sdk/client-` + the first dot (.) segment of the filename
converted to lowercase (this segment is also the AWS SDK service ID).
3. Runs the `software.amazon.smithy.gradle.tasks.SmithyBuild` Gradle task
to generate artifacts for each service model. Artifacts for each model
are written to `codegen/sdk-codegen/build/smithyprojections/sdk-codegen/{filename}/typescript-codegen`
where `{filename}` is the relative filename of a model without ".json".
For example, `codegen/sdk-codegen/build/smithyprojections/sdk-codegen/RDS-Data.2018-08-01/typescript-codegen`
would contain the code generated for `aws-models/RDS-Data.2018-08-01.json`.
69 changes: 69 additions & 0 deletions codegen/sdk-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import software.amazon.smithy.model.node.Node
import software.amazon.smithy.gradle.tasks.SmithyBuild

plugins {
id("software.amazon.smithy") version "0.4.1"
}

dependencies {
compile(project(":smithy-aws-typescript-codegen"))
}

// This project doesn't produce a JAR.
tasks["jar"].enabled = false

// Run the SmithyBuild task manually since this project needs the built JAR
// from smithy-aws-typescript-codegen.
tasks["smithyBuildJar"].enabled = false

tasks.create<SmithyBuild>("buildSdk") {
addRuntimeClasspath = true
}

// Generates a smithy-build.json file by creating a new projection for every
// JSON file found in aws-models/. The generated smithy-build.json file is
// not committed to git since it's rebuilt each time codegen is performed.
tasks.register("generate-smithy-build") {
doLast {
val projectionsBuilder = Node.objectNodeBuilder()

fileTree("aws-models").filter { it.isFile }.files.forEach { file ->
val (sdkId, version, remaining) = file.name.split(".")
val projectionContents = Node.objectNodeBuilder()
.withMember("imports", Node.fromStrings("aws-models/" + file.name))
.withMember("plugins", Node.objectNode()
.withMember("typescript-codegen", Node.objectNodeBuilder()
.withMember("package", "@aws-sdk/client-" + sdkId.toLowerCase())
// Note that this version is replaced by Lerna when publishing.
.withMember("packageVersion", "1.0.0")
.build()))
.build()
projectionsBuilder.withMember(sdkId + "." + version.toLowerCase(), projectionContents)
}

file("smithy-build.json").writeText(Node.prettyPrintJson(Node.objectNodeBuilder()
.withMember("version", "1.0")
.withMember("projections", projectionsBuilder.build())
.build()))
}
}

// Run the `buildSdk` automatically.
tasks["build"]
.dependsOn(tasks["generate-smithy-build"])
.finalizedBy(tasks["buildSdk"])
2 changes: 1 addition & 1 deletion codegen/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

rootProject.name = "codegen"
include(":smithy-aws-typescript-codegen")
include(":aws-typescript-models")
include(":sdk-codegen")

// TODO: remove once smithy-gradle-plugin:0.4.0 is published.
pluginManagement {
Expand Down