From 5cf8b053f63a597b5b0e416eb599ce20b032e5d0 Mon Sep 17 00:00:00 2001 From: ionite34 Date: Sat, 6 Jan 2024 14:58:19 +0800 Subject: [PATCH] Add CheckIsGitRepository, fix vlad git impl --- .../Helpers/UnixPrerequisiteHelper.cs | 21 +++------ .../Helpers/WindowsPrerequisiteHelper.cs | 44 ++++-------------- .../Helper/IPrerequisiteHelper.cs | 17 ++++--- .../Models/Packages/VladAutomatic.cs | 46 +++++++++++++++---- 4 files changed, 63 insertions(+), 65 deletions(-) diff --git a/StabilityMatrix.Avalonia/Helpers/UnixPrerequisiteHelper.cs b/StabilityMatrix.Avalonia/Helpers/UnixPrerequisiteHelper.cs index e31be0da..91124857 100644 --- a/StabilityMatrix.Avalonia/Helpers/UnixPrerequisiteHelper.cs +++ b/StabilityMatrix.Avalonia/Helpers/UnixPrerequisiteHelper.cs @@ -66,15 +66,9 @@ public class UnixPrerequisiteHelper : IPrerequisiteHelper public async Task UnpackResourcesIfNecessary(IProgress? progress = null) { // Array of (asset_uri, extract_to) - var assets = new[] - { - (Assets.SevenZipExecutable, AssetsDir), - (Assets.SevenZipLicense, AssetsDir), - }; + var assets = new[] { (Assets.SevenZipExecutable, AssetsDir), (Assets.SevenZipLicense, AssetsDir), }; - progress?.Report( - new ProgressReport(0, message: "Unpacking resources", isIndeterminate: true) - ); + progress?.Report(new ProgressReport(0, message: "Unpacking resources", isIndeterminate: true)); Directory.CreateDirectory(AssetsDir); foreach (var (asset, extractDir) in assets) @@ -82,9 +76,7 @@ public class UnixPrerequisiteHelper : IPrerequisiteHelper await asset.ExtractToDir(extractDir); } - progress?.Report( - new ProgressReport(1, message: "Unpacking resources", isIndeterminate: false) - ); + progress?.Report(new ProgressReport(1, message: "Unpacking resources", isIndeterminate: false)); } public async Task InstallGitIfNecessary(IProgress? progress = null) @@ -146,8 +138,7 @@ public class UnixPrerequisiteHelper : IPrerequisiteHelper if (result.ExitCode != 0) { Logger.Error( - "Git command [{Command}] failed with exit code " - + "{ExitCode}:\n{StdOut}\n{StdErr}", + "Git command [{Command}] failed with exit code " + "{ExitCode}:\n{StdOut}\n{StdErr}", command, result.ExitCode, result.StandardOutput, @@ -235,9 +226,9 @@ public class UnixPrerequisiteHelper : IPrerequisiteHelper progress?.Report(new ProgressReport(1, "Installing Python", isIndeterminate: false)); } - public Task GetGitOutput(string? workingDirectory = null, params string[] args) + public Task GetGitOutput(ProcessArgs args, string? workingDirectory = null) { - throw new NotImplementedException(); + return ProcessRunner.RunBashCommand(args.Prepend("git").ToArray(), workingDirectory ?? ""); } [UnsupportedOSPlatform("Linux")] diff --git a/StabilityMatrix.Avalonia/Helpers/WindowsPrerequisiteHelper.cs b/StabilityMatrix.Avalonia/Helpers/WindowsPrerequisiteHelper.cs index c6d007d0..92373783 100644 --- a/StabilityMatrix.Avalonia/Helpers/WindowsPrerequisiteHelper.cs +++ b/StabilityMatrix.Avalonia/Helpers/WindowsPrerequisiteHelper.cs @@ -96,19 +96,17 @@ public class WindowsPrerequisiteHelper : IPrerequisiteHelper result.EnsureSuccessExitCode(); } - public async Task GetGitOutput(string? workingDirectory = null, params string[] args) + public Task GetGitOutput(ProcessArgs args, string? workingDirectory = null) { - var process = await ProcessRunner.GetProcessOutputAsync( + return ProcessRunner.GetProcessResultAsync( GitExePath, - string.Join(" ", args), + args, workingDirectory: workingDirectory, environmentVariables: new Dictionary { { "PATH", Compat.GetEnvPathWithExtensions(GitBinPath) } } ); - - return process; } public async Task InstallAllIfNecessary(IProgress? progress = null) @@ -122,15 +120,9 @@ public class WindowsPrerequisiteHelper : IPrerequisiteHelper public async Task UnpackResourcesIfNecessary(IProgress? progress = null) { // Array of (asset_uri, extract_to) - var assets = new[] - { - (Assets.SevenZipExecutable, AssetsDir), - (Assets.SevenZipLicense, AssetsDir), - }; + var assets = new[] { (Assets.SevenZipExecutable, AssetsDir), (Assets.SevenZipLicense, AssetsDir), }; - progress?.Report( - new ProgressReport(0, message: "Unpacking resources", isIndeterminate: true) - ); + progress?.Report(new ProgressReport(0, message: "Unpacking resources", isIndeterminate: true)); Directory.CreateDirectory(AssetsDir); foreach (var (asset, extractDir) in assets) @@ -138,9 +130,7 @@ public class WindowsPrerequisiteHelper : IPrerequisiteHelper await asset.ExtractToDir(extractDir); } - progress?.Report( - new ProgressReport(1, message: "Unpacking resources", isIndeterminate: false) - ); + progress?.Report(new ProgressReport(1, message: "Unpacking resources", isIndeterminate: false)); } public async Task InstallPythonIfNecessary(IProgress? progress = null) @@ -262,11 +252,7 @@ public class WindowsPrerequisiteHelper : IPrerequisiteHelper if (!Directory.Exists(TkinterExistsPath)) { Logger.Info("Downloading Tkinter"); - await downloadService.DownloadToFileAsync( - TkinterDownloadUrl, - TkinterZipPath, - progress: progress - ); + await downloadService.DownloadToFileAsync(TkinterDownloadUrl, TkinterZipPath, progress: progress); progress?.Report( new ProgressReport( progress: 1f, @@ -281,11 +267,7 @@ public class WindowsPrerequisiteHelper : IPrerequisiteHelper } progress?.Report( - new ProgressReport( - progress: 1f, - message: "Tkinter install complete", - type: ProgressType.Generic - ) + new ProgressReport(progress: 1f, message: "Tkinter install complete", type: ProgressType.Generic) ); } @@ -319,10 +301,7 @@ public class WindowsPrerequisiteHelper : IPrerequisiteHelper public async Task InstallVcRedistIfNecessary(IProgress? progress = null) { var registry = Registry.LocalMachine; - var key = registry.OpenSubKey( - @"SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\X64", - false - ); + var key = registry.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\X64", false); if (key != null) { var buildId = Convert.ToUInt32(key.GetValue("Bld")); @@ -356,10 +335,7 @@ public class WindowsPrerequisiteHelper : IPrerequisiteHelper message: "Installing prerequisites..." ) ); - var process = ProcessRunner.StartAnsiProcess( - VcRedistDownloadPath, - "/install /quiet /norestart" - ); + var process = ProcessRunner.StartAnsiProcess(VcRedistDownloadPath, "/install /quiet /norestart"); await process.WaitForExitAsync(); progress?.Report( new ProgressReport( diff --git a/StabilityMatrix.Core/Helper/IPrerequisiteHelper.cs b/StabilityMatrix.Core/Helper/IPrerequisiteHelper.cs index 6c937584..3753e4ba 100644 --- a/StabilityMatrix.Core/Helper/IPrerequisiteHelper.cs +++ b/StabilityMatrix.Core/Helper/IPrerequisiteHelper.cs @@ -21,17 +21,22 @@ public interface IPrerequisiteHelper /// /// Run embedded git with the given arguments. /// - Task RunGit( - ProcessArgs args, - Action? onProcessOutput, - string? workingDirectory = null - ); + Task RunGit(ProcessArgs args, Action? onProcessOutput, string? workingDirectory = null); /// /// Run embedded git with the given arguments. /// Task RunGit(ProcessArgs args, string? workingDirectory = null); - Task GetGitOutput(string? workingDirectory = null, params string[] args); + Task GetGitOutput(ProcessArgs args, string? workingDirectory = null); + + async Task CheckIsGitRepository(string directory) + { + var result = await GetGitOutput(["rev-parse", "--is-inside-work-tree"], directory) + .ConfigureAwait(false); + + return result.ExitCode == 0 && result.StandardOutput?.Trim().ToLowerInvariant() == "true"; + } + Task InstallTkinterIfNecessary(IProgress? progress = null); } diff --git a/StabilityMatrix.Core/Models/Packages/VladAutomatic.cs b/StabilityMatrix.Core/Models/Packages/VladAutomatic.cs index 383ace11..96b65c42 100644 --- a/StabilityMatrix.Core/Models/Packages/VladAutomatic.cs +++ b/StabilityMatrix.Core/Models/Packages/VladAutomatic.cs @@ -204,7 +204,9 @@ public class VladAutomatic( break; default: // CPU - await venvRunner.CustomInstall("launch.py --debug --test", onConsoleOutput).ConfigureAwait(false); + await venvRunner + .CustomInstall("launch.py --debug --test", onConsoleOutput) + .ConfigureAwait(false); break; } @@ -314,7 +316,11 @@ public class VladAutomatic( ); await PrerequisiteHelper - .RunGit(new[] { "checkout", versionOptions.BranchName! }, onConsoleOutput, installedPackage.FullPath) + .RunGit( + new[] { "checkout", versionOptions.BranchName! }, + onConsoleOutput, + installedPackage.FullPath + ) .ConfigureAwait(false); var venvRunner = new PyVenvRunner(Path.Combine(installedPackage.FullPath!, "venv")); @@ -325,14 +331,17 @@ public class VladAutomatic( try { - var output = await PrerequisiteHelper - .GetGitOutput(installedPackage.FullPath, "rev-parse", "HEAD") + var result = await PrerequisiteHelper + .GetGitOutput(["rev-parse", "HEAD"], installedPackage.FullPath) + .EnsureSuccessExitCode() .ConfigureAwait(false); return new InstalledPackageVersion { InstalledBranch = versionOptions.BranchName, - InstalledCommitSha = output.Replace(Environment.NewLine, "").Replace("\n", ""), + InstalledCommitSha = result + .StandardOutput?.Replace(Environment.NewLine, "") + .Replace("\n", ""), IsPrerelease = false }; } @@ -343,7 +352,12 @@ public class VladAutomatic( finally { progress?.Report( - new ProgressReport(1f, message: "Update Complete", isIndeterminate: false, type: ProgressType.Update) + new ProgressReport( + 1f, + message: "Update Complete", + isIndeterminate: false, + type: ProgressType.Update + ) ); } @@ -354,7 +368,10 @@ public class VladAutomatic( }; } - public override Task SetupModelFolders(DirectoryPath installDirectory, SharedFolderMethod sharedFolderMethod) + public override Task SetupModelFolders( + DirectoryPath installDirectory, + SharedFolderMethod sharedFolderMethod + ) { switch (sharedFolderMethod) { @@ -404,13 +421,19 @@ public class VladAutomatic( configRoot["clip_models_path"] = Path.Combine(SettingsManager.ModelsDirectory, "CLIP"); configRoot["control_net_models_path"] = Path.Combine(SettingsManager.ModelsDirectory, "ControlNet"); - var configJsonStr = JsonSerializer.Serialize(configRoot, new JsonSerializerOptions { WriteIndented = true }); + var configJsonStr = JsonSerializer.Serialize( + configRoot, + new JsonSerializerOptions { WriteIndented = true } + ); File.WriteAllText(configJsonPath, configJsonStr); return Task.CompletedTask; } - public override Task UpdateModelFolders(DirectoryPath installDirectory, SharedFolderMethod sharedFolderMethod) => + public override Task UpdateModelFolders( + DirectoryPath installDirectory, + SharedFolderMethod sharedFolderMethod + ) => sharedFolderMethod switch { SharedFolderMethod.Symlink => base.UpdateModelFolders(installDirectory, sharedFolderMethod), @@ -476,7 +499,10 @@ public class VladAutomatic( configRoot.Remove("clip_models_path"); configRoot.Remove("control_net_models_path"); - var configJsonStr = JsonSerializer.Serialize(configRoot, new JsonSerializerOptions { WriteIndented = true }); + var configJsonStr = JsonSerializer.Serialize( + configRoot, + new JsonSerializerOptions { WriteIndented = true } + ); File.WriteAllText(configJsonPath, configJsonStr); return Task.CompletedTask;