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

chore: experimental cdk migrate command #25859

Merged
merged 31 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b53855e
experimental cdk migrate command
Jun 5, 2023
b3dc8e1
fix typescript erroring
Jun 5, 2023
1801196
add helpful import message
Jun 5, 2023
e71d63e
fix aliases to make it work
RomainMuller Jun 6, 2023
483c431
bring in some automation to avoid manual operations
RomainMuller Jun 6, 2023
5b5afee
Leverage list of supported languages
RomainMuller Jun 6, 2023
ad081a0
add warning to command
Jun 12, 2023
089df4f
Merge branch 'main' into experimental/migrate
HBobertz Jun 12, 2023
d57c47c
better warning messages
Jun 12, 2023
8060c44
Merge branch 'main' into experimental/migrate
HBobertz Jun 14, 2023
e50a4ef
modified how we get nocti for cdk migrate
Jun 16, 2023
84b10c4
Merge branch 'main' into experimental/migrate
HBobertz Jun 16, 2023
a2a26f3
remove mkdir as it was having idempotency issues
Jun 16, 2023
0bdd473
update readme to reflect new nocti consumption flow
Jun 16, 2023
94687d8
git fixes for submodule stuff
Jun 16, 2023
d51b074
modify generate to delete the generated package json
Jun 16, 2023
2a85864
Update warning wording to be more professional
HBobertz Jun 19, 2023
dbde5b7
Merge branch 'main' into experimental/migrate
HBobertz Jun 19, 2023
00eeede
update third party licensces
Jun 19, 2023
4ca5d2e
fix alpha gen script
Jun 19, 2023
95cf39c
change default input path
Jun 19, 2023
7444399
Merge branch 'main' into experimental/migrate
HBobertz Jun 19, 2023
a8665cd
Merge branch 'main' into experimental/migrate
HBobertz Jun 20, 2023
b3ab9f8
Merge branch 'main' into experimental/migrate
HBobertz Jun 20, 2023
a2fd134
add npx so ts-node can be called correctly
MrArnoldPalmer Jun 20, 2023
1727e02
Merge branch 'main' into experimental/migrate
HBobertz Jun 20, 2023
afbe3b3
fix irrelevant bug
Jun 20, 2023
da4280b
use yarn --silent instead of npx
RomainMuller Jun 23, 2023
2efd438
Hoist ts-node to root package.json + uniformize typescript deps
RomainMuller Jun 23, 2023
e15f084
adjust esbuild packaging to correctly bundle WASM resource
RomainMuller Jun 23, 2023
6e06664
Merge branch 'main' into experimental/migrate
HBobertz Jun 23, 2023
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
10 changes: 10 additions & 0 deletions packages/aws-cdk/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { CdkToolkit, AssetBuildTime } from '../lib/cdk-toolkit';
import { realHandler as context } from '../lib/commands/context';
import { realHandler as docs } from '../lib/commands/docs';
import { realHandler as doctor } from '../lib/commands/doctor';
import { cliMigrate } from '../lib/commands/migrate';
import { RequireApproval } from '../lib/diff';
import { availableInitLanguages, cliInit, printAvailableTemplates } from '../lib/init';
import { data, debug, error, print, setLogLevel, setCI } from '../lib/logging';
Expand Down Expand Up @@ -269,6 +270,12 @@ async function parseCommandLineArguments(args: string[]) {
.option('list', { type: 'boolean', desc: 'List the available templates' })
.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' }),
)
.command('migrate', 'Migrate a cloudformation template to cdk', (yargs: Argv) => yargs
.option('language', { type: 'string', alias: 'l', desc: 'The language to be used for the new project (default can be configured in ~/.cdk.json)', choices: initTemplateLanguages })
.option('inputpath', { type: 'string', alias: 'i', desc: 'The path to the CloudFormation template to migrate' })
.option('outputpath', { type: 'string', alias: 'o', desc: 'The output path for the migrated cdk code'})
.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' }),
)
.command('context', 'Manage cached context values', (yargs: Argv) => yargs
.option('reset', { alias: 'e', desc: 'The context key (or its index) to reset', type: 'string', requiresArg: true })
.option('force', { alias: 'f', desc: 'Ignore missing key error', type: 'boolean', default: false })
Expand Down Expand Up @@ -649,6 +656,9 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
} else {
return cliInit(args.TEMPLATE, language, undefined, args.generateOnly);
}
case 'migrate':
const migrateLanguage = configuration.settings.get(['language']);
return cliMigrate(args.inputpath, migrateLanguage, args.generateOnly, argv.outputpath);
case 'version':
return data(version.DISPLAY_VERSION);

