Skip to content

Commit

Permalink
Open ARC in Terminal / VSCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Lukasczyk committed Sep 30, 2024
1 parent d5efd01 commit 285843b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
93 changes: 92 additions & 1 deletion packages/main/src/mainWindow.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BrowserWindow,Menu,shell } from 'electron';
import { BrowserWindow,Menu,shell,ipcMain } from 'electron';
import path from 'path';
import { join } from 'path';
import { URL } from 'url';

import {exec,execSync,spawn} from 'child_process';

const contextMenu = require('electron-context-menu');
contextMenu({
showSaveImageAs: true
Expand Down Expand Up @@ -50,6 +52,15 @@ async function createWindow() {
return mainWindow;
}

const isCommandAvailable = command=>{
try {
execSync(`which ${command}`, { stdio: 'ignore' });
return true;
} catch (error) {
return false;
}
}

// -------------------------------------------------------------------------

/**
Expand All @@ -68,6 +79,23 @@ export async function restoreOrCreateWindow() {
{ role: 'toggleDevTools' },
]
},
{
label: 'Tools',
submenu: [
{
label: 'Command Window',
click: () => {
window.webContents.send('CORE.getArcRoot', 'CORE.openTerminal');
}
},
{
label: 'Visual Studio Code',
click: () => {
window.webContents.send('CORE.getArcRoot', 'CORE.openVSCode');
}
},
]
},
{
role: 'help',
submenu: [
Expand Down Expand Up @@ -111,6 +139,69 @@ export async function restoreOrCreateWindow() {
);
}

ipcMain.handle('CORE.openVSCode', (e,path)=>{
if(!path) return window.webContents.send('CORE.messagePrompt', 'No ARC Open');

const command = `code ${path}`;
exec(command, (error, stdout, stderr) => {
if (error) {
const msg = `Error opening VS Code: ${error.message}`;
console.error(msg);
window.webContents.send('CORE.messagePrompt', msg);
}
});
});

ipcMain.handle('CORE.openTerminal', (e,path)=>{
if(!path) return window.webContents.send('CORE.messagePrompt', 'No ARC Open');

let command;
// Detect platform and assign the correct terminal command
if (process.platform === 'win32') {
command = `start cmd.exe /k "cd /d ${path}"`;
} else if (process.platform === 'darwin') {
// macOS: Use 'open' to open Terminal app in the specified directory
command = `open -a Terminal "${path}"`;
} else if (process.platform === 'linux') {

let terminal;

if (isCommandAvailable('gnome-terminal')) {
terminal = 'gnome-terminal';
} else if (isCommandAvailable('xterm')) {
terminal = 'xterm';
} else if (isCommandAvailable('konsole')) {
terminal = 'konsole';
} else {
const msg = 'No supported terminal emulator found.';
console.error(msg);
return window.webContents.send('CORE.messagePrompt', msg);
}

// Linux: Set up terminal command depending on the emulator found
if (terminal === 'gnome-terminal') {
command = `gnome-terminal -- bash -c "cd '${path}' && exec bash"`;
} else if (terminal === 'xterm') {
command = `xterm -ls -e "cd '${path}' && exec bash"`;
} else if (terminal === 'konsole') {
command = `konsole --workdir '${path}' -e bash -c "exec bash"`;
}
} else {
const msg = 'Platform not supported.';
console.error(msg);
return window.webContents.send('CORE.messagePrompt', msg);
}

// Execute the terminal opening command
exec(command, { env: Object.create(process.env) }, (error, stdout, stderr) => {
if (error) {
const msg = `Error: ${error.message}`;
console.error(msg);
return window.webContents.send('CORE.messagePrompt', msg);
}
});
});

const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);

Expand Down
17 changes: 16 additions & 1 deletion packages/renderer/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,25 @@ const newLocalArc = async ()=>{
const showHomeView = ()=>{
AppProperties.state=AppProperties.STATES.HOME;
}
};
const messagePrompt = msg => {
console.log(msg);
$q.dialog({
component: ConfirmationDialog,
componentProps: {
title: `Error`,
msg: msg,
ok_text: 'Ok',
ok_color: 'secondary',
}
});
};
onMounted(async () => {
window.ipc.on('CORE.MSG', console.log);
window.ipc.on('CORE.messagePrompt', messagePrompt);
iProps.version = await window.ipc.invoke('CORE.getVersion');
const git_running = await window.ipc.invoke('GitService.run','--version');
if(!git_running[0]){
Expand Down
1 change: 1 addition & 0 deletions packages/renderer/src/ArcControlService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,6 @@ const ArcControlService = {
const debouncedReadARC = pDebounce(ArcControlService.readARC, 300);

window.ipc.on('LocalFileSystemService.updatePath', ArcControlService.updateARCfromFS);
window.ipc.on('CORE.getArcRoot', callback=>window.ipc.invoke(callback, ArcControlService.props.arc_root));

export default ArcControlService;

0 comments on commit 285843b

Please sign in to comment.