Skip to content

Commit 2308e5f

Browse files
authored
fix(scripts): generate clients in batches (#3319)
1 parent cc2e4a6 commit 2308e5f

File tree

4 files changed

+53
-41
lines changed

4 files changed

+53
-41
lines changed

Diff for: scripts/generate-clients/code-gen-dir.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const CODE_GEN_SDK_ROOT = getCodeGenDirRoot("sdk-codegen");
88
const CODE_GEN_PROTOCOL_TESTS_ROOT = getCodeGenDirRoot("protocol-test-codegen");
99

1010
const TEMP_CODE_GEN_INPUT_DIR = normalize(join(__dirname, ".aws-models"));
11+
const DEFAULT_CODE_GEN_INPUT_DIR = normalize(join(CODE_GEN_SDK_ROOT, "aws-models"));
1112

1213
const getCodeGenOutputDir = (dir) =>
1314
normalize(join(__dirname, "..", "..", "codegen", dir, "build", "smithyprojections", dir));
@@ -22,5 +23,6 @@ module.exports = {
2223
CODE_GEN_SDK_OUTPUT_DIR,
2324
CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR,
2425
CODE_GEN_GENERIC_CLIENT_OUTPUT_DIR,
26+
DEFAULT_CODE_GEN_INPUT_DIR,
2527
TEMP_CODE_GEN_INPUT_DIR,
2628
};

Diff for: scripts/generate-clients/code-gen.js

+17-40
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,28 @@
11
// @ts-check
2-
const path = require("path");
2+
const { basename, join, relative } = require("path");
33
const { emptyDirSync } = require("fs-extra");
4-
const { copyFileSync, readdirSync, lstatSync } = require("fs");
4+
const { copyFileSync } = require("fs");
55
const { spawnProcess } = require("../utils/spawn-process");
66
const { CODE_GEN_ROOT, CODE_GEN_SDK_ROOT, TEMP_CODE_GEN_INPUT_DIR } = require("./code-gen-dir");
7-
const Glob = require("glob");
7+
const { getModelFilepaths } = require("./get-model-filepaths");
88

9-
const generateClients = async (models) => {
10-
let designatedModels = false;
11-
if (typeof models === "string") {
12-
//`models` is a folder path
13-
designatedModels = true;
9+
const generateClients = async (models, batchSize) => {
10+
const filepaths = getModelFilepaths(models);
11+
const options = [
12+
":sdk-codegen:clean",
13+
":sdk-codegen:build",
14+
`-PmodelsDirProp=${relative(CODE_GEN_SDK_ROOT, TEMP_CODE_GEN_INPUT_DIR)}`,
15+
];
16+
17+
while (filepaths.length > 0) {
1418
emptyDirSync(TEMP_CODE_GEN_INPUT_DIR);
15-
console.log(`preparing models from ${models}...`);
16-
for (const modelFileName of readdirSync(models)) {
17-
const modelPath = path.join(models, modelFileName);
18-
if (!lstatSync(modelPath).isFile()) continue;
19-
console.log(`copying model ${modelFileName}...`);
20-
copyFileSync(modelPath, path.join(TEMP_CODE_GEN_INPUT_DIR, modelFileName));
19+
const filepathsToCopy = filepaths.splice(0, batchSize);
20+
for (const filepath of filepathsToCopy) {
21+
const filename = basename(filepath);
22+
copyFileSync(filepath, join(TEMP_CODE_GEN_INPUT_DIR, filename));
2123
}
22-
} else if (Array.isArray(models)) {
23-
//`models` is a list of globs
24-
designatedModels = true;
25-
emptyDirSync(TEMP_CODE_GEN_INPUT_DIR);
26-
models.forEach((pattern) => {
27-
const files = Glob.sync(pattern, {
28-
realpath: true,
29-
absolute: true,
30-
});
31-
files.forEach((file) => {
32-
if (!lstatSync(file).isFile()) return;
33-
const name = path.basename(file);
34-
console.log(`copying model ${name}...`);
35-
copyFileSync(file, path.join(TEMP_CODE_GEN_INPUT_DIR, name));
36-
});
37-
});
38-
} else {
39-
console.log("no model supplied, generating all AWS clients");
40-
}
41-
const options = [":sdk-codegen:clean", ":sdk-codegen:build"];
42-
if (designatedModels) {
43-
options.push(`-PmodelsDirProp=${path.relative(CODE_GEN_SDK_ROOT, TEMP_CODE_GEN_INPUT_DIR)}`);
24+
await spawnProcess("./gradlew", options, { cwd: CODE_GEN_ROOT });
4425
}
45-
46-
await spawnProcess("./gradlew", options, {
47-
cwd: CODE_GEN_ROOT,
48-
});
4926
};
5027

5128
const generateProtocolTests = async () => {

Diff for: scripts/generate-clients/get-model-filepaths.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { readdirSync, lstatSync } = require("fs");
2+
const Glob = require("glob");
3+
const { join } = require("path");
4+
5+
const getModelFilepaths = (models) => {
6+
if (typeof models === "string") {
7+
//`models` is a folder path
8+
console.log(`preparing models from ${models}...`);
9+
return readdirSync(models)
10+
.map((modelFileName) => join(models, modelFileName))
11+
.filter((modelFilepath) => lstatSync(modelFilepath).isFile());
12+
} else if (Array.isArray(models)) {
13+
//`models` is a list of globs
14+
console.log(`preparing models from ${models}...`);
15+
return models
16+
.map((pattern) =>
17+
Glob.sync(pattern, {
18+
realpath: true,
19+
absolute: true,
20+
})
21+
)
22+
.flat()
23+
.filter((modelFilepath) => lstatSync(modelFilepath).isFile());
24+
}
25+
};
26+
27+
module.exports = { getModelFilepaths };

Diff for: scripts/generate-clients/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
CODE_GEN_SDK_OUTPUT_DIR,
99
CODE_GEN_GENERIC_CLIENT_OUTPUT_DIR,
1010
CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR,
11+
DEFAULT_CODE_GEN_INPUT_DIR,
1112
TEMP_CODE_GEN_INPUT_DIR,
1213
} = require("./code-gen-dir");
1314
const { prettifyCode } = require("./code-prettify");
@@ -22,6 +23,7 @@ const {
2223
output: clientsDir,
2324
noPrivateClients,
2425
s: serverOnly,
26+
batchSize,
2527
} = yargs
2628
.alias("m", "models")
2729
.string("m")
@@ -41,6 +43,10 @@ const {
4143
.boolean("s")
4244
.describe("s", "Generate server artifacts instead of client ones")
4345
.conflicts("s", ["m", "g", "n"])
46+
.describe("b", "Batchsize for generating clients")
47+
.number("b")
48+
.alias("b", "batch-size")
49+
.default("b", 50)
4450
.help().argv;
4551

4652
(async () => {
@@ -58,7 +64,7 @@ const {
5864
return;
5965
}
6066

61-
await generateClients(models || globs);
67+
await generateClients(models || globs || DEFAULT_CODE_GEN_INPUT_DIR, batchSize);
6268
if (!noPrivateClients) {
6369
await generateGenericClient();
6470
await generateProtocolTests();

0 commit comments

Comments
 (0)