Expand Down
71 changes: 71 additions & 0 deletions packages/aws-cdk/lib/commands/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as fs from 'fs';
import * as path from 'path';
import { initializeProject, availableInitTemplates } from '../../lib/init';
import { warning } from '../logging';

// replace this string with the path to your own noctilucent pkg
import * as nocti from '/Users/bobertzh/work/noctilucent/noctilucent/pkg/noctilucent';

export async function cliMigrate(
inputpath: string = process.cwd() + '/../temp.txt',
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the chance of (eventual) name collision?

This hardcodes a bunch of input that should be accepted from the command line. Will this actually output things in "~/my-dir" if the cli comand was cdk migrate -l java -inputpath ~/template.json -outputpath ~/my-dir?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup it works, these are just setting the default. We could just delete the /../temp.txt default for the input path if we wanted, I just left it in there for now

language?: string,
generateOnly = false,
outputpath = process.cwd(),
) {
const type = 'default'; // "default" is the default type (and maps to 'app')
Copy link
Contributor

Choose a reason for hiding this comment

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

I had to double-take: why is this an option here, but not exposed as an argument?
Even if right now it has a single value, it can be expanded in the future (based on the comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah so this is basically just to get the init funcitonality to work. I'm happy to just pass the hardcoded string into the initliazeproject but I thought I'd be explicit about wat the default meant.

const template = (await availableInitTemplates()).find(t => t.hasName(type!));
if (!template) {
throw new Error(`couldn't find template for ${type} app type, this should never happen`);
}

if (!language) {
language = 'typescript';
warning(`No --language was provided, defaulting to --language=${language}`);
}

await initializeProject(template, language, true, generateOnly, outputpath);
const template_file = fs.readFileSync(inputpath, 'utf8');
const generated_app = nocti.transmute(template_file, language);

// clear out the init'd bin/lib files to replace with our own
delete_files(outputpath + '/lib/');

// we hardcode everything to be called noctstack still so this works for now.
// Will change this to be much smarter once we can change stack name in noct
const bin_app = `#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { NoctStack } from '../lib/generated_stack';

const app = new cdk.App();
new NoctStack(app, 'NoctStack', {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */

/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },

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

/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});`;
const myname = path.basename(outputpath);
warning(`path name = ${myname}`)
fs.writeFileSync(outputpath + '/lib/' + 'generated_stack.ts', generated_app);
fs.writeFileSync(outputpath + '/bin/' + `${myname}.ts`, bin_app);
}

function delete_files(filepath: string) {
fs.readdir(filepath, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(filepath + file, (err) => {
if (err) throw err;
});
}
});
}
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export async function printAvailableTemplates(language?: string) {
}
}

async function initializeProject(template: InitTemplate, language: string, canUseNetwork: boolean, generateOnly: boolean, workDir: string) {
export async function initializeProject(template: InitTemplate, language: string, canUseNetwork: boolean, generateOnly: boolean, workDir: string) {
await assertIsEmptyDirectory(workDir);
print(`Applying project template ${chalk.green(template.name)} for ${chalk.blue(language)}`);
await template.install(language, workDir);
Expand Down