From 5c9b92c3dbc1e0235a1a673ddde6050101865197 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 9 Apr 2024 11:12:22 +0800 Subject: [PATCH] fix: the system default powershell.exe does not recognize the -WorkingDirectory arg --- src/Native/Windows.cs | 44 +++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/Native/Windows.cs b/src/Native/Windows.cs index a14b6cc7..a0b5fbe0 100644 --- a/src/Native/Windows.cs +++ b/src/Native/Windows.cs @@ -60,16 +60,6 @@ namespace SourceGit.Native set; } = Models.Shell.Default; - public Windows() - { - var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey( - Microsoft.Win32.RegistryHive.LocalMachine, - Microsoft.Win32.RegistryView.Registry64); - - var pwsh = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\pwsh.exe"); - _powershellPath = pwsh != null ? pwsh.GetValue(null) as string : "powershell"; - } - public void SetupApp(AppBuilder builder) { builder.With(new FontManagerOptions() @@ -171,8 +161,8 @@ namespace SourceGit.Native startInfo.FileName = bash; break; case Models.Shell.PowerShell: - startInfo.FileName = _powershellPath; - startInfo.Arguments = $"-WorkingDirectory \"{workdir}\" -Nologo"; + startInfo.FileName = ChoosePowerShell(); + startInfo.Arguments = startInfo.FileName.EndsWith("pswd.exe") ? $"-WorkingDirectory \"{workdir}\" -Nologo" : "-Nologo"; break; case Models.Shell.CommandPrompt: startInfo.FileName = "cmd"; @@ -228,6 +218,36 @@ namespace SourceGit.Native Process.Start(start); } + // There's two version of PowerShell : pwsh.exe (preferred) and powershell.exe (system default) + private string ChoosePowerShell() + { + if (!string.IsNullOrEmpty(_powershellPath)) return _powershellPath; + + var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey( + Microsoft.Win32.RegistryHive.LocalMachine, + Microsoft.Win32.RegistryView.Registry64); + + var pwsh = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\pwsh.exe"); + if (pwsh != null) + { + var path = pwsh.GetValue(null) as string; + if (File.Exists(path)) + { + _powershellPath = path; + return _powershellPath; + } + } + + var finder = new StringBuilder("powershell.exe", 512); + if (PathFindOnPath(finder, null)) + { + _powershellPath = finder.ToString(); + return _powershellPath; + } + + return string.Empty; + } + #region EXTERNAL_EDITOR_FINDER private string FindVSCode() {