Browse Source

Merge pull request #67 from ionite34/fix-portable-git

pull/5/head
Ionite 1 year ago committed by GitHub
parent
commit
151ff86d93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 52
      StabilityMatrix/Helper/ArchiveHelper.cs
  2. 12
      StabilityMatrix/Helper/PrerequisiteHelper.cs
  3. 10
      StabilityMatrix/Models/ProgressReport.cs
  4. 8
      StabilityMatrix/Models/ProgressType.cs
  5. 2
      StabilityMatrix/ViewModels/InstallerViewModel.cs
  6. 2
      StabilityMatrix/ViewModels/OneClickInstallViewModel.cs

52
StabilityMatrix/Helper/ArchiveHelper.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
@ -22,6 +23,8 @@ public static class ArchiveHelper
public static string SevenZipPath => Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RelativeSevenZipPath)); public static string SevenZipPath => Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, RelativeSevenZipPath));
private static readonly Regex Regex7ZOutput = new(@"(?<=Size:\s*)\d+|(?<=Compressed:\s*)\d+"); private static readonly Regex Regex7ZOutput = new(@"(?<=Size:\s*)\d+|(?<=Compressed:\s*)\d+");
private static readonly Regex Regex7ZProgressDigits = new(@"(?<=\s*)\d+(?=%)");
private static readonly Regex Regex7ZProgressFull = new(@"(\d+)%.*- (.*)");
public static async Task<ArchiveInfo> TestArchive(string archivePath) public static async Task<ArchiveInfo> TestArchive(string archivePath)
{ {
@ -34,6 +37,55 @@ public static class ArchiveHelper
return new ArchiveInfo(size, compressed); return new ArchiveInfo(size, compressed);
} }
public static async Task<ArchiveInfo> Extract7Z(string archivePath, string extractDirectory)
{
var process = ProcessRunner.StartProcess(SevenZipPath, new[]
{
"x", archivePath, $"-o{ProcessRunner.Quote(extractDirectory)}", "-y"
});
await process.WaitForExitAsync();
var output = await process.StandardOutput.ReadToEndAsync();
var matches = Regex7ZOutput.Matches(output);
var size = ulong.Parse(matches[0].Value);
var compressed = ulong.Parse(matches[1].Value);
return new ArchiveInfo(size, compressed);
}
public static async Task<ArchiveInfo> Extract7Z(string archivePath, string extractDirectory, IProgress<ProgressReport> progress)
{
var outputStore = new StringBuilder();
var onOutput = new Action<string?>(s =>
{
// Parse progress
Logger.Trace($"7z: {s}");
outputStore.AppendLine(s);
var match = Regex7ZProgressFull.Match(s ?? "");
if (match.Success)
{
var percent = int.Parse(match.Groups[1].Value);
var currentFile = match.Groups[2].Value;
progress.Report(new ProgressReport(percent / (float) 100, "Extracting", currentFile, type: ProgressType.Extract));
}
});
progress.Report(new ProgressReport(-1, isIndeterminate: true, type: ProgressType.Extract));
// Need -bsp1 for progress reports
var process = ProcessRunner.StartProcess(SevenZipPath, new[]
{
"x", archivePath, $"-o{ProcessRunner.Quote(extractDirectory)}", "-y", "-bsp1"
}, outputDataReceived: onOutput);
await process.WaitForExitAsync();
progress.Report(new ProgressReport(1, "Finished extracting", type: ProgressType.Extract));
var output = outputStore.ToString();
var matches = Regex7ZOutput.Matches(output);
var size = ulong.Parse(matches[0].Value);
var compressed = ulong.Parse(matches[1].Value);
return new ArchiveInfo(size, compressed);
}
/// <summary> /// <summary>
/// Extract an archive to the output directory. /// Extract an archive to the output directory.
/// </summary> /// </summary>

12
StabilityMatrix/Helper/PrerequisiteHelper.cs

