From 246e753ddc9dc8bf630e538663584bf3423cc749 Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Mon, 6 Jan 2020 22:22:53 +0100 Subject: [PATCH] code-gen refactorings --- src/application/State/Code/ApplicationCode.ts | 8 +-- src/application/State/Code/CodeBuilder.ts | 64 +++++++++++++------ .../Renderer/AdminRightsFunctionRenderer.ts | 20 ------ .../State/Code/Renderer/AsciiArtRenderer.ts | 18 ------ .../State/Code/Renderer/CodeRenderer.ts | 11 ---- .../State/Code/Renderer/FunctionRenderer.ts | 31 --------- .../State/Code/UserScriptGenerator.ts | 31 +++++++++ 7 files changed, 79 insertions(+), 104 deletions(-) delete mode 100644 src/application/State/Code/Renderer/AdminRightsFunctionRenderer.ts delete mode 100644 src/application/State/Code/Renderer/AsciiArtRenderer.ts delete mode 100644 src/application/State/Code/Renderer/CodeRenderer.ts delete mode 100644 src/application/State/Code/Renderer/FunctionRenderer.ts create mode 100644 src/application/State/Code/UserScriptGenerator.ts diff --git a/src/application/State/Code/ApplicationCode.ts b/src/application/State/Code/ApplicationCode.ts index b39de706..293f5fcb 100644 --- a/src/application/State/Code/ApplicationCode.ts +++ b/src/application/State/Code/ApplicationCode.ts @@ -1,4 +1,4 @@ -import { CodeBuilder } from './CodeBuilder'; +import { UserScriptGenerator } from './UserScriptGenerator'; import { IUserSelection } from './../Selection/IUserSelection'; import { Signal } from '@/infrastructure/Events/Signal'; import { IApplicationCode } from './IApplicationCode'; @@ -8,12 +8,12 @@ export class ApplicationCode implements IApplicationCode { public readonly changed = new Signal(); public current: string; - private readonly codeBuilder: CodeBuilder; + private readonly generator: UserScriptGenerator; constructor(userSelection: IUserSelection, private readonly version: string) { if (!userSelection) { throw new Error('userSelection is null or undefined'); } if (!version) { throw new Error('version is null or undefined'); } - this.codeBuilder = new CodeBuilder(); + this.generator = new UserScriptGenerator(); this.setCode(userSelection.selectedScripts); userSelection.changed.on((scripts) => { this.setCode(scripts); @@ -21,7 +21,7 @@ export class ApplicationCode implements IApplicationCode { } private setCode(scripts: ReadonlyArray) { - this.current = this.codeBuilder.buildCode(scripts, this.version); + this.current = scripts.length === 0 ? '' : this.generator.buildCode(scripts, this.version); this.changed.notify(this.current); } } diff --git a/src/application/State/Code/CodeBuilder.ts b/src/application/State/Code/CodeBuilder.ts index 0fbde570..aa416843 100644 --- a/src/application/State/Code/CodeBuilder.ts +++ b/src/application/State/Code/CodeBuilder.ts @@ -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(); - 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