2021-12-31 21:38:26 +00:00
|
|
|
import * as FS from "fs";
|
2022-01-05 21:16:17 +00:00
|
|
|
import * as FSE from "fs-extra"
|
2021-12-31 21:38:26 +00:00
|
|
|
import * as Path from "path";
|
|
|
|
import {
|
|
|
|
EnumDefinition,
|
|
|
|
IR,
|
|
|
|
ServiceDefinition,
|
|
|
|
Step,
|
|
|
|
TypeDefinition,
|
|
|
|
} from "./ir";
|
|
|
|
|
2022-01-05 21:16:17 +00:00
|
|
|
export abstract class CompileTarget<T = any> {
|
2021-12-31 21:38:26 +00:00
|
|
|
abstract name: string;
|
2022-01-10 20:17:25 +00:00
|
|
|
constructor(private outputFolder: string, protected options: T & { use_messagepack: boolean }) {
|
2021-12-31 21:38:26 +00:00
|
|
|
if (!FS.existsSync(outputFolder)) {
|
|
|
|
FS.mkdirSync(outputFolder, {
|
|
|
|
recursive: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract start(): void;
|
|
|
|
|
|
|
|
abstract generateType(definition: TypeDefinition): void;
|
|
|
|
|
|
|
|
abstract generateEnum(definition: EnumDefinition): void;
|
|
|
|
|
|
|
|
abstract generateService(definition: ServiceDefinition): void;
|
|
|
|
|
|
|
|
abstract finalize(steps: Step[]): void;
|
|
|
|
|
|
|
|
protected writeFile(name: string, content: string | Promise<string>) {
|
|
|
|
if (content instanceof Promise) {
|
|
|
|
content.then((res) =>
|
|
|
|
FS.writeFileSync(Path.join(this.outputFolder, name), res)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
FS.writeFileSync(Path.join(this.outputFolder, name), content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getTemplate(name: string): string {
|
|
|
|
let path = Path.join(__dirname, "../templates/" + name);
|
|
|
|
let file = FS.readFileSync(path, "utf-8");
|
|
|
|
|
|
|
|
const splitted = file.split("\n");
|
|
|
|
let res = [];
|
|
|
|
let ignore = false;
|
|
|
|
for (const line of splitted) {
|
|
|
|
if (ignore) {
|
|
|
|
ignore = false;
|
|
|
|
} else if (line.trim().startsWith("//@template-ignore")) {
|
|
|
|
ignore = true;
|
|
|
|
} else {
|
|
|
|
res.push(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.join("\n");
|
|
|
|
}
|
2022-01-05 21:16:17 +00:00
|
|
|
|
|
|
|
protected loadTemplateFolder(name:string) {
|
|
|
|
let root = Path.join(__dirname, "../templates/", name);
|
|
|
|
|
|
|
|
FSE.copySync(root, this.outputFolder, {
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2021-12-31 21:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function compile(ir: IR, target: CompileTarget) {
|
|
|
|
// Types are verified. They are now ready to be compiled to targets
|
|
|
|
|
|
|
|
// setState("Building for target: " + target.name);
|
2022-01-02 22:02:47 +00:00
|
|
|
target.start();
|
2022-01-05 21:16:17 +00:00
|
|
|
ir.steps.forEach((step) => {
|
2021-12-31 21:38:26 +00:00
|
|
|
const [type, def] = step;
|
|
|
|
if (type == "type") target.generateType(def as TypeDefinition);
|
|
|
|
else if (type == "enum") target.generateEnum(def as EnumDefinition);
|
|
|
|
else if (type == "service")
|
|
|
|
target.generateService(def as ServiceDefinition);
|
|
|
|
});
|
2022-01-05 21:16:17 +00:00
|
|
|
if (target.finalize) target.finalize(ir.steps);
|
2021-12-31 21:38:26 +00:00
|
|
|
}
|