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 c35a4192..4c200fd4 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; + private readonly ILogger _logger; - public RetroRewind(IFileSystem fileSystem, IApiCaller api) + 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(); } @@ -73,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 { @@ -92,35 +95,34 @@ 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); 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); if (removeResult.IsFailure) { result = removeResult; - throw new Exception(removeResult.Error.Message); + throw removeResult.Error.Exception ?? new Exception(removeResult.Error.Message); } // 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) { @@ -133,7 +135,8 @@ private async Task DownloadAndExtractRetroRewind(ProgressWindow } catch (Exception e) { - exception = e; + result ??= Fail(e); + _logger.LogError(e, e.Message); } finally { @@ -143,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()