Skip to content

Commit b5b1f0c

Browse files
committed
refactor: add ExternalEditorFinder to detect supported external editors
1 parent 482fab9 commit b5b1f0c

File tree

4 files changed

+70
-300
lines changed

4 files changed

+70
-300
lines changed

src/Models/ExternalEditor.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Diagnostics;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
25

36
namespace SourceGit.Models
47
{
@@ -20,4 +23,52 @@ public void Open(string repo)
2023
});
2124
}
2225
}
26+
27+
public class ExternalEditorFinder
28+
{
29+
public List<ExternalEditor> Editors
30+
{
31+
get;
32+
private set;
33+
} = new List<ExternalEditor>();
34+
35+
public void VSCode(Func<string> platform_finder)
36+
{
37+
TryAdd("Visual Studio Code", "vscode.png", "\"{0}\"", "VSCODE_PATH", platform_finder);
38+
}
39+
40+
public void VSCodeInsiders(Func<string> platform_finder)
41+
{
42+
TryAdd("Visual Studio Code - Insiders", "vscode_insiders.png", "\"{0}\"", "VSCODE_INSIDERS_PATH", platform_finder);
43+
}
44+
45+
public void Fleet(Func<string> platform_finder)
46+
{
47+
TryAdd("JetBrains Fleet", "fleet.png", "\"{0}\"", "FLEET_PATH", platform_finder);
48+
}
49+
50+
public void SublimeText(Func<string> platform_finder)
51+
{
52+
TryAdd("Sublime Text", "sublime_text.png", "\"{0}\"", "SUBLIME_TEXT_PATH", platform_finder);
53+
}
54+
55+
private void TryAdd(string name, string icon, string args, string env, Func<string> finder)
56+
{
57+
var path = Environment.GetEnvironmentVariable(env);
58+
if (string.IsNullOrEmpty(path) || !File.Exists(path))
59+
{
60+
path = finder();
61+
if (string.IsNullOrEmpty(path) || !File.Exists(path))
62+
return;
63+
}
64+
65+
Editors.Add(new ExternalEditor
66+
{
67+
Name = name,
68+
Icon = icon,
69+
OpenCmdArgs = args,
70+
Executable = path,
71+
});
72+
}
73+
}
2374
}

src/Native/Linux.cs

Lines changed: 6 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -33,57 +33,12 @@ public string FindGitExecutable()
3333

3434
public List<Models.ExternalEditor> FindExternalEditors()
3535
{
36-
var editors = new List<Models.ExternalEditor>();
37-
38-
var vscode = FindVSCode();
39-
if (!string.IsNullOrEmpty(vscode) && File.Exists(vscode))
40-
{
41-
editors.Add(new Models.ExternalEditor
42-
{
43-
Name = "Visual Studio Code",
44-
Icon = "vscode.png",
45-
Executable = vscode,
46-
OpenCmdArgs = "\"{0}\"",
47-
});
48-
}
49-
50-
var vscodeInsiders = FindVSCodeInsiders();
51-
if (!string.IsNullOrEmpty(vscodeInsiders) && File.Exists(vscodeInsiders))
52-
{
53-
editors.Add(new Models.ExternalEditor
54-
{
55-
Name = "Visual Studio Code - Insiders",
56-
Icon = "vscode_insiders.png",
57-
Executable = vscodeInsiders,
58-
OpenCmdArgs = "\"{0}\"",
59-
});
60-
}
61-
62-
var fleet = FindFleet();
63-
if (!string.IsNullOrEmpty(fleet) && File.Exists(fleet))
64-
{
65-
editors.Add(new Models.ExternalEditor
66-
{
67-
Name = "JetBrains Fleet",
68-
Icon = "fleet.png",
69-
Executable = fleet,
70-
OpenCmdArgs = "\"{0}\"",
71-
});
72-
}
73-
74-
var sublime = FindSublimeText();
75-
if (!string.IsNullOrEmpty(sublime) && File.Exists(sublime))
76-
{
77-
editors.Add(new Models.ExternalEditor
78-
{
79-
Name = "Sublime Text",
80-
Icon = "sublime_text.png",
81-
Executable = sublime,
82-
OpenCmdArgs = "\"{0}\"",
83-
});
84-
}
85-
86-
return editors;
36+
var finder = new Models.ExternalEditorFinder();
37+
finder.VSCode(() => "/usr/share/code/code");
38+
finder.VSCodeInsiders(() => "/usr/share/code-insiders/code-insiders");
39+
finder.Fleet(() => $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}/JetBrains/Toolbox/apps/fleet/bin/Fleet");
40+
finder.SublimeText(() => File.Exists("/usr/bin/subl") ? "/usr/bin/subl" : "/usr/local/bin/subl");
41+
return finder.Editors;
8742
}
8843

8944
public void OpenBrowser(string url)
@@ -159,65 +114,5 @@ public void OpenWithDefaultEditor(string file)
159114

160115
proc.Close();
161116
}
162-
163-
#region EXTERNAL_EDITORS_FINDER
164-
private string FindVSCode()
165-
{
166-
var toolPath = "/usr/share/code/code";
167-
if (File.Exists(toolPath))
168-
return toolPath;
169-
170-
var customPath = Environment.GetEnvironmentVariable("VSCODE_PATH");
171-
if (!string.IsNullOrEmpty(customPath))
172-
return customPath;
173-
174-
return string.Empty;
175-
}
176-
177-
private string FindVSCodeInsiders()
178-
{
179-
var toolPath = "/usr/share/code/code";
180-
if (File.Exists(toolPath))
181-
return toolPath;
182-
183-
var customPath = Environment.GetEnvironmentVariable("VSCODE_INSIDERS_PATH");
184-
if (!string.IsNullOrEmpty(customPath))
185-
return customPath;
186-
187-
return string.Empty;
188-
}
189-
190-
private string FindFleet()
191-
{
192-
var toolPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/.local/share/JetBrains/Toolbox/apps/fleet/bin/Fleet";
193-
if (File.Exists(toolPath))
194-
return toolPath;
195-
196-
var customPath = Environment.GetEnvironmentVariable("FLEET_PATH");
197-
if (!string.IsNullOrEmpty(customPath))
198-
return customPath;
199-
200-
return string.Empty;
201-
}
202-
203-
private string FindSublimeText()
204-
{
205-
if (File.Exists("/usr/bin/subl"))
206-
{
207-
return "/usr/bin/subl";
208-
}
209-
210-
if (File.Exists("/usr/local/bin/subl"))
211-
{
212-
return "/usr/local/bin/subl";
213-
}
214-
215-
var customPath = Environment.GetEnvironmentVariable("SUBLIME_TEXT_PATH");
216-
if (!string.IsNullOrEmpty(customPath))
217-
return customPath;
218-
219-
return string.Empty;
220-
}
221-
#endregion
222117
}
223118
}

src/Native/MacOS.cs

Lines changed: 6 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -30,57 +30,12 @@ public string FindGitExecutable()
3030

3131
public List<Models.ExternalEditor> FindExternalEditors()
3232
{
33-
var editors = new List<Models.ExternalEditor>();
34-
35-
var vscode = FindVSCode();
36-
if (!string.IsNullOrEmpty(vscode) && File.Exists(vscode))
37-
{
38-
editors.Add(new Models.ExternalEditor
39-
{
40-
Name = "Visual Studio Code",
41-
Icon = "vscode.png",
42-
Executable = vscode,
43-
OpenCmdArgs = "\"{0}\"",
44-
});
45-
}
46-
47-
var vscodeInsiders = FindVSCodeInsiders();
48-
if (!string.IsNullOrEmpty(vscodeInsiders) && File.Exists(vscodeInsiders))
49-
{
50-
editors.Add(new Models.ExternalEditor
51-
{
52-
Name = "Visual Studio Code - Insiders",
53-
Icon = "vscode_insiders.png",
54-
Executable = vscodeInsiders,
55-
OpenCmdArgs = "\"{0}\"",
56-
});
57-
}
58-
59-
var fleet = FindFleet();
60-
if (!string.IsNullOrEmpty(fleet) && File.Exists(fleet))
61-
{
62-
editors.Add(new Models.ExternalEditor
63-
{
64-
Name = "JetBrains Fleet",
65-
Icon = "fleet.png",
66-
Executable = fleet,
67-
OpenCmdArgs = "\"{0}\"",
68-
});
69-
}
70-
71-
var sublime = FindSublimeText();
72-
if (!string.IsNullOrEmpty(sublime) && File.Exists(sublime))
73-
{
74-
editors.Add(new Models.ExternalEditor
75-
{
76-
Name = "Sublime Text",
77-
Icon = "sublime_text.png",
78-
Executable = sublime,
79-
OpenCmdArgs = "\"{0}\"",
80-
});
81-
}
82-
83-
return editors;
33+
var finder = new Models.ExternalEditorFinder();
34+
finder.VSCode(() => "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code");
35+
finder.VSCodeInsiders(() => "/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code");
36+
finder.Fleet(() => $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/Applications/Fleet.app/Contents/MacOS/Fleet");
37+
finder.SublimeText(() => "/Applications/Sublime Text.app/Contents/SharedSupport/bin");
38+
return finder.Editors;
8439
}
8540

8641
public void OpenBrowser(string url)
@@ -122,60 +77,5 @@ public void OpenWithDefaultEditor(string file)
12277
{
12378
Process.Start("open", file);
12479
}
125-
126-
#region EXTERNAL_EDITORS_FINDER
127-
private string FindVSCode()
128-
{
129-
var toolPath = "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code";
130-
if (File.Exists(toolPath))
131-
return toolPath;
132-
133-
var customPath = Environment.GetEnvironmentVariable("VSCODE_PATH");
134-
if (!string.IsNullOrEmpty(customPath))
135-
return customPath;
136-
137-
return string.Empty;
138-
}
139-
140-
private string FindVSCodeInsiders()
141-
{
142-
var toolPath = "/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code";
143-
if (File.Exists(toolPath))
144-
return toolPath;
145-
146-
var customPath = Environment.GetEnvironmentVariable("VSCODE_INSIDERS_PATH");
147-
if (!string.IsNullOrEmpty(customPath))
148-
return customPath;
149-
150-
return string.Empty;
151-
}
152-
153-
private string FindFleet()
154-
{
155-
var toolPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/Applications/Fleet.app/Contents/MacOS/Fleet";
156-
if (File.Exists(toolPath))
157-
return toolPath;
158-
159-
var customPath = Environment.GetEnvironmentVariable("FLEET_PATH");
160-
if (!string.IsNullOrEmpty(customPath))
161-
return customPath;
162-
163-
return string.Empty;
164-
}
165-
166-
private string FindSublimeText()
167-
{
168-
if (File.Exists("/Applications/Sublime Text.app/Contents/SharedSupport/bin"))
169-
{
170-
return "/Applications/Sublime Text.app/Contents/SharedSupport/bin";
171-
}
172-
173-
var customPath = Environment.GetEnvironmentVariable("SUBLIME_TEXT_PATH");
174-
if (!string.IsNullOrEmpty(customPath))
175-
return customPath;
176-
177-
return string.Empty;
178-
}
179-
#endregion
18080
}
18181
}

0 commit comments

Comments
 (0)