Skip to content

Commit

Permalink
feat: update protocol_tests when generate-clients is run (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed May 4, 2020
1 parent fee3b6f commit 4b16f7c
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 51 deletions.
2 changes: 1 addition & 1 deletion codegen/protocol-test-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
}

dependencies {
implementation("software.amazon.smithy:smithy-aws-protocol-tests:0.9.8")
implementation("software.amazon.smithy:smithy-aws-protocol-tests:0.9.9")
compile(project(":smithy-aws-typescript-codegen"))
}

Expand Down
35 changes: 25 additions & 10 deletions codegen/protocol-test-codegen/smithy-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
"projections": {
"aws-ec2": {
"transforms": [
{"name": "includeServices", "args": ["aws.protocols.tests.ec2#AwsEc2"]}
{
"name": "includeServices",
"args": ["aws.protocols.tests.ec2#AwsEc2"]
}
],
"plugins": {
"typescript-codegen": {
"package": "@aws-sdk/protocol-tests-aws-ec2",
"package": "@aws-sdk/aws-ec2",
"packageVersion": "1.0.0-alpha.1",
"packageJson": {
"author": {
Expand All @@ -23,11 +26,14 @@
},
"aws-json": {
"transforms": [
{"name": "includeServices", "args": ["aws.protocols.tests.json#JsonProtocol"]}
{
"name": "includeServices",
"args": ["aws.protocols.tests.json#JsonProtocol"]
}
],
"plugins": {
"typescript-codegen": {
"package": "@aws-sdk/protocol-tests-aws-json",
"package": "@aws-sdk/aws-json",
"packageVersion": "1.0.0-alpha.1",
"packageJson": {
"author": {
Expand All @@ -43,11 +49,14 @@
},
"aws-query": {
"transforms": [
{"name": "includeServices", "args": ["aws.protocols.tests.query#AwsQuery"]}
{
"name": "includeServices",
"args": ["aws.protocols.tests.query#AwsQuery"]
}
],
"plugins": {
"typescript-codegen": {
"package": "@aws-sdk/protocol-tests-aws-query",
"package": "@aws-sdk/aws-query",
"packageVersion": "1.0.0-alpha.1",
"packageJson": {
"author": {
Expand All @@ -63,11 +72,14 @@
},
"aws-restjson": {
"transforms": [
{"name": "includeServices", "args": ["aws.protocols.tests.restjson#RestJson"]}
{
"name": "includeServices",
"args": ["aws.protocols.tests.restjson#RestJson"]
}
],
"plugins": {
"typescript-codegen": {
"package": "@aws-sdk/protocol-tests-aws-restjson",
"package": "@aws-sdk/aws-restjson",
"packageVersion": "1.0.0-alpha.1",
"packageJson": {
"author": {
Expand All @@ -83,11 +95,14 @@
},
"aws-restxml": {
"transforms": [
{"name": "includeServices", "args": ["aws.protocols.tests.restxml#RestXml"]}
{
"name": "includeServices",
"args": ["aws.protocols.tests.restxml#RestXml"]
}
],
"plugins": {
"typescript-codegen": {
"package": "@aws-sdk/protocol-tests-aws-restxml",
"package": "@aws-sdk/aws-restxml",
"packageVersion": "1.0.0-alpha.1",
"packageJson": {
"author": {
Expand Down
37 changes: 23 additions & 14 deletions scripts/generate-clients/code-gen-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@ const { join, normalize } = require("path");

const CODE_GEN_ROOT = normalize(join(__dirname, "..", "..", "codegen"));

const CODE_GEN_TASK_ROOT = join(CODE_GEN_ROOT, "sdk-codegen");
const getCodeGenDirRoot = dir => join(CODE_GEN_ROOT, dir);
const CODE_GEN_SDK_ROOT = getCodeGenDirRoot("sdk-codegen");
const CODE_GEN_PROTOCOL_TESTS_ROOT = getCodeGenDirRoot("protocol-test-codegen");

const TEMP_CODE_GEN_INPUT_DIR = normalize(join(__dirname, ".aws-models"));

const CODE_GEN_OUTPUT_DIR = normalize(
join(
__dirname,
"..",
"..",
"codegen",
"sdk-codegen",
"build",
"smithyprojections",
"sdk-codegen"
)
const getCodeGenOutputDir = dir =>
normalize(
join(
__dirname,
"..",
"..",
"codegen",
dir,
"build",
"smithyprojections",
dir
)
);
const CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR = getCodeGenOutputDir(
"protocol-test-codegen"
);
const CODE_GEN_SDK_OUTPUT_DIR = getCodeGenOutputDir("sdk-codegen");

module.exports = {
CODE_GEN_ROOT,
CODE_GEN_TASK_ROOT,
CODE_GEN_OUTPUT_DIR,
CODE_GEN_SDK_ROOT,
CODE_GEN_PROTOCOL_TESTS_ROOT,
CODE_GEN_SDK_OUTPUT_DIR,
CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR,
TEMP_CODE_GEN_INPUT_DIR
};
22 changes: 17 additions & 5 deletions scripts/generate-clients/code-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const { readdirSync, lstatSync } = require("fs");
const { spawnProcess } = require("./spawn-process");
const {
CODE_GEN_ROOT,
CODE_GEN_TASK_ROOT,
CODE_GEN_SDK_ROOT,
TEMP_CODE_GEN_INPUT_DIR
} = require("./code-gen-dir");
const Glob = require("glob");

async function generateClients(models) {
const generateClients = async models => {
let designatedModels = false;
if (typeof models === "string") {
//`models` is a folder path
Expand Down Expand Up @@ -53,16 +53,28 @@ async function generateClients(models) {
if (designatedModels) {
options.push(
`-PmodelsDirProp=${path.relative(
CODE_GEN_TASK_ROOT,
CODE_GEN_SDK_ROOT,
TEMP_CODE_GEN_INPUT_DIR
)}`
);
}

await spawnProcess("./gradlew", options, {
cwd: CODE_GEN_ROOT
});
}
};

const generateProtocolTests = async () => {
await spawnProcess(
"./gradlew",
[":protocol-test-codegen:clean", ":protocol-test-codegen:build"],
{
cwd: CODE_GEN_ROOT
}
);
};

module.exports = {
generateClients
generateClients,
generateProtocolTests
};
5 changes: 2 additions & 3 deletions scripts/generate-clients/code-prettify.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const { spawnProcess } = require("./spawn-process");
const { CODE_GEN_OUTPUT_DIR } = require("./code-gen-dir");

const prettifyCode = async () => {
const prettifyCode = async dir => {
await spawnProcess("./node_modules/.bin/prettier", [
"--write",
`${CODE_GEN_OUTPUT_DIR}/**/*.{ts,js,md,json}`
`${dir}/**/*.{ts,js,md,json}`
]);
};

Expand Down
19 changes: 8 additions & 11 deletions scripts/generate-clients/copy-to-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
existsSync,
writeFileSync
} = require("fs");
const { CODE_GEN_OUTPUT_DIR } = require("./code-gen-dir");

const getOverwritablePredicate = packageName => pathName => {
const overwritablePathnames = [
Expand Down Expand Up @@ -63,14 +62,10 @@ const mergeManifest = (fromContent, toContent) => {
return merged;
};

async function copyToClients(clientsDir) {
for (const modelName of readdirSync(CODE_GEN_OUTPUT_DIR)) {
const copyToClients = async (sourceDir, destinationDir) => {
for (const modelName of readdirSync(sourceDir)) {
if (modelName === "source") continue;
const artifactPath = join(
CODE_GEN_OUTPUT_DIR,
modelName,
"typescript-codegen"
);
const artifactPath = join(sourceDir, modelName, "typescript-codegen");
const packageManifestPath = join(artifactPath, "package.json");
if (!existsSync(packageManifestPath)) {
console.error(`${modelName} generates empty client, skip.`);
Expand All @@ -81,8 +76,10 @@ async function copyToClients(clientsDir) {
readFileSync(packageManifestPath).toString()
);
const packageName = packageManifest.name;
console.log(`copying ${packageName} from ${artifactPath} to ${clientsDir}`);
const destPath = join(clientsDir, packageName.replace("@aws-sdk/", ""));
console.log(
`copying ${packageName} from ${artifactPath} to ${destinationDir}`
);
const destPath = join(destinationDir, packageName.replace("@aws-sdk/", ""));
const overwritablePredicate = getOverwritablePredicate(packageName);
for (const packageSub of readdirSync(artifactPath)) {
const packageSubPath = join(artifactPath, packageSub);
Expand All @@ -109,7 +106,7 @@ async function copyToClients(clientsDir) {
}
}
}
}
};

module.exports = {
copyToClients
Expand Down
31 changes: 24 additions & 7 deletions scripts/generate-clients/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const yargs = require("yargs");
const path = require("path");
const { emptyDirSync, rmdirSync } = require("fs-extra");
const { generateClients } = require("./code-gen");
const { generateClients, generateProtocolTests } = require("./code-gen");
const { copyToClients } = require("./copy-to-clients");
const {
CODE_GEN_OUTPUT_DIR,
CODE_GEN_SDK_OUTPUT_DIR,
CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR,
TEMP_CODE_GEN_INPUT_DIR
} = require("./code-gen-dir");
const { prettifyCode } = require("./code-prettify");

const CLIENTS_DIR = path.normalize(path.join(__dirname, "..", "..", "clients"));
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
.alias("m", "models")
Expand All @@ -22,16 +28,27 @@ const { models, globs, output: clientsDir } = yargs
.alias("o", "output")
.string("o")
.describe("o", "The output directory for built clients")
.default("o", CLIENTS_DIR)
.default("o", SDK_CLIENTS_DIR)
.help().argv;

(async () => {
try {
await generateClients(models || globs);
await prettifyCode();
await copyToClients(clientsDir);
emptyDirSync(CODE_GEN_OUTPUT_DIR);
await generateProtocolTests();

await prettifyCode(CODE_GEN_SDK_OUTPUT_DIR);
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
);

emptyDirSync(CODE_GEN_SDK_OUTPUT_DIR);
emptyDirSync(CODE_GEN_PROTOCOL_TESTS_OUTPUT_DIR);
emptyDirSync(TEMP_CODE_GEN_INPUT_DIR);

rmdirSync(TEMP_CODE_GEN_INPUT_DIR);
} catch (e) {
console.log(e);
Expand Down

0 comments on commit 4b16f7c

Please sign in to comment.