71 lines
1.5 KiB
TypeScript
71 lines
1.5 KiB
TypeScript
import { IBuilder } from ".";
|
|
import { Env } from "../models/Env";
|
|
|
|
export class EnvBuilder implements IBuilder<Env> {
|
|
|
|
readonly ident = " ";
|
|
readonly newLine = "\r\n";
|
|
private stack: string[] = [];
|
|
private _src!: Env;
|
|
|
|
|
|
get src(): Env {
|
|
return this._src;
|
|
}
|
|
set src(v: Env) {
|
|
this._src = v;
|
|
}
|
|
|
|
/**
|
|
* Builds XML for a single environment (static method for direct use)
|
|
*/
|
|
public buildEnv(env: Env): string {
|
|
this.src = env;
|
|
return this.build();
|
|
}
|
|
|
|
build(): string {
|
|
return this
|
|
.open()
|
|
.params()
|
|
.close()
|
|
.toString();
|
|
}
|
|
|
|
private params(): this {
|
|
const tag = `<parameter name="{name}" value="{val}" />`;
|
|
for (const p of this.src.params) {
|
|
this.stack.push(this.ident);
|
|
this.stack.push(tag
|
|
.replace("{name}", p.name ?? "!ERR!")
|
|
.replace("{val}", p.sanitize(p.value) ?? "!ERR!")
|
|
);
|
|
this.stack.push(this.newLine);
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
private open(): EnvBuilder {
|
|
const tag = `<environment name="${this.src.name}">`;
|
|
this.stack.push(tag);
|
|
this.stack.push(this.newLine);
|
|
return this;
|
|
}
|
|
private close(): EnvBuilder {
|
|
const tag = `</environment>`;
|
|
this.stack.push(tag);
|
|
return this;
|
|
}
|
|
|
|
toString(): string {
|
|
let res = "";
|
|
|
|
while (this.stack.length > 0) {
|
|
res = this.stack.pop() + res;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
}
|