mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-22 12:45:00 +00:00
Project Location
This commit is contained in:
parent
014e37e505
commit
a1a14f8858
305 changed files with 9783 additions and 9783 deletions
108
src/SourceGit/Native/Linux.cs
Normal file
108
src/SourceGit/Native/Linux.cs
Normal file
|
@ -0,0 +1,108 @@
|
|||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.Versioning;
|
||||
|
||||
using Avalonia;
|
||||
using Avalonia.Dialogs;
|
||||
|
||||
namespace SourceGit.Native
|
||||
{
|
||||
[SupportedOSPlatform("linux")]
|
||||
internal class Linux : OS.IBackend
|
||||
{
|
||||
public void SetupApp(AppBuilder builder)
|
||||
{
|
||||
#if USE_FONT_INTER
|
||||
builder.WithInterFont();
|
||||
#endif
|
||||
// Free-desktop file picker has an extra black background panel.
|
||||
builder.UseManagedSystemDialogs();
|
||||
}
|
||||
|
||||
public string FindGitExecutable()
|
||||
{
|
||||
if (File.Exists("/usr/bin/git")) return "/usr/bin/git";
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public string FindVSCode()
|
||||
{
|
||||
if (File.Exists("/usr/share/code/code")) return "/usr/share/code/code";
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public void OpenBrowser(string url)
|
||||
{
|
||||
if (!File.Exists("/usr/bin/xdg-open"))
|
||||
{
|
||||
App.RaiseException("", $"You should install xdg-open first!");
|
||||
return;
|
||||
}
|
||||
|
||||
Process.Start("xdg-open", $"\"{url}\"");
|
||||
}
|
||||
|
||||
public void OpenInFileManager(string path, bool select)
|
||||
{
|
||||
if (!File.Exists("/usr/bin/xdg-open"))
|
||||
{
|
||||
App.RaiseException("", $"You should install xdg-open first!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (Directory.Exists(path))
|
||||
{
|
||||
Process.Start("xdg-open", $"\"{path}\"");
|
||||
}
|
||||
else
|
||||
{
|
||||
var dir = Path.GetDirectoryName(path);
|
||||
if (Directory.Exists(dir))
|
||||
{
|
||||
Process.Start("xdg-open", $"\"{dir}\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OpenTerminal(string workdir)
|
||||
{
|
||||
var dir = string.IsNullOrEmpty(workdir) ? "~" : workdir;
|
||||
if (File.Exists("/usr/bin/gnome-terminal"))
|
||||
{
|
||||
Process.Start("/usr/bin/gnome-terminal", $"--working-directory=\"{dir}\"");
|
||||
}
|
||||
else if (File.Exists("/usr/bin/konsole"))
|
||||
{
|
||||
Process.Start("/usr/bin/konsole", $"--workdir \"{dir}\"");
|
||||
}
|
||||
else if (File.Exists("/usr/bin/xfce4-terminal"))
|
||||
{
|
||||
Process.Start("/usr/bin/xfce4-terminal", $"--working-directory=\"{dir}\"");
|
||||
}
|
||||
else
|
||||
{
|
||||
App.RaiseException("", $"Only supports gnome-terminal/konsole/xfce4-terminal!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void OpenWithDefaultEditor(string file)
|
||||
{
|
||||
if (!File.Exists("/usr/bin/xdg-open"))
|
||||
{
|
||||
App.RaiseException("", $"You should install xdg-open first!");
|
||||
return;
|
||||
}
|
||||
|
||||
var proc = Process.Start("xdg-open", $"\"{file}\"");
|
||||
proc.WaitForExit();
|
||||
|
||||
if (proc.ExitCode != 0)
|
||||
{
|
||||
App.RaiseException("", $"Failed to open \"{file}\"");
|
||||
}
|
||||
|
||||
proc.Close();
|
||||
}
|
||||
}
|
||||
}
|
81
src/SourceGit/Native/MacOS.cs
Normal file
81
src/SourceGit/Native/MacOS.cs
Normal file
|
@ -0,0 +1,81 @@
|
|||
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",
|
||||
FontFallbacks = [
|
||||
new FontFallback { FontFamily = new FontFamily("PingFang SC") }
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
public string FindGitExecutable()
|
||||
{
|
||||
if (File.Exists("/usr/bin/git")) return "/usr/bin/git";
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public string FindVSCode()
|
||||
{
|
||||
if (File.Exists("/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"))
|
||||
{
|
||||
return "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code";
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
107
src/SourceGit/Native/OS.cs
Normal file
107
src/SourceGit/Native/OS.cs
Normal file
|
@ -0,0 +1,107 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Avalonia;
|
||||
|
||||
namespace SourceGit.Native
|
||||
{
|
||||
public static class OS
|
||||
{
|
||||
public interface IBackend
|
||||
{
|
||||
void SetupApp(AppBuilder builder);
|
||||
|
||||
string FindGitExecutable();
|
||||
string FindVSCode();
|
||||
|
||||
void OpenTerminal(string workdir);
|
||||
void OpenInFileManager(string path, bool select);
|
||||
void OpenBrowser(string url);
|
||||
void OpenWithDefaultEditor(string file);
|
||||
}
|
||||
|
||||
public static string GitInstallPath
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public static string VSCodeExecutableFile
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
static OS()
|
||||
{
|
||||
if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
_backend = new MacOS();
|
||||
VSCodeExecutableFile = _backend.FindVSCode();
|
||||
}
|
||||
else if (OperatingSystem.IsWindows())
|
||||
{
|
||||
_backend = new Windows();
|
||||
VSCodeExecutableFile = _backend.FindVSCode();
|
||||
}
|
||||
else if (OperatingSystem.IsLinux())
|
||||
{
|
||||
_backend = new Linux();
|
||||
VSCodeExecutableFile = _backend.FindVSCode();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Platform unsupported!!!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetupApp(AppBuilder builder)
|
||||
{
|
||||
_backend?.SetupApp(builder);
|
||||
}
|
||||
|
||||
public static string FindGitExecutable()
|
||||
{
|
||||
return _backend?.FindGitExecutable();
|
||||
}
|
||||
|
||||
public static void OpenInFileManager(string path, bool select = false)
|
||||
{
|
||||
_backend?.OpenInFileManager(path, select);
|
||||
}
|
||||
|
||||
public static void OpenBrowser(string url)
|
||||
{
|
||||
_backend?.OpenBrowser(url);
|
||||
}
|
||||
|
||||
public static void OpenTerminal(string workdir)
|
||||
{
|
||||
_backend?.OpenTerminal(workdir);
|
||||
}
|
||||
|
||||
public static void OpenWithDefaultEditor(string file)
|
||||
{
|
||||
_backend?.OpenWithDefaultEditor(file);
|
||||
}
|
||||
|
||||
public static void OpenInVSCode(string repo)
|
||||
{
|
||||
if (string.IsNullOrEmpty(VSCodeExecutableFile))
|
||||
{
|
||||
App.RaiseException(repo, "Visual Studio Code can NOT be found in your system!!!");
|
||||
return;
|
||||
}
|
||||
|
||||
Process.Start(new ProcessStartInfo()
|
||||
{
|
||||
WorkingDirectory = repo,
|
||||
FileName = VSCodeExecutableFile,
|
||||
Arguments = $"\"{repo}\"",
|
||||
UseShellExecute = false,
|
||||
});
|
||||
}
|
||||
|
||||
private static readonly IBackend _backend = null;
|
||||
}
|
||||
}
|
140
src/SourceGit/Native/Windows.cs
Normal file
140
src/SourceGit/Native/Windows.cs
Normal file
|
@ -0,0 +1,140 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Text;
|
||||
|
||||
using Avalonia;
|
||||
using Avalonia.Media;
|
||||
|
||||
namespace SourceGit.Native
|
||||
{
|
||||
[SupportedOSPlatform("windows")]
|
||||
internal class Windows : OS.IBackend
|
||||
{
|
||||
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)]
|
||||
private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs);
|
||||
|
||||
public void SetupApp(AppBuilder builder)
|
||||
{
|
||||
builder.With(new FontManagerOptions()
|
||||
{
|
||||
DefaultFamilyName = "Microsoft YaHei UI",
|
||||
FontFallbacks = [
|
||||
new FontFallback { FontFamily = new FontFamily("Microsoft YaHei UI") }
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
public string FindGitExecutable()
|
||||
{
|
||||
var reg = Microsoft.Win32.RegistryKey.OpenBaseKey(
|
||||
Microsoft.Win32.RegistryHive.LocalMachine,
|
||||
Microsoft.Win32.RegistryView.Registry64);
|
||||
|
||||
var git = reg.OpenSubKey("SOFTWARE\\GitForWindows");
|
||||
if (git != null)
|
||||
{
|
||||
return Path.Combine(git.GetValue("InstallPath") as string, "bin", "git.exe");
|
||||
}
|
||||
|
||||
var builder = new StringBuilder("git.exe", 259);
|
||||
if (!PathFindOnPath(builder, null))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var exePath = builder.ToString();
|
||||
if (string.IsNullOrEmpty(exePath)) return null;
|
||||
|
||||
return exePath;
|
||||
}
|
||||
|
||||
public string FindVSCode()
|
||||
{
|
||||
var root = Microsoft.Win32.RegistryKey.OpenBaseKey(
|
||||
Microsoft.Win32.RegistryHive.LocalMachine,
|
||||
Environment.Is64BitOperatingSystem ? Microsoft.Win32.RegistryView.Registry64 : Microsoft.Win32.RegistryView.Registry32);
|
||||
|
||||
var vscode = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{C26E74D1-022E-4238-8B9D-1E7564A36CC9}_is1");
|
||||
if (vscode != null)
|
||||
{
|
||||
return vscode.GetValue("DisplayIcon") as string;
|
||||
}
|
||||
|
||||
vscode = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1287CAD5-7C8D-410D-88B9-0D1EE4A83FF2}_is1");
|
||||
if (vscode != null)
|
||||
{
|
||||
return vscode.GetValue("DisplayIcon") as string;
|
||||
}
|
||||
|
||||
vscode = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{F8A2A208-72B3-4D61-95FC-8A65D340689B}_is1");
|
||||
if (vscode != null)
|
||||
{
|
||||
return vscode.GetValue("DisplayIcon") as string;
|
||||
}
|
||||
|
||||
vscode = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{EA457B21-F73E-494C-ACAB-524FDE069978}_is1");
|
||||
if (vscode != null)
|
||||
{
|
||||
return vscode.GetValue("DisplayIcon") as string;
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public void OpenBrowser(string url)
|
||||
{
|
||||
var info = new ProcessStartInfo("cmd", $"/c start {url}");
|
||||
info.CreateNoWindow = true;
|
||||
Process.Start(info);
|
||||
}
|
||||
|
||||
public void OpenTerminal(string workdir)
|
||||
{
|
||||
var bash = Path.Combine(Path.GetDirectoryName(OS.GitInstallPath), "bash.exe");
|
||||
if (!File.Exists(bash))
|
||||
{
|
||||
App.RaiseException(string.IsNullOrEmpty(workdir) ? "" : workdir, $"Can NOT found bash.exe under '{Path.GetDirectoryName(OS.GitInstallPath)}'");
|
||||
return;
|
||||
}
|
||||
|
||||
var startInfo = new ProcessStartInfo();
|
||||
startInfo.UseShellExecute = true;
|
||||
startInfo.FileName = bash;
|
||||
if (!string.IsNullOrEmpty(workdir) && Path.Exists(workdir)) startInfo.WorkingDirectory = workdir;
|
||||
Process.Start(startInfo);
|
||||
}
|
||||
|
||||
public void OpenInFileManager(string path, bool select)
|
||||
{
|
||||
var fullpath = string.Empty;
|
||||
if (File.Exists(path))
|
||||
{
|
||||
fullpath = new FileInfo(path).FullName;
|
||||
}
|
||||
else
|
||||
{
|
||||
fullpath = new DirectoryInfo(path).FullName;
|
||||
}
|
||||
|
||||
if (select)
|
||||
{
|
||||
Process.Start("explorer", $"/select,\"{fullpath}\"");
|
||||
}
|
||||
else
|
||||
{
|
||||
Process.Start("explorer", fullpath);
|
||||
}
|
||||
}
|
||||
|
||||
public void OpenWithDefaultEditor(string file)
|
||||
{
|
||||
var info = new FileInfo(file);
|
||||
var start = new ProcessStartInfo("cmd", $"/c start {info.FullName}");
|
||||
start.CreateNoWindow = true;
|
||||
Process.Start(start);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue