Skip to content

Commit

Permalink
Add custom scripts for each branch record
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrushC committed Sep 20, 2024
1 parent 4180030 commit 090969c
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 19 deletions.
6 changes: 3 additions & 3 deletions dist/assets/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/assets/vendor.js

Large diffs are not rendered by default.

59 changes: 58 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { app, BrowserWindow, screen, ipcMain, dialog, session, shell, Menu } from "electron";
import electronUpdaterPkg from "electron-updater";
import fs from "fs";
import path from "path";
import { fork } from "child_process";
import { fileURLToPath } from "url";
Expand Down Expand Up @@ -390,7 +391,63 @@ ipcMain.handle("open-tortoisesvn-diff", async (event, data) => {
});
});

// Custom command IPCS
ipcMain.handle("fetch-custom-scripts", async () => {
const { configFolderPath } = packageJson;
const scripts = [];

try {
const files = fs.readdirSync(configFolderPath);

files.forEach((file) => {
const ext = path.extname(file);
if ([".bat", ".ps1"].includes(ext.toLowerCase())) {
const fullPath = path.join(configFolderPath, file);
scripts.push({
fileName: path.parse(file).name,
path: fullPath,
type: ext.toLowerCase() === ".bat" ? "batch" : "powershell",
});
}
});
} catch (error) {
logger.error(`Error reading the config folder: ${error.message}`);
return { success: false, error: error.message };
}

// Return the list of scripts found
return { success: true, scripts };
});

ipcMain.handle("run-custom-script", async (event, data) => {
const { scriptType, scriptPath, branchData } = data;
logger.info(`Running custom script: ${scriptPath} (${scriptType}) with branch data: ${JSON.stringify(branchData)}`);

return new Promise((resolve, reject) => {
let command = "";
const { id, "Branch Folder": branchFolder, "Branch Version": branchVersion, "SVN Branch": svnBranch } = branchData;

const args = `"${id}" "${branchFolder}" "${branchVersion}" "${svnBranch}"`;

if (scriptType === "batch") {
command = `start cmd /k "${scriptPath}" ${args}`;
} else if (scriptType === "powershell") {
command = `start powershell -NoExit -ExecutionPolicy Bypass -File "${scriptPath}" -id "${id}" -branchFolder "${branchFolder}" -branchVersion "${branchVersion}" -svnBranch "${svnBranch}"`;
}

exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
reject({ success: false, error: error.message });
} else if (stderr) {
console.error(`Stderr: ${stderr}`);
reject({ success: false, error: stderr });
} else {
console.log(`Stdout: ${stdout}`);
resolve({ success: true });
}
});
});
});

