67 lines
1.9 KiB
C#
Raw Normal View History

2023-05-14 22:35:06 -04:00
using Serilog;
using SharpCompress;
2023-08-25 19:09:36 -04:00
using SPTInstaller.CustomControls;
2023-05-11 23:11:39 -04:00
using SPTInstaller.Interfaces;
using SPTInstaller.Models;
using System.Collections.Generic;
using System.Linq;
2023-05-11 23:11:39 -04:00
using System.Threading.Tasks;
namespace SPTInstaller.Controllers;
2023-05-11 23:11:39 -04:00
public class InstallController
{
public event EventHandler<IProgressableTask> TaskChanged = delegate { };
2023-05-11 23:11:39 -04:00
private IPreCheck[] _preChecks { get; set; }
private IProgressableTask[] _tasks { get; set; }
2023-05-11 23:11:39 -04:00
public InstallController(IProgressableTask[] tasks, IPreCheck[] preChecks = null)
{
_tasks = tasks;
_preChecks = preChecks;
}
2023-05-11 23:11:39 -04:00
public async Task<IResult> RunPreChecks()
{
Log.Information("-<>--<>- Running PreChecks -<>--<>-");
var requiredResults = new List<IResult>();
2023-05-11 23:11:39 -04:00
2023-08-25 19:09:36 -04:00
_preChecks.ForEach(x => x.State = StatusSpinner.SpinnerState.Pending);
2023-05-14 22:35:06 -04:00
foreach (var check in _preChecks)
{
var result = await check.RunCheck();
2023-05-11 23:11:39 -04:00
Log.Information($"PreCheck: {check.Name} ({(check.IsRequired ? "Required" : "Optional")}) -> {(result.Succeeded ? "Passed" : "Failed")}\nDetail: {check.PreCheckDetails.ReplaceLineEndings(" ")}");
if (check.IsRequired)
2023-05-11 23:11:39 -04:00
{
requiredResults.Add(result);
2023-05-11 23:11:39 -04:00
}
}
if (requiredResults.Any(result => !result.Succeeded))
2023-05-11 23:11:39 -04:00
{
return Result.FromError("Some required checks have failed");
}
2023-05-14 22:35:06 -04:00
return Result.FromSuccess();
}
2023-05-11 23:11:39 -04:00
public async Task<IResult> RunTasks()
{
Log.Information("-<>--<>- Running Installer Tasks -<>--<>-");
2023-05-11 23:11:39 -04:00
foreach (var task in _tasks)
{
TaskChanged?.Invoke(null, task);
2023-05-11 23:11:39 -04:00
var result = await task.RunAsync();
if (!result.Succeeded) return result;
2023-05-11 23:11:39 -04:00
}
return Result.FromSuccess("Install Complete. Happy Playing!");
2023-05-11 23:11:39 -04:00
}
}