project: reorganize the structure of the project.

* remove dotnet-tool.json because the project does not rely on any dotnet tools.
* remove Directory.Build.props because the solution has only one project.
* move src/SourceGit to src. It's not needed to put all sources into a subfolder of src since there's only one project.
This commit is contained in:
leo 2024-04-02 20:00:33 +08:00
parent 96e60da7ad
commit 96d4150d26
319 changed files with 37 additions and 53 deletions

123
src/Native/Linux.cs Normal file
View file

@ -0,0 +1,123 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Versioning;
using Avalonia;
using Avalonia.Dialogs;
using Avalonia.Media;
namespace SourceGit.Native
{
[SupportedOSPlatform("linux")]
internal class Linux : OS.IBackend
{
public void SetupApp(AppBuilder builder)
{
builder.With(new FontManagerOptions()
{
DefaultFamilyName = "fonts:SourceGit#JetBrains Mono",
});
// 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()
{
var toolPath = "/usr/share/code/code";
if (File.Exists(toolPath))
return toolPath;
return string.Empty;
}
public string FindFleet()
{
var toolPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/.local/share/JetBrains/Toolbox/apps/fleet/bin/Fleet";
if (File.Exists(toolPath))
return toolPath;
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();
}
}
}

86
src/Native/MacOS.cs Normal file
View file

@ -0,0 +1,86 @@
using System;
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 string FindVSCode()
{
var toolPath = "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code";
if (File.Exists(toolPath))
return toolPath;
return string.Empty;
}
public string FindFleet()
{
var toolPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/Applications/Fleet.app/Contents/MacOS/Fleet";
if (File.Exists(toolPath))
return toolPath;
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);
}
}
}

119
src/Native/OS.cs Normal file
View file

@ -0,0 +1,119 @@
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();
string FindFleet();
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; }
public static string FleetExecutableFile { get; set; }
static OS()
{
if (OperatingSystem.IsWindows())
{
_backend = new Windows();
}
else if (OperatingSystem.IsMacOS())
{
_backend = new MacOS();
}
else if (OperatingSystem.IsLinux())
{
_backend = new Linux();
}
else
{
throw new Exception("Platform unsupported!!!");
}
VSCodeExecutableFile = _backend.FindVSCode();
FleetExecutableFile = _backend.FindFleet();
}
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,
});
}
public static void OpenInFleet(string repo)
{
if (string.IsNullOrEmpty(FleetExecutableFile))
{
App.RaiseException(repo, "Fleet can NOT be found in your system!!!");
return;
}
Process.Start(new ProcessStartInfo()
{
WorkingDirectory = repo,
FileName = FleetExecutableFile,
Arguments = $"\"{repo}\"",
UseShellExecute = false,
});
}
private static readonly IBackend _backend;
}
}

218
src/Native/Windows.cs Normal file
View file

@ -0,0 +1,218 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
namespace SourceGit.Native
{
[SupportedOSPlatform("windows")]
internal class Windows : OS.IBackend
{
[StructLayout(LayoutKind.Sequential)]
internal struct RTL_OSVERSIONINFOEX
{
internal uint dwOSVersionInfoSize;
internal uint dwMajorVersion;
internal uint dwMinorVersion;
internal uint dwBuildNumber;
internal uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
internal string szCSDVersion;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs);
[DllImport("ntdll")]
private static extern int RtlGetVersion(ref RTL_OSVERSIONINFOEX lpVersionInformation);
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern IntPtr ILCreateFromPathW(string pszPath);
[DllImport("shell32.dll", SetLastError = false)]
private static extern void ILFree(IntPtr pidl);
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags);
public void SetupApp(AppBuilder builder)
{
builder.With(new FontManagerOptions()
{
DefaultFamilyName = "Microsoft YaHei UI",
FontFallbacks = [new FontFallback { FontFamily = "Microsoft YaHei" }],
});
// Fix drop shadow issue on Windows 10
RTL_OSVERSIONINFOEX v = new RTL_OSVERSIONINFOEX();
v.dwOSVersionInfoSize = (uint)Marshal.SizeOf<RTL_OSVERSIONINFOEX>();
if (RtlGetVersion(ref v) == 0 && (v.dwMajorVersion < 10 || v.dwBuildNumber < 22000))
{
Window.WindowStateProperty.Changed.AddClassHandler<Window>((w, e) =>
{
if (w.WindowState != WindowState.Maximized)
{
var margins = new MARGINS { cxLeftWidth = 1, cxRightWidth = 1, cyTopHeight = 1, cyBottomHeight = 1 };
DwmExtendFrameIntoClientArea(w.TryGetPlatformHandle().Handle, ref margins);
}
});
Window.LoadedEvent.AddClassHandler<Window>((w, e) =>
{
if (w.WindowState != WindowState.Maximized)
{
var margins = new MARGINS { cxLeftWidth = 1, cxRightWidth = 1, cyTopHeight = 1, cyBottomHeight = 1 };
DwmExtendFrameIntoClientArea(w.TryGetPlatformHandle().Handle, ref margins);
}
});
}
}
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\{EA457B21-F73E-494C-ACAB-524FDE069978}_is1");
if (vscode != null)
{
return vscode.GetValue("DisplayIcon") as string;
}
var toolPath = Environment.ExpandEnvironmentVariables($"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe");
if (File.Exists(toolPath))
return toolPath;
return string.Empty;
}
public string FindFleet()
{
var toolPath = Environment.ExpandEnvironmentVariables($"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\\AppData\\Local\\Programs\\Fleet\\Fleet.exe");
if (File.Exists(toolPath))
return toolPath;
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;
// For security reason, we never execute a file.
// Instead, we open the folder and select it.
select = true;
}
else
{
fullpath = new DirectoryInfo(path).FullName;
}
if (select)
{
// The fullpath here may be a file or a folder.
OpenFolderAndSelectFile(fullpath);
}
else
{
// The fullpath here is always a folder.
Process.Start(new ProcessStartInfo(fullpath)
{
UseShellExecute = true,
CreateNoWindow = true,
});
}
}
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);
}
private void OpenFolderAndSelectFile(string folderPath)
{
var pidl = ILCreateFromPathW(folderPath);
try
{
SHOpenFolderAndSelectItems(pidl, 0, 0, 0);
}
finally
{
ILFree(pidl);
}
}
}
}