code-gen refactorings
This commit is contained in:
@@ -1,27 +1,51 @@
|
||||
import { AdminRightsFunctionRenderer } from './Renderer/AdminRightsFunctionRenderer';
|
||||
import { AsciiArtRenderer } from './Renderer/AsciiArtRenderer';
|
||||
import { FunctionRenderer } from './Renderer/FunctionRenderer';
|
||||
import { Script } from '@/domain/Script';
|
||||
const NewLine = '\n';
|
||||
const TotalFunctionSeparatorChars = 58;
|
||||
|
||||
export class CodeBuilder {
|
||||
private readonly functionRenderer: FunctionRenderer;
|
||||
private readonly adminRightsFunctionRenderer: AdminRightsFunctionRenderer;
|
||||
private readonly asciiArtRenderer: AsciiArtRenderer;
|
||||
private readonly lines = new Array<string>();
|
||||
|
||||
public constructor() {
|
||||
this.functionRenderer = new FunctionRenderer();
|
||||
this.adminRightsFunctionRenderer = new AdminRightsFunctionRenderer();
|
||||
this.asciiArtRenderer = new AsciiArtRenderer();
|
||||
public appendLine(code?: string): CodeBuilder {
|
||||
this.lines.push(code);
|
||||
return this;
|
||||
}
|
||||
|
||||
public buildCode(scripts: ReadonlyArray<Script>, version: string): string {
|
||||
if (!scripts) { throw new Error('scripts is undefined'); }
|
||||
if (!version) { throw new Error('version is undefined'); }
|
||||
return `@echo off\n\n${this.asciiArtRenderer.renderAsciiArt(version)}\n\n`
|
||||
+ `${this.adminRightsFunctionRenderer.renderAdminRightsFunction()}\n\n`
|
||||
+ scripts.map((script) => this.functionRenderer.renderFunction(script.name, script.code)).join('\n\n')
|
||||
+ '\n\n'
|
||||
+ 'pause\n'
|
||||
+ 'exit /b 0';
|
||||
public appendTrailingHyphensCommentLine(
|
||||
totalRepeatHyphens: number = TotalFunctionSeparatorChars): CodeBuilder {
|
||||
return this.appendCommentLine('-'.repeat(totalRepeatHyphens));
|
||||
}
|
||||
|
||||
public appendCommentLine(commentLine?: string): CodeBuilder {
|
||||
this.lines.push(`:: ${commentLine}`);
|
||||
return this;
|
||||
}
|
||||
|
||||
public appendFunction(name: string, code: string): CodeBuilder {
|
||||
if (!name) { throw new Error('name cannot be empty or null'); }
|
||||
if (!code) { throw new Error('code cannot be empty or null'); }
|
||||
return this
|
||||
.appendLine()
|
||||
.appendCommentLineWithHyphensAround(name)
|
||||
.appendLine(`echo --- ${name}`)
|
||||
.appendLine(code)
|
||||
.appendTrailingHyphensCommentLine();
|
||||
}
|
||||
|
||||
public appendCommentLineWithHyphensAround(
|
||||
sectionName: string,
|
||||
totalRepeatHyphens: number = TotalFunctionSeparatorChars): CodeBuilder {
|
||||
if (!sectionName) { throw new Error('sectionName cannot be empty or null'); }
|
||||
if (sectionName.length >= totalRepeatHyphens) {
|
||||
return this.appendCommentLine(sectionName);
|
||||
}
|
||||
const firstHyphens = '-'.repeat(Math.floor((totalRepeatHyphens - sectionName.length) / 2));
|
||||
const secondHyphens = '-'.repeat(Math.ceil((totalRepeatHyphens - sectionName.length) / 2));
|
||||
return this
|
||||
.appendTrailingHyphensCommentLine()
|
||||
.appendCommentLine(firstHyphens + sectionName + secondHyphens)
|
||||
.appendTrailingHyphensCommentLine();
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
return this.lines.join(NewLine);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user