add hook function 'onComplete' on build
This commit is contained in:
1 parent
645a88f661
commit
25ed8ecd56
4 files changed
+61
-97
No files matched your search
+2
-49
@@ -1,15 +1,12 @@
|
|||||||
import { app, BrowserWindow, ipcMain, shell } from "electron";
|
import { app, BrowserWindow, ipcMain, shell } from "electron";
|
||||||
import * as fs from "node:fs";
|
|
||||||
import { createRequire } from "node:module";
|
import { createRequire } from "node:module";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { argv } from "node:process";
|
import { argv } from "node:process";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
import { type WorkspaceModel } from "../../src/contexts/workspace-context";
|
import { VERSION } from "../../src/misc/b3type";
|
||||||
import { VERSION, type FileVarDecl } from "../../src/misc/b3type";
|
|
||||||
import * as b3util from "../../src/misc/b3util";
|
import * as b3util from "../../src/misc/b3util";
|
||||||
import Path from "../../src/misc/path";
|
import Path from "../../src/misc/path";
|
||||||
import { readJson } from "../../src/misc/util";
|
|
||||||
|
|
||||||
const require = createRequire(import.meta.url);
|
const require = createRequire(import.meta.url);
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
@@ -52,7 +49,6 @@ if (buildOutput || buildProject || buildHelp) {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
let hasError = false;
|
|
||||||
const project = Path.posixPath(buildProject!);
|
const project = Path.posixPath(buildProject!);
|
||||||
const buildDir = Path.posixPath(buildOutput!);
|
const buildDir = Path.posixPath(buildOutput!);
|
||||||
console.log("start build project:", project);
|
console.log("start build project:", project);
|
||||||
@@ -63,50 +59,7 @@ if (buildOutput || buildProject || buildHelp) {
|
|||||||
b3util.initWorkdir(workdir, (msg) => {
|
b3util.initWorkdir(workdir, (msg) => {
|
||||||
console.error(`${msg}`);
|
console.error(`${msg}`);
|
||||||
});
|
});
|
||||||
const settings = readJson<WorkspaceModel>(project).settings;
|
const hasError = b3util.buildProject(project, buildDir);
|
||||||
let buildScript: b3util.BuildScript | undefined;
|
|
||||||
if (settings.checkExpr) {
|
|
||||||
b3util.setCheckExpr(true);
|
|
||||||
}
|
|
||||||
if (settings.buildScript) {
|
|
||||||
const scriptPath = workdir + "/" + settings.buildScript;
|
|
||||||
try {
|
|
||||||
buildScript = eval(fs.readFileSync(scriptPath, "utf8"));
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`'${scriptPath}' is not a valid build script`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (const path of Path.ls(Path.dirname(project), true)) {
|
|
||||||
if (path.endsWith(".json")) {
|
|
||||||
const buildpath = buildDir + "/" + path.substring(workdir.length + 1);
|
|
||||||
let tree = b3util.createBuildData(path);
|
|
||||||
if (buildScript) {
|
|
||||||
tree = b3util.processBatch(tree, path, buildScript);
|
|
||||||
}
|
|
||||||
if (!tree) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (tree.export === false) {
|
|
||||||
console.log("skip:", buildpath);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
console.log("build:", buildpath);
|
|
||||||
const declare: FileVarDecl = {
|
|
||||||
import: tree.import.map((v) => ({ path: v, vars: [], depends: [] })),
|
|
||||||
declvar: tree.declvar.map((v) => ({ name: v.name, desc: v.desc })),
|
|
||||||
subtree: [],
|
|
||||||
};
|
|
||||||
b3util.refreshDeclare(tree.root, tree.group, declare);
|
|
||||||
if (!b3util.checkNodeData(tree?.root)) {
|
|
||||||
hasError = true;
|
|
||||||
}
|
|
||||||
if (buildScript?.writeFile) {
|
|
||||||
buildScript.writeFile(buildpath, tree);
|
|
||||||
}
|
|
||||||
fs.mkdirSync(Path.dirname(buildpath), { recursive: true });
|
|
||||||
fs.writeFileSync(buildpath, JSON.stringify(tree, null, 2));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (hasError) {
|
if (hasError) {
|
||||||
console.error("build failed***");
|
console.error("build failed***");
|
||||||
app.quit();
|
app.quit();
|
||||||
|
|||||||
@@ -29,11 +29,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface BuildScript {
|
interface BuildScript {
|
||||||
processTree?(tree: TreeModel, path: string): TreeModel | null;
|
processTree?(tree: TreeModel, path: string): TreeModel | null;
|
||||||
|
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
|
||||||
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
|
onWriteFile?(path: string, tree: TreeModel): void;
|
||||||
|
onComplete?(status: "success" | "failure"): void;
|
||||||
writeFile?(path: string, tree: TreeModel): void;
|
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
({
|
({
|
||||||
@@ -45,4 +44,7 @@
|
|||||||
console.log(`processNode ${tree.name} ${node.name}#${node.id}`);
|
console.log(`processNode ${tree.name} ${node.name}#${node.id}`);
|
||||||
return node;
|
return node;
|
||||||
},
|
},
|
||||||
|
onComplete: (status) => {
|
||||||
|
console.log(`onComplete ${status}`);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
@@ -394,47 +394,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
|
|||||||
const debug = console.debug;
|
const debug = console.debug;
|
||||||
console.debug = () => {};
|
console.debug = () => {};
|
||||||
try {
|
try {
|
||||||
let hasError = false;
|
const hasError = b3util.buildProject(workspace.path, buildDir);
|
||||||
|
|
||||||
let buildScript: b3util.BuildScript | undefined;
|
|
||||||
if (workspace.settings.buildScript) {
|
|
||||||
const scriptPath = workspace.workdir + "/" + workspace.settings.buildScript;
|
|
||||||
if (fs.existsSync(scriptPath)) {
|
|
||||||
buildScript = eval(fs.readFileSync(scriptPath, "utf8"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
workspace.allFiles.forEach((file) => {
|
|
||||||
const buildpath = buildDir + "/" + workspace.relative(file.path);
|
|
||||||
let tree = b3util.createBuildData(file.path);
|
|
||||||
if (buildScript) {
|
|
||||||
tree = b3util.processBatch(tree, file.path, buildScript);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tree) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tree.export === false) {
|
|
||||||
console.log("skip:", buildpath);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
console.log("build:", buildpath);
|
|
||||||
const declare: FileVarDecl = {
|
|
||||||
import: tree.import.map((v) => ({ path: v, vars: [], depends: [] })),
|
|
||||||
declvar: tree.declvar.map((v) => ({ name: v.name, desc: v.desc })),
|
|
||||||
subtree: [],
|
|
||||||
};
|
|
||||||
b3util.refreshDeclare(tree.root, tree.group, declare);
|
|
||||||
if (!b3util.checkNodeData(tree?.root)) {
|
|
||||||
hasError = true;
|
|
||||||
}
|
|
||||||
if (buildScript?.writeFile) {
|
|
||||||
buildScript.writeFile(buildpath, tree);
|
|
||||||
}
|
|
||||||
fs.mkdirSync(Path.dirname(buildpath), { recursive: true });
|
|
||||||
fs.writeFileSync(buildpath, JSON.stringify(tree, null, 2));
|
|
||||||
});
|
|
||||||
if (hasError) {
|
if (hasError) {
|
||||||
message.error(i18n.t("buildFailed"));
|
message.error(i18n.t("buildFailed"));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+51
-2
@@ -1,5 +1,6 @@
|
|||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { ExpressionEvaluator, NodeDef } from "../behavior3/src/behavior3";
|
import { ExpressionEvaluator, NodeDef } from "../behavior3/src/behavior3";
|
||||||
|
import { type WorkspaceModel } from "../contexts/workspace-context";
|
||||||
import {
|
import {
|
||||||
FileVarDecl,
|
FileVarDecl,
|
||||||
ImportDef,
|
ImportDef,
|
||||||
@@ -22,12 +23,12 @@ import { readJson, readTree } from "./util";
|
|||||||
|
|
||||||
export interface BatchScript {
|
export interface BatchScript {
|
||||||
processTree?(tree: TreeModel, path: string): TreeModel | null;
|
processTree?(tree: TreeModel, path: string): TreeModel | null;
|
||||||
|
|
||||||
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
|
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BuildScript extends BatchScript {
|
export interface BuildScript extends BatchScript {
|
||||||
writeFile?(path: string, tree: TreeModel): void;
|
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> {
|
||||||
@@ -807,6 +808,54 @@ export const processBatch = (tree: TreeModel | null, path: string, batch: BatchS
|
|||||||
return tree;
|
return tree;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const buildProject = (project: string, buildDir: string) => {
|
||||||
|
let hasError = false;
|
||||||
|
const settings = readJson<WorkspaceModel>(project).settings;
|
||||||
|
let buildScript: BuildScript | undefined;
|
||||||
|
if (settings.checkExpr) {
|
||||||
|
setCheckExpr(true);
|
||||||
|
}
|
||||||
|
if (settings.buildScript) {
|
||||||
|
const scriptPath = workdir + "/" + settings.buildScript;
|
||||||
|
try {
|
||||||
|
buildScript = eval(fs.readFileSync(scriptPath, "utf8"));
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`'${scriptPath}' is not a valid build script`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const path of Path.ls(Path.dirname(project), true)) {
|
||||||
|
if (path.endsWith(".json")) {
|
||||||
|
const buildpath = buildDir + "/" + path.substring(workdir.length + 1);
|
||||||
|
let tree = createBuildData(path);
|
||||||
|
if (buildScript) {
|
||||||
|
tree = processBatch(tree, path, buildScript);
|
||||||
|
}
|
||||||
|
if (!tree) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (tree.export === false) {
|
||||||
|
console.log("skip:", buildpath);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
console.log("build:", buildpath);
|
||||||
|
const declare: FileVarDecl = {
|
||||||
|
import: tree.import.map((v) => ({ path: v, vars: [], depends: [] })),
|
||||||
|
declvar: tree.declvar.map((v) => ({ name: v.name, desc: v.desc })),
|
||||||
|
subtree: [],
|
||||||
|
};
|
||||||
|
refreshDeclare(tree.root, tree.group, declare);
|
||||||
|
if (!checkNodeData(tree?.root)) {
|
||||||
|
hasError = true;
|
||||||
|
}
|
||||||
|
buildScript?.onWriteFile?.(buildpath, tree);
|
||||||
|
fs.mkdirSync(Path.dirname(buildpath), { recursive: true });
|
||||||
|
fs.writeFileSync(buildpath, JSON.stringify(tree, null, 2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buildScript?.onComplete?.(hasError ? "failure" : "success");
|
||||||
|
return hasError;
|
||||||
|
};
|
||||||
|
|
||||||
export const createFileData = (data: TreeGraphData, includeSubtree?: boolean) => {
|
export const createFileData = (data: TreeGraphData, includeSubtree?: boolean) => {
|
||||||
const nodeData: NodeModel = {
|
const nodeData: NodeModel = {
|
||||||
id: Number(data.id),
|
id: Number(data.id),
|
||||||
|
|||||||
Reference in new issue
Block a user