Initial commit

This commit is contained in:
undergroundwires
2019-12-31 16:09:39 +01:00
commit 4e7f244190
108 changed files with 17296 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
import { UserFilter } from './Filter/UserFilter';
import { IUserFilter } from './Filter/IUserFilter';
import { ApplicationCode } from './Code/ApplicationCode';
import { UserSelection } from './Selection/UserSelection';
import { IUserSelection } from './Selection/IUserSelection';
import { AsyncLazy } from '../../infrastructure/Threading/AsyncLazy';
import { Signal } from '../../infrastructure/Events/Signal';
import { ICategory } from '../../domain/ICategory';
import { ApplicationParser } from '../ApplicationParser';
import { IApplicationState } from './IApplicationState';
import { Script } from '../../domain/Script';
import { Application } from '../../domain/Application';
import { IApplicationCode } from './Code/IApplicationCode';
/** Mutatable singleton application state that's the single source of truth throughout the application */
export class ApplicationState implements IApplicationState {
/** Get singleton application state */
public static GetAsync(): Promise<IApplicationState> {
return ApplicationState.instance.getValueAsync();
}
/** Application instance with all scripts. */
private static instance = new AsyncLazy<IApplicationState>(() => {
const app = ApplicationParser.buildApplication();
const state = new ApplicationState(app.application, app.selectedScripts);
return Promise.resolve(state);
});
public readonly code: IApplicationCode;
public readonly stateChanged = new Signal<IApplicationState>();
public readonly selection: IUserSelection;
public readonly filter: IUserFilter;
private constructor(
/** Inner instance of the all scripts */
private readonly app: Application,
/** Initially selected scripts */
public readonly defaultScripts: Script[]) {
this.selection = new UserSelection(app, defaultScripts);
this.code = new ApplicationCode(this.selection, app.version.toString());
this.filter = new UserFilter(app);
}
public getCategory(categoryId: number): ICategory | undefined {
return this.app.findCategory(categoryId);
}
public get categories(): ReadonlyArray<ICategory> {
return this.app.categories;
}
public get appName(): string {
return this.app.name;
}
public get appVersion(): number {
return this.app.version;
}
public get appTotalScripts(): number {
return this.app.totalScripts;
}
}
export { IApplicationState, IUserFilter };

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

View 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';
}
}

View File

@@ -0,0 +1,6 @@
import { ISignal } from '@/infrastructure/Events/ISignal';
export interface IApplicationCode {
readonly changed: ISignal<string>;
readonly current: string;
}

View File

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

View 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()}`)}`;
}
}

