Skip to content

Commit 123ab70

Browse files
chore: update cdk migrate command to allow for stack name input and additional languages (#27134)
This change updates use of noctilucent to cdk-from-cfn and installs it from npm. It also adds some additional language support and stack renaming. Not included in this PR: 1. Automated testing - this PR has been manually tested and automated tests will be added in a subsequent PR. 2. Go support - there are still some bugs to work out with go (in cdk-from-cfn) so implementation for it will be added after that's been worked out. 3. The option to compress the app to a zip file 4. The other TODOs listed in the `migrate` file. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent dc87d28 commit 123ab70

File tree

30 files changed

+172
-137
lines changed

30 files changed

+172
-137
lines changed

packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES

+38-8
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,32 @@ OTHER DEALINGS IN THE SOFTWARE.
138138

139139
----------------
140140

141+
** [email protected] - https://www.npmjs.com/package/archiver-utils/v/3.0.4 | MIT
142+
Copyright (c) 2015 Chris Talkington.
143+
144+
Permission is hereby granted, free of charge, to any person
145+
obtaining a copy of this software and associated documentation
146+
files (the "Software"), to deal in the Software without
147+
restriction, including without limitation the rights to use,
148+
copy, modify, merge, publish, distribute, sublicense, and/or sell
149+
copies of the Software, and to permit persons to whom the
150+
Software is furnished to do so, subject to the following
151+
conditions:
152+
153+
The above copyright notice and this permission notice shall be
154+
included in all copies or substantial portions of the Software.
155+
156+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
157+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
158+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
159+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
160+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
161+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
162+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
163+
OTHER DEALINGS IN THE SOFTWARE.
164+
165+
----------------
166+
141167
** [email protected] - https://www.npmjs.com/package/archiver/v/5.3.2 | MIT
142168
Copyright (c) 2012-2014 Chris Talkington, contributors.
143169

@@ -238,7 +264,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH RE
238264

239265
----------------
240266

241-
** aws-sdk@2.1447.0 - https://www.npmjs.com/package/aws-sdk/v/2.1447.0 | Apache-2.0
267+
** aws-sdk@2.1451.0 - https://www.npmjs.com/package/aws-sdk/v/2.1451.0 | Apache-2.0
242268
AWS SDK for JavaScript
243269
Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
244270

@@ -433,6 +459,10 @@ The above copyright notice and this permission notice shall be included in all c
433459
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
434460

435461

462+
----------------
463+
464+
** [email protected] - https://www.npmjs.com/package/cdk-from-cfn/v/0.11.0 | MIT OR Apache-2.0
465+
436466
----------------
437467

438468
** [email protected] - https://www.npmjs.com/package/chalk/v/4.1.2 | MIT
@@ -532,7 +562,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
532562

533563
----------------
534564

535-
** [email protected].1 - https://www.npmjs.com/package/compress-commons/v/4.1.1 | MIT
565+
** [email protected].2 - https://www.npmjs.com/package/compress-commons/v/4.1.2 | MIT
536566
Copyright (c) 2014 Chris Talkington, contributors.
537567

538568
Permission is hereby granted, free of charge, to any person
@@ -811,7 +841,7 @@ IN THE SOFTWARE.
811841

812842
----------------
813843

814-
** [email protected].2 - https://www.npmjs.com/package/crc32-stream/v/4.0.2 | MIT
844+
** [email protected].3 - https://www.npmjs.com/package/crc32-stream/v/4.0.3 | MIT
815845
Copyright (c) 2014 Chris Talkington, contributors.
816846

817847
Permission is hereby granted, free of charge, to any person
@@ -1367,7 +1397,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13671397

13681398
----------------
13691399

1370-
** [email protected].1 - https://www.npmjs.com/package/https-proxy-agent/v/7.0.1 | MIT
1400+
** [email protected].2 - https://www.npmjs.com/package/https-proxy-agent/v/7.0.2 | MIT
13711401

13721402
----------------
13731403

@@ -2134,7 +2164,7 @@ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21342164

21352165
----------------
21362166

2137-
** [email protected].0 - https://www.npmjs.com/package/pac-proxy-agent/v/7.0.0 | MIT
2167+
** [email protected].1 - https://www.npmjs.com/package/pac-proxy-agent/v/7.0.1 | MIT
21382168

21392169
----------------
21402170

@@ -2224,7 +2254,7 @@ THE SOFTWARE.
22242254

22252255
----------------
22262256

2227-
** [email protected].0 - https://www.npmjs.com/package/proxy-agent/v/6.3.0 | MIT
2257+
** [email protected].1 - https://www.npmjs.com/package/proxy-agent/v/6.3.1 | MIT
22282258

22292259
----------------
22302260

@@ -2793,7 +2823,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27932823

27942824
----------------
27952825

2796-
** [email protected].1 - https://www.npmjs.com/package/socks-proxy-agent/v/8.0.1 | MIT
2826+
** [email protected].2 - https://www.npmjs.com/package/socks-proxy-agent/v/8.0.2 | MIT
27972827

27982828
----------------
27992829

@@ -3375,7 +3405,7 @@ THE SOFTWARE.
33753405

33763406
----------------
33773407

3378-
** [email protected].0 - https://www.npmjs.com/package/zip-stream/v/4.1.0 | MIT
3408+
** [email protected].1 - https://www.npmjs.com/package/zip-stream/v/4.1.1 | MIT
33793409
Copyright (c) 2014 Chris Talkington, contributors.
33803410

33813411
Permission is hereby granted, free of charge, to any person

packages/@aws-cdk/cli-lib-alpha/generate.sh

-16
This file was deleted.

packages/@aws-cdk/cli-lib-alpha/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
"build+test": "yarn build && yarn test",
5151
"build+test+extract": "yarn build+test && yarn rosetta:extract",
5252
"build+test+package": "yarn build+test && yarn package",
53-
"bundle": "esbuild --bundle lib/index.ts --target=node14 --platform=node --external:fsevents --minify-whitespace --outfile=lib/main.js",
53+
"bundle": "esbuild --bundle lib/index.ts --target=node18 --platform=node --external:fsevents --minify-whitespace --outfile=lib/main.js",
5454
"compat": "cdk-compat",
55-
"gen": "./generate.sh",
55+
"gen": "../../../packages/aws-cdk/generate.sh",
5656
"lint": "cdk-lint",
5757
"package": "cdk-package",
5858
"pkglint": "pkglint -f",
@@ -68,8 +68,8 @@
6868
"cdk-build": {
6969
"post": [
7070
"yarn attribute",
71-
"cp ../../aws-cdk/lib/vendor/noctilucent/index_bg.wasm ./lib/",
7271
"mkdir -p ./lib/api/bootstrap/ && cp ../../aws-cdk/lib/api/bootstrap/bootstrap-template.yaml ./lib/api/bootstrap/",
72+
"cp ../../../node_modules/cdk-from-cfn/index_bg.wasm ./lib/",
7373
"yarn bundle",
7474
"node ./lib/main.js >/dev/null 2>/dev/null </dev/null"
7575
],
@@ -126,4 +126,4 @@
126126
},
127127
"dependencies": {},
128128
"peerDependencies": {}
129-
}
129+
}

