sourcegit/src/Native/Linux.cs

188 lines
5.9 KiB
C#
Raw Normal View History

2024-03-27 23:48:20 -07:00
using System;
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;
using Avalonia.Media;
2024-03-20 00:36:10 -07:00
namespace SourceGit.Native
{
[SupportedOSPlatform("linux")]
internal class Linux : OS.IBackend
{
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)
{
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()
{
return FindExecutable("git");
2024-03-20 00:36:10 -07:00
}
public List<Models.ExternalTool> FindExternalTools()
2024-03-20 00:36:10 -07:00
{
var finder = new Models.ExternalToolsFinder();
finder.VSCode(() => FindExecutable("code"));
finder.VSCodeInsiders(() => FindExecutable("code-insiders"));
finder.Fleet(FindJetBrainFleet);
2024-04-27 00:12:03 -07:00
finder.FindJetBrainsFromToolbox(() => $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}/JetBrains/Toolbox/apps");
finder.SublimeText(() => FindExecutable("subl"));
return finder.Founded;
2024-03-27 22:49:32 -07:00
}
2024-03-20 00:36:10 -07:00
public void OpenBrowser(string url)
{
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)
{
if (string.IsNullOrEmpty(_xdgOpenPath))
2024-03-20 00:36:10 -07:00
{
App.RaiseException("", $"Can NOT find `xdg-open` command!!!");
2024-03-20 00:36:10 -07:00
return;
}
if (Directory.Exists(path))
{
Process.Start(_xdgOpenPath, $"\"{path}\"");
2024-03-20 00:36:10 -07:00
}
else
{
var dir = Path.GetDirectoryName(path);
if (Directory.Exists(dir))
{
Process.Start(_xdgOpenPath, $"\"{dir}\"");
2024-03-20 00:36:10 -07:00
}
}
}
public void OpenTerminal(string workdir)
{
var dir = string.IsNullOrEmpty(workdir) ? "~" : workdir;
if (_terminal == null)
2024-04-15 03:01:15 -07:00
App.RaiseException(dir, $"Only supports gnome-terminal/konsole/xfce4-terminal/lxterminal/deepin-terminal!");
else
_terminal.Open(dir);
2024-03-20 00:36:10 -07:00
}
public void OpenWithDefaultEditor(string file)
{
if (string.IsNullOrEmpty(_xdgOpenPath))
2024-03-20 00:36:10 -07:00
{
App.RaiseException("", $"Can NOT find `xdg-open` command!!!");
2024-03-20 00:36:10 -07:00
return;
}
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();
}
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}\"");
}
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
}
}