refactor: add ExternalEditorFinder to detect supported external editors

This commit is contained in:
leo 2024-04-07 17:56:53 +08:00
parent 482fab97c0
commit b5b1f0cb8d
4 changed files with 70 additions and 300 deletions

View file

@ -30,57 +30,12 @@ namespace SourceGit.Native
public List<Models.ExternalEditor> FindExternalEditors()
{
var editors = new List<Models.ExternalEditor>();
var vscode = FindVSCode();
if (!string.IsNullOrEmpty(vscode) && File.Exists(vscode))
{
editors.Add(new Models.ExternalEditor
{
Name = "Visual Studio Code",
Icon = "vscode.png",
Executable = vscode,
OpenCmdArgs = "\"{0}\"",
});
}
var vscodeInsiders = FindVSCodeInsiders();
if (!string.IsNullOrEmpty(vscodeInsiders) && File.Exists(vscodeInsiders))
{
editors.Add(new Models.ExternalEditor
{
Name = "Visual Studio Code - Insiders",
Icon = "vscode_insiders.png",
Executable = vscodeInsiders,
OpenCmdArgs = "\"{0}\"",
});
}
var fleet = FindFleet();
if (!string.IsNullOrEmpty(fleet) && File.Exists(fleet))
{
editors.Add(new Models.ExternalEditor
{
Name = "JetBrains Fleet",
Icon = "fleet.png",
Executable = fleet,
OpenCmdArgs = "\"{0}\"",
});
}
var sublime = FindSublimeText();
if (!string.IsNullOrEmpty(sublime) && File.Exists(sublime))
{
editors.Add(new Models.ExternalEditor
{
Name = "Sublime Text",
Icon = "sublime_text.png",
Executable = sublime,
OpenCmdArgs = "\"{0}\"",
});
}
return editors;
var finder = new Models.ExternalEditorFinder();
finder.VSCode(() => "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code");
finder.VSCodeInsiders(() => "/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code");
finder.Fleet(() => $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/Applications/Fleet.app/Contents/MacOS/Fleet");
finder.SublimeText(() => "/Applications/Sublime Text.app/Contents/SharedSupport/bin");
return finder.Editors;
}
public void OpenBrowser(string url)
@ -122,60 +77,5 @@ namespace SourceGit.Native
{
Process.Start("open", file);
}
#region EXTERNAL_EDITORS_FINDER
private string FindVSCode()
{
var toolPath = "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code";
if (File.Exists(toolPath))
return toolPath;
var customPath = Environment.GetEnvironmentVariable("VSCODE_PATH");
if (!string.IsNullOrEmpty(customPath))
return customPath;
return string.Empty;
}
private string FindVSCodeInsiders()
{
var toolPath = "/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code";
if (File.Exists(toolPath))
return toolPath;
var customPath = Environment.GetEnvironmentVariable("VSCODE_INSIDERS_PATH");
if (!string.IsNullOrEmpty(customPath))
return customPath;
return string.Empty;
}
private string FindFleet()
{
var toolPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/Applications/Fleet.app/Contents/MacOS/Fleet";
if (File.Exists(toolPath))
return toolPath;
var customPath = Environment.GetEnvironmentVariable("FLEET_PATH");
if (!string.IsNullOrEmpty(customPath))
return customPath;
return string.Empty;
}
private string FindSublimeText()
{
if (File.Exists("/Applications/Sublime Text.app/Contents/SharedSupport/bin"))
{
return "/Applications/Sublime Text.app/Contents/SharedSupport/bin";
}
var customPath = Environment.GetEnvironmentVariable("SUBLIME_TEXT_PATH");
if (!string.IsNullOrEmpty(customPath))
return customPath;
return string.Empty;
}
#endregion
}
}