Skip to content

Commit

Permalink
chore: generate client name according to sdkId (#2536)
Browse files Browse the repository at this point in the history
* chore: generate client name according sdkid not model file name

* chore: add option to disable protocol tests when generating clients

* chore: new client generated at v3.0.0
  • Loading branch information
AllanZhengYP authored Jul 13, 2021
1 parent 401c054 commit f239221
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
32 changes: 29 additions & 3 deletions codegen/sdk-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@
* permissions and limitations under the License.
*/

import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.gradle.tasks.SmithyBuild
import software.amazon.smithy.aws.traits.ServiceTrait
import kotlin.streams.toList

buildscript {
dependencies {
"classpath"("software.amazon.smithy:smithy-aws-traits:[1.5.1,2.0.0[")
}
}

plugins {
id("software.amazon.smithy") version "0.5.3"
Expand Down Expand Up @@ -44,8 +54,24 @@ tasks.register("generate-smithy-build") {
val modelsDirProp: String by project
val models = project.file(modelsDirProp);

fileTree(models).filter { it.isFile }.files.forEach { file ->
val (sdkId, version, remaining) = file.name.split(".")
fileTree(models).filter { it.isFile }.files.forEach eachFile@{ file ->
val model = Model.assembler()
.addImport(file.absolutePath)
.assemble().result.get();
val services = model.shapes(ServiceShape::class.javaObjectType).sorted().toList();
if (services.size != 1) {
throw Exception("There must be exactly one service in each aws model file, but found " +
"${services.size} in ${file.name}: ${services.map { it.id }}");
}
val service = services[0]

val serviceTrait = service.getTrait(ServiceTrait::class.javaObjectType).get();

val sdkId = serviceTrait.sdkId
.replace(" ", "-")
.toLowerCase();
val version = service.version.toLowerCase();

val clientName = sdkId.split("-").toTypedArray()
.map { it.capitalize() }
.joinToString(separator = " ")
Expand All @@ -59,7 +85,7 @@ tasks.register("generate-smithy-build") {
.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-rc.1")
.withMember("packageVersion", "3.0.0")
.withMember("packageJson", manifestOverwrites)
.withMember("packageDescription", "AWS SDK for JavaScript "
+ clientName + " Client for Node.js, Browser and React Native")
Expand Down
18 changes: 13 additions & 5 deletions scripts/generate-clients/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const { prettifyCode } = require("./code-prettify");
const SDK_CLIENTS_DIR = path.normalize(path.join(__dirname, "..", "..", "clients"));
const PROTOCOL_TESTS_CLIENTS_DIR = path.normalize(path.join(__dirname, "..", "..", "protocol_tests"));

const { models, globs, output: clientsDir } = yargs
const {
models,
globs,
output: clientsDir,
noProtocolTest,
} = yargs
.alias("m", "models")
.string("m")
.describe("m", "The path to directory with models.")
Expand All @@ -26,21 +31,24 @@ const { models, globs, output: clientsDir } = yargs
.string("o")
.describe("o", "The output directory for built clients")
.default("o", SDK_CLIENTS_DIR)
.alias("n", "noProtocolTest")
.boolean("n")
.describe("n", "Disable generating protocol test files")
.help().argv;

(async () => {
try {
await generateClients(models || globs);
await generateProtocolTests();
if (!noProtocolTest) await generateProtocolTests();

await prettifyCode(CODE_GEN_SDK_OUTPUT_DIR);
await prettifyCode(CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR);
if (!noProtocolTest) await prettifyCode(CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR);

await copyToClients(CODE_GEN_SDK_OUTPUT_DIR, clientsDir);
await copyToClients(CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR, PROTOCOL_TESTS_CLIENTS_DIR);
if (!noProtocolTest) await copyToClients(CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR, PROTOCOL_TESTS_CLIENTS_DIR);

emptyDirSync(CODE_GEN_SDK_OUTPUT_DIR);
emptyDirSync(CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR);
if (!noProtocolTest) emptyDirSync(CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR);
emptyDirSync(TEMP_CODE_GEN_INPUT_DIR);

rmdirSync(TEMP_CODE_GEN_INPUT_DIR);
Expand Down

0 comments on commit f239221

Please sign in to comment.