packages/aws-cdk/.eslintrc.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const baseConfig = require('@aws-cdk/cdk-build-tools/config/eslintrc');
22
baseConfig.ignorePatterns.push('lib/init-templates/**/typescript/**/*.ts');
33
baseConfig.ignorePatterns.push('test/integ/cli/sam_cdk_integ_app/**/*.ts');
4-
baseConfig.ignorePatterns.push('vendor/noctilucent/**/*.ts');
54
baseConfig.parserOptions.project = __dirname + '/tsconfig.json';
65
module.exports = baseConfig;

packages/aws-cdk/.gitignore

+2-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ test/integ/cli/*.js
3636
test/integ/cli/*.d.ts
3737
!test/integ/cli-regression-patches/**/*
3838

39-
lib/vendor/noctilucent/*
40-
!lib/vendor/noctilucent/.keepdir
41-
4239
.DS_Store
4340

4441
junit.xml
45-
tmp/
42+
43+
lib/**/*.wasm

packages/aws-cdk/.npmignore

-2
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,3 @@ tsconfig.json
3030
junit.xml
3131

3232
generate.sh
33-
lib/vendor/noctilucent/Dockerfile
34-
tmp/

packages/aws-cdk/THIRD_PARTY_LICENSES

+4
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ The above copyright notice and this permission notice shall be included in all c
459459
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
460460

