refactor: rewrite the way to find external editors (#347)

* do not find tools from environment variables since it needs a lot of works on some platforms, such as macOS
* add `external_editors.json` to allow user configure paths directly
This commit is contained in:
leo 2024-08-15 11:32:02 +08:00
parent eaad685d06
commit 0c6af27b40
No known key found for this signature in database
3 changed files with 59 additions and 22 deletions

View file

@ -84,17 +84,27 @@ For **Linux** users:
This app supports open repository in external tools listed in the table below. This app supports open repository in external tools listed in the table below.
| Tool | Windows | macOS | Linux | Environment Variable | | Tool | Windows | macOS | Linux | KEY IN `external_editors.json` |
|-------------------------------|---------|-------|-------|----------------------| |-------------------------------|---------|-------|-------|--------------------------------|
| Visual Studio Code | YES | YES | YES | VSCODE_PATH | | Visual Studio Code | YES | YES | YES | VSCODE |
| Visual Studio Code - Insiders | YES | YES | YES | VSCODE_INSIDERS_PATH | | Visual Studio Code - Insiders | YES | YES | YES | VSCODE_INSIDERS |
| VSCodium | YES | YES | YES | VSCODIUM_PATH | | VSCodium | YES | YES | YES | VSCODIUM |
| JetBrains Fleet | YES | YES | YES | FLEET_PATH | | JetBrains Fleet | YES | YES | YES | FLEET |
| Sublime Text | YES | YES | YES | SUBLIME_TEXT_PATH | | Sublime Text | YES | YES | YES | SUBLIME_TEXT |
* You can set the given environment variable for special tool if it can NOT be found by this app automatically. > [!NOTE]
* Installing `JetBrains Toolbox` will help this app to find other JetBrains tools installed on your device. > This app will try to find those tools based on some pre-defined or expected locations automatically. If you are using one portable version of these tools, it will not be detected by this app.
* On macOS, you may need to use `launchctl setenv` to make sure the app can read these environment variables. > To solve this problem you can add a file named `external_editors.json` and provide the path directly. For example:
```json
{
"tools": {
"VSCODE": "D:\\VSCode\\Code.exe"
}
}
```
> [!NOTE]
> This app also supports a lot of `JetBrains` IDEs, installing `JetBrains Toolbox` will help this app to find them.
## Screenshots ## Screenshots

View file

@ -58,6 +58,7 @@ namespace SourceGit
typeof(GridLengthConverter), typeof(GridLengthConverter),
] ]
)] )]
[JsonSerializable(typeof(Models.ExternalToolPaths))]
[JsonSerializable(typeof(Models.InteractiveRebaseJobCollection))] [JsonSerializable(typeof(Models.InteractiveRebaseJobCollection))]
[JsonSerializable(typeof(Models.JetBrainsState))] [JsonSerializable(typeof(Models.JetBrainsState))]
[JsonSerializable(typeof(Models.ThemeOverrides))] [JsonSerializable(typeof(Models.ThemeOverrides))]

View file

@ -79,6 +79,12 @@ namespace SourceGit.Models
public string LaunchCommand { get; set; } public string LaunchCommand { get; set; }
} }
public class ExternalToolPaths
{
[JsonPropertyName("tools")]
public Dictionary<string, string> Tools { get; set; } = new Dictionary<string, string>();
}
public class ExternalToolsFinder public class ExternalToolsFinder
{ {
public List<ExternalTool> Founded public List<ExternalTool> Founded
@ -87,42 +93,60 @@ namespace SourceGit.Models
private set; private set;
} = new List<ExternalTool>(); } = new List<ExternalTool>();
public void TryAdd(string name, string icon, string args, string env, Func<string> finder) public ExternalToolsFinder()
{ {
var path = Environment.GetEnvironmentVariable(env); var customPathsConfig = Path.Combine(Native.OS.DataDir, "external_editors.json");
if (string.IsNullOrEmpty(path) || !File.Exists(path)) try
{ {
path = finder(); if (File.Exists(customPathsConfig))
if (string.IsNullOrEmpty(path) || !File.Exists(path)) _customPaths = JsonSerializer.Deserialize(File.ReadAllText(customPathsConfig), JsonCodeGen.Default.ExternalToolPaths);
return; }
catch
{
// Ignore
} }
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)); Founded.Add(new ExternalTool(name, icon, path, args));
} }
}
public void VSCode(Func<string> platformFinder) public void VSCode(Func<string> platformFinder)
{ {
TryAdd("Visual Studio Code", "vscode", "\"{0}\"", "VSCODE_PATH", platformFinder); TryAdd("Visual Studio Code", "vscode", "\"{0}\"", "VSCODE", platformFinder);
} }
public void VSCodeInsiders(Func<string> platformFinder) public void VSCodeInsiders(Func<string> platformFinder)
{ {
TryAdd("Visual Studio Code - Insiders", "vscode_insiders", "\"{0}\"", "VSCODE_INSIDERS_PATH", platformFinder); TryAdd("Visual Studio Code - Insiders", "vscode_insiders", "\"{0}\"", "VSCODE_INSIDERS", platformFinder);
} }
public void VSCodium(Func<string> platformFinder) public void VSCodium(Func<string> platformFinder)
{ {
TryAdd("VSCodium", "codium", "\"{0}\"", "VSCODIUM_PATH", platformFinder); TryAdd("VSCodium", "codium", "\"{0}\"", "VSCODIUM", platformFinder);
} }
public void Fleet(Func<string> platformFinder) public void Fleet(Func<string> platformFinder)
{ {
TryAdd("Fleet", "fleet", "\"{0}\"", "FLEET_PATH", platformFinder); TryAdd("Fleet", "fleet", "\"{0}\"", "FLEET", platformFinder);
} }
public void SublimeText(Func<string> platformFinder) public void SublimeText(Func<string> platformFinder)
{ {
TryAdd("Sublime Text", "sublime_text", "\"{0}\"", "SUBLIME_TEXT_PATH", platformFinder); TryAdd("Sublime Text", "sublime_text", "\"{0}\"", "SUBLIME_TEXT", platformFinder);
} }
public void FindJetBrainsFromToolbox(Func<string> platformFinder) public void FindJetBrainsFromToolbox(Func<string> platformFinder)
@ -146,5 +170,7 @@ namespace SourceGit.Models
} }
} }
} }
private ExternalToolPaths _customPaths = null;
} }
} }