From b0e9ba46ecf92a874e27b824bb0db56054f17e20 Mon Sep 17 00:00:00 2001 From: Francis Date: Fri, 8 Dec 2023 00:54:00 -0800 Subject: [PATCH] core internal Signed-off-by: Francis --- .../lib/custom-resource-framework/classes.ts | 33 +++++++++++++++---- .../custom-resource-framework/framework.ts | 16 ++++++++- .../lib/custom-resource-framework/modules.ts | 26 +++++++++++++++ .../scripts/generate.ts | 4 +-- .../airlift-custom-resource-handlers.sh | 1 - 5 files changed, 70 insertions(+), 10 deletions(-) diff --git a/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/classes.ts b/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/classes.ts index 71ad5d98da55d..4ea6fb4c9d89d 100644 --- a/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/classes.ts +++ b/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/classes.ts @@ -15,7 +15,14 @@ import { Expression, } from '@cdklabs/typewriter'; import { CdkHandlerFrameworkModule } from './framework'; -import { CONSTRUCTS_MODULE, LAMBDA_MODULE, CORE_MODULE } from './modules'; +import { + CONSTRUCTS_MODULE, + LAMBDA_MODULE, + CORE_MODULE, + STACK, + CUSTOM_RESOURCE_PROVIDER_BASE, + CUSTOM_RESOURCE_PROVIDER_OPTIONS, +} from './modules'; import { Runtime } from './runtime'; /** @@ -171,15 +178,23 @@ export abstract class CdkHandlerFrameworkClass extends ClassType { */ public static buildCdkCustomResourceProvider(scope: CdkHandlerFrameworkModule, props: CdkHandlerClassProps): CdkHandlerFrameworkClass { return new (class CdkCustomResourceProvider extends CdkHandlerFrameworkClass { - protected readonly externalModules = [CONSTRUCTS_MODULE, CORE_MODULE]; + protected readonly externalModules: ExternalModule[] = [CONSTRUCTS_MODULE]; public constructor() { super(scope, { name: props.name, - extends: CORE_MODULE.CustomResourceProviderBase, + extends: scope.coreInternal + ? CUSTOM_RESOURCE_PROVIDER_BASE.CustomResourceProviderBase + : CORE_MODULE.CustomResourceProviderBase, export: true, }); + if (scope.coreInternal) { + this.externalModules.push(...[STACK, CUSTOM_RESOURCE_PROVIDER_BASE, CUSTOM_RESOURCE_PROVIDER_OPTIONS]); + } else { + this.externalModules.push(CORE_MODULE); + } + this.externalModules.forEach(module => scope.addExternalModule(module)); const getOrCreateMethod = this.addMethod({ @@ -200,7 +215,9 @@ export abstract class CdkHandlerFrameworkClass extends ClassType { }); getOrCreateMethod.addParameter({ name: 'props', - type: CORE_MODULE.CustomResourceProviderOptions, + type: scope.coreInternal + ? CUSTOM_RESOURCE_PROVIDER_OPTIONS.CustomResourceProviderOptions + : CORE_MODULE.CustomResourceProviderOptions, optional: true, }); getOrCreateMethod.addBody( @@ -225,7 +242,9 @@ export abstract class CdkHandlerFrameworkClass extends ClassType { }); getOrCreateProviderMethod.addParameter({ name: 'props', - type: CORE_MODULE.CustomResourceProviderOptions, + type: scope.coreInternal + ? CUSTOM_RESOURCE_PROVIDER_OPTIONS.CustomResourceProviderOptions + : CORE_MODULE.CustomResourceProviderOptions, optional: true, }); getOrCreateProviderMethod.addBody( @@ -241,7 +260,9 @@ export abstract class CdkHandlerFrameworkClass extends ClassType { ['runtimeName', expr.lit(props.runtime.name)], ]); this.buildConstructor({ - constructorPropsType: CORE_MODULE.CustomResourceProviderOptions, + constructorPropsType: scope.coreInternal + ? CUSTOM_RESOURCE_PROVIDER_OPTIONS.CustomResourceProviderOptions + : CORE_MODULE.CustomResourceProviderOptions, superProps, constructorVisbility: MemberVisibility.Private, optionalConstructorProps: true, diff --git a/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/framework.ts b/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/framework.ts index 481c05017be17..4cd7dbf3e5193 100644 --- a/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/framework.ts +++ b/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/framework.ts @@ -3,7 +3,7 @@ import { ExternalModule, InterfaceType, Module, TypeScriptRenderer } from '@cdkl import * as fs from 'fs-extra'; import { CdkHandlerClassProps, CdkHandlerFrameworkClass } from './classes'; import { ComponentType, ConfigProps } from './config'; -import { CONSTRUCTS_MODULE, CORE_MODULE, LAMBDA_MODULE, PATH_MODULE } from './modules'; +import { CONSTRUCTS_MODULE, CORE_MODULE, CUSTOM_RESOURCE_PROVIDER_BASE, CUSTOM_RESOURCE_PROVIDER_OPTIONS, LAMBDA_MODULE, PATH_MODULE, STACK } from './modules'; import { Runtime } from './runtime'; import { RuntimeDeterminer } from './runtime-determiner'; @@ -16,10 +16,12 @@ export class CdkHandlerFrameworkModule extends Module { private readonly renderer = new TypeScriptRenderer(); private readonly externalModules = new Map(); private readonly _interfaces = new Map(); + public readonly coreInternal: boolean; private hasComponents = false; public constructor(fqn: string) { super(fqn); + this.coreInternal = fqn.includes('core'); } /** @@ -105,6 +107,18 @@ export class CdkHandlerFrameworkModule extends Module { ]); break; } + case STACK.fqn: { + STACK.importSelective(this, ['Stack']); + break; + } + case CUSTOM_RESOURCE_PROVIDER_BASE.fqn: { + CUSTOM_RESOURCE_PROVIDER_BASE.importSelective(this, ['CustomResourceProviderBase']); + break; + } + case CUSTOM_RESOURCE_PROVIDER_OPTIONS.fqn: { + CUSTOM_RESOURCE_PROVIDER_OPTIONS.importSelective(this, ['CustomResourceProviderOptions']); + break; + } case LAMBDA_MODULE.fqn: { LAMBDA_MODULE.import(this, 'lambda'); break; diff --git a/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/modules.ts b/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/modules.ts index a7ed871696d15..f53bbc4883da4 100644 --- a/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/modules.ts +++ b/packages/@aws-cdk/custom-resource-handlers/lib/custom-resource-framework/modules.ts @@ -24,6 +24,28 @@ class CoreModule extends ExternalModule { } } +class Stack extends ExternalModule { + public constructor() { + super('../../lib/stack'); + } +} + +class CustomResourceProviderBase extends ExternalModule { + public readonly CustomResourceProviderBase = Type.fromName(this, 'CustomResourceProviderBase'); + + public constructor() { + super('../../lib/custom-resource-provider/custom-resource-provider-base'); + } +} + +class CustomResourceProviderOptions extends ExternalModule { + public readonly CustomResourceProviderOptions = Type.fromName(this, 'CustomResourceProviderOptions'); + + public constructor() { + super('../../lib/custom-resource-provider/shared'); + } +} + class LambdaModule extends ExternalModule { public readonly Function = Type.fromName(this, 'Function'); public readonly SingletonFunction = Type.fromName(this, 'SingletonFunction'); @@ -38,3 +60,7 @@ export const PATH_MODULE = new PathModule(); export const CONSTRUCTS_MODULE = new ConstructsModule(); export const CORE_MODULE = new CoreModule(); export const LAMBDA_MODULE = new LambdaModule(); + +export const STACK = new Stack(); +export const CUSTOM_RESOURCE_PROVIDER_BASE = new CustomResourceProviderBase(); +export const CUSTOM_RESOURCE_PROVIDER_OPTIONS = new CustomResourceProviderOptions(); diff --git a/packages/@aws-cdk/custom-resource-handlers/scripts/generate.ts b/packages/@aws-cdk/custom-resource-handlers/scripts/generate.ts index 57740c0fa2b00..b7ac3e9ee6d20 100644 --- a/packages/@aws-cdk/custom-resource-handlers/scripts/generate.ts +++ b/packages/@aws-cdk/custom-resource-handlers/scripts/generate.ts @@ -16,7 +16,7 @@ async function main() { fs.mkdirSync(path.dirname(outfile), { recursive: true }); fs.copyFileSync(component.sourceCode, outfile); } else { - await bundleAndMinify(component.sourceCode, outfile); + await minifyAndBundle(component.sourceCode, outfile); } const sourceCodeDirectory = path.dirname(outfile).split('/').pop(); module.build(component, `${sourceCodeDirectory}`); @@ -43,7 +43,7 @@ async function main() { } } -async function bundleAndMinify(infile: string, outfile: string) { +async function minifyAndBundle(infile: string, outfile: string) { const result = await esbuild.build({ entryPoints: [infile], outfile, diff --git a/packages/aws-cdk-lib/scripts/airlift-custom-resource-handlers.sh b/packages/aws-cdk-lib/scripts/airlift-custom-resource-handlers.sh index e4786e5119cbe..63a6ae5091272 100755 --- a/packages/aws-cdk-lib/scripts/airlift-custom-resource-handlers.sh +++ b/packages/aws-cdk-lib/scripts/airlift-custom-resource-handlers.sh @@ -5,7 +5,6 @@ awscdklibdir=${scriptdir}/.. customresourcedir=$(node -p "path.dirname(require.resolve('@aws-cdk/custom-resource-handlers/package.json'))") function airlift() { - echo $1 # core needs to be airlifted directly to core to prevent circular dependencies if [[ $1 = dist/core/* || $1 = dist/core ]]; then