461461

462+
----------------
463+
464+
** [email protected] - https://www.npmjs.com/package/cdk-from-cfn/v/0.11.0 | MIT OR Apache-2.0
465+
462466
----------------
463467

464468
** [email protected] - https://www.npmjs.com/package/chalk/v/4.1.2 | MIT

packages/aws-cdk/generate.sh

-16
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,3 @@ cat > build-info.json <<HERE
1414
"commit": "${commit:0:7}"
1515
}
1616
HERE
17-
18-
# Download noctilucent wasm-pack build
19-
NOCTILUCENT_VERSION=0.1.2
20-
PACK_URL=https://github.com/iph/noctilucent/releases/download/v${NOCTILUCENT_VERSION}/wasm-pack.zip
21-
zipfile=$PWD/tmp/noctilucent-wasm-${NOCTILUCENT_VERSION}.zip
22-
outdir=lib/vendor/noctilucent
23-
24-
mkdir -p tmp
25-
if [[ ! -f "$zipfile" ]]; then
26-
curl -sSfLo "$zipfile" "$PACK_URL"
27-
fi
28-
29-
(cd $outdir && unzip -qo $zipfile)
30-
31-
# Don't need these files
32-
rm -f $outdir/{.gitignore,README.md,package.json}

packages/aws-cdk/lib/cli.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ async function parseCommandLineArguments(args: string[]) {
272272
.option('generate-only', { type: 'boolean', default: false, desc: 'If true, only generates project files, without executing additional operations such as setting up a git repo, installing dependencies or compiling the project' }),
273273
)
274274
.command('migrate', false /* hidden from "cdk --help" */, (yargs: Argv) => yargs
275-
.option('language', { type: 'string', alias: 'l', desc: 'The language to be used for the new project (default can be configured in ~/.cdk.json)', choices: MIGRATE_SUPPORTED_LANGUAGES })
276-
.option('input-path', { type: 'string', alias: 'inputpath', desc: 'The path to the CloudFormation template to migrate' })
277-
.option('output-path', { type: 'string', alias: 'outputpath', desc: 'The output path for the migrated cdk code' })
278-
.option('generate-only', { type: 'boolean', default: false, desc: 'If true, only generates project files, without executing additional operations such as setting up a git repo, installing dependencies or compiling the project' }),
275+
.option('stack-name', { type: 'string', alias: 'n', desc: 'The name assigned to the stack created in the new project. The name of the app will be based off this name as well.', requiresArg: true })
276+
.option('language', { type: 'string', default: 'typescript', alias: 'l', desc: 'The language to be used for the new project', choices: MIGRATE_SUPPORTED_LANGUAGES })
277+
.option('from-path', { type: 'string', alias: 'p', desc: 'The path to the CloudFormation template to migrate. Use this for locally stored templates' })
278+
.option('output-path', { type: 'string', alias: 'o', desc: 'The output path for the migrated cdk app' }),
279279
)
280280
.command('context', 'Manage cached context values', (yargs: Argv) => yargs
281281
.option('reset', { alias: 'e', desc: 'The context key (or its index) to reset', type: 'string', requiresArg: true })
@@ -659,8 +659,12 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
659659
return cliInit(args.TEMPLATE, language, undefined, args.generateOnly);
660660
}
661661
case 'migrate':
662-
const migrateLanguage = configuration.settings.get(['language']);
663-
return cliMigrate(args.inputpath, migrateLanguage, args.generateOnly, args.outputpath);
662+
return cliMigrate({
663+
stackName: args['stack-name'],
664+
fromPath: args['from-path'],
665+
language: args.language,
666+
outputPath: args['output-path'],
667+
});
664668
case 'version':
665669
return data(version.DISPLAY_VERSION);
666670

