Skip to content

Commit 6cf55c6

Browse files
AllanZhengYPtrivikr
authored andcommitted
chore: add yarn codegen script calling Gradle script (#545)
* chore: add generate-clients script generating clients from models path * feat: clean the codegen input and output folder after generating * fix: update lerna commands use use '--include-dependencies' in replace of '--include-filtered-dependencies'
1 parent 87b5549 commit 6cf55c6

File tree

9 files changed

+221
-202
lines changed

9 files changed

+221
-202
lines changed

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
"description": "AWS SDK for JavaScript from the future",
66
"main": "index.js",
77
"scripts": {
8+
"generate-clients": "node ./scripts/generate-clients",
89
"bootstrap": "yarn",
910
"clean": "yarn clear-build-cache && yarn clear-build-info && lerna clean",
1011
"clear-build-cache": "rimraf ./packages/*/build/* ./clients/*/*/build/*",
1112
"clear-build-info": "rimraf ./packages/*/*.tsbuildinfo ./clients/*/*/*.tsbuildinfo",
12-
"copy-models": "node ./scripts/copyModels.js",
13-
"update-clients": "node ./packages/package-generator/build/cli.js import-all --matching './models/*/*/service-2.json'",
14-
"build:crypto-dependencies": "lerna run --scope '@aws-sdk/types' --scope '@aws-sdk/util-utf8-browser' --scope '@aws-sdk/util-locate-window' --scope '@aws-sdk/hash-node' --include-filtered-dependencies pretest",
15-
"build:smithy-client": "lerna run --scope '@aws-sdk/client-rds-data' --include-filtered-dependencies pretest",
13+
"build:crypto-dependencies": "lerna run --scope '@aws-sdk/types' --scope '@aws-sdk/util-utf8-browser' --scope '@aws-sdk/util-locate-window' --scope '@aws-sdk/hash-node' --include-dependencies pretest",
14+
"build:smithy-client": "lerna run --scope '@aws-sdk/client-rds-data' --include-dependencies pretest",
1615
"pretest": "yarn build:crypto-dependencies && yarn build:smithy-client",
1716
"test": "jest --coverage --passWithNoTests",
1817
"pretest-all": "lerna run pretest",
@@ -36,9 +35,11 @@
3635
"devDependencies": {
3736
"@commitlint/cli": "^8.1.0",
3837
"@commitlint/config-conventional": "^8.1.0",
38+
"@types/fs-extra": "^8.0.1",
3939
"@types/jest": "^24.0.12",
4040
"codecov": "^3.4.0",
4141
"cucumber": "0.5.x",
42+
"fs-extra": "^8.1.0",
4243
"generate-changelog": "^1.7.1",
4344
"husky": "^3.0.0",
4445
"jest": "^24.7.1",

scripts/copyModels.js

-31
This file was deleted.

scripts/generate-clients/code-gen.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const path = require("path");
2+
const { copyFileSync, emptyDirSync } = require("fs-extra");
3+
const { readdirSync, lstatSync } = require("fs");
4+
const { spawn } = require("child_process");
5+
6+
const CODE_GEN_INPUT_DIR = path.normalize(
7+
path.join(__dirname, "..", "..", "codegen", "sdk-codegen", "aws-models")
8+
);
9+
const CODE_GEN_ROOT = path.normalize(
10+
path.join(__dirname, "..", "..", "codegen")
11+
);
12+
13+
async function generateClients(models) {
14+
console.info("models directory: ", models);
15+
if (models === CODE_GEN_INPUT_DIR) {
16+
// console.log("skipping copying models to codegen directory");
17+
throw new Error(
18+
`models directory cannot be the same as ${CODE_GEN_INPUT_DIR}`
19+
);
20+
} else {
21+
console.log(`clearing code gen input folder...`);
22+
emptyDirSync(CODE_GEN_INPUT_DIR);
23+
console.log(`copying models from ${models} to ${CODE_GEN_INPUT_DIR}...`);
24+
// copySync(models, CODE_GEN_INPUT_DIR, {
25+
// overwrite: true
26+
// });
27+
for (const modelFileName of readdirSync(models)) {
28+
const modelPath = path.join(models, modelFileName);
29+
if (!lstatSync(modelPath).isFile()) continue;
30+
console.log(`copying model ${modelFileName}...`);
31+
copyFileSync(modelPath, path.join(CODE_GEN_INPUT_DIR, modelFileName), {
32+
overwrite: true
33+
});
34+
}
35+
}
36+
await spawnProcess(
37+
"./gradlew",
38+
[":sdk-codegen:clean", ":sdk-codegen:build"],
39+
{
40+
cwd: CODE_GEN_ROOT
41+
}
42+
);
43+
}
44+
45+
const spawnProcess = (command, args = [], options = {}) =>
46+
new Promise((resolve, reject) => {
47+
try {
48+
const ls = spawn(command, args, options);
49+
ls.stdout.on("data", data => {
50+
console.log(data.toString());
51+
});
52+
ls.stderr.on("data", data => {
53+
console.error(`stderr: ${data.toString()}`);
54+
});
55+
56+
ls.on("close", code => {
57+
console.log(`child process exited with code ${code}`);
58+
resolve();
59+
});
60+
} catch (e) {
61+
reject(e);
62+
}
63+
});
64+
65+
module.exports = { generateClients, CODE_GEN_INPUT_DIR };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const path = require("path");
2+
const { copySync, ensureDirSync } = require("fs-extra");
3+
const {
4+
readdirSync,
5+
lstatSync,
6+
readFileSync,
7+
existsSync,
8+
writeFileSync
9+
} = require("fs");
10+
11+
const CODE_GEN_OUTPUT_DIR = path.normalize(
12+
path.join(
13+
__dirname,
14+
"..",
15+
"..",
16+
"codegen",
17+
"sdk-codegen",
18+
"build",
19+
"smithyprojections",
20+
"sdk-codegen"
21+
)
22+
);
23+
24+
const unOverridables = [
25+
"package.json",
26+
"tsconfig.es.json",
27+
"tsconfig.json",
28+
"tsconfig.test.json"
29+
];
30+
31+
async function copyToClients(clientsDir) {
32+
for (const modelName of readdirSync(CODE_GEN_OUTPUT_DIR)) {
33+
if (modelName === "source") continue;
34+
const artifactPath = path.join(
35+
CODE_GEN_OUTPUT_DIR,
36+
modelName,
37+
"typescript-codegen"
38+
);
39+
const packageManifestPath = path.join(artifactPath, "package.json");
40+
if (!existsSync(packageManifestPath)) {
41+
console.error(`${modelName} generates empty client, skip.`);
42+
continue;
43+
}
44+
const packageManifest = JSON.parse(
45+
readFileSync(packageManifestPath).toString()
46+
);
47+
const packageName = packageManifest.name.replace("@aws-sdk/", "");
48+
console.log(`copying ${packageName} from ${artifactPath} to ${clientsDir}`);
49+
const destPath = path.join(clientsDir, packageName);
50+
for (const packageSub of readdirSync(artifactPath)) {
51+
const packageSubPath = path.join(artifactPath, packageSub);
52+
const destSubPath = path.join(destPath, packageSub);
53+
if (unOverridables.indexOf(packageSub) >= 0) {
54+
if (!existsSync(destSubPath))
55+
copySync(packageSubPath, destSubPath, { overwrite: true });
56+
else if (packageSub === "package.json") {
57+
/**
58+
* Copy package.json content in detail.
59+
* Basically merge the generated package.json and dest package.json
60+
* but prefer the values from dest when they contain the same key
61+
* */
62+
const destManifest = JSON.parse(readFileSync(destSubPath).toString());
63+
const updatedManifest = {
64+
...packageManifest,
65+
...destManifest,
66+
scripts: {
67+
...packageManifest.scripts,
68+
...destManifest.scripts
69+
},
70+
dependencies: {
71+
...packageManifest.dependencies,
72+
...destManifest.dependencies
73+
},
74+
devDependencies: {
75+
...packageManifest.devDependencies,
76+
...destManifest.devDependencies
77+
}
78+
};
79+
writeFileSync(destSubPath, JSON.stringify(updatedManifest, null, 2));
80+
}
81+
} else {
82+
if (lstatSync(packageSubPath).isDirectory()) ensureDirSync(destSubPath);
83+
copySync(packageSubPath, destSubPath, { overwrite: true });
84+
}
85+
}
86+
}
87+
}
88+
89+
module.exports = { copyToClients, CODE_GEN_OUTPUT_DIR };

scripts/generate-clients/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const yargs = require("yargs");
2+
const path = require("path");
3+
const { emptyDirSync } = require("fs-extra");
4+
const { generateClients, CODE_GEN_INPUT_DIR } = require("./code-gen");
5+
const { copyToClients, CODE_GEN_OUTPUT_DIR } = require("./copy-to-clients");
6+
7+
const CLIENTS_DIR = path.normalize(path.join(__dirname, "..", "..", "clients"));
8+
9+
const { models, output: clientsDir } = yargs
10+
.alias("m", "models")
11+
.string("m")
12+
.describe("m", "the directory of models")
13+
.required("m")
14+
.alias("o", "output")
15+
.string("o")
16+
.describe("o", "the output directory for built clients")
17+
.default("o", CLIENTS_DIR)
18+
.help().argv;
19+
20+
(async () => {
21+
try {
22+
await generateClients(models);
23+
await copyToClients(clientsDir);
24+
emptyDirSync(CODE_GEN_INPUT_DIR);
25+
emptyDirSync(CODE_GEN_OUTPUT_DIR);
26+
} catch (e) {
27+
console.log(e);
28+
process.exit(1);
29+
}
30+
})();

scripts/rebuildClients.js

-98
This file was deleted.

0 commit comments

Comments
 (0)