implement onSetup hook in BuildScript for environment setup
This commit is contained in:
1 parent
25ed8ecd56
commit
ceef9db425
5 files changed
+115
-75
No files matched your search
+39
-31
@@ -1,41 +1,49 @@
|
||||
/* 批处理脚本 */
|
||||
/*
|
||||
如果有processTree,则先运行prcessTree,如果有返回,则进行后面的针对每个节点运行processNode
|
||||
如果没有定义processTree,则直接针对每个节点运行processNode
|
||||
如果有processTree,则先运行prcessTree,如果有返回,则进行后面的针对每个节点运行processNode
|
||||
如果没有定义processTree,则直接针对每个节点运行processNode
|
||||
|
||||
export interface TreeModel {
|
||||
version: string;
|
||||
name: string;
|
||||
desc?: string;
|
||||
export?: boolean;
|
||||
firstid: number;
|
||||
group: string[];
|
||||
import: string[];
|
||||
declvar: VarDef[];
|
||||
root: NodeModel;
|
||||
}
|
||||
export interface TreeModel {
|
||||
version: string;
|
||||
name: string;
|
||||
desc?: string;
|
||||
export?: boolean;
|
||||
firstid: number;
|
||||
group: string[];
|
||||
import: string[];
|
||||
declvar: VarDef[];
|
||||
root: NodeModel;
|
||||
}
|
||||
|
||||
export interface NodeModel {
|
||||
id: number;
|
||||
name: string;
|
||||
desc?: string;
|
||||
args?: { [key: string]: any };
|
||||
input?: string[];
|
||||
output?: string[];
|
||||
children?: NodeModel[];
|
||||
debug?: boolean;
|
||||
disabled?: boolean;
|
||||
path?: string;
|
||||
}
|
||||
export interface NodeModel {
|
||||
id: number;
|
||||
name: string;
|
||||
desc?: string;
|
||||
args?: { [key: string]: any };
|
||||
input?: string[];
|
||||
output?: string[];
|
||||
children?: NodeModel[];
|
||||
debug?: boolean;
|
||||
disabled?: boolean;
|
||||
path?: string;
|
||||
}
|
||||
|
||||
interface BuildScript {
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
*/
|
||||
({
|
||||
onSetup: (env) => {
|
||||
console.log("onSetup", env);
|
||||
console.log(env.workdir);
|
||||
console.log(env.nodeDefs.get("Wait"));
|
||||
console.log(env.path.dirname("sample/scripts/build.js"));
|
||||
console.log(env.fs.readFileSync("sample/scripts/build.js", "utf8"));
|
||||
},
|
||||
processTree: (tree) => {
|
||||
console.log(`processTree ${tree.name}`);
|
||||
return tree;
|
||||
|
||||
+32
-30
@@ -1,38 +1,40 @@
|
||||
/* 批处理脚本 */
|
||||
/*
|
||||
如果有processTree,则先运行prcessTree,如果有返回,则进行后面的针对每个节点运行processNode
|
||||
如果没有定义processTree,则直接针对每个节点运行processNode
|
||||
如果有processTree,则先运行prcessTree,如果有返回,则进行后面的针对每个节点运行processNode
|
||||
如果没有定义processTree,则直接针对每个节点运行processNode
|
||||
|
||||
export interface TreeModel {
|
||||
version: string;
|
||||
name: string;
|
||||
desc?: string;
|
||||
export?: boolean;
|
||||
firstid: number;
|
||||
group: string[];
|
||||
import: string[];
|
||||
declvar: VarDef[];
|
||||
root: NodeModel;
|
||||
}
|
||||
export interface TreeModel {
|
||||
version: string;
|
||||
name: string;
|
||||
desc?: string;
|
||||
export?: boolean;
|
||||
firstid: number;
|
||||
group: string[];
|
||||
import: string[];
|
||||
declvar: VarDef[];
|
||||
root: NodeModel;
|
||||
}
|
||||
|
||||
export interface NodeModel {
|
||||
id: number;
|
||||
name: string;
|
||||
desc?: string;
|
||||
args?: { [key: string]: any };
|
||||
input?: string[];
|
||||
output?: string[];
|
||||
children?: NodeModel[];
|
||||
debug?: boolean;
|
||||
disabled?: boolean;
|
||||
path?: string;
|
||||
}
|
||||
export interface NodeModel {
|
||||
id: number;
|
||||
name: string;
|
||||
desc?: string;
|
||||
args?: { [key: string]: any };
|
||||
input?: string[];
|
||||
output?: string[];
|
||||
children?: NodeModel[];
|
||||
debug?: boolean;
|
||||
disabled?: boolean;
|
||||
path?: string;
|
||||
}
|
||||
|
||||
interface BatchScript {
|
||||
processTree?(tree: TreeModel, path: string): TreeModel;
|
||||
|
||||
processNode?(node: NodeModel, tree: TreeModel): NodeModel;
|
||||
}
|
||||
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;
|
||||
}
|
||||
*/
|
||||
({
|
||||
processTree: (tree) => {
|
||||
|
||||
@@ -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")}>
|
||||
|
||||
@@ -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
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user