SPT-AKI-Installer/SPTInstaller/Models/InstallerUpdateInfo.cs

183 lines
4.7 KiB
C#
Raw Normal View History

using Gitea.Api;
using Gitea.Client;
using ReactiveUI;
using Serilog;
2023-07-30 16:15:52 -04:00
using SPTInstaller.Helpers;
using System.Diagnostics;
using System.Threading.Tasks;
namespace SPTInstaller.Models;
public class InstallerUpdateInfo : ReactiveObject
{
2023-07-30 16:15:52 -04:00
private Version? _newVersion;
public string NewInstallerUrl = "";
private string _updateInfoText = "";
public string UpdateInfoText
{
get => _updateInfoText;
set => this.RaiseAndSetIfChanged(ref _updateInfoText, value);
}
2023-08-03 18:02:38 -04:00
private bool _showCard = false;
public bool ShowCard
{
2023-08-03 18:02:38 -04:00
get => _showCard;
set => this.RaiseAndSetIfChanged(ref _showCard, value);
}
2023-08-03 18:02:38 -04:00
private bool _updating = false;
2023-07-30 16:15:52 -04:00
public bool Updating
{
2023-07-30 16:15:52 -04:00
get => _updating;
set => this.RaiseAndSetIfChanged(ref _updating, value);
}
2023-08-03 18:02:38 -04:00
private bool _updateAvailable = false;
public bool UpdateAvailable
{
get => _updateAvailable;
set => this.RaiseAndSetIfChanged(ref _updateAvailable, value);
}
private bool _checkingForUpdates = false;
public bool CheckingForUpdates
{
get => _checkingForUpdates;
set => this.RaiseAndSetIfChanged(ref _checkingForUpdates, value);
}
2023-07-30 16:15:52 -04:00
private int _downloadProgress;
public int DownloadProgress
{
2023-07-30 16:15:52 -04:00
get => _downloadProgress;
set => this.RaiseAndSetIfChanged(ref _downloadProgress, value);
}
2023-07-30 16:15:52 -04:00
public async Task UpdateInstaller()
{
Updating = true;
2023-08-03 18:02:38 -04:00
UpdateAvailable = false;
2023-07-30 16:15:52 -04:00
var updater = new FileInfo(Path.Join(DownloadCacheHelper.CachePath, "update.ps1"));
FileHelper.StreamAssemblyResourceOut("update.ps1", updater.FullName);
if (!updater.Exists)
{
UpdateInfoText = "Failed to get updater from resources :(";
return;
}
var newInstallerPath = await DownloadNewInstaller();
if(string.IsNullOrWhiteSpace(newInstallerPath))
return;
Process.Start(new ProcessStartInfo
{
FileName = "powershell.exe",
ArgumentList = { "-ExecutionPolicy", "Bypass", "-File", $"{updater.FullName}", $"{newInstallerPath}", $"{Path.Join(Environment.CurrentDirectory, "SPTInstaller.exe")}" }
});
}
2023-07-30 16:15:52 -04:00
private async Task<string> DownloadNewInstaller()
{
2023-07-30 16:15:52 -04:00
UpdateInfoText = $"Downloading new installer v{_newVersion}";
var progress = new Progress<double>(x => DownloadProgress = (int)x);
var file = await DownloadCacheHelper.DownloadFileAsync("SPTInstller.exe", NewInstallerUrl, progress);
2023-07-30 16:15:52 -04:00
if (file == null || !file.Exists)
{
UpdateInfoText = "Failed to download new installer :(";
return "";
}
return file.FullName;
}
2023-11-12 09:52:02 -05:00
private void EndCheck(string infoText, bool updateAvailable, bool log = true)
2023-08-03 18:02:38 -04:00
{
2023-11-12 09:52:02 -05:00
if (log)
{
Log.Information(infoText);
}
2023-08-03 18:02:38 -04:00
UpdateInfoText = infoText;
if (!updateAvailable)
{
Task.Run(async () =>
{
// delay card dismiss
await Task.Delay(TimeSpan.FromSeconds(2));
ShowCard = updateAvailable;
});
}
else
{
ShowCard = updateAvailable;
}
CheckingForUpdates = false;
UpdateAvailable = updateAvailable;
}
2023-07-30 16:15:52 -04:00
public async Task CheckForUpdates(Version? currentVersion)
{
2023-07-30 16:15:52 -04:00
if (currentVersion == null)
return;
2023-08-03 18:02:38 -04:00
UpdateInfoText = "Checking for installer updates";
ShowCard = true;
CheckingForUpdates = true;
try
{
var repo = new RepositoryApi(Configuration.Default);
var releases = await repo.RepoListReleasesAsync("CWX", "SPT-AKI-Installer");
if (releases == null || releases.Count == 0)
2023-08-03 18:02:38 -04:00
{
2023-11-12 09:52:02 -05:00
EndCheck("No releases available", false);
2023-07-30 16:15:52 -04:00
return;
2023-08-03 18:02:38 -04:00
}
var latest = releases.FindAll(x => !x.Prerelease)[0];
if (latest == null)
2023-08-03 18:02:38 -04:00
{
2023-11-12 09:52:02 -05:00
EndCheck("could not get latest release", false);
2023-07-30 16:15:52 -04:00
return;
2023-08-03 18:02:38 -04:00
}
var latestVersion = new Version(latest.TagName);
2023-07-30 16:15:52 -04:00
if (latestVersion == null || latestVersion <= currentVersion)
2023-08-03 18:02:38 -04:00
{
EndCheck("No updates available", false);
2023-07-30 16:15:52 -04:00
return;
2023-08-03 18:02:38 -04:00
}
2023-07-30 16:15:52 -04:00
_newVersion = latestVersion;
NewInstallerUrl = latest.Assets[0].BrowserDownloadUrl;
2023-08-03 18:02:38 -04:00
EndCheck($"Update available, version {latestVersion}", true);
2023-07-30 16:15:52 -04:00
return;
}
catch (Exception ex)
{
2023-11-12 09:52:02 -05:00
EndCheck(ex.Message, false, false);
2023-08-03 18:02:38 -04:00
Log.Error(ex, "Failed to check for updates");
}
2023-07-30 16:15:52 -04:00
return;
}
}