|
1 | 1 | use std::path::PathBuf; |
2 | 2 |
|
| 3 | +#[cfg(windows)] |
| 4 | +use codex_utils_absolute_path::AbsolutePathBuf; |
| 5 | + |
3 | 6 | use crate::shell::ShellType; |
4 | 7 | use crate::shell::detect_shell_type; |
5 | 8 |
|
@@ -40,6 +43,93 @@ pub fn extract_powershell_command(command: &[String]) -> Option<(&str, &str)> { |
40 | 43 | None |
41 | 44 | } |
42 | 45 |
|
| 46 | +/// This function attempts to find a valid PowerShell executable on the system. |
| 47 | +/// It first tries to find pwsh.exe, and if that fails, it tries to find |
| 48 | +/// powershell.exe. |
| 49 | +#[cfg(windows)] |
| 50 | +#[allow(dead_code)] |
| 51 | +pub(crate) fn try_find_powershellish_executable_blocking() -> Option<AbsolutePathBuf> { |
| 52 | + if let Some(pwsh_path) = try_find_pwsh_executable_blocking() { |
| 53 | + Some(pwsh_path) |
| 54 | + } else { |
| 55 | + try_find_powershell_executable_blocking() |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// This function attempts to find a powershell.exe executable on the system. |
| 60 | +#[cfg(windows)] |
| 61 | +pub(crate) fn try_find_powershell_executable_blocking() -> Option<AbsolutePathBuf> { |
| 62 | + try_find_powershellish_executable_in_path(&["powershell.exe"]) |
| 63 | +} |
| 64 | + |
| 65 | +/// This function attempts to find a pwsh.exe executable on the system. |
| 66 | +/// Note that pwsh.exe and powershell.exe are different executables: |
| 67 | +/// |
| 68 | +/// - pwsh.exe is the cross-platform PowerShell Core (v6+) executable |
| 69 | +/// - powershell.exe is the Windows PowerShell (v5.1 and earlier) executable |
| 70 | +/// |
| 71 | +/// Further, while powershell.exe is included by default on Windows systems, |
| 72 | +/// pwsh.exe must be installed separately by the user. And even when the user |
| 73 | +/// has installed pwsh.exe, it may not be available in the system PATH, in which |
| 74 | +/// case we attempt to locate it via other means. |
| 75 | +#[cfg(windows)] |
| 76 | +pub(crate) fn try_find_pwsh_executable_blocking() -> Option<AbsolutePathBuf> { |
| 77 | + if let Some(ps_home) = std::process::Command::new("cmd") |
| 78 | + .args(["/C", "pwsh", "-NoProfile", "-Command", "$PSHOME"]) |
| 79 | + .output() |
| 80 | + .ok() |
| 81 | + .and_then(|out| { |
| 82 | + if !out.status.success() { |
| 83 | + return None; |
| 84 | + } |
| 85 | + let stdout = String::from_utf8_lossy(&out.stdout); |
| 86 | + let trimmed = stdout.trim(); |
| 87 | + (!trimmed.is_empty()).then(|| trimmed.to_string()) |
| 88 | + }) |
| 89 | + { |
| 90 | + let candidate = AbsolutePathBuf::resolve_path_against_base("pwsh.exe", &ps_home); |
| 91 | + |
| 92 | + if let Ok(candidate_abs_path) = candidate |
| 93 | + && is_powershellish_executable_available(candidate_abs_path.as_path()) |
| 94 | + { |
| 95 | + return Some(candidate_abs_path); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + try_find_powershellish_executable_in_path(&["pwsh.exe"]) |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(windows)] |
| 103 | +fn try_find_powershellish_executable_in_path(candidates: &[&str]) -> Option<AbsolutePathBuf> { |
| 104 | + for candidate in candidates { |
| 105 | + let Ok(resolved_path) = which::which(candidate) else { |
| 106 | + continue; |
| 107 | + }; |
| 108 | + |
| 109 | + if !is_powershellish_executable_available(&resolved_path) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + let Ok(abs_path) = AbsolutePathBuf::from_absolute_path(resolved_path) else { |
| 114 | + continue; |
| 115 | + }; |
| 116 | + |
| 117 | + return Some(abs_path); |
| 118 | + } |
| 119 | + |
| 120 | + None |
| 121 | +} |
| 122 | + |
| 123 | +#[cfg(windows)] |
| 124 | +fn is_powershellish_executable_available(powershell_or_pwsh_exe: &std::path::Path) -> bool { |
| 125 | + // This test works for both powershell.exe and pwsh.exe. |
| 126 | + std::process::Command::new(powershell_or_pwsh_exe) |
| 127 | + .args(["-NoLogo", "-NoProfile", "-Command", "Write-Output ok"]) |
| 128 | + .output() |
| 129 | + .map(|output| output.status.success()) |
| 130 | + .unwrap_or(false) |
| 131 | +} |
| 132 | + |
43 | 133 | #[cfg(test)] |
44 | 134 | mod tests { |
45 | 135 | use super::extract_powershell_command; |
|
0 commit comments