-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathExternalTool.cs
More file actions
247 lines (215 loc) · 8.1 KB
/
ExternalTool.cs
File metadata and controls
247 lines (215 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
namespace SourceGit.Models
{
public class ExternalTool
{
public class LaunchOption
{
public string Title { get; set; }
public string Args { get; set; }
public LaunchOption(string title, string args)
{
Title = title;
Args = args;
}
}
public string Name { get; }
public string ExecFile { get; }
public Bitmap IconImage { get; }
public ExternalTool(string name, string icon, string execFile, Func<string, List<LaunchOption>> optionsGenerator = null)
{
Name = name;
ExecFile = execFile;
_optionsGenerator = optionsGenerator;
try
{
var asset = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/Images/ExternalToolIcons/{icon}.png",
UriKind.RelativeOrAbsolute));
IconImage = new Bitmap(asset);
}
catch
{
// ignore
}
}
public List<LaunchOption> MakeLaunchOptions(string repo)
{
return _optionsGenerator?.Invoke(repo);
}
public void Launch(string args)
{
if (File.Exists(ExecFile))
{
Process.Start(new ProcessStartInfo()
{
FileName = ExecFile,
Arguments = args,
UseShellExecute = false,
});
}
}
private Func<string, List<LaunchOption>> _optionsGenerator = null;
}
public class VisualStudioInstance
{
[JsonPropertyName("displayName")]
public string DisplayName { get; set; } = string.Empty;
[JsonPropertyName("productPath")]
public string ProductPath { get; set; } = string.Empty;
[JsonPropertyName("isPrerelease")]
public bool IsPrerelease { get; set; } = false;
}
public class JetBrainsState
{
[JsonPropertyName("version")]
public int Version { get; set; } = 0;
[JsonPropertyName("appVersion")]
public string AppVersion { get; set; } = string.Empty;
[JsonPropertyName("tools")]
public List<JetBrainsTool> Tools { get; set; } = new List<JetBrainsTool>();
}
public class JetBrainsTool
{
[JsonPropertyName("channelId")]
public string ChannelId { get; set; }
[JsonPropertyName("toolId")]
public string ToolId { get; set; }
[JsonPropertyName("productCode")]
public string ProductCode { get; set; }
[JsonPropertyName("tag")]
public string Tag { get; set; }
[JsonPropertyName("displayName")]
public string DisplayName { get; set; }
[JsonPropertyName("displayVersion")]
public string DisplayVersion { get; set; }
[JsonPropertyName("buildNumber")]
public string BuildNumber { get; set; }
[JsonPropertyName("installLocation")]
public string InstallLocation { get; set; }
[JsonPropertyName("launchCommand")]
public string LaunchCommand { get; set; }
}
public class ExternalToolCustomization
{
[JsonPropertyName("tools")]
public Dictionary<string, string> Tools { get; set; } = new Dictionary<string, string>();
[JsonPropertyName("excludes")]
public List<string> Excludes { get; set; } = new List<string>();
}
public class ExternalToolsFinder
{
public List<ExternalTool> Tools
{
get;
private set;
} = new List<ExternalTool>();
public ExternalToolsFinder()
{
var customPathsConfig = Path.Combine(Native.OS.DataDir, "external_editors.json");
try
{
if (File.Exists(customPathsConfig))
{
using var stream = File.OpenRead(customPathsConfig);
_customization = JsonSerializer.Deserialize(stream, JsonCodeGen.Default.ExternalToolCustomization);
}
}
catch
{
// Ignore
}
_customization ??= new ExternalToolCustomization();
}
public void TryAdd(string name, string icon, Func<string> finder, Func<string, List<ExternalTool.LaunchOption>> optionsGenerator = null)
{
if (_customization.Excludes.Contains(name))
return;
if (_customization.Tools.TryGetValue(name, out var customPath) && File.Exists(customPath))
{
Tools.Add(new ExternalTool(name, icon, customPath, optionsGenerator));
}
else
{
var path = finder();
if (!string.IsNullOrEmpty(path) && File.Exists(path))
Tools.Add(new ExternalTool(name, icon, path, optionsGenerator));
}
}
public void VSCode(Func<string> platformFinder)
{
TryAdd("Visual Studio Code", "vscode", platformFinder, GenerateVSCodeLaunchOptions);
}
public void VSCodeInsiders(Func<string> platformFinder)
{
TryAdd("Visual Studio Code - Insiders", "vscode_insiders", platformFinder, GenerateVSCodeLaunchOptions);
}
public void VSCodium(Func<string> platformFinder)
{
TryAdd("VSCodium", "codium", platformFinder, GenerateVSCodeLaunchOptions);
}
public void SublimeText(Func<string> platformFinder)
{
TryAdd("Sublime Text", "sublime_text", platformFinder);
}
public void Zed(Func<string> platformFinder)
{
TryAdd("Zed", "zed", platformFinder);
}
public void Cursor(Func<string> platformFinder)
{
TryAdd("Cursor", "cursor", platformFinder);
}
public void FindJetBrainsFromToolbox(Func<string> platformFinder)
{
var exclude = new List<string> { "fleet", "dotmemory", "dottrace", "resharper-u", "androidstudio" };
var supportedIcons = new List<string> { "CL", "DB", "DL", "DS", "GO", "JB", "PC", "PS", "PY", "QA", "QD", "RD", "RM", "RR", "WRS", "WS" };
var state = Path.Combine(platformFinder(), "state.json");
if (File.Exists(state))
{
try
{
using var stream = File.OpenRead(state);
var stateData = JsonSerializer.Deserialize(stream, JsonCodeGen.Default.JetBrainsState);
foreach (var tool in stateData.Tools)
{
if (exclude.Contains(tool.ToolId.ToLowerInvariant()))
continue;
Tools.Add(new ExternalTool(
$"{tool.DisplayName} {tool.DisplayVersion}",
supportedIcons.Contains(tool.ProductCode) ? $"JetBrains/{tool.ProductCode}" : "JetBrains/JB",
Path.Combine(tool.InstallLocation, tool.LaunchCommand)));
}
}
catch
{
// Ignore exceptions.
}
}
}
private List<ExternalTool.LaunchOption> GenerateVSCodeLaunchOptions(string path)
{
var root = new DirectoryInfo(path);
if (!root.Exists)
return null;
var options = new List<ExternalTool.LaunchOption>();
var prefixLen = root.FullName.Length;
root.WalkFiles(f =>
{
if (f.EndsWith(".code-workspace", StringComparison.OrdinalIgnoreCase))
{
var display = f.Substring(prefixLen).TrimStart(Path.DirectorySeparatorChar);
options.Add(new(display, f.Quoted()));
}
}, 2);
return options;
}
private ExternalToolCustomization _customization = null;
}
}