Skip to content

Commit 6b02165

Browse files
authored
fix(app): open in powershell (#15112)
1 parent 799b262 commit 6b02165

4 files changed

Lines changed: 53 additions & 6 deletions

File tree

packages/desktop/src-tauri/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ fn resolve_app_path(app_name: &str) -> Option<String> {
179179
}
180180
}
181181

182+
#[tauri::command]
183+
#[specta::specta]
184+
fn open_in_powershell(path: String) -> Result<(), String> {
185+
#[cfg(target_os = "windows")]
186+
{
187+
return os::windows::open_in_powershell(path);
188+
}
189+
190+
#[cfg(not(target_os = "windows"))]
191+
Err("PowerShell is only supported on Windows".to_string())
192+
}
193+
182194
#[cfg(target_os = "macos")]
183195
fn check_macos_app(app_name: &str) -> bool {
184196
// Check common installation locations
@@ -373,7 +385,8 @@ fn make_specta_builder() -> tauri_specta::Builder<tauri::Wry> {
373385
markdown::parse_markdown_command,
374386
check_app_exists,
375387
wsl_path,
376-
resolve_app_path
388+
resolve_app_path,
389+
open_in_powershell
377390
])
378391
.events(tauri_specta::collect_events![
379392
LoadingWindowComplete,

packages/desktop/src-tauri/src/os/windows.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ use std::{
66
};
77
use windows_sys::Win32::{
88
Foundation::ERROR_SUCCESS,
9-
System::Registry::{
10-
HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, REG_EXPAND_SZ, REG_SZ, RRF_RT_REG_EXPAND_SZ,
11-
RRF_RT_REG_SZ, RegGetValueW,
9+
System::{
10+
Registry::{
11+
RegGetValueW, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, REG_EXPAND_SZ, REG_SZ,
12+
RRF_RT_REG_EXPAND_SZ, RRF_RT_REG_SZ,
13+
},
14+
Threading::{CREATE_NEW_CONSOLE, CREATE_NO_WINDOW},
1215
},
1316
};
1417

@@ -310,7 +313,7 @@ pub fn resolve_windows_app_path(app_name: &str) -> Option<String> {
310313

311314
let resolve_where = |query: &str| -> Option<String> {
312315
let output = Command::new("where")
313-
.creation_flags(0x08000000)
316+
.creation_flags(CREATE_NO_WINDOW)
314317
.arg(query)
315318
.output()
316319
.ok()?;
@@ -437,3 +440,24 @@ pub fn resolve_windows_app_path(app_name: &str) -> Option<String> {
437440

438441
None
439442
}
443+
444+
pub fn open_in_powershell(path: String) -> Result<(), String> {
445+
let path = PathBuf::from(path);
446+
let dir = if path.is_dir() {
447+
path
448+
} else if let Some(parent) = path.parent() {
449+
parent.to_path_buf()
450+
} else {
451+
std::env::current_dir()
452+
.map_err(|e| format!("Failed to determine current directory: {e}"))?
453+
};
454+
455+
Command::new("powershell.exe")
456+
.creation_flags(CREATE_NEW_CONSOLE)
457+
.current_dir(dir)
458+
.args(["-NoExit"])
459+
.spawn()
460+
.map_err(|e| format!("Failed to start PowerShell: {e}"))?;
461+
462+
Ok(())
463+
}

packages/desktop/src/bindings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const commands = {
1818
checkAppExists: (appName: string) => __TAURI_INVOKE<boolean>("check_app_exists", { appName }),
1919
wslPath: (path: string, mode: "windows" | "linux" | null) => __TAURI_INVOKE<string>("wsl_path", { path, mode }),
2020
resolveAppPath: (appName: string) => __TAURI_INVOKE<string | null>("resolve_app_path", { appName }),
21+
openInPowershell: (path: string) => __TAURI_INVOKE<null>("open_in_powershell", { path }),
2122
};
2223

2324
/** Events */

packages/desktop/src/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ const createPlatform = (): Platform => {
118118
async openPath(path: string, app?: string) {
119119
const os = ostype()
120120
if (os === "windows") {
121-
const resolvedApp = (app && (await commands.resolveAppPath(app))) || app
122121
const resolvedPath = await (async () => {
123122
if (window.__OPENCODE__?.wsl) {
124123
const converted = await commands.wslPath(path, "windows").catch(() => null)
@@ -127,6 +126,16 @@ const createPlatform = (): Platform => {
127126

128127
return path
129128
})()
129+
const resolvedApp = (app && (await commands.resolveAppPath(app))) || app
130+
const isPowershell = (value?: string) => {
131+
if (!value) return false
132+
const name = value.toLowerCase().replaceAll("/", "\\").split("\\").pop()
133+
return name === "powershell" || name === "powershell.exe"
134+
}
135+
if (isPowershell(resolvedApp)) {
136+
await commands.openInPowershell(resolvedPath)
137+
return
138+
}
130139
return openerOpenPath(resolvedPath, resolvedApp)
131140
}
132141
return openerOpenPath(path, app)

0 commit comments

Comments
 (0)