View 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}` : ':: ';
}
}

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

View File

@@ -0,0 +1,7 @@
import { IScript, ICategory } from '@/domain/ICategory';
export interface IFilterMatches {
readonly scriptMatches: ReadonlyArray<IScript>;
readonly categoryMatches: ReadonlyArray<ICategory>;
readonly query: string;
}

View File

@@ -0,0 +1,9 @@
import { IFilterMatches } from './IFilterMatches';
import { ISignal } from '@/infrastructure/Events/Signal';
export interface IUserFilter {
readonly filtered: ISignal<IFilterMatches>;
readonly filterRemoved: ISignal<void>;
setFilter(filter: string): void;
removeFilter(): void;
}

View File

@@ -0,0 +1,34 @@
import { IFilterMatches } from './IFilterMatches';
import { Application } from '../../../domain/Application';
import { IUserFilter } from './IUserFilter';
import { Signal } from '@/infrastructure/Events/Signal';
export class UserFilter implements IUserFilter {
public readonly filtered = new Signal<IFilterMatches>();
public readonly filterRemoved = new Signal<void>();
constructor(private application: Application) {
}
public setFilter(filter: string): void {
if (!filter) {
throw new Error('Filter must be defined and not empty. Use removeFilter() to remove the filter');
}
const filteredScripts = this.application.getAllScripts().filter(
(script) => script.name.toLowerCase().includes(filter.toLowerCase())
|| script.code.toLowerCase().includes(filter.toLowerCase()));
const matches: IFilterMatches = {
scriptMatches: filteredScripts,
categoryMatches: null,
query: filter,
};
this.filtered.notify(matches);
}
public removeFilter(): void {
this.filterRemoved.notify();
}
}

View File

@@ -0,0 +1,21 @@
import { IUserFilter } from './Filter/IUserFilter';
import { IUserSelection } from './Selection/IUserSelection';
import { ISignal } from '@/infrastructure/Events/ISignal';
import { ICategory, IScript } from '@/domain/ICategory';
import { IApplicationCode } from './Code/IApplicationCode';
export { IUserSelection, IApplicationCode, IUserFilter };
export interface IApplicationState {
/** Event that fires when the application states changes with new application state as parameter */
readonly code: IApplicationCode;
readonly filter: IUserFilter;
readonly stateChanged: ISignal<IApplicationState>;
readonly categories: ReadonlyArray<ICategory>;
readonly appName: string;
readonly appVersion: number;
readonly appTotalScripts: number;
readonly selection: IUserSelection;
readonly defaultScripts: ReadonlyArray<IScript>;
getCategory(categoryId: number): ICategory | undefined;
}

View File

@@ -0,0 +1,14 @@
import { ISignal } from '@/infrastructure/Events/Signal';
import { IScript } from '@/domain/IScript';
export interface IUserSelection {
readonly changed: ISignal<ReadonlyArray<IScript>>;
readonly selectedScripts: ReadonlyArray<IScript>;
readonly totalSelected: number;
addSelectedScript(scriptId: string): void;
removeSelectedScript(scriptId: string): void;
selectOnly(scripts: ReadonlyArray<IScript>): void;
isSelected(script: IScript): boolean;
selectAll(): void;
deselectAll(): void;
}

View File

@@ -0,0 +1,86 @@
import { IApplication } from '@/domain/IApplication';
import { IUserSelection } from './IUserSelection';
import { InMemoryRepository } from '@/infrastructure/Repository/InMemoryRepository';
import { IScript } from '@/domain/Script';
import { Signal } from '@/infrastructure/Events/Signal';
export class UserSelection implements IUserSelection {
public readonly changed = new Signal<ReadonlyArray<IScript>>();
private readonly scripts = new InMemoryRepository<string, IScript>();
constructor(
private readonly app: IApplication,
/** Initially selected scripts */
selectedScripts: ReadonlyArray<IScript>) {
if (selectedScripts && selectedScripts.length > 0) {
for (const script of selectedScripts) {
this.scripts.addItem(script);
}
}
}
/** Add a script to users application */
public addSelectedScript(scriptId: string): void {
const script = this.app.findScript(scriptId);
if (!script) {
throw new Error(`Cannot add (id: ${scriptId}) as it is unknown`);
}
this.scripts.addItem(script);
this.changed.notify(this.scripts.getItems());
}
/** Remove a script from users application */
public removeSelectedScript(scriptId: string): void {
this.scripts.removeItem(scriptId);
this.changed.notify(this.scripts.getItems());
}
public isSelected(script: IScript): boolean {
return this.scripts.exists(script);
}
/** Get users scripts based on his/her selections */
public get selectedScripts(): ReadonlyArray<IScript> {
return this.scripts.getItems();
}
public get totalSelected(): number {
return this.scripts.getItems().length;
}
public selectAll(): void {
for (const script of this.app.getAllScripts()) {
if (!this.scripts.exists(script)) {
this.scripts.addItem(script);
}
}
this.changed.notify(this.scripts.getItems());
}
public deselectAll(): void {
const selectedScriptIds = this.scripts.getItems().map((script) => script.id);
for (const scriptId of selectedScriptIds) {
this.scripts.removeItem(scriptId);
}
this.changed.notify([]);
}
public selectOnly(scripts: readonly IScript[]): void {
if (!scripts || scripts.length === 0) {
throw new Error('Scripts are empty. Use deselectAll() if you want to deselect everything');
}
// Unselect from selected scripts
if (this.scripts.length !== 0) {
this.scripts.getItems()
.filter((existing) => !scripts.some((script) => existing.id === script.id))
.map((script) => script.id)
.forEach((scriptId) => this.scripts.removeItem(scriptId));
}
// Select from unselected scripts
scripts
.filter((script) => !this.scripts.exists(script))
.forEach((script) => this.scripts.addItem(script));
this.changed.notify(this.scripts.getItems());
}
}