From 7ea79d67f8193fc8008c52973fb68e5c5772b220 Mon Sep 17 00:00:00 2001 From: WantToBeeMe <93130991+WantToBeeMe@users.noreply.github.com> Date: Tue, 1 Jul 2025 16:18:27 +0200 Subject: [PATCH 1/2] adding logging --- .../CustomDistributions/RetroRewind.cs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/WheelWizard/Features/CustomDistributions/RetroRewind.cs b/WheelWizard/Features/CustomDistributions/RetroRewind.cs index c35a4192..5bf5cce2 100644 --- a/WheelWizard/Features/CustomDistributions/RetroRewind.cs +++ b/WheelWizard/Features/CustomDistributions/RetroRewind.cs @@ -2,6 +2,7 @@ using System.IO.Compression; using System.Text.RegularExpressions; using Avalonia.Threading; +using Microsoft.Extensions.Logging; using Semver; using WheelWizard.CustomDistributions.Domain; using WheelWizard.Helpers; @@ -18,11 +19,13 @@ public class RetroRewind : IDistribution { private readonly IFileSystem _fileSystem; private readonly IApiCaller _api; - - public RetroRewind(IFileSystem fileSystem, IApiCaller api) + private readonly ILogger _logger; + + public RetroRewind(IFileSystem fileSystem, IApiCaller api, ILogger logger) { _api = api; _fileSystem = fileSystem; + _logger = logger; } public string Title => "Retro Rewind"; @@ -51,15 +54,16 @@ public async Task InstallAsync(ProgressWindow progressWindow) } var serverResponse = await _api.CallApiAsync(api => api.Ping()); // actual response doesnt matter if (serverResponse.IsFailure) - { return Fail("Could not connect to the server"); - } + var downloadResult = await DownloadAndExtractRetroRewind(progressWindow); if (downloadResult.IsFailure) return downloadResult; + var updateResult = await UpdateAsync(progressWindow); if (updateResult.IsFailure) return updateResult; + return Ok(); } @@ -92,7 +96,10 @@ private async Task DownloadAndExtractRetroRewind(ProgressWindow var extractResult = await Task.Run(() => ExtractZipFile(tempZipPath, tempExtractionPath, progressWindow)); if (extractResult.IsFailure) - return extractResult; + { + result = extractResult; + throw extractResult.Error.Exception ?? new Exception(extractResult.Error.Message); + } // 3) Locate the extracted sub-folder var sourceFolder = _fileSystem.Path.Combine(tempExtractionPath, FolderName); @@ -110,7 +117,7 @@ private async Task DownloadAndExtractRetroRewind(ProgressWindow if (removeResult.IsFailure) { result = removeResult; - throw new Exception(removeResult.Error.Message); + throw removeResult.Error.Exception ?? new Exception(removeResult.Error.Message); } // 5) Move over RetroRewind @@ -134,6 +141,7 @@ private async Task DownloadAndExtractRetroRewind(ProgressWindow catch (Exception e) { exception = e; + _logger.LogError(exception, exception.Message); } finally { From 572742138d026425c5d1ef9c14969cefab0bd3b9 Mon Sep 17 00:00:00 2001 From: WantToBeeMe <93130991+WantToBeeMe@users.noreply.github.com> Date: Tue, 1 Jul 2025 16:44:34 +0200 Subject: [PATCH 2/2] did a little changing --- .../CustomDistributionSingletonService.cs | 11 ++++-- .../CustomDistributions/RetroRewind.cs | 37 ++++++++----------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/WheelWizard/Features/CustomDistributions/CustomDistributionSingletonService.cs b/WheelWizard/Features/CustomDistributions/CustomDistributionSingletonService.cs index 6232ff7f..2bfe181b 100644 --- a/WheelWizard/Features/CustomDistributions/CustomDistributionSingletonService.cs +++ b/WheelWizard/Features/CustomDistributions/CustomDistributionSingletonService.cs @@ -1,4 +1,5 @@ using System.IO.Abstractions; +using Microsoft.Extensions.Logging; using WheelWizard.CustomDistributions.Domain; using WheelWizard.Shared.Services; @@ -7,18 +8,20 @@ namespace WheelWizard.CustomDistributions; public interface ICustomDistributionSingletonService { List GetAllDistributions(); + + // FIXME: Abstract this reference away. A generic Distributions service kinda loses its purpose when you still have to reference a distribution by name (like done here) + // Instead you would want something like DistService.GetCurrentDistro() + // The rest of the application should not have to know what distribution is currently active. RetroRewind RetroRewind { get; } } public class CustomDistributionSingletonService : ICustomDistributionSingletonService { - public IFileSystem FileSystem { get; } public RetroRewind RetroRewind { get; } - public CustomDistributionSingletonService(IFileSystem fileSystem, IApiCaller api) + public CustomDistributionSingletonService(IFileSystem fileSystem, IApiCaller api, ILogger logger) { - FileSystem = fileSystem; - RetroRewind = new RetroRewind(fileSystem, api); + RetroRewind = new RetroRewind(fileSystem, api, logger); } public List GetAllDistributions() diff --git a/WheelWizard/Features/CustomDistributions/RetroRewind.cs b/WheelWizard/Features/CustomDistributions/RetroRewind.cs index 5bf5cce2..4c200fd4 100644 --- a/WheelWizard/Features/CustomDistributions/RetroRewind.cs +++ b/WheelWizard/Features/CustomDistributions/RetroRewind.cs @@ -19,9 +19,9 @@ public class RetroRewind : IDistribution { private readonly IFileSystem _fileSystem; private readonly IApiCaller _api; - private readonly ILogger _logger; - - public RetroRewind(IFileSystem fileSystem, IApiCaller api, ILogger logger) + private readonly ILogger _logger; + + public RetroRewind(IFileSystem fileSystem, IApiCaller api, ILogger logger) { _api = api; _fileSystem = fileSystem; @@ -55,15 +55,15 @@ public async Task InstallAsync(ProgressWindow progressWindow) var serverResponse = await _api.CallApiAsync(api => api.Ping()); // actual response doesnt matter if (serverResponse.IsFailure) return Fail("Could not connect to the server"); - + var downloadResult = await DownloadAndExtractRetroRewind(progressWindow); if (downloadResult.IsFailure) return downloadResult; - + var updateResult = await UpdateAsync(progressWindow); if (updateResult.IsFailure) return updateResult; - + return Ok(); } @@ -77,8 +77,7 @@ private async Task DownloadAndExtractRetroRewind(ProgressWindow //where all distributions are stored var destinationParentDir = _fileSystem.DirectoryInfo.New(PathManager.RiivolutionWhWzFolderPath); - - Exception? exception = null; + OperationResult? result = null; try { @@ -104,13 +103,7 @@ private async Task DownloadAndExtractRetroRewind(ProgressWindow // 3) Locate the extracted sub-folder var sourceFolder = _fileSystem.Path.Combine(tempExtractionPath, FolderName); if (!_fileSystem.Directory.Exists(sourceFolder)) - { - var directories = _fileSystem.Directory.GetDirectories(tempExtractionPath); - if (directories.Length == 1) - sourceFolder = directories[0]; - else - return new DirectoryNotFoundException($"Could not find a '{FolderName}' folder inside {tempExtractionPath}"); - } + throw new DirectoryNotFoundException($"Could not find a '{FolderName}' folder inside {tempExtractionPath}"); // 4) Remove existing install, if any var removeResult = await RemoveAsync(progressWindow); @@ -123,11 +116,13 @@ private async Task DownloadAndExtractRetroRewind(ProgressWindow // 5) Move over RetroRewind var xmlFolderSource = _fileSystem.Path.Combine(tempExtractionPath, XMLFolderName); var riivolutionFiles = _fileSystem.Directory.EnumerateFiles(xmlFolderSource, "*", SearchOption.AllDirectories); - var folderSource = _fileSystem.Path.Combine(tempExtractionPath, FolderName); - var retroRewindFiles = _fileSystem.Directory.EnumerateFiles(folderSource, "*", SearchOption.AllDirectories); + var retroRewindFiles = _fileSystem.Directory.EnumerateFiles(sourceFolder, "*", SearchOption.AllDirectories); foreach (var file in riivolutionFiles.Concat(retroRewindFiles)) { - var destinationPath = _fileSystem.Path.Combine(destinationParentDir.FullName, _fileSystem.Path.GetRelativePath(tempExtractionPath, file)); + var destinationPath = _fileSystem.Path.Combine( + destinationParentDir.FullName, + _fileSystem.Path.GetRelativePath(tempExtractionPath, file) + ); var destinationDirectoryName = _fileSystem.Path.GetDirectoryName(destinationPath); if (destinationDirectoryName != null) { @@ -140,8 +135,8 @@ private async Task DownloadAndExtractRetroRewind(ProgressWindow } catch (Exception e) { - exception = e; - _logger.LogError(exception, exception.Message); + result ??= Fail(e); + _logger.LogError(e, e.Message); } finally { @@ -151,7 +146,7 @@ private async Task DownloadAndExtractRetroRewind(ProgressWindow if (_fileSystem.Directory.Exists(tempExtractionPath)) _fileSystem.Directory.Delete(tempExtractionPath, recursive: true); } - return result ?? (exception is not null ? Fail(exception) : Ok()); + return result ?? Ok(); } private async Task BackupOldrksys()