@ -22,7 +22,7 @@ public class PrerequisiteHelper : IPrerequisiteHelper
private static readonly string PortableGitDownloadPath = private static readonly string PortableGitDownloadPath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StabilityMatrix", Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StabilityMatrix",
"PortableGit.7z"); "PortableGit.7z.exe");
private static readonly string GitExePath = Path.Combine(PortableGitInstallDir, "bin", "git.exe"); private static readonly string GitExePath = Path.Combine(PortableGitInstallDir, "bin", "git.exe");
@ -62,8 +62,14 @@ public class PrerequisiteHelper : IPrerequisiteHelper
private async Task UnzipGit(IProgress<ProgressReport>? progress = null) private async Task UnzipGit(IProgress<ProgressReport>? progress = null)
{ {
progress?.Report(new ProgressReport(1, isIndeterminate: true, message: "Installing git...")); if (progress == null)
await ArchiveHelper.Extract(PortableGitDownloadPath, PortableGitInstallDir, progress); {
await ArchiveHelper.Extract7Z(PortableGitDownloadPath, PortableGitInstallDir);
}
else
{
await ArchiveHelper.Extract7Z(PortableGitDownloadPath, PortableGitInstallDir, progress);
}
logger.LogInformation("Extracted Git"); logger.LogInformation("Extracted Git");

10
StabilityMatrix/Models/ProgressReport.cs

@ -20,16 +20,18 @@ public record struct ProgressReport
public string? Message { get; init; } public string? Message { get; init; }
public bool IsIndeterminate { get; init; } = false; public bool IsIndeterminate { get; init; } = false;
public float Percentage => (float) Math.Ceiling(Math.Clamp(Progress ?? 0, 0, 1) * 100); public float Percentage => (float) Math.Ceiling(Math.Clamp(Progress ?? 0, 0, 1) * 100);
public ProgressType Type { get; init; } = ProgressType.Generic;
public ProgressReport(double progress, string? title = null, string? message = null, bool isIndeterminate = false) public ProgressReport(double progress, string? title = null, string? message = null, bool isIndeterminate = false, ProgressType type = ProgressType.Generic)
{ {
Progress = progress; Progress = progress;
Title = title; Title = title;
Message = message; Message = message;
IsIndeterminate = isIndeterminate; IsIndeterminate = isIndeterminate;
Type = type;
} }
public ProgressReport(ulong current, ulong total, string? title = null, string? message = null, bool isIndeterminate = false) public ProgressReport(ulong current, ulong total, string? title = null, string? message = null, bool isIndeterminate = false, ProgressType type = ProgressType.Generic)
{ {
Current = current; Current = current;
Total = total; Total = total;
@ -37,13 +39,15 @@ public record struct ProgressReport
Title = title; Title = title;
Message = message; Message = message;
IsIndeterminate = isIndeterminate; IsIndeterminate = isIndeterminate;
Type = type;
} }
public ProgressReport(ulong current, string? title = null, string? message = null) public ProgressReport(ulong current, string? title = null, string? message = null, ProgressType type = ProgressType.Generic)
{ {
Current = current; Current = current;
Title = title; Title = title;
Message = message; Message = message;
IsIndeterminate = true; IsIndeterminate = true;
Type = type;
} }
} }

8
StabilityMatrix/Models/ProgressType.cs

@ -0,0 +1,8 @@
namespace StabilityMatrix.Models;
public enum ProgressType
{
Generic,
Download,
Extract,
}

2
StabilityMatrix/ViewModels/InstallerViewModel.cs

@ -397,7 +397,7 @@ public partial class InstallerViewModel : ObservableObject
{ {
ProgressText = $"Downloading Git... {progress.Percentage:N0}%"; ProgressText = $"Downloading Git... {progress.Percentage:N0}%";
} }
else if (string.IsNullOrWhiteSpace(progress.Message)) else if (progress.Type == ProgressType.Extract)
{ {
ProgressText = $"Installing Git... {progress.Percentage:N0}%"; ProgressText = $"Installing Git... {progress.Percentage:N0}%";
} }

2
StabilityMatrix/ViewModels/OneClickInstallViewModel.cs

@ -81,7 +81,7 @@ public partial class OneClickInstallViewModel : ObservableObject
{ {
SubHeaderText = $"Downloading Git... {progress.Percentage:N0}%"; SubHeaderText = $"Downloading Git... {progress.Percentage:N0}%";
} }
else if (progress.Message?.Contains("Extracting") ?? false) else if (progress.Type == ProgressType.Extract)
{ {
SubHeaderText = $"Installing Git... {progress.Percentage:N0}%"; SubHeaderText = $"Installing Git... {progress.Percentage:N0}%";
} }

Loading…
Cancel
Save