Skip to content

Commit 5ee70f4

Browse files
committed
fix: move static files generation to codegen
1 parent 20b41ae commit 5ee70f4

File tree

14 files changed

+106
-170
lines changed

14 files changed

+106
-170
lines changed

codegen/sdk-codegen/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,18 @@ tasks.register("generate-smithy-build") {
4444

4545
fileTree("aws-models").filter { it.isFile }.files.forEach { file ->
4646
val (sdkId, version, remaining) = file.name.split(".")
47+
var manifestOverwrites = Node.parse(
48+
File("smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/package.json.template")
49+
.readText()
50+
).expectObjectNode()
4751
val projectionContents = Node.objectNodeBuilder()
4852
.withMember("imports", Node.fromStrings("aws-models/" + file.name))
4953
.withMember("plugins", Node.objectNode()
5054
.withMember("typescript-codegen", Node.objectNodeBuilder()
5155
.withMember("package", "@aws-sdk/client-" + sdkId.toLowerCase())
5256
// Note that this version is replaced by Lerna when publishing.
5357
.withMember("packageVersion", "1.0.0")
58+
.withMember("packageJson", manifestOverwrites)
5459
.build()))
5560
.build()
5661
projectionsBuilder.withMember(sdkId + "." + version.toLowerCase(), projectionContents)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
package software.amazon.smithy.aws.typescript.codegen;
17+
18+
import java.util.Calendar;
19+
import java.util.function.BiConsumer;
20+
import java.util.function.Consumer;
21+
import software.amazon.smithy.codegen.core.SymbolProvider;
22+
import software.amazon.smithy.model.Model;
23+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
24+
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
25+
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
26+
import software.amazon.smithy.utils.IoUtils;
27+
28+
29+
public class AwsPackageFixturesGeneratorIntegration implements TypeScriptIntegration {
30+
@Override
31+
public void writeAdditionalFiles(
32+
TypeScriptSettings settings,
33+
Model model,
34+
SymbolProvider symbolProvider,
35+
BiConsumer<String, Consumer<TypeScriptWriter>> writerFactory
36+
) {
37+
writerFactory.accept(".gitignore", writer -> {
38+
String resource = IoUtils.readUtf8Resource(
39+
getClass().getClassLoader(), "software/amazon/smithy/aws/typescript/codegen/gitignore");
40+
writer.write(resource);
41+
});
42+
writerFactory.accept(".npmignore", writer -> {
43+
String resource = IoUtils.readUtf8Resource(
44+
getClass().getClassLoader(), "software/amazon/smithy/aws/typescript/codegen/npmignore");
45+
writer.write(resource);
46+
});
47+
writerFactory.accept("LICENCE", writer -> {
48+
String resource = IoUtils.readUtf8Resource(
49+
getClass().getClassLoader(), "software/amazon/smithy/aws/typescript/codegen/LICENCE.template");
50+
resource = resource.replace("${year}", Integer.toString(Calendar.getInstance().get(Calendar.YEAR)));
51+
writer.write(resource);
52+
});
53+
writerFactory.accept("README.md", writer -> {
54+
String resource = IoUtils.readUtf8Resource(
55+
getClass().getClassLoader(), "software/amazon/smithy/aws/typescript/codegen/README.md.template");
56+
resource = resource.replaceAll("\\$\\{packageName\\}", settings.getPackageName());
57+
writer.write(resource);
58+
});
59+
}
60+
}

codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ software.amazon.smithy.aws.typescript.codegen.AddAwsRuntimeConfig
22
software.amazon.smithy.aws.typescript.codegen.AddBuiltinPlugins
33
software.amazon.smithy.aws.typescript.codegen.AddProtocols
44
software.amazon.smithy.aws.typescript.codegen.AwsServiceIdIntegration
5+
software.amazon.smithy.aws.typescript.codegen.AwsPackageFixturesGeneratorIntegration
56
software.amazon.smithy.aws.typescript.codegen.AddMd5HashDependency
67
software.amazon.smithy.aws.typescript.codegen.AddStreamHasherDependency

scripts/generate-clients/templates/LICENSE.sample renamed to codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/LICENCE.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright {{year}} Amazon.com, Inc. or its affiliates. All Rights Reserved.
189+
Copyright ${year} Amazon.com, Inc. or its affiliates. All Rights Reserved.
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
${packageName}
2+
3+
[![NPM version](https://img.shields.io/npm/v/${packageName}/preview.svg)](https://www.npmjs.com/package/${packageName})
4+
[![NPM downloads](https://img.shields.io/npm/dm/${packageName}.svg)](https://www.npmjs.com/package/${packageName})
5+
6+
For SDK usage, please step to [SDK reademe](https://github.com/aws/aws-sdk-js-v3).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"author": {
3+
"name": "AWS SDK for JavaScript Team",
4+
"url": "https://aws.amazon.com/javascript/"
5+
},
6+
"license": "Apache-2.0"
7+
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"cucumber": "0.5.x",
4242
"fs-extra": "^8.1.0",
4343
"generate-changelog": "^1.7.1",
44-
"handlebars": "^4.5.3",
4544
"husky": "^3.0.0",
4645
"jest": "^24.7.1",
4746
"jmespath": "^0.15.0",
Lines changed: 26 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
const { join, normalize } = require("path");
22
const { copySync, ensureDirSync } = require("fs-extra");
3-
const handlebars = require("handlebars");
4-
const {
5-
readdirSync,
6-
lstatSync,
7-
readFileSync,
8-
existsSync,
9-
writeFileSync
10-
} = require("fs");
3+
const { readdirSync, lstatSync, readFileSync, existsSync } = require("fs");
114

125
const CODE_GEN_OUTPUT_DIR = normalize(
136
join(
@@ -22,18 +15,27 @@ const CODE_GEN_OUTPUT_DIR = normalize(
2215
)
2316
);
2417

25-
/**
26-
* templates are a black list of files we don't want codegen artifact
27-
* to override in the clients folder
28-
*/
29-
const templates = readdirSync(join(__dirname, "templates"))
30-
.filter(name => /.sample$/.test(name))
31-
.reduce((accumulator, curr) => {
32-
const templatePath = join(__dirname, "templates", curr);
33-
const template = readFileSync(templatePath).toString();
34-
accumulator[curr.replace(/.sample$/, "")] = template;
35-
return accumulator;
36-
}, {});
18+
const getOverwritablePredicate = packageName => pathName => {
19+
const overwritablePathnames = [
20+
"commands",
21+
"lib",
22+
"models",
23+
"protocols",
24+
"LICENCE",
25+
"runtimeConfig.ts",
26+
"runtimeConfig.browser.ts",
27+
"runtimeConfig.shared.ts",
28+
"index.ts"
29+
];
30+
return (
31+
pathName.toLowerCase().indexOf(
32+
packageName
33+
.toLowerCase()
34+
.replace("@aws-sdk/client-", "")
35+
.replace("-", "")
36+
) >= 0 || overwritablePathnames.indexOf(pathName) >= 0
37+
);
38+
};
3739

3840
async function copyToClients(clientsDir) {
3941
for (const modelName of readdirSync(CODE_GEN_OUTPUT_DIR)) {
@@ -55,68 +57,17 @@ async function copyToClients(clientsDir) {
5557
const packageName = packageManifest.name;
5658
console.log(`copying ${packageName} from ${artifactPath} to ${clientsDir}`);
5759
const destPath = join(clientsDir, packageName.replace("@aws-sdk/", ""));
58-
59-
//Data used to generate files from template
60-
const templateData = {
61-
year: new Date().getFullYear(),
62-
packageName
63-
};
64-
65-
for (const packageSub of [
66-
...readdirSync(artifactPath),
67-
...Object.keys(templates)
68-
]) {
60+
const overwritablePredicate = getOverwritablePredicate(packageName);
61+
for (const packageSub of readdirSync(artifactPath)) {
6962
const packageSubPath = join(artifactPath, packageSub);
7063
const destSubPath = join(destPath, packageSub);
71-
72-
if (Object.keys(templates).indexOf(packageSub) >= 0) {
73-
if (packageSub === "package.json") {
74-
/**
75-
* Copy package.json content in detail.
76-
* Basically merge the generated package.json and dest package.json
77-
* but prefer the values from dest when they contain the same key
78-
* */
79-
const destManifest = JSON.parse(
80-
existsSync(destSubPath)
81-
? readFileSync(destSubPath).toString()
82-
: handlebars.compile(templates[packageSub])(templateData)
83-
);
84-
const updatedManifest = mergeManifest(packageManifest, destManifest);
85-
writeFileSync(destSubPath, JSON.stringify(updatedManifest, null, 2));
86-
} else if (!existsSync(destSubPath)) {
87-
//for files not yet exists and we have a template for it; generate from template
88-
const file = handlebars.compile(templates[packageSub])(templateData);
89-
writeFileSync(destSubPath, file);
90-
} else {
91-
//for files we have template but we already have a new version in clients folder, always prefer current one
92-
//PASS
93-
}
94-
} else {
95-
//For things not in codegen artifact black list, overwrite the existing ones.
64+
if (overwritablePredicate(packageSub) || !existsSync(destSubPath)) {
65+
//Overwrite the directories and files that are overwritable, or not yet exists
9666
if (lstatSync(packageSubPath).isDirectory()) ensureDirSync(destSubPath);
9767
copySync(packageSubPath, destSubPath, { overwrite: true });
9868
}
9969
}
10070
}
10171
}
10272

103-
const mergeManifest = (source, dest) => {
104-
return {
105-
...source,
106-
...dest,
107-
scripts: {
108-
...source.scripts,
109-
...dest.scripts
110-
},
111-
dependencies: {
112-
...source.dependencies,
113-
...dest.dependencies
114-
},
115-
devDependencies: {
116-
...source.devDependencies,
117-
...dest.devDependencies
118-
}
119-
};
120-
};
121-
12273
module.exports = { copyToClients, CODE_GEN_OUTPUT_DIR };

scripts/generate-clients/templates/README.md.sample

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

scripts/generate-clients/templates/package.json.sample

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

scripts/generate-clients/templates/tsconfig.es.json.sample

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

scripts/generate-clients/templates/tsconfig.json.sample

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

0 commit comments

Comments
 (0)