Skip to content

Commit

Permalink
refactor and naming update
Browse files Browse the repository at this point in the history
Signed-off-by: Francis <colifran@amazon.com>
  • Loading branch information
colifran committed Dec 11, 2023
1 parent c3fdb61 commit 187c7df
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import {
CONSTRUCTS_MODULE,
LAMBDA_MODULE,
CORE_MODULE,
STACK,
CUSTOM_RESOURCE_PROVIDER_BASE,
CUSTOM_RESOURCE_PROVIDER_OPTIONS,
CORE_INTERNAL,
PATH_MODULE,
} from './modules';
import { Runtime } from './runtime';

Expand Down Expand Up @@ -86,7 +85,7 @@ export abstract class CdkCustomResourceClass extends ClassType {
*/
public static buildCdkFunction(scope: CdkCustomResourceModule, props: CdkCustomResourceClassProps): CdkCustomResourceClass {
return new (class CdkFunction extends CdkCustomResourceClass {
protected readonly externalModules = [CONSTRUCTS_MODULE, LAMBDA_MODULE];
protected readonly externalModules = [PATH_MODULE, CONSTRUCTS_MODULE, LAMBDA_MODULE];

public constructor() {
super(scope, {
Expand All @@ -95,7 +94,7 @@ export abstract class CdkCustomResourceClass extends ClassType {
export: true,
});

this.externalModules.forEach(module => scope.addExternalModule(module));
this.importExternalModulesInto(scope);

const superProps = new ObjectLiteral([
new Splat(expr.ident('props')),
Expand All @@ -118,7 +117,7 @@ export abstract class CdkCustomResourceClass extends ClassType {
*/
public static buildCdkSingletonFunction(scope: CdkCustomResourceModule, props: CdkCustomResourceClassProps): CdkCustomResourceClass {
return new (class CdkSingletonFunction extends CdkCustomResourceClass {
protected readonly externalModules = [CONSTRUCTS_MODULE, LAMBDA_MODULE];
protected readonly externalModules = [PATH_MODULE, CONSTRUCTS_MODULE, LAMBDA_MODULE];

public constructor() {
super(scope, {
Expand All @@ -127,7 +126,7 @@ export abstract class CdkCustomResourceClass extends ClassType {
export: true,
});

this.externalModules.forEach(module => scope.addExternalModule(module));
this.importExternalModulesInto(scope);

const uuid: PropertySpec = {
name: 'uuid',
Expand Down Expand Up @@ -176,24 +175,19 @@ export abstract class CdkCustomResourceClass extends ClassType {
*/
public static buildCdkCustomResourceProvider(scope: CdkCustomResourceModule, props: CdkCustomResourceClassProps): CdkCustomResourceClass {
return new (class CdkCustomResourceProvider extends CdkCustomResourceClass {
protected readonly externalModules: ExternalModule[] = [CONSTRUCTS_MODULE];
protected readonly externalModules: ExternalModule[] = [PATH_MODULE, CONSTRUCTS_MODULE];

public constructor() {
super(scope, {
name: props.name,
extends: scope.coreInternal
? CUSTOM_RESOURCE_PROVIDER_BASE.CustomResourceProviderBase
? CORE_INTERNAL.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));
this.externalModules.push(scope.coreInternal ? CORE_INTERNAL : CORE_MODULE);
this.importExternalModulesInto(scope);

const getOrCreateMethod = this.addMethod({
name: 'getOrCreate',
Expand All @@ -214,7 +208,7 @@ export abstract class CdkCustomResourceClass extends ClassType {
getOrCreateMethod.addParameter({
name: 'props',
type: scope.coreInternal
? CUSTOM_RESOURCE_PROVIDER_OPTIONS.CustomResourceProviderOptions
? CORE_INTERNAL.CustomResourceProviderOptions
: CORE_MODULE.CustomResourceProviderOptions,
optional: true,
});
Expand All @@ -241,7 +235,7 @@ export abstract class CdkCustomResourceClass extends ClassType {
getOrCreateProviderMethod.addParameter({
name: 'props',
type: scope.coreInternal
? CUSTOM_RESOURCE_PROVIDER_OPTIONS.CustomResourceProviderOptions
? CORE_INTERNAL.CustomResourceProviderOptions
: CORE_MODULE.CustomResourceProviderOptions,
optional: true,
});
Expand All @@ -259,7 +253,7 @@ export abstract class CdkCustomResourceClass extends ClassType {
]);
this.buildConstructor({
constructorPropsType: scope.coreInternal
? CUSTOM_RESOURCE_PROVIDER_OPTIONS.CustomResourceProviderOptions
? CORE_INTERNAL.CustomResourceProviderOptions
: CORE_MODULE.CustomResourceProviderOptions,
superProps,
constructorVisbility: MemberVisibility.Private,
Expand All @@ -274,6 +268,48 @@ export abstract class CdkCustomResourceClass extends ClassType {
*/
protected abstract readonly externalModules: ExternalModule[];

private importExternalModulesInto(scope: CdkCustomResourceModule) {
for (const module of this.externalModules) {
if (!scope.hasExternalModule(module)) {
scope.addExternalModule(module);
this.importExternalModuleInto(scope, module);
}
}
}

private importExternalModuleInto(scope: CdkCustomResourceModule, module: ExternalModule) {
switch (module.fqn) {
case PATH_MODULE.fqn: {
PATH_MODULE.import(scope, 'path');
return;
}
case CONSTRUCTS_MODULE.fqn: {
CONSTRUCTS_MODULE.importSelective(scope, ['Construct']);
return;
}
case CORE_MODULE.fqn: {
CORE_MODULE.importSelective(scope, [
'Stack',
'CustomResourceProviderBase',
'CustomResourceProviderOptions',
]);
return;
}
case CORE_INTERNAL.fqn: {
CORE_INTERNAL.importSelective(scope, [
'Stack',
'CustomResourceProviderBase',
'CustomResourceProviderOptions',
]);
return;
}
case LAMBDA_MODULE.fqn: {
LAMBDA_MODULE.import(scope, 'lambda');
return;
}
}
}

private getOrCreateInterface(scope: CdkCustomResourceModule, spec: InterfaceSpec) {
const existing = scope.getInterface(spec.name);
if (existing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ExternalModule, InterfaceType, Module, TypeScriptRenderer } from '@cdkl
import * as fs from 'fs-extra';
import { CdkCustomResourceClass, CdkCustomResourceClassProps } from './classes';
import { ComponentType, ConfigProps } from './config';
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 './utils/runtime-determiner';

Expand Down Expand Up @@ -75,10 +74,9 @@ export class CdkCustomResourceModule extends Module {
}

/**
* Render built framework into an output file.
* Render module with components into an output file.
*/
public render(file: string) {
this.importExternalModules();
fs.outputFileSync(file, this.renderer.render(this));
}

Expand All @@ -91,6 +89,13 @@ export class CdkCustomResourceModule extends Module {
}
}

/**
* If an external module has been added as an import to this module.
*/
public hasExternalModule(module: ExternalModule) {
return this.externalModules.has(module.fqn);
}

/**
* Register an interface with this module.
*/
Expand All @@ -113,40 +118,4 @@ export class CdkCustomResourceModule extends Module {
)) + (handler.charAt(0).toUpperCase() + handler.slice(1));
return name.charAt(0).toUpperCase() + name.slice(1) + 'Provider';
}

private importExternalModules() {
PATH_MODULE.import(this, 'path');
for (const fqn of this.externalModules.keys()) {
switch (fqn) {
case CONSTRUCTS_MODULE.fqn: {
CONSTRUCTS_MODULE.importSelective(this, ['Construct']);
break;
}
case CORE_MODULE.fqn: {
CORE_MODULE.importSelective(this, [
'Stack',
'CustomResourceProviderBase',
'CustomResourceProviderOptions',
]);
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;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ConstructsModule extends ExternalModule {
}

class CoreModule extends ExternalModule {
public readonly Stack = Type.fromName(this, 'Stack');
public readonly CustomResourceProviderBase = Type.fromName(this, 'CustomResourceProviderBase');
public readonly CustomResourceProviderOptions = Type.fromName(this, 'CustomResourceProviderOptions');

Expand All @@ -24,25 +25,13 @@ class CoreModule extends ExternalModule {
}
}

class Stack extends ExternalModule {
public constructor() {
super('../../lib/stack');
}
}

class CustomResourceProviderBase extends ExternalModule {
class CoreInternal extends ExternalModule {
public readonly Stack = Type.fromName(this, 'Stack');
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');
super('../../lib');
}
}

Expand All @@ -59,8 +48,5 @@ class LambdaModule extends ExternalModule {
export const PATH_MODULE = new PathModule();
export const CONSTRUCTS_MODULE = new ConstructsModule();
export const CORE_MODULE = new CoreModule();
export const CORE_INTERNAL = new CoreInternal();
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();

0 comments on commit 187c7df

Please sign in to comment.