fix: the system default powershell.exe does not recognize the -WorkingDirectory arg

This commit is contained in:
leo 2024-04-09 11:12:22 +08:00
parent a3c81745aa
commit 5c9b92c3db

View file

@ -60,16 +60,6 @@ namespace SourceGit.Native
set; set;
} = Models.Shell.Default; } = 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) public void SetupApp(AppBuilder builder)
{ {
builder.With(new FontManagerOptions() builder.With(new FontManagerOptions()
@ -171,8 +161,8 @@ namespace SourceGit.Native
startInfo.FileName = bash; startInfo.FileName = bash;
break; break;
case Models.Shell.PowerShell: case Models.Shell.PowerShell:
startInfo.FileName = _powershellPath; startInfo.FileName = ChoosePowerShell();
startInfo.Arguments = $"-WorkingDirectory \"{workdir}\" -Nologo"; startInfo.Arguments = startInfo.FileName.EndsWith("pswd.exe") ? $"-WorkingDirectory \"{workdir}\" -Nologo" : "-Nologo";
break; break;
case Models.Shell.CommandPrompt: case Models.Shell.CommandPrompt:
startInfo.FileName = "cmd"; startInfo.FileName = "cmd";
@ -228,6 +218,36 @@ namespace SourceGit.Native
Process.Start(start); 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 #region EXTERNAL_EDITOR_FINDER
private string FindVSCode() private string FindVSCode()
{ {