sourcegit/src/Native/MacOS.cs

156 lines
4.9 KiB
C#
Raw Normal View History

2024-03-27 23:58:55 -07:00
using System;
using System.Collections.Generic;
2024-03-27 23:58:55 -07:00
using System.Diagnostics;
using System.IO;
using System.Runtime.Versioning;
using System.Text;
using Avalonia;
using Avalonia.Media;
namespace SourceGit.Native
{
[SupportedOSPlatform("macOS")]
internal class MacOS : OS.IBackend
{
public void SetupApp(AppBuilder builder)
{
builder.With(new FontManagerOptions()
{
DefaultFamilyName = "PingFang SC",
});
}
public string FindGitExecutable()
{
if (File.Exists("/usr/bin/git"))
return "/usr/bin/git";
return string.Empty;
}
public List<Models.ExternalEditor> FindExternalEditors()
{
var editors = new List<Models.ExternalEditor>();
var vscode = FindVSCode();
if (!string.IsNullOrEmpty(vscode) && File.Exists(vscode))
{
editors.Add(new Models.ExternalEditor
{
Name = "Visual Studio Code",
Icon = new Uri("avares://SourceGit/Resources/ExternalToolIcons/vscode.png", UriKind.Absolute),
Executable = vscode,
OpenCmdArgs = "\"{0}\"",
});
}
var vscodeInsiders = FindVSCodeInsiders();
if (!string.IsNullOrEmpty(vscodeInsiders) && File.Exists(vscodeInsiders))
{
editors.Add(new Models.ExternalEditor
{
Name = "Visual Studio Code - Insiders",
Icon = new Uri("avares://SourceGit/Resources/ExternalToolIcons/vscode_insiders.png", UriKind.Absolute),
Executable = vscodeInsiders,
OpenCmdArgs = "\"{0}\"",
});
}
var fleet = FindFleet();
if (!string.IsNullOrEmpty(fleet) && File.Exists(fleet))
{
editors.Add(new Models.ExternalEditor
{
Name = "JetBrains Fleet",
Icon = new Uri("avares://SourceGit/Resources/ExternalToolIcons/fleet.png", UriKind.Absolute),
Executable = fleet,
OpenCmdArgs = "\"{0}\"",
});
}
return editors;
2024-03-27 22:49:32 -07:00
}
public void OpenBrowser(string url)
{
Process.Start("open", url);
}
public void OpenInFileManager(string path, bool select)
{
if (Directory.Exists(path))
{
Process.Start("open", path);
}
else if (File.Exists(path))
{
Process.Start("open", $"\"{path}\" -R");
}
}
public void OpenTerminal(string workdir)
{
var dir = string.IsNullOrEmpty(workdir) ? "~" : workdir;
var builder = new StringBuilder();
builder.AppendLine("on run argv");
builder.AppendLine(" tell application \"Terminal\"");
builder.AppendLine($" do script \"cd '{dir}'\"");
builder.AppendLine(" activate");
builder.AppendLine(" end tell");
builder.AppendLine("end run");
var tmp = Path.GetTempFileName();
File.WriteAllText(tmp, builder.ToString());
var proc = Process.Start("/usr/bin/osascript", $"\"{tmp}\"");
proc.Exited += (o, e) => File.Delete(tmp);
}
public void OpenWithDefaultEditor(string file)
{
Process.Start("open", file);
}
#region EXTERNAL_EDITORS_FINDER
private string FindVSCode()
{
var toolPath = "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code";
if (File.Exists(toolPath))
return toolPath;
var customPath = Environment.GetEnvironmentVariable("VSCODE_PATH");
if (!string.IsNullOrEmpty(customPath))
return customPath;
return string.Empty;
}
private string FindVSCodeInsiders()
{
var toolPath = "/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code";
if (File.Exists(toolPath))
return toolPath;
var customPath = Environment.GetEnvironmentVariable("VSCODE_INSIDERS_PATH");
if (!string.IsNullOrEmpty(customPath))
return customPath;
return string.Empty;
}
private string FindFleet()
{
var toolPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/Applications/Fleet.app/Contents/MacOS/Fleet";
if (File.Exists(toolPath))
return toolPath;
var customPath = Environment.GetEnvironmentVariable("FLEET_PATH");
if (!string.IsNullOrEmpty(customPath))
return customPath;
return string.Empty;
}
#endregion
}
}