Initial commit
This commit is contained in:
27
src/application/State/Code/ApplicationCode.ts
Normal file
27
src/application/State/Code/ApplicationCode.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { CodeBuilder } from './CodeBuilder';
|
||||
import { IUserSelection } from './../Selection/IUserSelection';
|
||||
import { Signal } from '@/infrastructure/Events/Signal';
|
||||
import { IApplicationCode } from './IApplicationCode';
|
||||
import { IScript } from '@/domain/IScript';
|
||||
|
||||
export class ApplicationCode implements IApplicationCode {
|
||||
public readonly changed = new Signal<string>();
|
||||
public current: string;
|
||||
|
||||
private readonly codeBuilder: CodeBuilder;
|
||||
|
||||
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.setCode(userSelection.selectedScripts);
|
||||
userSelection.changed.on((scripts) => {
|
||||
this.setCode(scripts);
|
||||
});
|
||||
}
|
||||
|
||||
private setCode(scripts: ReadonlyArray<IScript>) {
|
||||
this.current = this.codeBuilder.buildCode(scripts, this.version);
|
||||
this.changed.notify(this.current);
|
||||
}
|
||||
}
|
||||
27
src/application/State/Code/CodeBuilder.ts
Normal file
27
src/application/State/Code/CodeBuilder.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { AdminRightsFunctionRenderer } from './Renderer/AdminRightsFunctionRenderer';
|
||||
import { AsciiArtRenderer } from './Renderer/AsciiArtRenderer';
|
||||
import { FunctionRenderer } from './Renderer/FunctionRenderer';
|
||||
import { Script } from '@/domain/Script';
|
||||
|
||||
export class CodeBuilder {
|
||||
private readonly functionRenderer: FunctionRenderer;
|
||||
private readonly adminRightsFunctionRenderer: AdminRightsFunctionRenderer;
|
||||
private readonly asciiArtRenderer: AsciiArtRenderer;
|
||||
|
||||
public constructor() {
|
||||
this.functionRenderer = new FunctionRenderer();
|
||||
this.adminRightsFunctionRenderer = new AdminRightsFunctionRenderer();
|
||||
this.asciiArtRenderer = new AsciiArtRenderer();
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
}
|
||||
6
src/application/State/Code/IApplicationCode.ts
Normal file
6
src/application/State/Code/IApplicationCode.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { ISignal } from '@/infrastructure/Events/ISignal';
|
||||
|
||||
export interface IApplicationCode {
|
||||
readonly changed: ISignal<string>;
|
||||
readonly current: string;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { FunctionRenderer } from './FunctionRenderer';
|
||||
|
||||
export class AdminRightsFunctionRenderer {
|
||||
private readonly functionRenderer: FunctionRenderer;
|
||||
constructor() {
|
||||
this.functionRenderer = new FunctionRenderer();
|
||||
}
|
||||
public renderAdminRightsFunction() {
|
||||
const name = 'Ensure admin priviliges';
|
||||
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);
|
||||
}
|
||||
}
|
||||
16
src/application/State/Code/Renderer/AsciiArtRenderer.ts
Normal file
16
src/application/State/Code/Renderer/AsciiArtRenderer.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
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()}`)}`;
|
||||
}
|
||||
}
|
||||
11
src/application/State/Code/Renderer/CodeRenderer.ts
Normal file
11
src/application/State/Code/Renderer/CodeRenderer.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
export abstract class CodeRenderer {
|
||||
|
||||
protected readonly totalFunctionSeparatorChars = 58;
|
||||
|
||||
protected readonly trailingHyphens = '-'.repeat(this.totalFunctionSeparatorChars);
|
||||
|
||||
protected renderComment(line?: string): string {
|
||||
return line ? `:: ${line}` : ':: ';
|
||||
}
|
||||
}
|
||||
31
src/application/State/Code/Renderer/FunctionRenderer.ts
Normal file
31
src/application/State/Code/Renderer/FunctionRenderer.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
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 {
|
||||
if (functionName.length >= this.totalFunctionSeparatorChars) {
|
||||
return this.renderComment(functionName);
|
||||
}
|
||||
return this.renderComment(this.trailingHyphens) + '\n' +
|
||||
this.renderFunctionName(functionName) + '\n' +
|
||||
this.renderComment(this.trailingHyphens);
|
||||
}
|
||||
|
||||
private renderFunctionName(functionName: string) {
|
||||
const autoFirstHypens = '-'.repeat(Math.floor((this.totalFunctionSeparatorChars - functionName.length) / 2));
|
||||
const secondHypens = '-'.repeat(Math.ceil((this.totalFunctionSeparatorChars - functionName.length) / 2));
|
||||
return `${this.renderComment()}${autoFirstHypens}${functionName}${secondHypens}`;
|
||||
}
|
||||
|
||||
private renderFunctionEndComment(): string {
|
||||
return this.renderComment(this.trailingHyphens);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user