Skip to content

Commit

Permalink
feat: synth implementation classes from module name
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Jul 25, 2022
1 parent d320efb commit c99b574
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .projen/files.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions .projenrc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { typescript } from 'projen';
const project = new typescript.TypeScriptProject({
defaultReleaseBranch: 'main',
name: 'l2-projen',
projenrcTs: true,
import { L2ConstructProject } from './projenrc/L2ConstructProject';
// import * as meta from './projenrc/meta/';

// deps: [], /* Runtime dependencies of this module. */
// description: undefined, /* The description is just a string that helps people understand the purpose of the package. */
// devDeps: [], /* Build dependencies for this module. */
// packageName: undefined, /* The "name" in package.json. */
const project = new L2ConstructProject({
moduleName: 'aws-kinesis',
});
project.synth();
project.synth();
// project.addImplementation(new meta.StreamImplementation());
// project.addImplementation(new meta.StreamConsumerImplementation());
5 changes: 4 additions & 1 deletion package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions projenrc/Implementation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface BaseImplementationOptions {

}

export interface IImplementation {
readonly resourceName: string;
}

export abstract class BaseImplementation implements IImplementation {
public readonly resourceName: string = '';
protected readonly options: BaseImplementationOptions;

public constructor(options: BaseImplementationOptions) {
this.options = options;
}
}
81 changes: 81 additions & 0 deletions projenrc/L2ConstructProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as cdk from 'aws-cdk-lib';
import { TextFile } from 'projen';
import { TypeScriptProject, TypeScriptProjectOptions } from 'projen/lib/typescript';
import { StructureKind } from 'ts-morph';
import { IImplementation } from './Implementation';
import { TSSourceCode } from './TSSourceCode';


export interface L2ConstructProjectOptions extends Omit<TypeScriptProjectOptions, 'name' | 'defaultReleaseBranch'> {
moduleName: string;
defaultReleaseBranch?: string;
}


export class L2ConstructProject extends TypeScriptProject {
public constructor(options: L2ConstructProjectOptions) {
super({
name: '@aws-cdk/' + options.moduleName,
defaultReleaseBranch: 'main',
projenrcTs: true,
...options,
devDeps: (options.devDeps ?? []).concat('aws-cdk-lib', 'constructs@^10', 'ts-morph'),
});

const cfnResources = Object.keys(cdk[options.moduleName.replace('-', '_') as keyof typeof cdk])
.filter(resource => resource.startsWith('Cfn'));

const exportFile = new TextFile(this, 'projenrc/meta/index.ts', {});

for (const resource of cfnResources) {
const className = `${resource.substring(3)}Implementation`;
const optionsName = `${className}Options`;

exportFile.addLine(`export * from './${className}';`);
new TSSourceCode(this, `projenrc/meta/${className}.ts`, {
kind: StructureKind.SourceFile,
statements: [{
kind: StructureKind.ImportDeclaration,
namedImports: [
{ name: 'BaseImplementation' },
{ name: 'BaseImplementationOptions' },
],
moduleSpecifier: '../Implementation',
},
{
kind: StructureKind.Interface,
name: optionsName,
extends: ['BaseImplementationOptions'],
},
{
kind: StructureKind.Class,
isAbstract: false,
isExported: true,
extends: 'BaseImplementation',
name: className,
ctors: [{
kind: StructureKind.Constructor,
parameters: [{
name: 'options',
type: optionsName,
}],
statements: 'super(options);',
}],
typeParameters: [],
properties: [{
name: 'resourceName',
initializer: `'${resource}'`,
type: 'string',
isReadonly: true,
isStatic: false,
}],
methods: [],
}],
});
}
}

public addImplementation(implementation: IImplementation) {
console.log(implementation.resourceName);
}
}
29 changes: 29 additions & 0 deletions projenrc/TSSourceCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { tmpdir } from 'os';
import { join } from 'path';
import { FileBase, Project } from 'projen';
import { SourceFileStructure, Project as TSProject, SourceFile, IndentationText, QuoteKind } from 'ts-morph';

export class TSSourceCode extends FileBase {

public readonly filePath: string;
protected ts: TSProject;
protected file: SourceFile;

public constructor(project: Project, filePath: string, structure: SourceFileStructure) {
super(project, filePath);
this.ts = new TSProject({
manipulationSettings: {
indentationText: IndentationText.TwoSpaces,
quoteKind: QuoteKind.Single,
},
});

this.filePath = filePath;
this.file = this.ts.createSourceFile(join(tmpdir(), filePath));
this.file.set(structure);
}

protected synthesizeContent(): string {
return this.file.getFullText();
}
}
12 changes: 12 additions & 0 deletions projenrc/meta/StreamConsumerImplementation.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions projenrc/meta/StreamImplementation.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions projenrc/meta/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c99b574

Please sign in to comment.