Skip to content

Commit 5fc1b13

Browse files
rohang9000github-actionsiankhou
authored
feat: add local template support with --from-path option (#763)
This PR adds local template support through the new `--from-path` CLI option. Users can initialize CDK projects from local custom templates, copying the template as is without placeholder processing. The feature includes language auto-detection when templates contain a single language option and error handling for invalid paths. 3 new test cases covering basic local template functionality, language auto-detection, and error handling scenarios were added. Fixes # --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license --------- Signed-off-by: github-actions <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: Ian Hou <[email protected]>
1 parent 5128946 commit 5fc1b13

File tree

11 files changed

+986
-94
lines changed

11 files changed

+986
-94
lines changed

packages/@aws-cdk/user-input-gen/lib/yargs-gen.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ function makeYargs(config: CliConfig, helpers: CliHelpers): Statement {
109109
commandCallArgs.push(optionsExpr);
110110
}
111111

112+
// Add implies calls if present
113+
if (commandFacts.implies) {
114+
for (const [key, value] of Object.entries(commandFacts.implies)) {
115+
optionsExpr = optionsExpr.callMethod('implies', lit(key), lit(value));
116+
}
117+
}
118+
112119
yargsExpr = yargsExpr.callMethod('command', ...commandCallArgs);
113120
}
114121

packages/@aws-cdk/user-input-gen/lib/yargs-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface YargsCommand {
77

88
export interface CliAction extends YargsCommand {
99
options?: { [optionName: string]: CliOption };
10+
implies?: { [key: string]: string };
1011
}
1112

1213
interface YargsArg {

packages/aws-cdk/lib/cli/cli-config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,11 @@ export async function makeConfig(): Promise<CliConfig> {
400400
'language': { type: 'string', alias: 'l', desc: 'The language to be used for the new project (default can be configured in ~/.cdk.json)', choices: await availableInitLanguages() },
401401
'list': { type: 'boolean', desc: 'List the available templates' },
402402
'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' },
403-
'lib-version': { type: 'string', alias: 'V', default: undefined, desc: 'The version of the CDK library (aws-cdk-lib) to initialize the project with. Defaults to the version that was current when this CLI was built.' },
403+
'lib-version': { type: 'string', alias: 'V', default: undefined, desc: 'The version of the CDK library (aws-cdk-lib) to initialize built-in templates with. Defaults to the version that was current when this CLI was built.' },
404+
'from-path': { type: 'string', desc: 'Path to a local custom template directory or multi-template repository', requiresArg: true, conflicts: ['lib-version'] },
405+
'template-path': { type: 'string', desc: 'Path to a specific template within a multi-template repository', requiresArg: true },
404406
},
407+
implies: { 'template-path': 'from-path' },
405408
},
406409
'migrate': {
407410
description: 'Migrate existing AWS resources into a CDK app',

packages/aws-cdk/lib/cli/cli-type-registry.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,24 @@
865865
"lib-version": {
866866
"type": "string",
867867
"alias": "V",
868-
"desc": "The version of the CDK library (aws-cdk-lib) to initialize the project with. Defaults to the version that was current when this CLI was built."
868+
"desc": "The version of the CDK library (aws-cdk-lib) to initialize built-in templates with. Defaults to the version that was current when this CLI was built."
869+
},
870+
"from-path": {
871+
"type": "string",
872+
"desc": "Path to a local custom template directory or multi-template repository",
873+
"requiresArg": true,
874+
"conflicts": [
875+
"lib-version"
876+
]
877+
},
878+
"template-path": {
879+
"type": "string",
880+
"desc": "Path to a specific template within a multi-template repository",
881+
"requiresArg": true
869882
}
883+
},
884+
"implies": {
885+
"template-path": "from-path"
870886
}
871887
},
872888
"migrate": {

packages/aws-cdk/lib/cli/cli.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,19 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
516516
if (args.list) {
517517
return printAvailableTemplates(ioHelper, language);
518518
} else {
519+
// Gate custom template support with unstable flag
520+
if (args['from-path'] && !configuration.settings.get(['unstable']).includes('init')) {
521+
throw new ToolkitError('Unstable feature use: \'init\' with custom templates is unstable. It must be opted in via \'--unstable\', e.g. \'cdk init --from-path=./my-template --unstable=init\'');
522+
}
519523
return cliInit({
520524
ioHelper,
521525
type: args.TEMPLATE,
522526
language,
523527
canUseNetwork: undefined,
524528
generateOnly: args.generateOnly,
525529
libVersion: args.libVersion,
530+
fromPath: args['from-path'],
531+
templatePath: args['template-path'],
526532
});
527533
}
528534
case 'migrate':

packages/aws-cdk/lib/cli/convert-to-user-input.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ export function convertYargsToUserInput(args: any): UserInput {
245245
list: args.list,
246246
generateOnly: args.generateOnly,
247247
libVersion: args.libVersion,
248+
fromPath: args.fromPath,
249+
templatePath: args.templatePath,
248250
TEMPLATE: args.TEMPLATE,
249251
};
250252
break;
@@ -481,6 +483,8 @@ export function convertConfigToUserInput(config: any): UserInput {
481483
list: config.init?.list,
482484
generateOnly: config.init?.generateOnly,
483485
libVersion: config.init?.libVersion,
486+
fromPath: config.init?.fromPath,
487+
templatePath: config.init?.templatePath,
484488
};
485489
const migrateOptions = {
486490
stackName: config.migrate?.stackName,

packages/aws-cdk/lib/cli/parse-command-line-arguments.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,20 @@ export function parseCommandLineArguments(args: Array<string>): any {
862862
default: undefined,
863863
type: 'string',
864864
alias: 'V',
865-
desc: 'The version of the CDK library (aws-cdk-lib) to initialize the project with. Defaults to the version that was current when this CLI was built.',
865+
desc: 'The version of the CDK library (aws-cdk-lib) to initialize built-in templates with. Defaults to the version that was current when this CLI was built.',
866+
})
867+
.option('from-path', {
868+
default: undefined,
869+
type: 'string',
870+
desc: 'Path to a local custom template directory or multi-template repository',
871+
requiresArg: true,
872+
conflicts: ['lib-version'],
873+
})
874+
.option('template-path', {
875+
default: undefined,
876+
type: 'string',
877+
desc: 'Path to a specific template within a multi-template repository',
878+
requiresArg: true,
866879
}),
867880
)
868881
.command('migrate', 'Migrate existing AWS resources into a CDK app', (yargs: Argv) =>

packages/aws-cdk/lib/cli/user-input.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,14 +1363,28 @@ export interface InitOptions {
13631363
readonly generateOnly?: boolean;
13641364

13651365
/**
1366-
* The version of the CDK library (aws-cdk-lib) to initialize the project with. Defaults to the version that was current when this CLI was built.
1366+
* The version of the CDK library (aws-cdk-lib) to initialize built-in templates with. Defaults to the version that was current when this CLI was built.
13671367
*
13681368
* aliases: V
13691369
*
13701370
* @default - undefined
13711371
*/
13721372
readonly libVersion?: string;
13731373

1374+
/**
1375+
* Path to a local custom template directory or multi-template repository
1376+
*
1377+
* @default - undefined
1378+
*/
1379+
readonly fromPath?: string;
1380+
1381+
/**
1382+
* Path to a specific template within a multi-template repository
1383+
*
1384+
* @default - undefined
1385+
*/
1386+
readonly templatePath?: string;
1387+
13741388
/**
13751389
* Positional argument for init
13761390
*/

0 commit comments

Comments
 (0)