Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO.Abstractions;
using Microsoft.Extensions.Logging;
using WheelWizard.CustomDistributions.Domain;
using WheelWizard.Shared.Services;

Expand All @@ -7,18 +8,20 @@ namespace WheelWizard.CustomDistributions;
public interface ICustomDistributionSingletonService
{
List<IDistribution> 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<IRetroRewindApi> api)
public CustomDistributionSingletonService(IFileSystem fileSystem, IApiCaller<IRetroRewindApi> api, ILogger<IDistribution> logger)
{
FileSystem = fileSystem;
RetroRewind = new RetroRewind(fileSystem, api);
RetroRewind = new RetroRewind(fileSystem, api, logger);
}

public List<IDistribution> GetAllDistributions()
Expand Down
41 changes: 22 additions & 19 deletions WheelWizard/Features/CustomDistributions/RetroRewind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,11 +19,13 @@ public class RetroRewind : IDistribution
{
private readonly IFileSystem _fileSystem;
private readonly IApiCaller<IRetroRewindApi> _api;
private readonly ILogger<IDistribution> _logger;

public RetroRewind(IFileSystem fileSystem, IApiCaller<IRetroRewindApi> api)
public RetroRewind(IFileSystem fileSystem, IApiCaller<IRetroRewindApi> api, ILogger<IDistribution> logger)
{
_api = api;
_fileSystem = fileSystem;
_logger = logger;
}

public string Title => "Retro Rewind";
Expand Down Expand Up @@ -51,15 +54,16 @@ public async Task<OperationResult> 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();
}

Expand All @@ -73,8 +77,7 @@ private async Task<OperationResult> DownloadAndExtractRetroRewind(ProgressWindow

//where all distributions are stored
var destinationParentDir = _fileSystem.DirectoryInfo.New(PathManager.RiivolutionWhWzFolderPath);

Exception? exception = null;

OperationResult? result = null;
try
{
Expand All @@ -92,35 +95,34 @@ private async Task<OperationResult> 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)
{
Expand All @@ -133,7 +135,8 @@ private async Task<OperationResult> DownloadAndExtractRetroRewind(ProgressWindow
}
catch (Exception e)
{
exception = e;
result ??= Fail(e);
_logger.LogError(e, e.Message);
}
finally
{
Expand All @@ -143,7 +146,7 @@ private async Task<OperationResult> 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()
Expand Down
Loading