Explorar o código

fix(app): open in powershell (#15112)

Filip hai 5 meses
pai
achega
6b021658ad

+ 14 - 1
packages/desktop/src-tauri/src/lib.rs

@@ -179,6 +179,18 @@ fn resolve_app_path(app_name: &str) -> Option<String> {
     }
 }
 
+#[tauri::command]
+#[specta::specta]
+fn open_in_powershell(path: String) -> Result<(), String> {
+    #[cfg(target_os = "windows")]
+    {
+        return os::windows::open_in_powershell(path);
+    }
+
+    #[cfg(not(target_os = "windows"))]
+    Err("PowerShell is only supported on Windows".to_string())
+}
+
 #[cfg(target_os = "macos")]
 fn check_macos_app(app_name: &str) -> bool {
     // Check common installation locations
@@ -373,7 +385,8 @@ fn make_specta_builder() -> tauri_specta::Builder<tauri::Wry> {
             markdown::parse_markdown_command,
             check_app_exists,
             wsl_path,
-            resolve_app_path
+            resolve_app_path,
+            open_in_powershell
         ])
         .events(tauri_specta::collect_events![
             LoadingWindowComplete,

+ 28 - 4
packages/desktop/src-tauri/src/os/windows.rs

@@ -6,9 +6,12 @@ use std::{
 };
 use windows_sys::Win32::{
     Foundation::ERROR_SUCCESS,
-    System::Registry::{
-        HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, REG_EXPAND_SZ, REG_SZ, RRF_RT_REG_EXPAND_SZ,
-        RRF_RT_REG_SZ, RegGetValueW,
+    System::{
+        Registry::{
+            RegGetValueW, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, REG_EXPAND_SZ, REG_SZ,
+            RRF_RT_REG_EXPAND_SZ, RRF_RT_REG_SZ,
+        },
+        Threading::{CREATE_NEW_CONSOLE, CREATE_NO_WINDOW},
     },
 };
 
@@ -310,7 +313,7 @@ pub fn resolve_windows_app_path(app_name: &str) -> Option<String> {
 
     let resolve_where = |query: &str| -> Option<String> {
         let output = Command::new("where")
-            .creation_flags(0x08000000)
+            .creation_flags(CREATE_NO_WINDOW)
             .arg(query)
             .output()
             .ok()?;
@@ -437,3 +440,24 @@ pub fn resolve_windows_app_path(app_name: &str) -> Option<String> {
 
     None
 }
+
+pub fn open_in_powershell(path: String) -> Result<(), String> {
+    let path = PathBuf::from(path);
+    let dir = if path.is_dir() {
+        path
+    } else if let Some(parent) = path.parent() {
+        parent.to_path_buf()
+    } else {
+        std::env::current_dir()
+            .map_err(|e| format!("Failed to determine current directory: {e}"))?
+    };
+
+    Command::new("powershell.exe")
+        .creation_flags(CREATE_NEW_CONSOLE)
+        .current_dir(dir)
+        .args(["-NoExit"])
+        .spawn()
+        .map_err(|e| format!("Failed to start PowerShell: {e}"))?;
+
+    Ok(())
+}

+ 1 - 0
packages/desktop/src/bindings.ts

@@ -18,6 +18,7 @@ export const commands = {
 	checkAppExists: (appName: string) => __TAURI_INVOKE<boolean>("check_app_exists", { appName }),
 	wslPath: (path: string, mode: "windows" | "linux" | null) => __TAURI_INVOKE<string>("wsl_path", { path, mode }),
 	resolveAppPath: (appName: string) => __TAURI_INVOKE<string | null>("resolve_app_path", { appName }),
+	openInPowershell: (path: string) => __TAURI_INVOKE<null>("open_in_powershell", { path }),
 };
 
 /** Events */

+ 10 - 1
packages/desktop/src/index.tsx

@@ -118,7 +118,6 @@ const createPlatform = (): Platform => {
     async openPath(path: string, app?: string) {
       const os = ostype()
       if (os === "windows") {
-        const resolvedApp = (app && (await commands.resolveAppPath(app))) || app
         const resolvedPath = await (async () => {
           if (window.__OPENCODE__?.wsl) {
             const converted = await commands.wslPath(path, "windows").catch(() => null)
@@ -127,6 +126,16 @@ const createPlatform = (): Platform => {
 
           return path
         })()
+        const resolvedApp = (app && (await commands.resolveAppPath(app))) || app
+        const isPowershell = (value?: string) => {
+          if (!value) return false
+          const name = value.toLowerCase().replaceAll("/", "\\").split("\\").pop()
+          return name === "powershell" || name === "powershell.exe"
+        }
+        if (isPowershell(resolvedApp)) {
+          await commands.openInPowershell(resolvedPath)
+          return
+        }
         return openerOpenPath(resolvedPath, resolvedApp)
       }
       return openerOpenPath(path, app)