refactor: external tools and shells

* rename Models.ExternalMergeTools to Models.ExternalMerger
* supports Git Bash/PowerShell/Command Prompt/Default Shell in Windows Terminal
This commit is contained in:
leo 2024-04-09 10:41:37 +08:00
parent 4ac705f8ca
commit 4882fd9d69
19 changed files with 243 additions and 152 deletions

View file

@ -21,7 +21,6 @@ namespace SourceGit.Native
}
public static string GitExecutable { get; set; } = string.Empty;
public static bool UsePowershellOnWindows { get; set; } = false;
public static List<Models.ExternalTool> ExternalTools { get; set; } = new List<Models.ExternalTool>();
static OS()
@ -46,6 +45,33 @@ namespace SourceGit.Native
ExternalTools = _backend.FindExternalTools();
}
public static Models.Shell GetShell()
{
if (OperatingSystem.IsWindows())
{
return (_backend as Windows).Shell;
}
else
{
return Models.Shell.Default;
}
}
public static bool SetShell(Models.Shell shell)
{
if (OperatingSystem.IsWindows())
{
var windows = (_backend as Windows);
if (windows.Shell != shell)
{
windows.Shell = shell;
return true;
}
}
return false;
}
public static void SetupApp(AppBuilder builder)
{
_backend.SetupApp(builder);

View file

@ -54,6 +54,12 @@ namespace SourceGit.Native
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags);
public Models.Shell Shell
{
get;
set;
} = Models.Shell.Default;
public Windows()
{
var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey(
@ -143,27 +149,44 @@ namespace SourceGit.Native
public void OpenTerminal(string workdir)
{
var startInfo = new ProcessStartInfo() { UseShellExecute = true };
if (!string.IsNullOrEmpty(workdir) && Path.Exists(workdir))
startInfo.WorkingDirectory = workdir;
var startInfo = new ProcessStartInfo();
if (OS.UsePowershellOnWindows)
if (!string.IsNullOrEmpty(workdir) && Path.Exists(workdir))
{
startInfo.FileName = _powershellPath;
startInfo.WorkingDirectory = workdir;
}
else
{
var binDir = Path.GetDirectoryName(OS.GitExecutable);
var bash = Path.Combine(binDir, "bash.exe");
if (!File.Exists(bash))
{
App.RaiseException(string.IsNullOrEmpty(workdir) ? "" : workdir, $"Can NOT found bash.exe under '{binDir}'");
return;
}
startInfo.FileName = bash;
startInfo.WorkingDirectory = ".";
}
switch (Shell)
{
case Models.Shell.Default:
var binDir = Path.GetDirectoryName(OS.GitExecutable);
var bash = Path.Combine(binDir, "bash.exe");
if (!File.Exists(bash))
{
App.RaiseException(string.IsNullOrEmpty(workdir) ? "" : workdir, $"Can NOT found bash.exe under '{binDir}'");
return;
}
startInfo.FileName = bash;
break;
case Models.Shell.PowerShell:
startInfo.FileName = _powershellPath;
break;
case Models.Shell.CommandPrompt:
startInfo.FileName = "cmd";
break;
case Models.Shell.DefaultShellOfWindowsTerminal:
startInfo.FileName = "wt";
break;
default:
App.RaiseException(string.IsNullOrEmpty(workdir) ? "" : workdir, $"Bad shell configuration!");
return;
}
Process.Start(startInfo);
}