+61-54
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,78 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import { initializeProject, availableInitTemplates } from '../../lib/init';
3+
import * as cdk_from_cfn from 'cdk-from-cfn';
4+
import { cliInit } from '../../lib/init';
45
import { warning } from '../logging';
5-
import * as nocti from '../vendor/noctilucent';
6+
7+
/* eslint-disable @typescript-eslint/no-var-requires */ // Packages don't have @types module
8+
// eslint-disable-next-line @typescript-eslint/no-require-imports
9+
const camelCase = require('camelcase');
10+
// eslint-disable-next-line @typescript-eslint/no-require-imports
11+
const decamelize = require('decamelize');
612

713
/** The list of languages supported by the built-in noctilucent binary. */
8-
export const MIGRATE_SUPPORTED_LANGUAGES: readonly string[] = nocti.supported_languages();
14+
export const MIGRATE_SUPPORTED_LANGUAGES: readonly string[] = cdk_from_cfn.supported_languages();
915

10-
export async function cliMigrate(
11-
inputpath: string = process.cwd() + '/../template.txt',
12-
language = MIGRATE_SUPPORTED_LANGUAGES[0],
13-
generateOnly = false,
14-
outputpath = process.cwd(),
15-
) {
16-
warning('This is an experimental feature. We make no guarantees about the outcome or stability of the functionality.');
17-
const type = 'default'; // "default" is the default type (and maps to 'app')
18-
const template = (await availableInitTemplates()).find(t => t.hasName(type!));
19-
if (!template) {
20-
throw new Error(`couldn't find template for ${type} app type, this should never happen`);
21-
}
16+
export interface CliMigrateOptions {
17+
readonly stackName: string;
18+
readonly language?: string;
19+
readonly fromPath?: string;
20+
readonly outputPath?: string;
21+
}
2222

23-
if (!MIGRATE_SUPPORTED_LANGUAGES.includes(language)) {
24-
throw new Error(`Unsupported language for cdk migrate: ${language}. Supported languages are: ${MIGRATE_SUPPORTED_LANGUAGES.join(', ')}`);
25-
}
23+
export async function cliMigrate(options: CliMigrateOptions) {
24+
warning('This is an experimental feature. We make no guarantees about the outcome or stability of the functionality.');
2625

27-
await initializeProject(template, language, true, generateOnly, outputpath);
28-
const template_file = fs.readFileSync(inputpath, 'utf8');
29-
const generated_app = nocti.transmute(template_file, language);
26+
// TODO: Validate stack name
3027

31-
// clear out the init'd bin/lib files to replace with our own
32-
delete_files(outputpath + '/lib/');
28+
const language = options.language ?? 'typescript';
29+
const outputPath = path.join(options.outputPath ?? process.cwd(), options.stackName);
3330

34-
// we hardcode everything to be called noctstack still so this works for now.
35-
// Will change this to be much smarter once we can change stack name in noct
36-
const bin_app = `#!/usr/bin/env node
37-
import 'source-map-support/register';
38-
import * as cdk from 'aws-cdk-lib';
39-
import { NoctStack } from '../lib/generated_stack';
31+
const generatedStack = generateStack(options, language);
32+
const stackName = decamelize(options.stackName);
4033

41-
const app = new cdk.App();
42-
new NoctStack(app, 'NoctStack', {
43-
/* If you don't specify 'env', this stack will be environment-agnostic.
44-
* Account/Region-dependent features and context lookups will not work,
45-
* but a single synthesized template can be deployed anywhere. */
34+
try {
35+
fs.rmSync(outputPath, { recursive: true, force: true });
36+
fs.mkdirSync(outputPath, { recursive: true });
37+
await cliInit('app', language, true, false, outputPath, options.stackName);
4638

47-
/* Uncomment the next line to specialize this stack for the AWS Account
48-
* and Region that are implied by the current CLI configuration. */
49-
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
39+
let stackFileName: string;
40+
switch (language) {
41+
case 'typescript':
42+
stackFileName = `${outputPath}/lib/${stackName}-stack.ts`;
43+
break;
44+
case 'java':
45+
stackFileName = `${outputPath}/src/main/java/com/myorg/${camelCase(stackName, { pascalCase: true })}Stack.java`;
46+
break;
47+
case 'python':
48+
stackFileName = `${outputPath}/${stackName.replace(/-/g, '_')}/${stackName.replace(/-/g, '_')}_stack.py`;
49+
break;
50+
case 'csharp':
51+
stackFileName = `${outputPath}/src/${camelCase(stackName, { pascalCase: true })}/${camelCase(stackName, { pascalCase: true })}Stack.cs`;
52+
break;
53+
// TODO: Add Go support
54+
default:
55+
throw new Error(`${language} is not supported by CDK Migrate. Please choose from: ${MIGRATE_SUPPORTED_LANGUAGES.join(', ')}`);
56+
}
57+
fs.writeFileSync(stackFileName!, generatedStack);
58+
} catch (error) {
59+
fs.rmSync(outputPath, { recursive: true, force: true });
60+
throw error;
61+
}
5062

51-
/* Uncomment the next line if you know exactly what Account and Region you
52-
* want to deploy the stack to. */
53-
// env: { account: '123456789012', region: 'us-east-1' },
63+
}
5464

55-
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
56-
});`;
57-
const myname = path.basename(outputpath);
58-
fs.writeFileSync(outputpath + '/lib/' + 'generated_stack.ts', generated_app);
59-
fs.writeFileSync(outputpath + '/bin/' + `${myname}.ts`, bin_app);
65+
function generateStack(options: CliMigrateOptions, language: string) {
66+
const stackName = `${camelCase(decamelize(options.stackName), { pascalCase: true })}Stack`;
67+
// We will add other options here in a future change.
68+
if (options.fromPath) {
69+
return fromPath(stackName, options.fromPath, language);
70+
}
71+
// TODO: replace with actual output for other options.
72+
return '';
6073
}
6174

62-
function delete_files(filepath: string) {
63-
fs.readdir(filepath, (err, files) => {
64-
if (err) throw err;
65-
for (const file of files) {
66-
fs.unlink(filepath + file, (cause) => {
67-
if (cause) throw cause;
68-
});
69-
}
70-
});
75+
function fromPath(stackName: string, inputPath: string, language: string): string {
76+
const templateFile = fs.readFileSync(inputPath, 'utf8');
77+
return cdk_from_cfn.transmute(templateFile, language, stackName);
7178
}

packages/aws-cdk/lib/init-templates/app/csharp/src/%name.PascalCased%/Program.template.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ sealed class Program
1010
public static void Main(string[] args)
1111
{
1212
var app = new App();
13-
new %name.PascalCased%Stack(app, "%name.PascalCased%Stack", new StackProps
13+
new %name.PascalCased%Stack(app, "%stackname%", new StackProps
1414
{
1515
// If you don't specify 'env', this stack will be environment-agnostic.
1616
// Account/Region-dependent features and context lookups will not work,

0 commit comments

Comments
 (0)