Updater
This commit is contained in:
38
Mlaumcherb.Client.Avalonia/сеть/Update/Gitea.cs
Normal file
38
Mlaumcherb.Client.Avalonia/сеть/Update/Gitea.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace Mlaumcherb.Client.Avalonia.сеть.Update;
|
||||
|
||||
public class Release
|
||||
{
|
||||
[JsonRequired] public int id { get; set; }
|
||||
[JsonRequired] public string tag_name { get; set; } = "";
|
||||
[JsonRequired] public DateTime created_at { get; set; }
|
||||
// ReSharper disable once CollectionNeverUpdated.Global
|
||||
public List<Asset> assets { get; set; } = new();
|
||||
|
||||
public class Asset
|
||||
{
|
||||
[JsonRequired] public int id { get; set; }
|
||||
[JsonRequired] public string name { get; set; } = "";
|
||||
[JsonRequired] public int size { get; set; }
|
||||
[JsonRequired] public DateTime created_at { get; set; }
|
||||
[JsonRequired] public string browser_download_url { get; set; } = "";
|
||||
|
||||
public async Task Download(IOPath localPath)
|
||||
{
|
||||
await NetworkHelper.DownloadFile(browser_download_url, localPath);
|
||||
}
|
||||
}
|
||||
|
||||
public Asset? FindAssetByName(string name) =>
|
||||
assets.FirstOrDefault(a => a.name == name);
|
||||
}
|
||||
|
||||
public class GiteaClient(string ServerUrl)
|
||||
{
|
||||
public async Task<Release> GetLatestRelease(string user, string repo)
|
||||
{
|
||||
string url = $"{ServerUrl}//api/v1/repos/{user}/{repo}/releases/latest";
|
||||
return await NetworkHelper.DownloadStringAndDeserialize<Release>(url);
|
||||
}
|
||||
}
|
||||
74
Mlaumcherb.Client.Avalonia/сеть/Update/UpdateHelper.cs
Normal file
74
Mlaumcherb.Client.Avalonia/сеть/Update/UpdateHelper.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Mlaumcherb.Client.Avalonia.сеть.Update;
|
||||
|
||||
public class UpdateHelper
|
||||
{
|
||||
private readonly string _executableName = "млаумчерб.exe";
|
||||
private readonly IOPath executablePathActual;
|
||||
private readonly IOPath executablePathOld;
|
||||
private readonly IOPath executablePathNew;
|
||||
|
||||
|
||||
public class GiteaConfig
|
||||
{
|
||||
[JsonRequired] public required string serverUrl { get; set; }
|
||||
[JsonRequired] public required string user { get; set; }
|
||||
[JsonRequired] public required string repo { get; set; }
|
||||
}
|
||||
|
||||
private GiteaConfig _giteaConfig;
|
||||
|
||||
public UpdateHelper(GiteaConfig giteaConfig)
|
||||
{
|
||||
_giteaConfig = giteaConfig;
|
||||
executablePathActual = _executableName;
|
||||
executablePathOld = _executableName + ".old";
|
||||
executablePathNew = _executableName + ".new";
|
||||
Gitea = new GiteaClient(giteaConfig.serverUrl);
|
||||
}
|
||||
|
||||
public GiteaClient Gitea { get; }
|
||||
|
||||
public void DeleteBackupFiles()
|
||||
{
|
||||
if(File.Exists(executablePathOld))
|
||||
File.Delete(executablePathOld);
|
||||
}
|
||||
|
||||
public async Task<bool> IsUpdateAvailable()
|
||||
{
|
||||
var latest = await Gitea.GetLatestRelease(_giteaConfig.user, _giteaConfig.repo);
|
||||
if (!Version.TryParse(latest.tag_name, out var latestVersion))
|
||||
throw new Exception($"Can't parse version of latest release: {latest.tag_name}");
|
||||
|
||||
Version? currentVersion = Assembly.GetExecutingAssembly().GetName().Version;
|
||||
if(currentVersion is null)
|
||||
throw new Exception($"Can't get current version from {Assembly.GetExecutingAssembly().GetName()}");
|
||||
|
||||
return currentVersion < latestVersion;
|
||||
}
|
||||
|
||||
public async Task UpdateSelf()
|
||||
{
|
||||
if(File.Exists(executablePathNew))
|
||||
File.Delete(executablePathNew);
|
||||
|
||||
var latest = await Gitea.GetLatestRelease(_giteaConfig.user, _giteaConfig.repo);
|
||||
var asset = latest.FindAssetByName(_executableName);
|
||||
if(asset == null)
|
||||
throw new Exception($"Can't find updated executable on gitea: {_executableName}");
|
||||
await asset.Download(executablePathNew);
|
||||
|
||||
File.Move(executablePathActual, executablePathOld, true);
|
||||
File.Move(executablePathNew, executablePathActual, true);
|
||||
}
|
||||
|
||||
public void RestartSelf()
|
||||
{
|
||||
Process.Start(executablePathActual.ToString());
|
||||
Thread.Sleep(500);
|
||||
Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user