Skip to content

Commit

Permalink
refactor(cli): generate yargs configuration from TS (#31850)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

N/A

### Reason for this change

We'd like to create a programmatic interface to the CDK Toolkit. A bonus of this overhaul is moving to a single source of truth for both the programmatic interface to the CDK Toolkit and the command line interface to the CDK Toolkit. This PR generates the existing `yargs` configuration from a TS configuration. In the long term, we'd generate the `yargs` configuration purely from the programmatic interface to the Toolkit, but this is an improvement with less effort. 

### Description of changes

Creates a new package, `@aws-cdk/yargs-gen`, which generates our `yargs` configuration from a `CliConfig` defined in `aws-cdk/config.ts` using `@cdklabs/typewriter`. 

### Description of how you validated changes

N/A yet. 

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
comcalvi authored Nov 5, 2024
1 parent 5a3a32f commit 4f8ecae
Show file tree
Hide file tree
Showing 26 changed files with 1,924 additions and 335 deletions.
1 change: 1 addition & 0 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"packages/@aws-cdk-testing/*",
"packages/@aws-cdk/*/lambda-packages/*",
"tools/@aws-cdk/cdk-build-tools",
"tools/@aws-cdk/yargs-gen",
"tools/@aws-cdk/cdk-release",
"tools/@aws-cdk/node-bundle",
"tools/@aws-cdk/pkglint",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"packages/@aws-cdk-testing/*",
"packages/@aws-cdk/*/lambda-packages/*",
"tools/@aws-cdk/cdk-build-tools",
"tools/@aws-cdk/yargs-gen",
"tools/@aws-cdk/cdk-release",
"tools/@aws-cdk/node-bundle",
"tools/@aws-cdk/pkglint",
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ The @aws-cdk/cli-lib-alpha package includes the following third-party software/l

----------------

** @jsii/check-node@1.103.1 - https://www.npmjs.com/package/@jsii/check-node/v/1.103.1 | Apache-2.0
** @jsii/check-node@1.104.0 - https://www.npmjs.com/package/@jsii/check-node/v/1.104.0 | Apache-2.0
jsii
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Expand Down Expand Up @@ -3562,7 +3562,7 @@ THE SOFTWARE.

----------------

** tslib@2.7.0 - https://www.npmjs.com/package/tslib/v/2.7.0 | 0BSD
** tslib@2.8.0 - https://www.npmjs.com/package/tslib/v/2.8.0 | 0BSD
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
Expand Down
23 changes: 23 additions & 0 deletions packages/aws-cdk/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
## CLI Commands

All CDK CLI Commands are defined in `lib/config.ts`. This file is translated
into a valid `yargs` configuration by `bin/yargs-gen`, which is generated by `@aws-cdk/yargs-gen`.
The `yargs` configuration is generated into the function `parseCommandLineArguments()`,
in `lib/parse-command-line-arguments.ts`, and is checked into git for readability and
inspectability; do not edit this file by hand, as every subsequent `yarn build` will
overwrite any manual edits. If you need to leverage a `yargs` feature not used by
the CLI, you must add support for it to `@aws-cdk/yargs-gen`.

Note that `bin/yargs-gen` is executed by `ts-node`, which allows `config.ts` to
reference functions and other identifiers defined in the CLI before the CLI is
built.

### Dynamic Values

Some values, such as the user's platform, cannot be computed at build time.
Some commands depend on these values, and thus `yargs-gen` must generate the
code to compute these values at build time.

The only way to do this today is to reference a parameter with `DynamicValue.fromParameter`.
The caller of `parseCommandLineArguments()` must pass the parameter.

## Integration Tests

Unit tests are automatically run as part of the regular build. Integration tests
Expand Down
342 changes: 14 additions & 328 deletions packages/aws-cdk/lib/cli.ts

Large diffs are not rendered by default.

419 changes: 419 additions & 0 deletions packages/aws-cdk/lib/config.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/aws-cdk/lib/notices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface NoticesProps {
*
* @default false
*/
readonly includeAcknowlegded?: boolean;
readonly includeAcknowledged?: boolean;

}

Expand Down Expand Up @@ -223,7 +223,7 @@ export class Notices {
private constructor(props: NoticesProps) {
this.configuration = props.configuration;
this.acknowledgedIssueNumbers = new Set(this.configuration.context.get('acknowledged-issue-numbers') ?? []);
this.includeAcknowlegded = props.includeAcknowlegded ?? false;
this.includeAcknowlegded = props.includeAcknowledged ?? false;
}

/**
Expand Down
Loading

0 comments on commit 4f8ecae

Please sign in to comment.