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 string Name { get; private set; } public string Executable { get; private set; } public string OpenCmdArgs { get; private set; } public Bitmap IconImage { get; private set; } = null; public ExternalTool(string name, string icon, string executable, string openCmdArgs) { Name = name; Executable = executable; OpenCmdArgs = openCmdArgs; try { var asset = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/Images/ExternalToolIcons/{icon}.png", UriKind.RelativeOrAbsolute)); IconImage = new Bitmap(asset); } catch { // ignore } } public void Open(string repo) { Process.Start(new ProcessStartInfo() { WorkingDirectory = repo, FileName = Executable, Arguments = string.Format(OpenCmdArgs, repo), UseShellExecute = 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 Tools { get; set; } = new List(); } 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 ExternalToolPaths { [JsonPropertyName("tools")] public Dictionary Tools { get; set; } = new Dictionary(); } public class ExternalToolsFinder { public List Founded { get; private set; } = new List(); public ExternalToolsFinder() { var customPathsConfig = Path.Combine(Native.OS.DataDir, "external_editors.json"); try { if (File.Exists(customPathsConfig)) _customPaths = JsonSerializer.Deserialize(File.ReadAllText(customPathsConfig), JsonCodeGen.Default.ExternalToolPaths); } catch { // Ignore } if (_customPaths == null) _customPaths = new ExternalToolPaths(); } public void TryAdd(string name, string icon, string args, string key, Func finder) { if (_customPaths.Tools.TryGetValue(key, out var customPath) && File.Exists(customPath)) { Founded.Add(new ExternalTool(name, icon, customPath, args)); } else { var path = finder(); if (!string.IsNullOrEmpty(path) && File.Exists(path)) Founded.Add(new ExternalTool(name, icon, path, args)); } } public void VSCode(Func platformFinder) { TryAdd("Visual Studio Code", "vscode", "\"{0}\"", "VSCODE", platformFinder); } public void VSCodeInsiders(Func platformFinder) { TryAdd("Visual Studio Code - Insiders", "vscode_insiders", "\"{0}\"", "VSCODE_INSIDERS", platformFinder); } public void VSCodium(Func platformFinder) { TryAdd("VSCodium", "codium", "\"{0}\"", "VSCODIUM", platformFinder); } public void Fleet(Func platformFinder) { TryAdd("Fleet", "fleet", "\"{0}\"", "FLEET", platformFinder); } public void SublimeText(Func platformFinder) { TryAdd("Sublime Text", "sublime_text", "\"{0}\"", "SUBLIME_TEXT", platformFinder); } public void FindJetBrainsFromToolbox(Func platformFinder) { var exclude = new List { "fleet", "dotmemory", "dottrace", "resharper-u", "androidstudio" }; var supported_icons = new List { "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)) { var stateData = JsonSerializer.Deserialize(File.ReadAllText(state), JsonCodeGen.Default.JetBrainsState); foreach (var tool in stateData.Tools) { if (exclude.Contains(tool.ToolId.ToLowerInvariant())) continue; Founded.Add(new ExternalTool( $"{tool.DisplayName} {tool.DisplayVersion}", supported_icons.Contains(tool.ProductCode) ? $"JetBrains/{tool.ProductCode}" : "JetBrains/JB", Path.Combine(tool.InstallLocation, tool.LaunchCommand), "\"{0}\"")); } } } private ExternalToolPaths _customPaths = null; } }