Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions extension/src/tree_view/treeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { registerWebview } from '../util/webview_handler';
import { trimFileName } from '../util/trim_file_name';
import { readExtensionConfig, updateExtensionConfig } from '../util/extensionHandler';
import webviewReceiveMessageHandler from "../util/webview_receive_message_handler";
import runTerminalCommand from '../util/run_terminal_command';
// import runTerminalCommand from '../util/run_python';
import { checkExtensionConfigEnv } from '../util/extension_initial_check';

export default function treeview(context: vscode.ExtensionContext) {
Expand Down Expand Up @@ -97,22 +97,24 @@ export default function treeview(context: vscode.ExtensionContext) {

const commandIdaesRunInfo = `${sorceCommand} && ${activateCommand} && idaes-run "${fileName}" "${outputFileName}" --info`;

let resolvedStepsData: any = null;
try {
resolvedStepsData = await runTerminalCommand(context, commandIdaesRunInfo, shellType, outputFileName, "currentFileInfo");
console.log(resolvedStepsData);
} catch (err: any) {
console.error(`Error running terminal command during tree view load: ${err.message}`);
webviewView.webview.postMessage({
type: 'switch_tab',
activate_tab_name: trimFileName(fileName),
idaesRunInfo: null,
initError: `Failed to load flowsheet info: ${err.message}. Please check your configuration.`,
isLoading: false,
time: new Date().toISOString(),
});
return;
}
// TODO: Get correct value from <somewhere>
const resolvedStepsData = {"steps": ["step1", "step2"]};
// let resolvedStepsData: any = null;
// try {
// resolvedStepsData = await runTerminalCommand(context, commandIdaesRunInfo, shellType, outputFileName, "currentFileInfo");
// console.log(resolvedStepsData);
// } catch (err: any) {
// console.error(`Error running terminal command during tree view load: ${err.message}`);
// webviewView.webview.postMessage({
// type: 'switch_tab',
// activate_tab_name: trimFileName(fileName),
// idaesRunInfo: null,
// initError: `Failed to load flowsheet info: ${err.message}. Please check your configuration.`,
// isLoading: false,
// time: new Date().toISOString(),
// });
// return;
// }

// 5. Update UI with the result (success)
webviewView.webview.postMessage({
Expand Down
44 changes: 23 additions & 21 deletions extension/src/util/activate_tab_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode';
import { brodcastMessage } from './webview_handler';
import { trimFileName } from './trim_file_name';
import { readExtensionConfig } from './extensionHandler';
import runTerminalCommand from './run_terminal_command';
import runTerminalCommand from './run_python';
import { checkExtensionConfigEnv } from './extension_initial_check';

function getOpenPythonFiles() {
Expand Down Expand Up @@ -101,26 +101,28 @@ export default function activateTabListener(context: vscode.ExtensionContext) {

const commandIdaesRunInfo = `${sorceCommand} && ${activateCommand} && idaes-run "${currentActivateTabFileName}" "${outputFileName}" --info`;

let stepsData: any;
try {
stepsData = await runTerminalCommand(context, commandIdaesRunInfo, shellType, outputFileName, "currentFileInfo");
} catch (err: any) {
console.error(`Error running terminal command during tab switch: ${err.message}`);
stepsData = null;
brodcastMessage(
{
type: 'switch_tab',
message: `Failed to load flowsheet info for new tab: ${err.message}`,
activate_tab_name: activateFileName,
idaesRunInfo: null,
initError: `Failed to load flowsheet info for new tab: ${err.message}`,
isLoading: false,
open_python_files: getOpenPythonFiles(),
time: new Date().toISOString(),
}
);
return;
}
//let stepsData: any;
// TODO: Get correct value from <somewhere>
const stepsData = {"steps": ["step1", "step2"]};
// try {
// stepsData = await runTerminalCommand(context, commandIdaesRunInfo, shellType, outputFileName, "currentFileInfo");
// } catch (err: any) {
// console.error(`Error running terminal command during tab switch: ${err.message}`);
// stepsData = null;
// brodcastMessage(
// {
// type: 'switch_tab',
// message: `Failed to load flowsheet info for new tab: ${err.message}`,
// activate_tab_name: activateFileName,
// idaesRunInfo: null,
// initError: `Failed to load flowsheet info for new tab: ${err.message}`,
// isLoading: false,
// open_python_files: getOpenPythonFiles(),
// time: new Date().toISOString(),
// }
// );
// return;
// }

// brodcast to all web app panel notice tab is switched
console.log('Brodcast switch_tab to all web app panels');
Expand Down
132 changes: 68 additions & 64 deletions extension/src/util/run_flowsheet.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,76 @@
import * as vscode from 'vscode';
import { activateWebviews, brodcastMessage } from "./webview_handler";
import { IExtensionConfig, IFlowsheetRunResult } from '../interface';
import runTerminalCommand from "./run_terminal_command";
import execFlowsheet from "./run_python";
import openWebView from '../web_view/web_view_panel';

export default async function runFlowsheet(context: vscode.ExtensionContext, webview: vscode.Webview, selectedStep: string | undefined) {
const me = "runFlowsheet";
try {
const activateFileName = context.globalState.get<string>("activatedFileName");
const extensionConfig = context.globalState.get<IExtensionConfig>("extensionConfig");

// read run_flowsheet necessary params
let activateCommand = undefined;
let sourceTerminal = undefined;
let outputFileName = undefined;
let shell = undefined;
let vscodeContextStateName = 'flowsheetRunResult';

if (extensionConfig) {
sourceTerminal = extensionConfig.sorce_treminal;
activateCommand = extensionConfig.activate_command;
outputFileName = extensionConfig.output_file_name;
shell = extensionConfig.shell;
}

// error handler if missing param
if (!sourceTerminal || !activateCommand || !outputFileName || !shell || !vscodeContextStateName) {
webview.postMessage({
type: 'error',
message: `run_flowsheet raise an error, looks like you are trying to run a flowsheet, but missing one of following params: [
sourceTerminal: ${sourceTerminal},
activateCommand: ${activateCommand},
outputFileName: ${outputFileName},
shell: ${shell},
vscodeContextStateName: ${vscodeContextStateName}
From file webview_receive_message_handler.ts`
});
const activatedFileName = context.globalState.get<string>("activatedFileName");
if (activatedFileName === undefined) {
return;
}
const extensionConfig = context.globalState.get<IExtensionConfig>("extensionConfig");

// if webview is closed then open it to prevent extension cant find webview
if (!activateWebviews.get('webView')) {
await openWebView(context);
}

// GUARD: Prevent arbitrary file overwriting (e.g. wiping out ~/.zshrc)
if (!outputFileName.toLowerCase().trim().endsWith('.json')) {
vscode.window.showErrorMessage(`DANGER: Target output file "${outputFileName}" must be a .json file! Or it may overwrite your files. Check Extension Settings.`);
webview.postMessage({
type: 'error',
message: `run_flowsheet aborted: The 'Output File Name' parameter (${outputFileName}) must be a .json file. Refusing to execute to prevent overwriting critical system files.`
});
return;
}
// // read run_flowsheet necessary params
// let activateCommand = undefined;
// let sourceTerminal = undefined;
// let outputFileName = undefined;
// let shell = undefined;
const vscodeContextStateName = 'flowsheetRunResult';

// if (extensionConfig) {
// sourceTerminal = extensionConfig.sorce_treminal;
// activateCommand = extensionConfig.activate_command;
// outputFileName = extensionConfig.output_file_name;
// shell = extensionConfig.shell;
// }

// // error handler if missing param
// if (!sourceTerminal || !activateCommand || !outputFileName || !shell || !vscodeContextStateName) {
// webview.postMessage({
// type: 'error',
// message: `run_flowsheet raise an error, looks like you are trying to run a flowsheet, but missing one of following params: [
// sourceTerminal: ${sourceTerminal},
// activateCommand: ${activateCommand},
// outputFileName: ${outputFileName},
// shell: ${shell},
// vscodeContextStateName: ${vscodeContextStateName}
// From file webview_receive_message_handler.ts`
// });
// return;
// }

// // if webview is closed then open it to prevent extension cant find webview
// if (!activateWebviews.get('webView')) {
// await openWebView(context);
// }

// // GUARD: Prevent arbitrary file overwriting (e.g. wiping out ~/.zshrc)
// if (!outputFileName.toLowerCase().trim().endsWith('.json')) {
// vscode.window.showErrorMessage(`DANGER: Target output file "${outputFileName}" must be a .json file! Or it may overwrite your files. Check Extension Settings.`);
// webview.postMessage({
// type: 'error',
// message: `run_flowsheet aborted: The 'Output File Name' parameter (${outputFileName}) must be a .json file. Refusing to execute to prevent overwriting critical system files.`
// });
// return;
// }

// run command
let command = `${sourceTerminal} && ${activateCommand} && idaes-run "${activateFileName}" "${outputFileName}"`;
if (selectedStep) {
command += ` --to ${selectedStep}`;
}
console.log(`Run command: ${command}`);
// let command = `${sourceTerminal} && ${activateCommand} && idaes-run "${activateFileName}" "${outputFileName}"`;
// if (selectedStep) {
// command += ` --to ${selectedStep}`;
// }
//console.log(`Run command: ${command}`);

console.info(`Begin ${me}: flowsheet=${activatedFileName}`)

// Broadcast a signal to clear logs across ALL active webviews BEFORE starting new command
brodcastMessage({ type: 'clear_terminal_logs' });

await runTerminalCommand(context, command, shell, outputFileName, vscodeContextStateName);
await execFlowsheet(context, activatedFileName, vscodeContextStateName);
//runTerminalCommand(context, command, shell, outputFileName, vscodeContextStateName);


let webViewPanel = activateWebviews.get('webView');
Expand All @@ -89,7 +96,7 @@ export default async function runFlowsheet(context: vscode.ExtensionContext, web
return;
}

console.log('Start post run flowsheet done to all panels');
console.debug('Start post run flowsheet done to all panels');
// post flowsheet result to web view
webViewPanel.webview.postMessage({
type: "flowsheet_runner_result",
Expand All @@ -106,23 +113,20 @@ export default async function runFlowsheet(context: vscode.ExtensionContext, web
treePanel.webview.postMessage({
type: 'run_flowsheet_done',
});
console.log('Done');
console.info(`End: ${me}`);

} catch (e) {
const errorMessage = e instanceof Error ? e.message : String(e);

if (errorMessage.startsWith('CANCELED_BY_USER')) {
// Silently swallow the rejection and log to console
console.log(`runFlowsheet was canceled by the user: ${errorMessage}`);
const pidChunk = errorMessage.split(':')[1] || '';
vscode.window.showInformationMessage(`Run flowsheet stopped manually. PID: ${pidChunk}`);
return;
}
// if (errorMessage.startsWith('CANCELED_BY_USER')) {
// // Silently swallow the rejection and log to console
// console.log(`runFlowsheet was canceled by the user: ${errorMessage}`);
// const pidChunk = errorMessage.split(':')[1] || '';
// vscode.window.showInformationMessage(`Run flowsheet stopped manually. PID: ${pidChunk}`);
// return;
// }

console.error(`
runFlowsheet from webview_receive_message_handler.ts raise an error:
${e}
`);
console.error(`ERROR in ${me}: Running flowsheet: ${errorMessage}`);

let webViewPanel = activateWebviews.get('webView');

Expand All @@ -149,4 +153,4 @@ export default async function runFlowsheet(context: vscode.ExtensionContext, web
});
}
}
}
}
Loading