ipcMain.handle("download-update", () => {
return autoUpdater.downloadUpdate();
Expand Down
2 changes: 2 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("electron", {
getAppVersion: () => ipcRenderer.invoke("app-version"),
openTortoiseSVNDiff: (data) => ipcRenderer.invoke("open-tortoisesvn-diff", data),
fetchCustomScripts: () => ipcRenderer.invoke("fetch-custom-scripts"),
runCustomScript: (data) => ipcRenderer.invoke("run-custom-script", data),
onAppClosing: (callback) => ipcRenderer.on("app-closing", callback),
removeAppClosingListener: () => ipcRenderer.removeAllListeners("app-closing"),
downloadUpdate: () => ipcRenderer.invoke("download-update"),
Expand Down
16 changes: 16 additions & 0 deletions src/AppContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const AppContext = createContext({
setSelectedBranches: (_) => {},
showSelectedBranchesLog: false,
setShowSelectedBranchesLog: (_) => {},
customScripts: [],
setCustomScripts: (_) => {},
isCommitMode: false,
setIsCommitMode: (_) => {},
selectedBranchStatuses: [],
Expand Down Expand Up @@ -118,6 +120,7 @@ export const AppProvider = ({ children }) => {
const branchTableGridRef = useRef(null);
const [selectedBranches, setSelectedBranches] = useState([]);
const [showSelectedBranchesLog, setShowSelectedBranchesLog] = useState(false);
const [customScripts, setCustomScripts] = useState([]);

// Props used in SectionCommit
const [isCommitMode, setIsCommitMode] = useState(false);
Expand Down Expand Up @@ -173,6 +176,17 @@ export const AppProvider = ({ children }) => {
setShowCommitView(false);
}, [configurableRowData]);

useEffect(() => {
if (!window.electron) return;
window.electron.fetchCustomScripts().then((data) => {
if (data.success) {
setCustomScripts(data.scripts);
return;
}
toast(createToastConfig(data.error, "error", 0, true));
});
}, [configurableRowData]);

/**** SectionCommit ****/
// Scroll to the commit region when it is in commit mode
useEffect(() => {
Expand Down Expand Up @@ -263,6 +277,8 @@ export const AppProvider = ({ children }) => {
setSelectedBranches,
showSelectedBranchesLog,
setShowSelectedBranchesLog,
customScripts,
setCustomScripts,
isCommitMode,
setIsCommitMode,
selectedBranchStatuses,
Expand Down
1 change: 0 additions & 1 deletion src/components/ButtonIconTooltip.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useCallback } from "react";
export default function ButtonIconTooltip(props) {
const { icon, onClick, colorScheme, label, size, placement, isDisabled = false } = props;


const handleClick = useCallback(() => {
if (onClick) onClick();
}, [onClick]);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Heading, Icon, IconButton, Image, Link, Tooltip, useColorMode, Wrap, WrapItem } from "@chakra-ui/react";
import { Heading, Icon, Image, Link, useColorMode, Wrap, WrapItem } from "@chakra-ui/react";
import React, { useCallback } from "react";
import Logo from "../assets/Titan.png";
import { useApp } from "../AppContext";
Expand All @@ -11,7 +11,7 @@ import ButtonElectron from "./ButtonElectron";
import ButtonIconTooltip from "./ButtonIconTooltip";

export default function Header() {
const { config, isDebug, setIsDebug } = useApp();
const { isDebug, setIsDebug } = useApp();
const { emitOpenConfig } = useSocketEmits();
const { RaiseClientNotificaiton } = useNotifications();
const { colorMode, toggleColorMode } = useColorMode();
Expand Down
24 changes: 14 additions & 10 deletions src/components/TableBranches.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ import { CopyIcon, DragHandleIcon } from "@chakra-ui/icons";
import { AgGridReact } from "ag-grid-react";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { useApp } from "../AppContext";
import { Flex, IconButton, Tooltip } from "@chakra-ui/react";
import { Flex } from "@chakra-ui/react";
import { stripBranchInfo } from "../utils/CommonConfig";
import useWindowDimensions from "../hooks/useWindowDimensions";
import { VscVscode } from "react-icons/vsc";
import { FaTerminal } from "react-icons/fa6";
import { RiFilePaper2Fill } from "react-icons/ri";
import ButtonElectron from "./ButtonElectron";
import ButtonIconTooltip from "./ButtonIconTooltip";

export default function TableBranches({ rowData, onRowValueChanged }) {
const { config, branchTableGridRef, updateConfig, isDebug, selectedBranches, setSelectedBranches, setSelectedBranchStatuses, setShowCommitView } = useApp();
const { config, branchTableGridRef, updateConfig, isDebug, selectedBranches, setSelectedBranches, setSelectedBranchStatuses, customScripts, setShowCommitView } = useApp();
const windowDimensions = useWindowDimensions();
const [isTallScreen, setIsTallScreen] = useState(windowDimensions.height > 768);

const executeCustomScript = useCallback((scriptType, scriptPath, branchData) => {
window.electron.runCustomScript({ scriptType, scriptPath, branchData }).then((result) => {
console.log("Custom Script Result: ", result);
});
}, []);

const copyRow = useCallback(
(currentRowData) => {
const newRow = {
Expand Down Expand Up @@ -102,18 +107,17 @@ export default function TableBranches({ rowData, onRowValueChanged }) {
editable: false,
cellRenderer: (params) => (
<Flex columnGap={1}>
{/* Custom commands which is dynamic in size */}
<ButtonElectron icon={<RiFilePaper2Fill />} onClick={() => console.warn("Unused button")} colorScheme={"yellow"} label="Script file name" size="sm" />
<Tooltip label="Copy Row" hasArrow>
<IconButton colorScheme={"yellow"} aria-label="Copy Row" size="sm" onClick={() => copyRow(params.data)} icon={<CopyIcon />} />
</Tooltip>
{customScripts.map((script) => (
<ButtonElectron icon={<RiFilePaper2Fill />} onClick={() => executeCustomScript(script.type, script.path, params.data)} colorScheme={"yellow"} label={script.fileName} size="sm" />
))}
<ButtonIconTooltip icon={<CopyIcon />} onClick={() => copyRow(params.data)} colorScheme={"yellow"} label="Copy Row" size="sm" />
</Flex>
),
},
];

return isTallScreen ? [{ field: "", rowDrag: true, resizable: false, filter: false, suppressMovable: false, editable: false, width: 20, cellRenderer: DragHandleIcon, headerClass: "branch-table-header-cell", cellClass: "branch-table-body-cell" }, ...commonColDefs] : commonColDefs;
}, [config, isTallScreen, copyRow]);
}, [isTallScreen, customScripts, copyRow]);

useEffect(() => {
setIsTallScreen(windowDimensions.height > 768);
Expand Down

0 comments on commit 090969c

Please sign in to comment.