Skip to content

Commit 7445176

Browse files
authored
chore(spec2cdk): refactor for mixins (#35999)
### Reason for this change Refactoring some spec2cdk code to make it more re-usable for mixins codegen. ### Description of changes - Updates `jsii` and `@cdklabs/typewriter` to the latest versions - Fixes multiple issue (and typos) in `@aws-cdk/custom-resource-handlers` that arouse after the typewriter update. It's a private package and the changed generated code is also private, so this is safe. - Refactor `spec2cdk` code to make it more reusable. ### Describe any new or updated permissions being added n/a ### Description of how you validated changes Existing tests passing. Additionally compared generated code by hand. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 0d773b1 commit 7445176

File tree

29 files changed

+275
-164
lines changed

29 files changed

+275
-164
lines changed

packages/@aws-cdk/custom-resource-handlers/lib/custom-resources-framework/classes.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ interface ConstructorBuildProps {
5959
readonly optionalConstructorProps?: boolean;
6060

6161
/**
62-
* Visbility for the constructor.
62+
* Visibility for the constructor.
6363
*
64-
* @default MemberVisbility.Public
64+
* @default MemberVisibility.Public
6565
*/
66-
readonly constructorVisbility?: MemberVisibility;
66+
readonly constructorVisibility?: MemberVisibility;
6767

6868
/**
6969
* These statements are added to the constructor body in the order they appear in this property.
@@ -98,6 +98,13 @@ export interface HandlerFrameworkClassProps {
9898
* @default - the latest Lambda runtime available in the region.
9999
*/
100100
readonly runtime?: Runtime;
101+
102+
/**
103+
* Visibility for the constructor.
104+
*
105+
* @default MemberVisibility.Public
106+
*/
107+
readonly constructorVisibility?: MemberVisibility;
101108
}
102109

103110
interface BuildRuntimePropertyOptions {
@@ -134,13 +141,13 @@ export abstract class HandlerFrameworkClass extends ClassType {
134141
['runtime', this.buildRuntimeProperty(scope, { runtime: props.runtime })],
135142
]);
136143
const metadataStatements: Statement[] = [
137-
expr.directCode(`this.node.addMetadata('${CUSTOM_RESOURCE_RUNTIME_FAMILY}', this.runtime.family)`),
144+
stmt.directCode(`this.node.addMetadata('${CUSTOM_RESOURCE_RUNTIME_FAMILY}', this.runtime.family)`),
138145
];
139146
this.buildConstructor({
140147
constructorPropsType: LAMBDA_MODULE.FunctionOptions,
141148
superProps,
142149
optionalConstructorProps: true,
143-
constructorVisbility: MemberVisibility.Public,
150+
constructorVisibility: MemberVisibility.Public,
144151
statements: metadataStatements,
145152
});
146153
}
@@ -226,16 +233,16 @@ export abstract class HandlerFrameworkClass extends ClassType {
226233
['runtime', this.buildRuntimeProperty(scope, { runtime: props.runtime, isEvalNodejsProvider })],
227234
]);
228235
const metadataStatements: Statement[] = [
229-
expr.directCode(`this.addMetadata('${CUSTOM_RESOURCE_SINGLETON}', true)`),
230-
expr.directCode(`this.addMetadata('${CUSTOM_RESOURCE_RUNTIME_FAMILY}', this.runtime.family)`),
231-
expr.directCode(`if (props?.logGroup) { this.logGroup.node.addMetadata('${CUSTOM_RESOURCE_SINGLETON_LOG_GROUP}', true) }`),
236+
stmt.directCode(`this.addMetadata('${CUSTOM_RESOURCE_SINGLETON}', true)`),
237+
stmt.directCode(`this.addMetadata('${CUSTOM_RESOURCE_RUNTIME_FAMILY}', this.runtime.family)`),
238+
stmt.directCode(`if (props?.logGroup) { this.logGroup.node.addMetadata('${CUSTOM_RESOURCE_SINGLETON_LOG_GROUP}', true) }`),
232239
// We need to access the private `_logRetention` custom resource, the only public property - `logGroup` - provides an ARN reference to the resource, instead of the resource itself.
233-
expr.directCode(`if (props?.logRetention) { ((this as any).lambdaFunction as lambda.Function)._logRetention?.node.addMetadata('${CUSTOM_RESOURCE_SINGLETON_LOG_RETENTION}', true) }`),
240+
stmt.directCode(`if (props?.logRetention) { ((this as any).lambdaFunction as lambda.Function)._logRetention?.node.addMetadata('${CUSTOM_RESOURCE_SINGLETON_LOG_RETENTION}', true) }`),
234241
];
235242
this.buildConstructor({
236243
constructorPropsType: _interface.type,
237244
superProps,
238-
constructorVisbility: MemberVisibility.Public,
245+
constructorVisibility: MemberVisibility.Public,
239246
statements: metadataStatements,
240247
});
241248
}
@@ -337,11 +344,11 @@ export abstract class HandlerFrameworkClass extends ClassType {
337344
isCustomResourceProvider: true,
338345
})],
339346
]);
340-
const metadataStatements: Statement[] = [expr.directCode(`this.node.addMetadata('${CUSTOM_RESOURCE_PROVIDER}', true)`)];
347+
const metadataStatements: Statement[] = [stmt.directCode(`this.node.addMetadata('${CUSTOM_RESOURCE_PROVIDER}', true)`)];
341348
this.buildConstructor({
342349
constructorPropsType: CORE_MODULE.CustomResourceProviderOptions,
343350
superProps,
344-
constructorVisbility: MemberVisibility.Private,
351+
constructorVisibility: props.constructorVisibility ?? MemberVisibility.Private,
345352
optionalConstructorProps: true,
346353
statements: metadataStatements,
347354
});
@@ -370,7 +377,7 @@ export abstract class HandlerFrameworkClass extends ClassType {
370377

371378
private buildConstructor(props: ConstructorBuildProps) {
372379
const init = this.addInitializer({
373-
visibility: props.constructorVisbility,
380+
visibility: props.constructorVisibility,
374381
});
375382
const scope = init.addParameter({
376383
name: 'scope',

packages/@aws-cdk/custom-resource-handlers/lib/custom-resources-framework/config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/* eslint-disable import/no-extraneous-dependencies */
21
import * as path from 'path';
2+
/* eslint-disable import/no-extraneous-dependencies */
3+
import { MemberVisibility } from '@cdklabs/typewriter';
34

45
/**
56
* Handler framework runtimes used for code generation.
@@ -97,6 +98,13 @@ export interface ComponentProps {
9798
* @default true
9899
*/
99100
readonly minifyAndBundle?: boolean;
101+
102+
/**
103+
* Visibility for the constructor.
104+
*
105+
* @default MemberVisibility.Public
106+
*/
107+
readonly constructorVisibility?: MemberVisibility;
100108
}
101109

102110
export type HandlerFrameworkConfig = { [module: string]: { [identifier: string]: ComponentProps[] } };
@@ -341,6 +349,7 @@ export const config: HandlerFrameworkConfig = {
341349
{
342350
type: ComponentType.CUSTOM_RESOURCE_PROVIDER,
343351
sourceCode: path.resolve(__dirname, '..', 'core', 'cross-region-ssm-writer-handler', 'index.ts'),
352+
constructorVisibility: MemberVisibility.Public,
344353
},
345354
],
346355
'cross-region-ssm-reader-provider': [

packages/@aws-cdk/custom-resource-handlers/lib/custom-resources-framework/framework.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class HandlerFrameworkModule extends Module {
5454
handler,
5555
codeDirectory,
5656
runtime: component.runtime,
57+
constructorVisibility: component.constructorVisibility,
5758
};
5859

5960
switch (component.type) {

packages/@aws-cdk/custom-resource-handlers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@types/jest": "^29.5.14",
4949
"aws-sdk-client-mock": "4.1.0",
5050
"aws-sdk-client-mock-jest": "4.1.0",
51-
"@cdklabs/typewriter": "^0.0.6",
51+
"@cdklabs/typewriter": "^0.0.10",
5252
"jest": "^29.7.0",
5353
"sinon": "^9.2.4",
5454
"nock": "^13.5.6",

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/node-runtime/custom-resource-provider-core.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export class TestProvider extends CustomResourceProviderBase {
2222
return existing ?? new TestProvider(stack, id, props);
2323
}
2424

25-
public constructor(scope: Construct, id: string, props?: CustomResourceProviderOptions) {
25+
private constructor(scope: Construct, id: string, props?: CustomResourceProviderOptions) {
2626
super(scope, id, {
2727
...props,
28-
"codeDirectory": path.join(__dirname, 'my-handler'),
29-
"runtimeName": determineLatestNodeRuntimeName(scope)
28+
codeDirectory: path.join(__dirname, 'my-handler'),
29+
runtimeName: determineLatestNodeRuntimeName(scope)
3030
});
3131
this.node.addMetadata('aws:cdk:is-custom-resource-handler-customResourceProvider', true);
3232
}

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/node-runtime/custom-resource-provider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export class TestProvider extends CustomResourceProviderBase {
2121
return existing ?? new TestProvider(stack, id, props);
2222
}
2323

24-
public constructor(scope: Construct, id: string, props?: CustomResourceProviderOptions) {
24+
private constructor(scope: Construct, id: string, props?: CustomResourceProviderOptions) {
2525
super(scope, id, {
2626
...props,
27-
"codeDirectory": path.join(__dirname, 'my-handler'),
28-
"runtimeName": determineLatestNodeRuntimeName(scope)
27+
codeDirectory: path.join(__dirname, 'my-handler'),
28+
runtimeName: determineLatestNodeRuntimeName(scope)
2929
});
3030
this.node.addMetadata('aws:cdk:is-custom-resource-handler-customResourceProvider', true);
3131
}

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/node-runtime/function.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export class TestFunction extends lambda.Function {
77
public constructor(scope: Construct, id: string, props?: lambda.FunctionOptions) {
88
super(scope, id, {
99
...props,
10-
"code": lambda.Code.fromAsset(path.join(__dirname, 'my-handler')),
11-
"handler": "index.handler",
12-
"runtime": lambda.determineLatestNodeRuntime(scope)
10+
code: lambda.Code.fromAsset(path.join(__dirname, 'my-handler')),
11+
handler: "index.handler",
12+
runtime: lambda.determineLatestNodeRuntime(scope)
1313
});
1414
this.node.addMetadata('aws:cdk:is-custom-resource-handler-runtime-family', this.runtime.family);
1515
}

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/node-runtime/singleton-function.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export class TestSingletonFunction extends lambda.SingletonFunction {
77
public constructor(scope: Construct, id: string, props: TestSingletonFunctionProps) {
88
super(scope, id, {
99
...props,
10-
"code": lambda.Code.fromAsset(path.join(__dirname, 'my-handler')),
11-
"handler": "index.handler",
12-
"runtime": lambda.determineLatestNodeRuntime(scope)
10+
code: lambda.Code.fromAsset(path.join(__dirname, 'my-handler')),
11+
handler: "index.handler",
12+
runtime: lambda.determineLatestNodeRuntime(scope)
1313
});
1414
this.addMetadata('aws:cdk:is-custom-resource-handler-singleton', true);
1515
this.addMetadata('aws:cdk:is-custom-resource-handler-runtime-family', this.runtime.family);

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/python-runtime/custom-resource-provider-core.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export class TestProvider extends CustomResourceProviderBase {
2222
return existing ?? new TestProvider(stack, id, props);
2323
}
2424

25-
public constructor(scope: Construct, id: string, props?: CustomResourceProviderOptions) {
25+
private constructor(scope: Construct, id: string, props?: CustomResourceProviderOptions) {
2626
super(scope, id, {
2727
...props,
28-
"codeDirectory": path.join(__dirname, 'my-handler'),
29-
"runtimeName": "python3.11"
28+
codeDirectory: path.join(__dirname, 'my-handler'),
29+
runtimeName: "python3.11"
3030
});
3131
this.node.addMetadata('aws:cdk:is-custom-resource-handler-customResourceProvider', true);
3232
}

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/python-runtime/custom-resource-provider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export class TestProvider extends CustomResourceProviderBase {
2121
return existing ?? new TestProvider(stack, id, props);
2222
}
2323

24-
public constructor(scope: Construct, id: string, props?: CustomResourceProviderOptions) {
24+
private constructor(scope: Construct, id: string, props?: CustomResourceProviderOptions) {
2525
super(scope, id, {
2626
...props,
27-
"codeDirectory": path.join(__dirname, 'my-handler'),
28-
"runtimeName": "python3.11"
27+
codeDirectory: path.join(__dirname, 'my-handler'),
28+
runtimeName: "python3.11"
2929
});
3030
this.node.addMetadata('aws:cdk:is-custom-resource-handler-customResourceProvider', true);
3131
}

0 commit comments

Comments
 (0)