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,则先运行prcessTree,如果有返回,则进行后面的针对每个节点运行processNode
|
||||||
如果没有定义processTree,则直接针对每个节点运行processNode
|
如果没有定义processTree,则直接针对每个节点运行processNode
|
||||||
|
|
||||||
export interface TreeModel {
|
export interface TreeModel {
|
||||||
version: string;
|
version: string;
|
||||||
name: string;
|
name: string;
|
||||||
desc?: string;
|
desc?: string;
|
||||||
export?: boolean;
|
export?: boolean;
|
||||||
firstid: number;
|
firstid: number;
|
||||||
group: string[];
|
group: string[];
|
||||||
import: string[];
|
import: string[];
|
||||||
declvar: VarDef[];
|
declvar: VarDef[];
|
||||||
root: NodeModel;
|
root: NodeModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NodeModel {
|
export interface NodeModel {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
desc?: string;
|
desc?: string;
|
||||||
args?: { [key: string]: any };
|
args?: { [key: string]: any };
|
||||||
input?: string[];
|
input?: string[];
|
||||||
output?: string[];
|
output?: string[];
|
||||||
children?: NodeModel[];
|
children?: NodeModel[];
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
path?: string;
|
path?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BuildScript {
|
interface BatchScript {
|
||||||
processTree?(tree: TreeModel, path: string): TreeModel | null;
|
onSetup?(env: Env): void;
|
||||||
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
|
processTree?(tree: TreeModel, path: string): TreeModel | null;
|
||||||
onWriteFile?(path: string, tree: TreeModel): void;
|
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
|
||||||
onComplete?(status: "success" | "failure"): void;
|
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) => {
|
processTree: (tree) => {
|
||||||
console.log(`processTree ${tree.name}`);
|
console.log(`processTree ${tree.name}`);
|
||||||
return tree;
|
return tree;
|
||||||
|
|||||||
+32
-30
@@ -1,38 +1,40 @@
|
|||||||
/* 批处理脚本 */
|
/* 批处理脚本 */
|
||||||
/*
|
/*
|
||||||
如果有processTree,则先运行prcessTree,如果有返回,则进行后面的针对每个节点运行processNode
|
如果有processTree,则先运行prcessTree,如果有返回,则进行后面的针对每个节点运行processNode
|
||||||
如果没有定义processTree,则直接针对每个节点运行processNode
|
如果没有定义processTree,则直接针对每个节点运行processNode
|
||||||
|
|
||||||
export interface TreeModel {
|
export interface TreeModel {
|
||||||
version: string;
|
version: string;
|
||||||
name: string;
|
name: string;
|
||||||
desc?: string;
|
desc?: string;
|
||||||
export?: boolean;
|
export?: boolean;
|
||||||
firstid: number;
|
firstid: number;
|
||||||
group: string[];
|
group: string[];
|
||||||
import: string[];
|
import: string[];
|
||||||
declvar: VarDef[];
|
declvar: VarDef[];
|
||||||
root: NodeModel;
|
root: NodeModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NodeModel {
|
export interface NodeModel {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
desc?: string;
|
desc?: string;
|
||||||
args?: { [key: string]: any };
|
args?: { [key: string]: any };
|
||||||
input?: string[];
|
input?: string[];
|
||||||
output?: string[];
|
output?: string[];
|
||||||
children?: NodeModel[];
|
children?: NodeModel[];
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
path?: string;
|
path?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BatchScript {
|
interface BatchScript {
|
||||||
processTree?(tree: TreeModel, path: string): TreeModel;
|
onSetup?(env: Env): void;
|
||||||
|
processTree?(tree: TreeModel, path: string): TreeModel | null;
|
||||||
processNode?(node: NodeModel, tree: TreeModel): NodeModel;
|
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
|
||||||
}
|
onWriteFile?(path: string, tree: TreeModel): void;
|
||||||
|
onComplete?(status: "success" | "failure"): void;
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
({
|
({
|
||||||
processTree: (tree) => {
|
processTree: (tree) => {
|
||||||
|
|||||||
@@ -1487,7 +1487,10 @@ const NodeDefInspector: FC = () => {
|
|||||||
form.setFieldValue("type", def.type);
|
form.setFieldValue("type", def.type);
|
||||||
form.setFieldValue("desc", def.desc);
|
form.setFieldValue("desc", def.desc);
|
||||||
form.setFieldValue("doc", def.doc);
|
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) {
|
if (def.children === undefined || def.children === -1) {
|
||||||
form.setFieldValue("children", t("node.children.unlimited"));
|
form.setFieldValue("children", t("node.children.unlimited"));
|
||||||
} else {
|
} else {
|
||||||
@@ -1526,7 +1529,12 @@ const NodeDefInspector: FC = () => {
|
|||||||
</Form.Item>
|
</Form.Item>
|
||||||
{workspace.groupDefs.length > 0 && def.group?.length && (
|
{workspace.groupDefs.length > 0 && def.group?.length && (
|
||||||
<Form.Item name="group" label={t("node.group")}>
|
<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>
|
||||||
)}
|
)}
|
||||||
<Form.Item name="children" label={t("node.children")}>
|
<Form.Item name="children" label={t("node.children")}>
|
||||||
|
|||||||
@@ -421,14 +421,22 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
|
|||||||
try {
|
try {
|
||||||
console.log("run script", scriptPath);
|
console.log("run script", scriptPath);
|
||||||
const str = fs.readFileSync(scriptPath, "utf8");
|
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) => {
|
workspace.allFiles.forEach((file) => {
|
||||||
let tree: TreeModel | null = readTree(file.path);
|
let tree: TreeModel | null = readTree(file.path);
|
||||||
tree = b3util.processBatch(tree, file.path, batch);
|
tree = b3util.processBatch(tree, file.path, batch);
|
||||||
if (tree) {
|
if (tree) {
|
||||||
|
batch.onWriteFile?.(file.path, tree);
|
||||||
fs.writeFileSync(file.path, JSON.stringify(tree, null, 2));
|
fs.writeFileSync(file.path, JSON.stringify(tree, null, 2));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
batch.onComplete?.("success");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
hasError = true;
|
hasError = true;
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|||||||
+25
-11
@@ -21,22 +21,27 @@ import {
|
|||||||
import Path from "./path";
|
import Path from "./path";
|
||||||
import { readJson, readTree } from "./util";
|
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> {
|
export class NodeDefs extends Map<string, NodeDef> {
|
||||||
get(key: string): NodeDef {
|
get(key: string): NodeDef {
|
||||||
return super.get(key) ?? unknownNodeDef;
|
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 nodeDefs: NodeDefs = new NodeDefs();
|
||||||
export let groupDefs: string[] = [];
|
export let groupDefs: string[] = [];
|
||||||
export let usingGroups: Record<string, boolean> = {};
|
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) => {
|
export const buildProject = (project: string, buildDir: string) => {
|
||||||
let hasError = false;
|
let hasError = false;
|
||||||
const settings = readJson<WorkspaceModel>(project).settings;
|
const settings = readJson<WorkspaceModel>(project).settings;
|
||||||
let buildScript: BuildScript | undefined;
|
let buildScript: BatchScript | undefined;
|
||||||
if (settings.checkExpr) {
|
if (settings.checkExpr) {
|
||||||
setCheckExpr(true);
|
setCheckExpr(true);
|
||||||
}
|
}
|
||||||
@@ -823,6 +828,15 @@ export const buildProject = (project: string, buildDir: string) => {
|
|||||||
console.error(`'${scriptPath}' is not a valid build script`);
|
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)) {
|
for (const path of Path.ls(Path.dirname(project), true)) {
|
||||||
if (path.endsWith(".json")) {
|
if (path.endsWith(".json")) {
|
||||||
const buildpath = buildDir + "/" + path.substring(workdir.length + 1);
|
const buildpath = buildDir + "/" + path.substring(workdir.length + 1);
|
||||||
|
|||||||
Reference in new issue
Block a user