implement onSetup hook in BuildScript for environment setup

This commit is contained in:
codetypes committed 2025-03-24 13:03:42 +08:00
1 parent 25ed8ecd56
commit ceef9db425
5 files changed
+115 -75

No files matched your search

+10 -2
View File
@@ -1487,7 +1487,10 @@ const NodeDefInspector: FC = () => {
form.setFieldValue("type", def.type);
form.setFieldValue("desc", def.desc);
form.setFieldValue("doc", def.doc);
form.setFieldValue("group", def.group);
form.setFieldValue(
"group",
def.group?.map((g) => ({ label: g, value: g }))
);
if (def.children === undefined || def.children === -1) {
form.setFieldValue("children", t("node.children.unlimited"));
} else {
@@ -1526,7 +1529,12 @@ const NodeDefInspector: FC = () => {
</Form.Item>
{workspace.groupDefs.length > 0 && def.group?.length && (
<Form.Item name="group" label={t("node.group")}>
<GroupDefItem disabled={true} />
<Select
style={{ fontSize: "13px" }}
mode="multiple"
suffixIcon={null}
disabled={true}
/>
</Form.Item>
)}
<Form.Item name="children" label={t("node.children")}>
+9 -1
View File
@@ -421,14 +421,22 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
try {
console.log("run script", scriptPath);
const str = fs.readFileSync(scriptPath, "utf8");
const batch = eval(str) as b3util.BuildScript;
const batch = eval(str) as b3util.BatchScript;
batch.onSetup?.({
fs,
path: Path,
workdir: workspace.workdir,
nodeDefs: get().nodeDefs,
});
workspace.allFiles.forEach((file) => {
let tree: TreeModel | null = readTree(file.path);
tree = b3util.processBatch(tree, file.path, batch);
if (tree) {
batch.onWriteFile?.(file.path, tree);
fs.writeFileSync(file.path, JSON.stringify(tree, null, 2));
}
});
batch.onComplete?.("success");
} catch (error) {
hasError = true;
console.error(error);
+25 -11
View File
@@ -21,22 +21,27 @@ import {
import Path from "./path";
import { readJson, readTree } from "./util";
export interface BatchScript {
processTree?(tree: TreeModel, path: string): TreeModel | null;
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
}
export interface BuildScript extends BatchScript {
onWriteFile?(path: string, tree: TreeModel): void;
onComplete?(status: "success" | "failure"): void;
}
export class NodeDefs extends Map<string, NodeDef> {
get(key: string): NodeDef {
return super.get(key) ?? unknownNodeDef;
}
}
type Env = {
fs: typeof fs;
path: typeof Path;
workdir: string;
nodeDefs: NodeDefs;
};
export interface BatchScript {
onSetup?(env: Env): void;
processTree?(tree: TreeModel, path: string): TreeModel | null;
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
onWriteFile?(path: string, tree: TreeModel): void;
onComplete?(status: "success" | "failure"): void;
}
export let nodeDefs: NodeDefs = new NodeDefs();
export let groupDefs: string[] = [];
export let usingGroups: Record<string, boolean> = {};
@@ -811,7 +816,7 @@ export const processBatch = (tree: TreeModel | null, path: string, batch: BatchS
export const buildProject = (project: string, buildDir: string) => {
let hasError = false;
const settings = readJson<WorkspaceModel>(project).settings;
let buildScript: BuildScript | undefined;
let buildScript: BatchScript | undefined;
if (settings.checkExpr) {
setCheckExpr(true);
}
@@ -823,6 +828,15 @@ export const buildProject = (project: string, buildDir: string) => {
console.error(`'${scriptPath}' is not a valid build script`);
}
}
if (buildScript) {
buildScript.onSetup?.({
fs,
path: Path,
workdir,
nodeDefs,
});
}
for (const path of Path.ls(Path.dirname(project), true)) {
if (path.endsWith(".json")) {
const buildpath = buildDir + "/" + path.substring(workdir.length + 1);