add support for shared functions #41
This commit is contained in:
@@ -4,14 +4,16 @@ import { IApplication } from '@/domain/IApplication';
|
||||
import { IProjectInformation } from '@/domain/IProjectInformation';
|
||||
import { ApplicationYaml } from 'js-yaml-loader!./../application.yaml';
|
||||
import { parseCategory } from './CategoryParser';
|
||||
import { ProjectInformation } from '../../domain/ProjectInformation';
|
||||
import { ProjectInformation } from '@/domain/ProjectInformation';
|
||||
import { ScriptCompiler } from './Compiler/ScriptCompiler';
|
||||
|
||||
|
||||
export function parseApplication(content: ApplicationYaml, env: NodeJS.ProcessEnv = process.env): IApplication {
|
||||
validate(content);
|
||||
const compiler = new ScriptCompiler(content.functions);
|
||||
const categories = new Array<Category>();
|
||||
for (const action of content.actions) {
|
||||
const category = parseCategory(action);
|
||||
const category = parseCategory(action, compiler);
|
||||
categories.push(category);
|
||||
}
|
||||
const info = readAppInformation(env);
|
||||
@@ -21,7 +23,7 @@ export function parseApplication(content: ApplicationYaml, env: NodeJS.ProcessEn
|
||||
return app;
|
||||
}
|
||||
|
||||
function readAppInformation(environment): IProjectInformation {
|
||||
function readAppInformation(environment: NodeJS.ProcessEnv): IProjectInformation {
|
||||
return new ProjectInformation(
|
||||
environment.VUE_APP_NAME,
|
||||
environment.VUE_APP_VERSION,
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Script } from '@/domain/Script';
|
||||
import { Category } from '@/domain/Category';
|
||||
import { parseDocUrls } from './DocumentationParser';
|
||||
import { parseScript } from './ScriptParser';
|
||||
import { IScriptCompiler } from './Compiler/IScriptCompiler';
|
||||
|
||||
let categoryIdCounter: number = 0;
|
||||
|
||||
@@ -11,14 +12,17 @@ interface ICategoryChildren {
|
||||
subScripts: Script[];
|
||||
}
|
||||
|
||||
export function parseCategory(category: YamlCategory): Category {
|
||||
export function parseCategory(category: YamlCategory, compiler: IScriptCompiler): Category {
|
||||
if (!compiler) {
|
||||
throw new Error('undefined compiler');
|
||||
}
|
||||
ensureValid(category);
|
||||
const children: ICategoryChildren = {
|
||||
subCategories: new Array<Category>(),
|
||||
subScripts: new Array<Script>(),
|
||||
};
|
||||
for (const categoryOrScript of category.children) {
|
||||
parseCategoryChild(categoryOrScript, children, category);
|
||||
parseCategoryChild(categoryOrScript, children, category, compiler);
|
||||
}
|
||||
return new Category(
|
||||
/*id*/ categoryIdCounter++,
|
||||
@@ -42,13 +46,16 @@ function ensureValid(category: YamlCategory) {
|
||||
}
|
||||
|
||||
function parseCategoryChild(
|
||||
categoryOrScript: any, children: ICategoryChildren, parent: YamlCategory) {
|
||||
categoryOrScript: any,
|
||||
children: ICategoryChildren,
|
||||
parent: YamlCategory,
|
||||
compiler: IScriptCompiler) {
|
||||
if (isCategory(categoryOrScript)) {
|
||||
const subCategory = parseCategory(categoryOrScript as YamlCategory);
|
||||
const subCategory = parseCategory(categoryOrScript as YamlCategory, compiler);
|
||||
children.subCategories.push(subCategory);
|
||||
} else if (isScript(categoryOrScript)) {
|
||||
const yamlScript = categoryOrScript as YamlScript;
|
||||
const script = parseScript(yamlScript);
|
||||
const script = parseScript(yamlScript, compiler);
|
||||
children.subScripts.push(script);
|
||||
} else {
|
||||
throw new Error(`Child element is neither a category or a script.
|
||||
@@ -57,7 +64,8 @@ function parseCategoryChild(
|
||||
}
|
||||
|
||||
function isScript(categoryOrScript: any): boolean {
|
||||
return categoryOrScript.code && categoryOrScript.code.length > 0;
|
||||
return (categoryOrScript.code && categoryOrScript.code.length > 0)
|
||||
|| categoryOrScript.call;
|
||||
}
|
||||
|
||||
function isCategory(categoryOrScript: any): boolean {
|
||||
|
||||
7
src/application/Parser/Compiler/IScriptCompiler.ts
Normal file
7
src/application/Parser/Compiler/IScriptCompiler.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { IScriptCode } from '@/domain/IScriptCode';
|
||||
import { YamlScript } from 'js-yaml-loader!./application.yaml';
|
||||
|
||||
export interface IScriptCompiler {
|
||||
canCompile(script: YamlScript): boolean;
|
||||
compile(script: YamlScript): IScriptCode;
|
||||
}
|
||||
200
src/application/Parser/Compiler/ScriptCompiler.ts
Normal file
200
src/application/Parser/Compiler/ScriptCompiler.ts
Normal file
@@ -0,0 +1,200 @@
|
||||
import { IScriptCode } from '@/domain/IScriptCode';
|
||||
import { ScriptCode } from '@/domain/ScriptCode';
|
||||
import { YamlScript, YamlFunction, FunctionCall, ScriptFunctionCall, FunctionCallParameters } from 'js-yaml-loader!./application.yaml';
|
||||
import { IScriptCompiler } from './IScriptCompiler';
|
||||
|
||||
interface ICompiledCode {
|
||||
readonly code: string;
|
||||
readonly revertCode: string;
|
||||
}
|
||||
|
||||
export class ScriptCompiler implements IScriptCompiler {
|
||||
constructor(private readonly functions: readonly YamlFunction[]) {
|
||||
ensureValidFunctions(functions);
|
||||
}
|
||||
public canCompile(script: YamlScript): boolean {
|
||||
if (!script.call) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public compile(script: YamlScript): IScriptCode {
|
||||
this.ensureCompilable(script.call);
|
||||
const compiledCodes = new Array<ICompiledCode>();
|
||||
const calls = getCallSequence(script.call);
|
||||
calls.forEach((currentCall, currentCallIndex) => {
|
||||
ensureValidCall(currentCall, script.name);
|
||||
const commonFunction = this.getFunctionByName(currentCall.function);
|
||||
let functionCode = compileCode(commonFunction, currentCall.parameters);
|
||||
if (currentCallIndex !== calls.length - 1) {
|
||||
functionCode = appendLine(functionCode);
|
||||
}
|
||||
compiledCodes.push(functionCode);
|
||||
});
|
||||
const scriptCode = merge(compiledCodes);
|
||||
return new ScriptCode(script.name, scriptCode.code, scriptCode.revertCode);
|
||||
}
|
||||
|
||||
private getFunctionByName(name: string): YamlFunction {
|
||||
const func = this.functions.find((f) => f.name === name);
|
||||
if (!func) {
|
||||
throw new Error(`called function is not defined "${name}"`);
|
||||
}
|
||||
return func;
|
||||
}
|
||||
|
||||
private ensureCompilable(call: ScriptFunctionCall) {
|
||||
if (!this.functions || this.functions.length === 0) {
|
||||
throw new Error('cannot compile without shared functions');
|
||||
}
|
||||
if (typeof call !== 'object') {
|
||||
throw new Error('called function(s) must be an object');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getDuplicates(texts: readonly string[]): string[] {
|
||||
return texts.filter((item, index) => texts.indexOf(item) !== index);
|
||||
}
|
||||
|
||||
function printList(list: readonly string[]): string {
|
||||
return `"${list.join('","')}"`;
|
||||
}
|
||||
|
||||
function ensureNoDuplicatesInFunctionNames(functions: readonly YamlFunction[]) {
|
||||
const duplicateFunctionNames = getDuplicates(functions
|
||||
.map((func) => func.name.toLowerCase()));
|
||||
if (duplicateFunctionNames.length) {
|
||||
throw new Error(`duplicate function name: ${printList(duplicateFunctionNames)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function ensureNoDuplicatesInParameterNames(functions: readonly YamlFunction[]) {
|
||||
const functionsWithParameters = functions
|
||||
.filter((func) => func.parameters && func.parameters.length > 0);
|
||||
for (const func of functionsWithParameters) {
|
||||
const duplicateParameterNames = getDuplicates(func.parameters);
|
||||
if (duplicateParameterNames.length) {
|
||||
throw new Error(`"${func.name}": duplicate parameter name: ${printList(duplicateParameterNames)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function ensureNoDuplicateCode(functions: readonly YamlFunction[]) {
|
||||
const duplicateCodes = getDuplicates(functions.map((func) => func.code));
|
||||
if (duplicateCodes.length > 0) {
|
||||
throw new Error(`duplicate "code" in functions: ${printList(duplicateCodes)}`);
|
||||
}
|
||||
const duplicateRevertCodes = getDuplicates(functions
|
||||
.filter((func) => func.revertCode)
|
||||
.map((func) => func.revertCode));
|
||||
if (duplicateRevertCodes.length > 0) {
|
||||
throw new Error(`duplicate "revertCode" in functions: ${printList(duplicateRevertCodes)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function ensureValidFunctions(functions: readonly YamlFunction[]) {
|
||||
if (!functions) {
|
||||
return;
|
||||
}
|
||||
ensureNoDuplicatesInFunctionNames(functions);
|
||||
ensureNoDuplicatesInParameterNames(functions);
|
||||
ensureNoDuplicateCode(functions);
|
||||
}
|
||||
|
||||
function appendLine(code: ICompiledCode): ICompiledCode {
|
||||
const appendLineIfNotEmpty = (str: string) => str ? `${str}\n` : str;
|
||||
return {
|
||||
code: appendLineIfNotEmpty(code.code),
|
||||
revertCode: appendLineIfNotEmpty(code.revertCode),
|
||||
};
|
||||
}
|
||||
|
||||
function merge(codes: readonly ICompiledCode[]): ICompiledCode {
|
||||
return {
|
||||
code: codes.map((code) => code.code).join(''),
|
||||
revertCode: codes.map((code) => code.revertCode).join(''),
|
||||
};
|
||||
}
|
||||
|
||||
function compileCode(func: YamlFunction, parameters: FunctionCallParameters): ICompiledCode {
|
||||
return {
|
||||
code: compileExpressions(func.code, parameters),
|
||||
revertCode: compileExpressions(func.revertCode, parameters),
|
||||
};
|
||||
}
|
||||
|
||||
function compileExpressions(code: string, parameters: FunctionCallParameters): string {
|
||||
let intermediateCode = compileToIL(code);
|
||||
intermediateCode = substituteParameters(intermediateCode, parameters);
|
||||
ensureNoExpressionLeft(intermediateCode);
|
||||
return intermediateCode;
|
||||
}
|
||||
|
||||
function substituteParameters(intermediateCode: string, parameters: FunctionCallParameters): string {
|
||||
const parameterNames = getUniqueParameterNamesFromIL(intermediateCode);
|
||||
if (parameterNames.length && !parameters) {
|
||||
throw new Error(`no parameters defined, expected: ${printList(parameterNames)}`);
|
||||
}
|
||||
for (const parameterName of parameterNames) {
|
||||
const parameterValue = parameters[parameterName];
|
||||
intermediateCode = substituteParameter(intermediateCode, parameterName, parameterValue);
|
||||
}
|
||||
return intermediateCode;
|
||||
}
|
||||
|
||||
function ensureValidCall(call: FunctionCall, scriptName: string) {
|
||||
if (!call) {
|
||||
throw new Error(`undefined function call in script "${scriptName}"`);
|
||||
}
|
||||
if (!call.function) {
|
||||
throw new Error(`empty function name called in script "${scriptName}"`);
|
||||
}
|
||||
}
|
||||
|
||||
function getCallSequence(call: ScriptFunctionCall): FunctionCall[] {
|
||||
if (call instanceof Array) {
|
||||
return call as FunctionCall[];
|
||||
}
|
||||
return [ call as FunctionCall ];
|
||||
}
|
||||
|
||||
function getDistinctValues(values: readonly string[]): string[] {
|
||||
return values.filter((value, index, self) => {
|
||||
return self.indexOf(value) === index;
|
||||
});
|
||||
}
|
||||
|
||||
// Trim each expression and put them inside "{{exp|}}" e.g. "{{ $hello }}" becomes "{{exp|$hello}}"
|
||||
function compileToIL(code: string) {
|
||||
return code.replace(/\{\{([\s]*[^;\s\{]+[\s]*)\}\}/g, (_, match) => {
|
||||
return `\{\{exp|${match.trim()}\}\}`;
|
||||
});
|
||||
}
|
||||
|
||||
// Parses all distinct usages of {{exp|$parameterName}}
|
||||
function getUniqueParameterNamesFromIL(ilCode: string) {
|
||||
const allSubstitutions = ilCode.matchAll(/\{\{exp\|\$([^;\s\{]+[\s]*)\}\}/g);
|
||||
const allParameters = Array.from(allSubstitutions, (match) => match[1]);
|
||||
const uniqueParameterNames = getDistinctValues(allParameters);
|
||||
return uniqueParameterNames;
|
||||
}
|
||||
|
||||
// substitutes {{exp|$parameterName}} to value of the parameter
|
||||
function substituteParameter(ilCode: string, parameterName: string, parameterValue: string) {
|
||||
if (!parameterValue) {
|
||||
throw Error(`parameter value is not provided for "${parameterName}" in function call`);
|
||||
}
|
||||
const pattern = `{{exp|$${parameterName}}}`;
|
||||
return ilCode.split(pattern).join(parameterValue); // as .replaceAll() is not yet supported by TS
|
||||
}
|
||||
|
||||
// finds all "{{exp|..}} left"
|
||||
function ensureNoExpressionLeft(ilCode: string) {
|
||||
const allSubstitutions = ilCode.matchAll(/\{\{exp\|(.*?)\}\}/g);
|
||||
const allMatches = Array.from(allSubstitutions, (match) => match[1]);
|
||||
const uniqueExpressions = getDistinctValues(allMatches);
|
||||
if (uniqueExpressions.length > 0) {
|
||||
throw new Error(`unknown expression: ${printList(uniqueExpressions)}`);
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,18 @@ import { Script } from '@/domain/Script';
|
||||
import { YamlScript } from 'js-yaml-loader!./application.yaml';
|
||||
import { parseDocUrls } from './DocumentationParser';
|
||||
import { RecommendationLevelNames, RecommendationLevel } from '@/domain/RecommendationLevel';
|
||||
import { IScriptCompiler } from './Compiler/IScriptCompiler';
|
||||
import { IScriptCode } from '@/domain/IScriptCode';
|
||||
import { ScriptCode } from '@/domain/ScriptCode';
|
||||
|
||||
export function parseScript(yamlScript: YamlScript): Script {
|
||||
if (!yamlScript) {
|
||||
throw new Error('script is null or undefined');
|
||||
export function parseScript(yamlScript: YamlScript, compiler: IScriptCompiler): Script {
|
||||
validateScript(yamlScript);
|
||||
if (!compiler) {
|
||||
throw new Error('undefined compiler');
|
||||
}
|
||||
const script = new Script(
|
||||
/* name */ yamlScript.name,
|
||||
/* code */ yamlScript.code,
|
||||
/* revertCode */ yamlScript.revertCode,
|
||||
/* code */ parseCode(yamlScript, compiler),
|
||||
/* docs */ parseDocUrls(yamlScript),
|
||||
/* level */ getLevel(yamlScript.recommend));
|
||||
return script;
|
||||
@@ -30,3 +33,29 @@ function getLevel(level: string): RecommendationLevel | undefined {
|
||||
}
|
||||
return RecommendationLevel[typedLevel as keyof typeof RecommendationLevel];
|
||||
}
|
||||
|
||||
function parseCode(yamlScript: YamlScript, compiler: IScriptCompiler): IScriptCode {
|
||||
if (compiler.canCompile(yamlScript)) {
|
||||
return compiler.compile(yamlScript);
|
||||
}
|
||||
return new ScriptCode(yamlScript.name, yamlScript.code, yamlScript.revertCode);
|
||||
}
|
||||
|
||||
function ensureNotBothCallAndCode(yamlScript: YamlScript) {
|
||||
if (yamlScript.code && yamlScript.call) {
|
||||
throw new Error('cannot define both "call" and "code"');
|
||||
}
|
||||
if (yamlScript.revertCode && yamlScript.call) {
|
||||
throw new Error('cannot define "revertCode" if "call" is defined');
|
||||
}
|
||||
}
|
||||
|
||||
function validateScript(yamlScript: YamlScript) {
|
||||
if (!yamlScript) {
|
||||
throw new Error('undefined script');
|
||||
}
|
||||
if (!yamlScript.code && !yamlScript.call) {
|
||||
throw new Error('must define either "call" or "code"');
|
||||
}
|
||||
ensureNotBothCallAndCode(yamlScript);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user