2024-03-27 23:48:20 -07:00
|
|
|
|
using System;
|
2024-04-05 22:14:22 -07:00
|
|
|
|
using System.Collections.Generic;
|
2024-03-27 23:48:20 -07:00
|
|
|
|
using System.Diagnostics;
|
2024-03-20 00:36:10 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.Versioning;
|
|
|
|
|
|
|
|
|
|
using Avalonia;
|
|
|
|
|
using Avalonia.Dialogs;
|
2024-03-21 05:55:08 -07:00
|
|
|
|
using Avalonia.Media;
|
2024-03-20 00:36:10 -07:00
|
|
|
|
|
|
|
|
|
namespace SourceGit.Native
|
|
|
|
|
{
|
|
|
|
|
[SupportedOSPlatform("linux")]
|
|
|
|
|
internal class Linux : OS.IBackend
|
|
|
|
|
{
|
2024-04-13 20:13:32 -07:00
|
|
|
|
class Terminal
|
|
|
|
|
{
|
|
|
|
|
public string FilePath { get; set; } = string.Empty;
|
|
|
|
|
public string OpenArgFormat { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
public Terminal(string exec, string fmt)
|
|
|
|
|
{
|
|
|
|
|
FilePath = exec;
|
|
|
|
|
OpenArgFormat = fmt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Open(string dir)
|
|
|
|
|
{
|
|
|
|
|
Process.Start(FilePath, string.Format(OpenArgFormat, dir));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Linux()
|
|
|
|
|
{
|
|
|
|
|
_xdgOpenPath = FindExecutable("xdg-open");
|
|
|
|
|
_terminal = FindTerminal();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 00:36:10 -07:00
|
|
|
|
public void SetupApp(AppBuilder builder)
|
|
|
|
|
{
|
2024-03-21 05:55:08 -07:00
|
|
|
|
builder.With(new FontManagerOptions()
|
|
|
|
|
{
|
|
|
|
|
DefaultFamilyName = "fonts:SourceGit#JetBrains Mono",
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-20 00:36:10 -07:00
|
|
|
|
// Free-desktop file picker has an extra black background panel.
|
|
|
|
|
builder.UseManagedSystemDialogs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string FindGitExecutable()
|
|
|
|
|
{
|
2024-04-13 20:13:32 -07:00
|
|
|
|
return FindExecutable("git");
|
2024-03-20 00:36:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 02:39:52 -07:00
|
|
|
|
public List<Models.ExternalTool> FindExternalTools()
|
2024-03-20 00:36:10 -07:00
|
|
|
|
{
|
2024-04-08 02:39:52 -07:00
|
|
|
|
var finder = new Models.ExternalToolsFinder();
|
2024-04-13 20:13:32 -07:00
|
|
|
|
finder.VSCode(() => FindExecutable("code"));
|
|
|
|
|
finder.VSCodeInsiders(() => FindExecutable("code-insiders"));
|
|
|
|
|
finder.Fleet(FindJetBrainFleet);
|
|
|
|
|
finder.SublimeText(() => FindExecutable("subl"));
|
2024-04-08 02:39:52 -07:00
|
|
|
|
return finder.Founded;
|
2024-03-27 22:49:32 -07:00
|
|
|
|
}
|
2024-03-20 00:36:10 -07:00
|
|
|
|
|
|
|
|
|
public void OpenBrowser(string url)
|
|
|
|
|
{
|
2024-04-13 20:13:32 -07:00
|
|
|
|
if (string.IsNullOrEmpty(_xdgOpenPath))
|
|
|
|
|
App.RaiseException("", $"Can NOT find `xdg-open` command!!!");
|
|
|
|
|
else
|
|
|
|
|
Process.Start(_xdgOpenPath, $"\"{url}\"");
|
2024-03-20 00:36:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OpenInFileManager(string path, bool select)
|
|
|
|
|
{
|
2024-04-13 20:13:32 -07:00
|
|
|
|
if (string.IsNullOrEmpty(_xdgOpenPath))
|
2024-03-20 00:36:10 -07:00
|
|
|
|
{
|
2024-04-13 20:13:32 -07:00
|
|
|
|
App.RaiseException("", $"Can NOT find `xdg-open` command!!!");
|
2024-03-20 00:36:10 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(path))
|
|
|
|
|
{
|
2024-04-13 20:13:32 -07:00
|
|
|
|
Process.Start(_xdgOpenPath, $"\"{path}\"");
|
2024-03-20 00:36:10 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var dir = Path.GetDirectoryName(path);
|
|
|
|
|
if (Directory.Exists(dir))
|
|
|
|
|
{
|
2024-04-13 20:13:32 -07:00
|
|
|
|
Process.Start(_xdgOpenPath, $"\"{dir}\"");
|
2024-03-20 00:36:10 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OpenTerminal(string workdir)
|
|
|
|
|
{
|
|
|
|
|
var dir = string.IsNullOrEmpty(workdir) ? "~" : workdir;
|
2024-04-13 20:13:32 -07:00
|
|
|
|
if (_terminal == null)
|
2024-04-15 03:01:15 -07:00
|
|
|
|
App.RaiseException(dir, $"Only supports gnome-terminal/konsole/xfce4-terminal/lxterminal/deepin-terminal!");
|
2024-04-13 20:13:32 -07:00
|
|
|
|
else
|
|
|
|
|
_terminal.Open(dir);
|
2024-03-20 00:36:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OpenWithDefaultEditor(string file)
|
|
|
|
|
{
|
2024-04-13 20:13:32 -07:00
|
|
|
|
if (string.IsNullOrEmpty(_xdgOpenPath))
|
2024-03-20 00:36:10 -07:00
|
|
|
|
{
|
2024-04-13 20:13:32 -07:00
|
|
|
|
App.RaiseException("", $"Can NOT find `xdg-open` command!!!");
|
2024-03-20 00:36:10 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 20:13:32 -07:00
|
|
|
|
var proc = Process.Start(_xdgOpenPath, $"\"{file}\"");
|
2024-03-20 00:36:10 -07:00
|
|
|
|
proc.WaitForExit();
|
|
|
|
|
|
|
|
|
|
if (proc.ExitCode != 0)
|
|
|
|
|
App.RaiseException("", $"Failed to open \"{file}\"");
|
|
|
|
|
|
|
|
|
|
proc.Close();
|
|
|
|
|
}
|
2024-04-13 20:13:32 -07:00
|
|
|
|
|
|
|
|
|
private string FindExecutable(string filename)
|
|
|
|
|
{
|
|
|
|
|
var pathVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
|
|
|
|
|
var pathes = pathVariable.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (var path in pathes)
|
|
|
|
|
{
|
|
|
|
|
var test = Path.Combine(path, filename);
|
|
|
|
|
if (File.Exists(test))
|
|
|
|
|
{
|
|
|
|
|
return test;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Terminal FindTerminal()
|
|
|
|
|
{
|
|
|
|
|
var pathVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
|
|
|
|
|
var pathes = pathVariable.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (var path in pathes)
|
|
|
|
|
{
|
|
|
|
|
var test = Path.Combine(path, "gnome-terminal");
|
|
|
|
|
if (File.Exists(test))
|
|
|
|
|
{
|
|
|
|
|
return new Terminal(test, "--working-directory=\"{0}\"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test = Path.Combine(path, "konsole");
|
|
|
|
|
if (File.Exists(test))
|
|
|
|
|
{
|
|
|
|
|
return new Terminal(test, "--workdir \"{0}\"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test = Path.Combine(path, "xfce4-terminal");
|
|
|
|
|
if (File.Exists(test))
|
|
|
|
|
{
|
|
|
|
|
return new Terminal(test, "--working-directory=\"{0}\"");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 03:01:15 -07:00
|
|
|
|
test = Path.Combine(path, "lxterminal");
|
|
|
|
|
if (File.Exists(test))
|
|
|
|
|
{
|
|
|
|
|
return new Terminal(test, "--working-directory=\"{0}\"");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 20:13:32 -07:00
|
|
|
|
test = Path.Combine(path, "deepin-terminal");
|
|
|
|
|
if (File.Exists(test))
|
|
|
|
|
{
|
|
|
|
|
return new Terminal(test, "--work-directory \"{0}\"");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string FindJetBrainFleet()
|
|
|
|
|
{
|
|
|
|
|
var path = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}/JetBrains/Toolbox/apps/fleet/bin/Fleet";
|
|
|
|
|
return File.Exists(path) ? path : FindExecutable("fleet");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _xdgOpenPath = string.Empty;
|
|
|
|
|
private Terminal _terminal = null;
|
2024-03-20 00:36:10 -07:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|