2024-04-07 02:56:53 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2024-04-27 00:12:03 -07:00
|
|
|
|
using System.Text.Json;
|
2024-04-27 07:05:17 -07:00
|
|
|
|
using System.Text.Json.Serialization;
|
2024-04-05 22:14:22 -07:00
|
|
|
|
|
2024-04-09 00:00:52 -07:00
|
|
|
|
using Avalonia.Media.Imaging;
|
|
|
|
|
using Avalonia.Platform;
|
|
|
|
|
|
2024-04-05 22:14:22 -07:00
|
|
|
|
namespace SourceGit.Models
|
|
|
|
|
{
|
2024-04-08 02:39:52 -07:00
|
|
|
|
public class ExternalTool
|
2024-04-05 22:14:22 -07:00
|
|
|
|
{
|
2024-07-14 09:30:31 -07:00
|
|
|
|
public string Name { get; private set; }
|
|
|
|
|
public string Executable { get; private set; }
|
|
|
|
|
public string OpenCmdArgs { get; private set; }
|
2024-05-16 21:01:29 -07:00
|
|
|
|
public Bitmap IconImage { get; private set; } = null;
|
2024-04-05 22:14:22 -07:00
|
|
|
|
|
2024-05-16 21:01:29 -07:00
|
|
|
|
public ExternalTool(string name, string icon, string executable, string openCmdArgs)
|
2024-04-09 00:00:52 -07:00
|
|
|
|
{
|
2024-05-16 21:01:29 -07:00
|
|
|
|
Name = name;
|
|
|
|
|
Executable = executable;
|
|
|
|
|
OpenCmdArgs = openCmdArgs;
|
2024-04-27 07:26:38 -07:00
|
|
|
|
|
2024-05-16 21:01:29 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-02 03:06:45 -07:00
|
|
|
|
var asset = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/Images/ExternalToolIcons/{icon}.png",
|
2024-07-14 09:30:31 -07:00
|
|
|
|
UriKind.RelativeOrAbsolute));
|
2024-05-16 21:01:29 -07:00
|
|
|
|
IconImage = new Bitmap(asset);
|
2024-04-09 00:00:52 -07:00
|
|
|
|
}
|
2024-07-14 09:30:31 -07:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
2024-04-09 00:00:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-05 22:14:22 -07:00
|
|
|
|
public void Open(string repo)
|
|
|
|
|
{
|
|
|
|
|
Process.Start(new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
WorkingDirectory = repo,
|
|
|
|
|
FileName = Executable,
|
|
|
|
|
Arguments = string.Format(OpenCmdArgs, repo),
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-07 02:56:53 -07:00
|
|
|
|
|
2024-04-27 05:54:26 -07:00
|
|
|
|
public class JetBrainsState
|
|
|
|
|
{
|
2024-04-27 07:05:17 -07:00
|
|
|
|
[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>();
|
2024-04-27 05:54:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 00:12:03 -07:00
|
|
|
|
public class JetBrainsTool
|
|
|
|
|
{
|
2024-04-27 07:05:17 -07:00
|
|
|
|
[JsonPropertyName("channelId")]
|
2024-04-27 05:54:26 -07:00
|
|
|
|
public string ChannelId { get; set; }
|
2024-04-27 07:05:17 -07:00
|
|
|
|
[JsonPropertyName("toolId")]
|
2024-04-27 05:54:26 -07:00
|
|
|
|
public string ToolId { get; set; }
|
2024-04-27 07:05:17 -07:00
|
|
|
|
[JsonPropertyName("productCode")]
|
2024-04-27 00:12:03 -07:00
|
|
|
|
public string ProductCode { get; set; }
|
2024-04-27 07:05:17 -07:00
|
|
|
|
[JsonPropertyName("tag")]
|
2024-04-27 05:54:26 -07:00
|
|
|
|
public string Tag { get; set; }
|
2024-04-27 07:05:17 -07:00
|
|
|
|
[JsonPropertyName("displayName")]
|
2024-04-27 05:54:26 -07:00
|
|
|
|
public string DisplayName { get; set; }
|
2024-04-27 07:05:17 -07:00
|
|
|
|
[JsonPropertyName("displayVersion")]
|
2024-04-27 05:54:26 -07:00
|
|
|
|
public string DisplayVersion { get; set; }
|
2024-04-27 07:05:17 -07:00
|
|
|
|
[JsonPropertyName("buildNumber")]
|
2024-04-27 05:54:26 -07:00
|
|
|
|
public string BuildNumber { get; set; }
|
2024-04-27 07:05:17 -07:00
|
|
|
|
[JsonPropertyName("installLocation")]
|
2024-04-27 05:54:26 -07:00
|
|
|
|
public string InstallLocation { get; set; }
|
2024-04-27 07:05:17 -07:00
|
|
|
|
[JsonPropertyName("launchCommand")]
|
2024-04-27 05:54:26 -07:00
|
|
|
|
public string LaunchCommand { get; set; }
|
2024-04-27 00:12:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 20:32:02 -07:00
|
|
|
|
public class ExternalToolPaths
|
|
|
|
|
{
|
|
|
|
|
[JsonPropertyName("tools")]
|
2024-08-18 08:14:44 -07:00
|
|
|
|
public Dictionary<string, string> Tools { get; set; } = new Dictionary<string, string>();
|
2024-08-14 20:32:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 02:39:52 -07:00
|
|
|
|
public class ExternalToolsFinder
|
2024-04-07 02:56:53 -07:00
|
|
|
|
{
|
2024-04-08 02:39:52 -07:00
|
|
|
|
public List<ExternalTool> Founded
|
2024-04-07 02:56:53 -07:00
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
private set;
|
2024-04-08 02:39:52 -07:00
|
|
|
|
} = new List<ExternalTool>();
|
2024-04-07 02:56:53 -07:00
|
|
|
|
|
2024-08-14 20:32:02 -07:00
|
|
|
|
public ExternalToolsFinder()
|
2024-04-27 05:54:26 -07:00
|
|
|
|
{
|
2024-08-14 20:32:02 -07:00
|
|
|
|
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
|
2024-04-27 05:54:26 -07:00
|
|
|
|
{
|
2024-08-14 20:32:02 -07:00
|
|
|
|
// Ignore
|
2024-04-27 05:54:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 20:32:02 -07:00
|
|
|
|
if (_customPaths == null)
|
|
|
|
|
_customPaths = new ExternalToolPaths();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TryAdd(string name, string icon, string args, string key, Func<string> 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));
|
|
|
|
|
}
|
2024-04-27 05:54:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 02:55:38 -07:00
|
|
|
|
public void VSCode(Func<string> platformFinder)
|
2024-04-07 02:56:53 -07:00
|
|
|
|
{
|
2024-08-14 20:32:02 -07:00
|
|
|
|
TryAdd("Visual Studio Code", "vscode", "\"{0}\"", "VSCODE", platformFinder);
|
2024-04-07 02:56:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 02:55:38 -07:00
|
|
|
|
public void VSCodeInsiders(Func<string> platformFinder)
|
2024-04-07 02:56:53 -07:00
|
|
|
|
{
|
2024-08-14 20:32:02 -07:00
|
|
|
|
TryAdd("Visual Studio Code - Insiders", "vscode_insiders", "\"{0}\"", "VSCODE_INSIDERS", platformFinder);
|
2024-04-07 02:56:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 06:54:10 -07:00
|
|
|
|
public void VSCodium(Func<string> platformFinder)
|
|
|
|
|
{
|
2024-08-14 20:32:02 -07:00
|
|
|
|
TryAdd("VSCodium", "codium", "\"{0}\"", "VSCODIUM", platformFinder);
|
2024-05-03 06:54:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 02:55:38 -07:00
|
|
|
|
public void Fleet(Func<string> platformFinder)
|
2024-04-07 02:56:53 -07:00
|
|
|
|
{
|
2024-08-14 20:32:02 -07:00
|
|
|
|
TryAdd("Fleet", "fleet", "\"{0}\"", "FLEET", platformFinder);
|
2024-04-07 02:56:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 02:55:38 -07:00
|
|
|
|
public void SublimeText(Func<string> platformFinder)
|
2024-04-07 02:56:53 -07:00
|
|
|
|
{
|
2024-08-14 20:32:02 -07:00
|
|
|
|
TryAdd("Sublime Text", "sublime_text", "\"{0}\"", "SUBLIME_TEXT", platformFinder);
|
2024-04-07 02:56:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 02:55:38 -07:00
|
|
|
|
public void FindJetBrainsFromToolbox(Func<string> platformFinder)
|
2024-04-07 02:56:53 -07:00
|
|
|
|
{
|
2024-04-27 05:54:26 -07:00
|
|
|
|
var exclude = new List<string> { "fleet", "dotmemory", "dottrace", "resharper-u", "androidstudio" };
|
2024-07-22 21:07:39 -07:00
|
|
|
|
var supported_icons = new List<string> { "CL", "DB", "DL", "DS", "GO", "JB", "PC", "PS", "PY", "QA", "QD", "RD", "RM", "RR", "WRS", "WS" };
|
2024-04-29 02:55:38 -07:00
|
|
|
|
var state = Path.Combine(platformFinder(), "state.json");
|
2024-04-27 00:12:03 -07:00
|
|
|
|
if (File.Exists(state))
|
2024-04-07 02:56:53 -07:00
|
|
|
|
{
|
2024-04-27 05:54:26 -07:00
|
|
|
|
var stateData = JsonSerializer.Deserialize(File.ReadAllText(state), JsonCodeGen.Default.JetBrainsState);
|
|
|
|
|
foreach (var tool in stateData.Tools)
|
2024-04-27 00:12:03 -07:00
|
|
|
|
{
|
2024-04-27 05:54:26 -07:00
|
|
|
|
if (exclude.Contains(tool.ToolId.ToLowerInvariant()))
|
|
|
|
|
continue;
|
2024-04-07 02:56:53 -07:00
|
|
|
|
|
2024-05-16 21:01:29 -07:00
|
|
|
|
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}\""));
|
2024-04-27 05:54:26 -07:00
|
|
|
|
}
|
2024-04-27 00:12:03 -07:00
|
|
|
|
}
|
2024-04-07 02:56:53 -07:00
|
|
|
|
}
|
2024-08-14 20:32:02 -07:00
|
|
|
|
|
|
|
|
|
private ExternalToolPaths _customPaths = null;
|
2024-04-07 02:56:53 -07:00
|
|
|
|
}
|
2024-04-05 22:14:22 -07:00
|
|
|
|
}
|