Skip to content

Commit

Permalink
default selection is now none
Browse files Browse the repository at this point in the history
  • Loading branch information
undergroundwires committed Jan 6, 2020
1 parent 20020af commit 3140cc6
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 237 deletions.
15 changes: 3 additions & 12 deletions src/application/Parser/ApplicationParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,17 @@ import { Script } from '@/domain/Script';
import applicationFile from 'js-yaml-loader!./../application.yaml';
import { parseCategory } from './CategoryParser';

interface ApplicationResult {
readonly application: Application;
readonly selectedScripts: Script[];
}

export function buildApplication(): ApplicationResult {
export function parseApplication(): Application {
const name = applicationFile.name as string;
const version = applicationFile.version as number;
const categories = new Array<Category>();
const selectedScripts = new Array<Script>();
if (!applicationFile.actions || applicationFile.actions.length <= 0) {
throw new Error('Application does not define any action');
}
for (const action of applicationFile.actions) {
const category = parseCategory({
category: action,
selectedScripts,
});
const category = parseCategory(action);
categories.push(category);
}
const app = new Application(name, version, categories);
return { application: app, selectedScripts };
return app;
}
32 changes: 11 additions & 21 deletions src/application/Parser/CategoryParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,48 @@ import { parseDocUrls } from './DocumentationParser';

let categoryIdCounter: number = 0;

interface IParsingContext {
category: YamlCategory;
selectedScripts: Script[];
}

interface ICategoryChildren {
subCategories: Category[];
subScripts: Script[];
}

export function parseCategory(context: IParsingContext): Category {
if (!context.category.children || context.category.children.length <= 0) {
export function parseCategory(category: YamlCategory): Category {
if (!category.children || category.children.length <= 0) {
throw Error('Category has no children');
}
const children: ICategoryChildren = {
subCategories: new Array<Category>(),
subScripts: new Array<Script>(),
};
for (const categoryOrScript of context.category.children) {
parseCategoryChild(categoryOrScript, children, context);
for (const categoryOrScript of category.children) {
parseCategoryChild(categoryOrScript, children, category);
}
return new Category(
/*id*/ categoryIdCounter++,
/*name*/ context.category.category,
/*docs*/ parseDocUrls(context.category),
/*name*/ category.category,
/*docs*/ parseDocUrls(category),
/*categories*/ children.subCategories,
/*scripts*/ children.subScripts,
);
}

function parseCategoryChild(
categoryOrScript: any, children: ICategoryChildren, parent: IParsingContext) {
categoryOrScript: any, children: ICategoryChildren, parent: YamlCategory) {
if (isCategory(categoryOrScript)) {
const subCategory = parseCategory(
{
category: categoryOrScript as YamlCategory,
selectedScripts: parent.selectedScripts,
});
const subCategory = parseCategory(categoryOrScript as YamlCategory);
children.subCategories.push(subCategory);
} else if (isScript(categoryOrScript)) {
const yamlScript = categoryOrScript as YamlScript;
const script = new Script(
/* name */ yamlScript.name,
/* code */ yamlScript.code,
/* docs */ parseDocUrls(yamlScript));
/* docs */ parseDocUrls(yamlScript),
/* is recommended? */ yamlScript.recommend);
children.subScripts.push(script);
if (yamlScript.default === true) {
parent.selectedScripts.push(script);
}
} else {
throw new Error(`Child element is neither a category or a script.
Parent: ${parent.category.category}, element: ${categoryOrScript}`);
Parent: ${parent.category}, element: ${categoryOrScript}`);
}
}

Expand Down
29 changes: 5 additions & 24 deletions src/application/State/ApplicationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IUserSelection } from './Selection/IUserSelection';
import { AsyncLazy } from '../../infrastructure/Threading/AsyncLazy';
import { Signal } from '../../infrastructure/Events/Signal';
import { ICategory } from '../../domain/ICategory';
import { buildApplication } from '../Parser/ApplicationParser';
import { parseApplication } from '../Parser/ApplicationParser';
import { IApplicationState } from './IApplicationState';
import { Script } from '../../domain/Script';
import { Application } from '../../domain/Application';
Expand All @@ -21,8 +21,9 @@ export class ApplicationState implements IApplicationState {

/** Application instance with all scripts. */
private static instance = new AsyncLazy<IApplicationState>(() => {
const app = buildApplication();
const state = new ApplicationState(app.application, app.selectedScripts);
const application = parseApplication();
const selectedScripts = new Array<Script>();
const state = new ApplicationState(application, selectedScripts);
return Promise.resolve(state);
});

Expand All @@ -33,33 +34,13 @@ export class ApplicationState implements IApplicationState {

private constructor(
/** Inner instance of the all scripts */
private readonly app: Application,
public 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 };
8 changes: 2 additions & 6 deletions src/application/State/IApplicationState.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IApplication } from './../../domain/IApplication';
import { IUserFilter } from './Filter/IUserFilter';
import { IUserSelection } from './Selection/IUserSelection';
import { ISignal } from '@/infrastructure/Events/ISignal';
Expand All @@ -10,11 +11,6 @@ export interface IApplicationState {
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;
readonly app: IApplication;
}
Loading

0 comments on commit 3140cc6

Please sign in to comment.