build project in command line

This commit is contained in:
codetypes committed 2025-01-25 21:55:00 +08:00
1 parent da41704322
commit a0d0ca0ca9
5 files changed
+110 -11

No files matched your search

+27 -4
View File
@@ -1,9 +1,7 @@
import { app, BrowserWindow, shell, ipcMain } from "electron"; import { app, BrowserWindow, ipcMain, shell } from "electron";
import { release } from "node:os"; import { release } from "node:os";
import path, { join } from "node:path"; import path, { join } from "node:path";
import { update } from "./update"; import { argv } from "node:process";
import * as fs from "fs";
// The built directory structure // The built directory structure
// //
@@ -47,6 +45,20 @@ const url = process.env.VITE_DEV_SERVER_URL;
const indexHtml = join(process.env.DIST, "index.html"); const indexHtml = join(process.env.DIST, "index.html");
const windows: Workspace[] = []; const windows: Workspace[] = [];
let buildProject: string;
let buildOutput: string;
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
if (arg === "--project") {
buildProject = argv[i + 1];
i++;
} else if (arg === "--build") {
buildOutput = argv[i + 1];
i++;
}
}
async function createWindow(projectPath?: string) { async function createWindow(projectPath?: string) {
const win = new BrowserWindow({ const win = new BrowserWindow({
title: "Behaviour3 Editor", title: "Behaviour3 Editor",
@@ -98,6 +110,10 @@ async function createWindow(projectPath?: string) {
win?.webContents.send("open-project", workspace.projectPath); win?.webContents.send("open-project", workspace.projectPath);
} }
if (buildProject && buildOutput) {
win?.webContents.send("build-project", buildProject, buildOutput);
}
const nextWin = BrowserWindow.getAllWindows().at(-1); const nextWin = BrowserWindow.getAllWindows().at(-1);
if (nextWin) { if (nextWin) {
nextWin.focus(); nextWin.focus();
@@ -110,6 +126,13 @@ async function createWindow(projectPath?: string) {
win.on("closed", () => { win.on("closed", () => {
const index = windows.findIndex((w) => w.window === win); const index = windows.findIndex((w) => w.window === win);
windows.splice(index, 1); windows.splice(index, 1);
if (buildOutput && buildProject && windows.length === 0) {
app.exit(0);
} else {
buildOutput = undefined;
buildProject = undefined;
}
}); });
// Make all links open with the browser, not with the application // Make all links open with the browser, not with the application
+48 -1
View File
@@ -3,8 +3,16 @@ import { useWorkspace } from "@/contexts/workspace-context";
import * as b3util from "@/misc/b3util"; import * as b3util from "@/misc/b3util";
import i18n from "@/misc/i18n"; import i18n from "@/misc/i18n";
import { Hotkey, isMacos, sendInputEvent } from "@/misc/keys"; import { Hotkey, isMacos, sendInputEvent } from "@/misc/keys";
import Path from "@/misc/path";
import { CheckOutlined } from "@ant-design/icons"; import { CheckOutlined } from "@ant-design/icons";
import { Menu as AppMenu, BrowserWindow, app, dialog } from "@electron/remote"; import {
Menu as AppMenu,
BrowserWindow,
app,
dialog,
getCurrentWindow,
getGlobal,
} from "@electron/remote";
import { Button, DropDownProps, Dropdown, Flex, FlexProps, LayoutProps, Space } from "antd"; import { Button, DropDownProps, Dropdown, Flex, FlexProps, LayoutProps, Space } from "antd";
import { ItemType } from "antd/es/menu/interface"; import { ItemType } from "antd/es/menu/interface";
import { ipcRenderer } from "electron"; import { ipcRenderer } from "electron";
@@ -37,6 +45,45 @@ ipcRenderer.on("refresh-app-men", () => {
useSetting.getState().load(); useSetting.getState().load();
}); });
ipcRenderer.on("build-project", (_, project, buildDir) => {
const mainConsole = getGlobal("console") as Console;
try {
let hasError = false;
project = Path.posixPath(project);
buildDir = Path.posixPath(buildDir);
if (!project.endsWith(".b3-workspace")) {
throw new Error(`'${project}' is not a workspace`);
}
const workspace = useWorkspace.getState();
workspace.workdir = Path.dirname(project);
workspace.loadNodeDefs();
for (const path of Path.ls(Path.dirname(project), true)) {
if (path.endsWith(".json")) {
const buildpath = buildDir + "/" + path.substring(workspace.workdir.length + 1);
const treeModel = b3util.createBuildData(path);
if (treeModel && treeModel.export === false) {
mainConsole.log("skip:", buildpath);
continue;
}
mainConsole.log("build:", buildpath);
if (!b3util.checkNodeData(treeModel?.root)) {
hasError = true;
}
fs.mkdirSync(Path.dirname(buildpath), { recursive: true });
fs.writeFileSync(buildpath, JSON.stringify(treeModel, null, 2));
}
}
if (hasError) {
mainConsole.error(i18n.t("buildFailed"));
} else {
mainConsole.log(i18n.t("buildCompleted"));
}
} catch (error) {
mainConsole.error(i18n.t("buildFailed"));
}
getCurrentWindow().close();
});
export const Menu: FC<LayoutProps> = () => { export const Menu: FC<LayoutProps> = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const [trigger, setTrigger] = useState<DropDownProps["trigger"]>(["click"]); const [trigger, setTrigger] = useState<DropDownProps["trigger"]>(["click"]);
+18
View File
@@ -1,3 +1,4 @@
import * as fs from "fs";
import path from "path"; import path from "path";
const Path = path; const Path = path;
@@ -6,6 +7,7 @@ declare module "path" {
interface PlatformPath { interface PlatformPath {
basenameWithoutExt(path: string): string; basenameWithoutExt(path: string): string;
posixPath(path: string): string; posixPath(path: string): string;
ls(path: string, recursive?: boolean): string[];
} }
} }
@@ -17,4 +19,20 @@ path.posixPath = (str: string) => {
return path.normalize(str).replace(/\\/g, "/"); return path.normalize(str).replace(/\\/g, "/");
}; };
const readdir = (dir: string, recursive: boolean, callback: (file: string) => void) => {
fs.readdirSync(dir).forEach((file) => {
file = Path.posixPath(dir + "/" + file);
callback(file);
if (recursive && fs.statSync(file).isDirectory()) {
readdir(file, recursive, callback);
}
});
};
path.ls = (dir: string, recursive?: boolean) => {
const paths: string[] = [];
readdir(dir, recursive ?? false, (file) => paths.push(file));
return paths.sort();
};
export default Path; export default Path;
+16 -5
View File
@@ -1,10 +1,21 @@
import { Context, NodeDef } from "@/behavior3/src/behavior3"; import { Context, Node, NodeDef } from "@/behavior3/src/behavior3";
import { DeepReadonly } from "../behavior3/src/behavior3/context";
class NodeContext extends Context {
loadTree(path: string): Promise<Node> {
throw new Error("Method not implemented.");
}
}
export const zhNodeDef = () => { export const zhNodeDef = () => {
const context = new Context(); const context = new (class extends Context {
const defs: NodeDef[] = []; loadTree(path: string): Promise<Node> {
for (const v of context.processResolvers.values()) { throw new Error("Method not implemented.");
const descriptor = v.descriptor; }
})();
const defs: DeepReadonly<NodeDef>[] = [];
for (const k in context.nodeDefs) {
const descriptor = context.nodeDefs[k];
defs.push(descriptor); defs.push(descriptor);
} }
defs.sort((a, b) => a.name.localeCompare(b.name)); defs.sort((a, b) => a.name.localeCompare(b.name));