Enable auto cfg loading and running from arguments.
This commit is contained in:
+53
-2
@@ -120,13 +120,51 @@ function fallbackBrowseForFolder() {
|
||||
picker.click();
|
||||
}
|
||||
|
||||
function getConfigValue(content, key) {
|
||||
const lines = content.toString().split('\n');
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('[')) {
|
||||
continue;
|
||||
}
|
||||
const separator = trimmed.indexOf('=');
|
||||
if (separator === -1) {
|
||||
continue;
|
||||
}
|
||||
const param = trimmed.slice(0, separator).trim();
|
||||
if (param === key) {
|
||||
return trimmed.slice(separator + 1).trim();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function resolveConfigWorkPath(filename, content) {
|
||||
const configuredWorkPath = getConfigValue(content, 'workPath');
|
||||
if (!configuredWorkPath) {
|
||||
return null;
|
||||
}
|
||||
if (configuredWorkPath.startsWith('./')) {
|
||||
return path.resolve(path.dirname(filename), configuredWorkPath);
|
||||
}
|
||||
if (path.isAbsolute(configuredWorkPath)) {
|
||||
return configuredWorkPath;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function loadConfigFile(filename, autoRun = false) {
|
||||
console.log('Open file ' + filename);
|
||||
fs.readFile(filename.toString(), function read(err, data) {
|
||||
const configPath = Array.isArray(filename) ? filename[0] : filename.toString();
|
||||
console.log('Open file ' + configPath);
|
||||
fs.readFile(configPath, function read(err, data) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
setValues(data);
|
||||
const resolvedWorkPath = resolveConfigWorkPath(configPath, data);
|
||||
if (resolvedWorkPath) {
|
||||
setWorkFolder([resolvedWorkPath]);
|
||||
}
|
||||
if (autoRun) {
|
||||
setTimeout(() => startSim(), 0);
|
||||
}
|
||||
@@ -215,3 +253,16 @@ function firstLoad() {
|
||||
fallbackBrowseForFolder();
|
||||
});
|
||||
}
|
||||
|
||||
window.addEventListener('load', async function() {
|
||||
try {
|
||||
const startupOptions = await ipcRenderer.invoke('getStartupOptions');
|
||||
if (startupOptions && startupOptions.configPath) {
|
||||
setTimeout(() => {
|
||||
loadConfigFile(startupOptions.configPath, !!startupOptions.autoRun);
|
||||
}, 0);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('Failed to read startup options:', err);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const path = require('path');
|
||||
const { app, BrowserWindow, Menu, dialog, ipcMain, fs } = require('electron');
|
||||
const fs = require('fs');
|
||||
const { app, BrowserWindow, Menu, dialog, ipcMain } = require('electron');
|
||||
|
||||
let currentWorkPath = process.cwd();
|
||||
const startupOptions = parseStartupOptions(process.argv);
|
||||
@@ -12,11 +13,18 @@ function parseStartupOptions(argv) {
|
||||
autoRun: false,
|
||||
};
|
||||
|
||||
for (const arg of argv.slice(1)) {
|
||||
for (const arg of argv.slice(2)) {
|
||||
if (arg === '--run') {
|
||||
options.autoRun = true;
|
||||
} else if (!arg.startsWith('--') && options.configPath == null) {
|
||||
options.configPath = path.resolve(arg);
|
||||
const candidate = path.resolve(arg);
|
||||
if (
|
||||
path.extname(candidate).toLowerCase() === '.cfg' &&
|
||||
fs.existsSync(candidate) &&
|
||||
fs.statSync(candidate).isFile()
|
||||
) {
|
||||
options.configPath = candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,16 +93,6 @@ function createWindow () {
|
||||
}
|
||||
})
|
||||
|
||||
win.webContents.on('did-finish-load', () => {
|
||||
if (startupOptions.configPath) {
|
||||
win.webContents.send('openFileAndMaybeRun', {
|
||||
filename: startupOptions.configPath,
|
||||
autoRun: startupOptions.autoRun,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
let template = [
|
||||
{
|
||||
label: 'File',
|
||||
@@ -344,6 +342,10 @@ ipcMain.on('updateWorkPath', (event, folder) => {
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('getStartupOptions', async () => {
|
||||
return startupOptions;
|
||||
});
|
||||
|
||||
//ipcMain.on('browseFolder-send', (event, args) => {
|
||||
// dialog.showOpenDialog(null, args).then(filePaths => {
|
||||
// event.sender.dend('browseFolder', filePaths);
|
||||
|
||||
+5
-4
@@ -102,12 +102,13 @@ function resizePl(flag){
|
||||
width: newx,
|
||||
height: newy
|
||||
};
|
||||
const canRelayout = (plotDiv) => plotDiv && plotDiv.data && plotDiv.layout;
|
||||
if (flag == null) {
|
||||
Plotly.relayout(plotDiv1,update);
|
||||
Plotly.relayout(plotDiv2,update);
|
||||
if (canRelayout(plotDiv1)) Plotly.relayout(plotDiv1,update);
|
||||
if (canRelayout(plotDiv2)) Plotly.relayout(plotDiv2,update);
|
||||
} else if (flag == 1) {
|
||||
Plotly.relayout(plotDiv1,update);
|
||||
if (canRelayout(plotDiv1)) Plotly.relayout(plotDiv1,update);
|
||||
} else if ( flag == 2) {
|
||||
Plotly.relayout(plotDiv2,update);
|
||||
if (canRelayout(plotDiv2)) Plotly.relayout(plotDiv2,update);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
"build:fortran": "make -C fortran trimspNL-static && cp fortran/trimspNL-static resources/bin/trimspNL",
|
||||
"build:fortran:dynamic": "make -C fortran trimspNL && cp fortran/trimspNL resources/bin/trimspNL",
|
||||
"build:fortran:static": "npm run build:fortran",
|
||||
"start": "electron-forge start",
|
||||
"start": "electron-forge start --",
|
||||
"prepackage": "npm run build:fortran",
|
||||
"package": "electron-forge package",
|
||||
"premake": "npm run build:fortran",
|
||||
|
||||
Reference in New Issue
Block a user