2021-12-31 21:38:26 +00:00
|
|
|
import * as FS from "fs";
|
2022-04-13 19:01:12 +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-04-13 19:01:12 +00:00
|
|
|
constructor(
|
|
|
|
private outputFolder: string,
|
|
|
|
protected options: T & { allow_bytes: 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>) {
|
2022-04-13 19:01:12 +00:00
|
|
|
let resPath = Path.join(this.outputFolder, name);
|
|
|
|
let resDir = Path.dirname(resPath);
|
|
|
|
if (!FS.existsSync(resDir)) FS.mkdirSync(resDir, { recursive: true });
|
2021-12-31 21:38:26 +00:00
|
|
|
if (content instanceof Promise) {
|
2022-04-13 19:01:12 +00:00
|
|
|
content.then((res) => FS.writeFileSync(resPath, res));
|
2021-12-31 21:38:26 +00:00
|
|
|
} else {
|
2022-04-13 19:01:12 +00:00
|
|
|
FS.writeFileSync(resPath, content);
|
2021-12-31 21:38:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-04-13 19:01:12 +00:00
|
|
|
protected loadTemplateFolder(name: string) {
|
2022-01-05 21:16:17 +00:00
|
|
|
let root = Path.join(__dirname, "../templates/", name);
|
|
|
|
|
2022-04-13 19:01:12 +00:00
|
|
|
FSE.copySync(root, this.outputFolder, {});
|
2022-01-05 21:16:17 +00:00
|
|
|
}
|
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
|
|
|
}
|