code-gen refactorings

This commit is contained in:
undergroundwires
2020-01-06 22:22:53 +01:00
parent 60e6348dc8
commit 246e753ddc
7 changed files with 79 additions and 104 deletions

View File

@@ -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<string>();
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<IScript>) {
this.current = this.codeBuilder.buildCode(scripts, this.version);
this.current = scripts.length === 0 ? '' : this.generator.buildCode(scripts, this.version);
this.changed.notify(this.current);
}
}

View File

@@ -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);
}
}

View File

@@ -1,20 +0,0 @@
import { FunctionRenderer } from './FunctionRenderer';
export class AdminRightsFunctionRenderer {
private readonly functionRenderer: FunctionRenderer;
constructor() {
this.functionRenderer = new FunctionRenderer();
}
public renderAdminRightsFunction() {
const name = 'Ensure admin privileges';
const code = 'fltmc >nul 2>&1 || (\n' +
' echo This batch script requires administrator privileges. Right-click on\n' +
' echo the script and select "Run as administrator".\n' +
' pause\n' +
' exit 1\n' +
')';
return this.functionRenderer.renderFunction(name, code);
}
}

View File

@@ -1,18 +0,0 @@
import { CodeRenderer } from './CodeRenderer';
export class AsciiArtRenderer extends CodeRenderer {
public renderAsciiArt(version: string): string {
if (!version) {
throw new Error('Version is not defined');
}
return (
'██████╗ ██████╗ ██╗██╗ ██╗ █████╗ ██████╗██╗ ██╗███████╗███████╗██╗ ██╗██╗ ██╗\n' +
'██╔══██╗██╔══██╗██║██║ ██║██╔══██╗██╔════╝╚██╗ ██╔╝██╔════╝██╔════╝╚██╗██╔╝╚██╗ ██╔╝\n' +
'██████╔╝██████╔╝██║██║ ██║███████║██║ ╚████╔╝ ███████╗█████╗ ╚███╔╝ ╚████╔╝ \n' +
'██╔═══╝ ██╔══██╗██║╚██╗ ██╔╝██╔══██║██║ ╚██╔╝ ╚════██║██╔══╝ ██╔██╗ ╚██╔╝ \n' +
'██║ ██║ ██║██║ ╚████╔╝ ██║ ██║╚██████╗ ██║██╗███████║███████╗██╔╝ ██╗ ██║ \n' +
'╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝╚═╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═╝ ')
.split('\n').map((line) => this.renderComment(line)).join('\n') +
`\n${this.renderComment(`https://privacy.sexy — v${version}${new Date().toUTCString()}`)}`;
}
}

View File

@@ -1,11 +0,0 @@
export abstract class CodeRenderer {
protected readonly totalFunctionSeparatorChars = 58;
protected readonly trailingHyphens = '-'.repeat(this.totalFunctionSeparatorChars);
protected renderComment(line?: string): string {
return line ? `:: ${line}` : ':: ';
}
}

View File

@@ -1,31 +0,0 @@
import { CodeRenderer } from './CodeRenderer';
export class FunctionRenderer extends CodeRenderer {
public renderFunction(name: string, code: string) {
if (!name) { throw new Error('name cannot be empty or null'); }
if (!code) { throw new Error('code cannot be empty or null'); }
return this.renderFunctionStartComment(name) + '\n'
+ `echo --- ${name}` + '\n'
+ code + '\n'
+ this.renderFunctionEndComment();
}
private renderFunctionStartComment(functionName: string): string {
return this.renderComment(this.trailingHyphens) + '\n' +
this.renderFunctionName(functionName) + '\n' +
this.renderComment(this.trailingHyphens);
}
private renderFunctionName(functionName: string) {
if (functionName.length >= this.totalFunctionSeparatorChars) {
return this.renderComment(functionName);
}
const firstHyphens = '-'.repeat(Math.floor((this.totalFunctionSeparatorChars - functionName.length) / 2));
const secondHyphens = '-'.repeat(Math.ceil((this.totalFunctionSeparatorChars - functionName.length) / 2));
return `${this.renderComment()}${firstHyphens}${functionName}${secondHyphens}`;
}
private renderFunctionEndComment(): string {
return this.renderComment(this.trailingHyphens);
}
}

View File

@@ -0,0 +1,31 @@
import { CodeBuilder } from './CodeBuilder';
import { Script } from '@/domain/Script';
const adminRightsScript = {
name: 'Ensure admin privileges',
code: 'fltmc >nul 2>&1 || (\n' +
' echo This batch script requires administrator privileges. Right-click on\n' +
' echo the script and select "Run as administrator".\n' +
' pause\n' +
' exit 1\n' +
')',
};
export class UserScriptGenerator {
public buildCode(scripts: ReadonlyArray<Script>, version: string): string {
if (!scripts) { throw new Error('scripts is undefined'); }
if (!scripts.length) { throw new Error('scripts are empty'); }
if (!version) { throw new Error('version is undefined'); }
const builder = new CodeBuilder()
.appendLine('@echo off')
.appendCommentLine(`https://privacy.sexy — v${version}${new Date().toUTCString()}`)
.appendFunction(adminRightsScript.name, adminRightsScript.code).appendLine();
for (const script of scripts) {
builder.appendFunction(script.name, script.code).appendLine();
}
return builder.appendLine()
.appendLine('pause')
.appendLine('exit /b 0')
.toString();
}
}