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
15 changes: 14 additions & 1 deletion packages/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 28 additions & 4 deletions packages/desktop/src-tauri/src/os/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
};

Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -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 change: 1 addition & 0 deletions packages/desktop/src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
11 changes: 10 additions & 1 deletion packages/desktop/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading