diff --git a/src/Models/ExternalEditor.cs b/src/Models/ExternalEditor.cs index 1fab67d8..094d0a5f 100644 --- a/src/Models/ExternalEditor.cs +++ b/src/Models/ExternalEditor.cs @@ -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 Editors + { + get; + private set; + } = new List(); + + public void VSCode(Func platform_finder) + { + TryAdd("Visual Studio Code", "vscode.png", "\"{0}\"", "VSCODE_PATH", platform_finder); + } + + public void VSCodeInsiders(Func platform_finder) + { + TryAdd("Visual Studio Code - Insiders", "vscode_insiders.png", "\"{0}\"", "VSCODE_INSIDERS_PATH", platform_finder); + } + + public void Fleet(Func platform_finder) + { + TryAdd("JetBrains Fleet", "fleet.png", "\"{0}\"", "FLEET_PATH", platform_finder); + } + + public void SublimeText(Func 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 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, + }); + } + } } diff --git a/src/Native/Linux.cs b/src/Native/Linux.cs index b56ac31a..56d42306 100644 --- a/src/Native/Linux.cs +++ b/src/Native/Linux.cs @@ -33,57 +33,12 @@ namespace SourceGit.Native public List FindExternalEditors() { - var editors = new List(); - - 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 } } diff --git a/src/Native/MacOS.cs b/src/Native/MacOS.cs index a695b7dc..ac864986 100644 --- a/src/Native/MacOS.cs +++ b/src/Native/MacOS.cs @@ -30,57 +30,12 @@ namespace SourceGit.Native public List FindExternalEditors() { - var editors = new List(); - - 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 } } diff --git a/src/Native/Windows.cs b/src/Native/Windows.cs index e7ef0fd8..78fef704 100644 --- a/src/Native/Windows.cs +++ b/src/Native/Windows.cs @@ -116,57 +116,12 @@ namespace SourceGit.Native public List FindExternalEditors() { - var editors = new List(); - - 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