From b4322334c823e044623f3e3238f6ff6b5d053569 Mon Sep 17 00:00:00 2001 From: Madeline Kusters <80541297+madeline-k@users.noreply.github.com> Date: Thu, 2 Feb 2023 07:52:51 -0800 Subject: [PATCH 1/3] generate index.ts and .jsiirc.json files --- .../cfn2ts/lib/augmentation-generator.ts | 2 +- .../cfn2ts/lib/canned-metrics-generator.ts | 2 +- tools/@aws-cdk/cfn2ts/lib/index.ts | 39 +++++++++++++++++-- .../packages/aws-cdk-lib/scripts/gen.ts | 2 +- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/tools/@aws-cdk/cfn2ts/lib/augmentation-generator.ts b/tools/@aws-cdk/cfn2ts/lib/augmentation-generator.ts index ec7500f05a1dc..a4392b69fa0f9 100644 --- a/tools/@aws-cdk/cfn2ts/lib/augmentation-generator.ts +++ b/tools/@aws-cdk/cfn2ts/lib/augmentation-generator.ts @@ -33,8 +33,8 @@ import { SpecName } from './spec-utils'; * ``` */ export class AugmentationGenerator { + public readonly outputFile: string; private readonly code = new CodeMaker(); - private readonly outputFile: string; constructor(moduleName: string, private readonly spec: schema.Specification, private readonly affix: string) { this.outputFile = `${moduleName}-augmentations.generated.ts`; diff --git a/tools/@aws-cdk/cfn2ts/lib/canned-metrics-generator.ts b/tools/@aws-cdk/cfn2ts/lib/canned-metrics-generator.ts index facb1c4aff476..93bed92b35e65 100644 --- a/tools/@aws-cdk/cfn2ts/lib/canned-metrics-generator.ts +++ b/tools/@aws-cdk/cfn2ts/lib/canned-metrics-generator.ts @@ -25,8 +25,8 @@ import { CodeMaker, toCamelCase } from 'codemaker'; * ``` */ export class CannedMetricsGenerator { + public readonly outputFile: string; private readonly code = new CodeMaker({ indentationLevel: 2 }); - private readonly outputFile: string; constructor(moduleName: string, private readonly namespace: string) { this.outputFile = `${moduleName}-canned-metrics.generated.ts`; diff --git a/tools/@aws-cdk/cfn2ts/lib/index.ts b/tools/@aws-cdk/cfn2ts/lib/index.ts index 01817c0574ca6..165d64c58a124 100644 --- a/tools/@aws-cdk/cfn2ts/lib/index.ts +++ b/tools/@aws-cdk/cfn2ts/lib/index.ts @@ -47,6 +47,7 @@ export async function generateAll(outPath: string, options: CodeGeneratorOptions const spec = cfnSpec.filteredSpecification(s => s.startsWith(`${scope}::`)); const module = pkglint.createModuleDefinitionFromCfnNamespace(scope); const packagePath = path.join(outPath, module.moduleName); + const libPath = path.join(packagePath, '/lib'); if (Object.keys(spec.ResourceTypes).length === 0) { throw new Error(`No resource was found for scope ${scope}`); @@ -56,16 +57,48 @@ export async function generateAll(outPath: string, options: CodeGeneratorOptions const generator = new CodeGenerator(name, spec, affix, options); generator.emitCode(); - await generator.save(packagePath); + await generator.save(libPath); + const outputFiles = [generator.outputFile]; const augs = new AugmentationGenerator(name, spec, affix); if (augs.emitCode()) { - await augs.save(packagePath); + await augs.save(libPath); + outputFiles.push(augs.outputFile); } const canned = new CannedMetricsGenerator(name, scope); if (canned.generate()) { - await canned.save(packagePath); + await canned.save(libPath); + outputFiles.push(canned.outputFile); + } + + // Create index.ts files if needed + if (!fs.existsSync(path.join(packagePath, 'index.ts'))) { + await fs.writeFile(path.join(packagePath, 'index.ts'), 'export * from ./lib;\n'); + } + if (!fs.existsSync(path.join(libPath, 'index.ts'))) { + const lines = [`// ${scope} CloudFormation Resources:`]; + lines.push(...outputFiles.map((f) => `export * from './${f.replace('.ts', '')}'`)); + + await fs.writeFile(path.join(libPath, 'index.ts'), lines.join('\n') + '\n'); + } + + // Create .jsiirc.json files if needed + if (!fs.existsSync(path.join(packagePath, '.jsiirc.json'))) { + const jsiirc = { + targets: { + java: { + package: module.javaPackage, + }, + dotnet: { + package: module.dotnetPackage, + }, + python: { + module: module.pythonModuleName, + }, + }, + }; + await fs.writeJson(path.join(packagePath, '.jsiirc.json'), jsiirc, { spaces: 2 }); } } } diff --git a/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts b/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts index fe61a3ee927fb..f6679573ea8f6 100644 --- a/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts +++ b/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts @@ -1,7 +1,7 @@ import { generateAll } from '@aws-cdk/cfn2ts'; import * as path from 'path'; -const srcDir = path.join(__dirname, '..', 'lib'); +const srcDir = path.join(__dirname, '..'); generateAll(srcDir, { coreImport: 'aws-cdk-lib', }); From 6fdfe1eaac73ec64ee4c10a5f801e9b6a956dcf0 Mon Sep 17 00:00:00 2001 From: Madeline Kusters <80541297+madeline-k@users.noreply.github.com> Date: Thu, 2 Feb 2023 08:10:50 -0800 Subject: [PATCH 2/3] fix core import --- .../remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts b/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts index f6679573ea8f6..6773cd29a0ac0 100644 --- a/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts +++ b/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts @@ -3,5 +3,5 @@ import * as path from 'path'; const srcDir = path.join(__dirname, '..'); generateAll(srcDir, { - coreImport: 'aws-cdk-lib', + coreImport: '../../core', }); From 4b80e0865e6c680ef3c7271a074694c5dc06cdd8 Mon Sep 17 00:00:00 2001 From: Madeline Kusters <80541297+madeline-k@users.noreply.github.com> Date: Thu, 2 Feb 2023 14:30:01 -0800 Subject: [PATCH 3/3] update based on feedback: --- tools/@aws-cdk/cfn2ts/lib/index.ts | 16 ++++++---------- .../template/packages/aws-cdk-lib/scripts/gen.ts | 2 +- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/tools/@aws-cdk/cfn2ts/lib/index.ts b/tools/@aws-cdk/cfn2ts/lib/index.ts index 165d64c58a124..3cfcfd1f5703e 100644 --- a/tools/@aws-cdk/cfn2ts/lib/index.ts +++ b/tools/@aws-cdk/cfn2ts/lib/index.ts @@ -47,7 +47,6 @@ export async function generateAll(outPath: string, options: CodeGeneratorOptions const spec = cfnSpec.filteredSpecification(s => s.startsWith(`${scope}::`)); const module = pkglint.createModuleDefinitionFromCfnNamespace(scope); const packagePath = path.join(outPath, module.moduleName); - const libPath = path.join(packagePath, '/lib'); if (Object.keys(spec.ResourceTypes).length === 0) { throw new Error(`No resource was found for scope ${scope}`); @@ -57,33 +56,30 @@ export async function generateAll(outPath: string, options: CodeGeneratorOptions const generator = new CodeGenerator(name, spec, affix, options); generator.emitCode(); - await generator.save(libPath); + await generator.save(packagePath); const outputFiles = [generator.outputFile]; const augs = new AugmentationGenerator(name, spec, affix); if (augs.emitCode()) { - await augs.save(libPath); + await augs.save(packagePath); outputFiles.push(augs.outputFile); } const canned = new CannedMetricsGenerator(name, scope); if (canned.generate()) { - await canned.save(libPath); + await canned.save(packagePath); outputFiles.push(canned.outputFile); } - // Create index.ts files if needed + // Create index.ts file if needed if (!fs.existsSync(path.join(packagePath, 'index.ts'))) { - await fs.writeFile(path.join(packagePath, 'index.ts'), 'export * from ./lib;\n'); - } - if (!fs.existsSync(path.join(libPath, 'index.ts'))) { const lines = [`// ${scope} CloudFormation Resources:`]; lines.push(...outputFiles.map((f) => `export * from './${f.replace('.ts', '')}'`)); - await fs.writeFile(path.join(libPath, 'index.ts'), lines.join('\n') + '\n'); + await fs.writeFile(path.join(packagePath, 'index.ts'), lines.join('\n') + '\n'); } - // Create .jsiirc.json files if needed + // Create .jsiirc.json file if needed if (!fs.existsSync(path.join(packagePath, '.jsiirc.json'))) { const jsiirc = { targets: { diff --git a/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts b/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts index 6773cd29a0ac0..1d5005be646df 100644 --- a/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts +++ b/tools/@aws-cdk/remodel/lib/template/packages/aws-cdk-lib/scripts/gen.ts @@ -1,7 +1,7 @@ import { generateAll } from '@aws-cdk/cfn2ts'; import * as path from 'path'; -const srcDir = path.join(__dirname, '..'); +const srcDir = path.join(__dirname, '..', 'lib'); generateAll(srcDir, { coreImport: '../../core', });