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

@ -1,4 +1,7 @@
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace SourceGit.Models
{
@ -20,4 +23,52 @@ namespace SourceGit.Models
});
}
}
public class ExternalEditorFinder
{
public List<ExternalEditor> Editors
{
get;
private set;
} = new List<ExternalEditor>();
public void VSCode(Func<string> platform_finder)
{
TryAdd("Visual Studio Code", "vscode.png", "\"{0}\"", "VSCODE_PATH", platform_finder);
}
public void VSCodeInsiders(Func<string> platform_finder)
{
TryAdd("Visual Studio Code - Insiders", "vscode_insiders.png", "\"{0}\"", "VSCODE_INSIDERS_PATH", platform_finder);
}
public void Fleet(Func<string> platform_finder)
{
TryAdd("JetBrains Fleet", "fleet.png", "\"{0}\"", "FLEET_PATH", platform_finder);
}
public void SublimeText(Func<string> platform_finder)
{
TryAdd("Sublime Text", "sublime_text.png", "\"{0}\"", "SUBLIME_TEXT_PATH", platform_finder);
}
private void TryAdd(string name, string icon, string args, string env, Func<string> finder)
{
var path = Environment.GetEnvironmentVariable(env);
if (string.IsNullOrEmpty(path) || !File.Exists(path))
{
path = finder();
if (string.IsNullOrEmpty(path) || !File.Exists(path))
return;
}
Editors.Add(new ExternalEditor
{
Name = name,
Icon = icon,
OpenCmdArgs = args,
Executable = path,
});
}
}
}

View file

@ -33,57 +33,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(() => "/usr/share/code/code");
finder.VSCodeInsiders(() => "/usr/share/code-insiders/code-insiders");
finder.Fleet(() => $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}/JetBrains/Toolbox/apps/fleet/bin/Fleet");
finder.SublimeText(() => File.Exists("/usr/bin/subl") ? "/usr/bin/subl" : "/usr/local/bin/subl");
return finder.Editors;
}
public void OpenBrowser(string url)
@ -159,65 +114,5 @@ namespace SourceGit.Native
proc.Close();
}
#region EXTERNAL_EDITORS_FINDER
private string FindVSCode()
{
var toolPath = "/usr/share/code/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 = "/usr/share/code/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)}/.local/share/JetBrains/Toolbox/apps/fleet/bin/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("/usr/bin/subl"))
{
return "/usr/bin/subl";
}
if (File.Exists("/usr/local/bin/subl"))
{
return "/usr/local/bin/subl";
}
var customPath = Environment.GetEnvironmentVariable("SUBLIME_TEXT_PATH");
if (!string.IsNullOrEmpty(customPath))
return customPath;
return string.Empty;
}
#endregion
}
}

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
}
}

View file

@ -116,57 +116,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(FindVSCode);
finder.VSCodeInsiders(FindVSCodeInsiders);
finder.Fleet(() => $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\Programs\\Fleet\\Fleet.exe");
finder.SublimeText(FindSublimeText);
return finder.Editors;
}
public void OpenBrowser(string url)
@ -259,13 +214,6 @@ namespace SourceGit.Native
return vscode.GetValue("DisplayIcon") as string;
}
// ENV
var customPath = Environment.GetEnvironmentVariable("VSCODE_PATH");
if (!string.IsNullOrEmpty(customPath))
{
return customPath;
}
return string.Empty;
}
@ -293,26 +241,6 @@ namespace SourceGit.Native
return vscodeInsiders.GetValue("DisplayIcon") as string;
}
// ENV
var customPath = Environment.GetEnvironmentVariable("VSCODE_INSIDERS_PATH");
if (!string.IsNullOrEmpty(customPath))
{
return customPath;
}
return string.Empty;
}
private string FindFleet()
{
var toolPath = Environment.ExpandEnvironmentVariables($"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\\AppData\\Local\\Programs\\Fleet\\Fleet.exe");
if (File.Exists(toolPath))
return toolPath;
var customPath = Environment.GetEnvironmentVariable("FLEET_PATH");
if (!string.IsNullOrEmpty(customPath))
return customPath;
return string.Empty;
}
@ -336,10 +264,6 @@ namespace SourceGit.Native
return Path.Combine(Path.GetDirectoryName(icon), "subl.exe");
}
var customPath = Environment.GetEnvironmentVariable("SUBLIME_TEXT_PATH");
if (!string.IsNullOrEmpty(customPath))
return customPath;
return string.Empty;
}
#endregion