Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): added build field to cdk.json #17176

Merged
merged 19 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/aws-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ Some of the interesting keys that can be used in the JSON configuration files:
```json5
{
"app": "node bin/main.js", // Command to start the CDK app (--app='node bin/main.js')
"build": "mvn package", // Specify pre-synth build (no command line option)
"context": { // Context entries (--context=key=value)
"key": "value"
},
Expand All @@ -473,6 +474,12 @@ Some of the interesting keys that can be used in the JSON configuration files:
}
```

If specified, the command in the `build` key will be executed immediately before synthesis.
This can be used to build Lambda Functions, CDK Application code, or other assets.
`build` cannot be specified on the command line, and must be specified in either
the Project configuration or the User configuration. The command specified
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
in `build` will be executed by the "watch" process before deployment.

### Environment

The following environment variables affect aws-cdk:
Expand Down
13 changes: 10 additions & 3 deletions packages/aws-cdk/lib/api/cxapp/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
debug('context:', context);
env[cxapi.CONTEXT_ENV] = JSON.stringify(context);

const build = config.settings.get(['build']);
if (build) {
await exec([build]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if "build" is set to something like "mvn package"? How does this split into program and args?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exec() now splits its argument into command and args before spawning the process.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like exec() treats the first item in the array as the command but this is not true in the case I described above.

Can you add a test to verify ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the split doesn't happen in the case of the build key. It winds up working because the whole command just get put into command and spawn() happily accepts it.

}

const app = config.settings.get(['app']);
if (!app) {
throw new Error(`--app is required either in command-line, in ${PROJECT_CONFIG} or in ${USER_DEFAULTS}`);
Expand Down Expand Up @@ -74,7 +79,7 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom

debug('env:', env);

await exec();
await exec(commandLine);

return createAssembly(outdir);

Expand All @@ -91,7 +96,7 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
}
}

async function exec() {
async function exec(commandAndArgs: string[]) {
return new Promise<void>((ok, fail) => {
// We use a slightly lower-level interface to:
//
Expand All @@ -103,7 +108,9 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
// anyway, and if the subprocess is printing to it for debugging purposes the
// user gets to see it sooner. Plus, capturing doesn't interact nicely with some
// processes like Maven.
const proc = childProcess.spawn(commandLine[0], commandLine.slice(1), {
const command = commandAndArgs[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if commandAndArgs[0] is mvn package

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we just need to accept a single string and pass it down to spawn(). Since you use shell:true this should just work without splitting to arguments imho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to split the arguments because of the other usage of exec(), namely this line:

await exec(commandLine);

commandLine is defined by

const commandLine = await guessExecutable(appToArray(app));

guessExecutable() needs a string[], not a string. Do you want me to rework this so that we don't need to have any string[]s passed to exec(), and instead make exec() take just a string? exec() previously operated on a string[].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eladb if commandAndArgs[0] is mvn package then spawn() will still correctly start the process.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit messy but if this works as is i am okay with that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eladb just made this cleaner, now mvn package will be split into ["mvn", "package"] instead of the previous ["mvn package"]. Would appreciate another pass on the PR!

const args = commandAndArgs.slice(1);
const proc = childProcess.spawn(command, args, {
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
stdio: ['ignore', 'inherit', 'inherit'],
detached: false,
shell: true,
Expand Down
21 changes: 21 additions & 0 deletions packages/aws-cdk/test/api/exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ test('application set in --app is `*.js` and executable', async () => {
await execProgram(sdkProvider, config);
});

test('cli throws when the `build` script fails', async () => {
config.settings.set(['build'], 'fake-command');
mockSpawn({
commandLine: ['fake-command'],
exitCode: 127,
});

await expect(execProgram(sdkProvider, config)).rejects.toEqual(new Error('Subprocess exited with error 127'));
}, TEN_SECOND_TIMEOUT);

test('cli does not throw when the `build` script succeeds', async () => {
config.settings.set(['build'], 'real-command');
mockSpawn({
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
commandLine: ['real-command'],
exitCode: 0,
});

await expect(execProgram(sdkProvider, config)).resolves;
}, TEN_SECOND_TIMEOUT);


function writeOutputAssembly() {
const asm = testAssembly({
stacks: [],
Expand Down