update projects (versions, nuget, references)

This commit is contained in:
2023-09-14 11:19:07 +02:00
parent 232f991db0
commit ddc04442c4
15 changed files with 104 additions and 164 deletions

View File

@@ -5,8 +5,10 @@ using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.IO.Pipes;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.VisualBasic.CompilerServices;
@@ -21,7 +23,7 @@ namespace Pilz.Updating
public event UpdateStatusChangedEventHandler UpdateStatusChanged;
public delegate void UpdateStatusChangedEventHandler(UpdateStatus newStatus, int progress);
public delegate void UpdateStatusChangedEventHandler(UpdateStatus newStatus);
public event DownloadingUpdateEventHandler DownloadingUpdate;
@@ -46,7 +48,7 @@ namespace Pilz.Updating
// P r o p e r t i e s
public WebClient WebClient { get; private set; } = new WebClient();
public HttpClient WebClient { get; private set; } = new();
public string UpdateUrl { get; private set; }
public ApplicationVersion CurrentVersion { get; private set; }
public Channels MinimumChannel { get; private set; }
@@ -69,7 +71,6 @@ namespace Pilz.Updating
UpdateUrl = updateUrl;
CurrentVersion = currentVersion;
MinimumChannel = (Channels)Math.Max((int)minimumChannel, (int)currentVersion.Channel);
WebClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
}
// E v e n t M e t h o d s
@@ -88,70 +89,48 @@ namespace Pilz.Updating
return e.Cancel;
}
// W e b C l i e n t E v e n t s
private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
RaiseUpdateStatusChanged(curDownloadingStatus, e.ProgressPercentage);
}
// U p d a t e R o u t i n e s
public Task UpdateInteractiveAsync()
public async Task UpdateInteractive()
{
return Task.Run(UpdateInteractive);
}
public void UpdateInteractive()
{
var latestVersion = CheckForUpdate();
var latestVersion = await CheckForUpdate();
if (latestVersion is null)
{
NoUpdatesFound?.Invoke();
}
else
{
UpdateInteractive(latestVersion);
}
await UpdateInteractive(latestVersion);
}
public void UpdateInteractive(UpdatePackageInfo package)
public async Task UpdateInteractive(UpdatePackageInfo package)
{
if (!RaiseDownloadingUpdate(package) && DownloadPackage(package))
if (!RaiseDownloadingUpdate(package) && await DownloadPackageAsync(package))
{
if (!RaiseInstallingUpdate(package))
{
InstallPackage(package);
}
await InstallPackage(package);
}
}
public void RaiseUpdateStatusChanged(UpdateStatus newStatus, int progress = -1)
public void RaiseUpdateStatusChanged(UpdateStatus newStatus)
{
UpdateStatusChanged?.Invoke(newStatus, progress);
UpdateStatusChanged?.Invoke(newStatus);
}
// F e a t u r e s
public UpdateInfo GetUpdateInfo()
public async Task<UpdateInfo> GetUpdateInfo()
{
string str = WebClient.DownloadString(UpdateUrl);
string str = await WebClient.GetStringAsync(UpdateUrl);
var info = UpdateInfo.Parse(str);
return info;
}
public UpdatePackageInfo CheckForUpdate()
public async Task<UpdatePackageInfo> CheckForUpdate()
{
RaiseUpdateStatusChanged(UpdateStatus.Searching);
UpdateInfo = GetUpdateInfo();
if (UpdateInfo is object)
{
UpdateInfo = await GetUpdateInfo();
if (UpdateInfo is not null)
return CheckForUpdate(UpdateInfo);
}
else
{
return null;
}
}
public UpdatePackageInfo CheckForUpdate(UpdateInfo updateInfo)
@@ -172,7 +151,7 @@ namespace Pilz.Updating
return foundPkgInfo;
}
public bool DownloadPackage(UpdatePackageInfo package)
public async Task<bool> DownloadPackageAsync(UpdatePackageInfo package)
{
curDownloadingStatus = UpdateStatus.DownloadingPackage;
RaiseUpdateStatusChanged(curDownloadingStatus);
@@ -183,14 +162,14 @@ namespace Pilz.Updating
{
// Ensure existing and empty directory for the Zip File
if (dir.Exists)
{
dir.Delete(true);
}
dir.Create();
// Download zip package
WebClient.DownloadFile(package.Packagelink, zipPath);
using var zipFile = new FileStream(zipPath, FileMode.Create, FileAccess.ReadWrite);
using var zipStream = await WebClient.GetStreamAsync(package.Packagelink);
await zipStream.CopyToAsync(zipFile);
// Remember path to package directory
dicPackagePaths.Add(package, dirPath);
@@ -203,7 +182,7 @@ namespace Pilz.Updating
return true;
}
private FileInfo DownloadUpdateInstaller()
private async Task<FileInfo> DownloadUpdateInstaller()
{
curDownloadingStatus = UpdateStatus.DownloadingInstaller;
RaiseUpdateStatusChanged(curDownloadingStatus);
@@ -211,17 +190,19 @@ namespace Pilz.Updating
// Ensure update installer path is empty
var installerDirPath = new DirectoryInfo(Path.Combine(MyPaths.GetMyAppDataPath(), "UpdateInstallerTool"));
if (installerDirPath.Exists)
{
installerDirPath.Delete(true);
}
Task.Delay(100);
await Task.Delay(100);
installerDirPath.Create();
Task.Delay(100);
await Task.Delay(100);
// Download update installer zip
string installerZipPath = Path.Combine(installerDirPath.FullName, "UpdatenInstaller.zip");
WebClient.DownloadFile(UpdateInfo.UpdateInstallerLink, installerZipPath);
var installerZipPath = Path.Combine(installerDirPath.FullName, "UpdatenInstaller.zip");
using (var installerZipFile = new FileStream(installerZipPath, FileMode.Create, FileAccess.ReadWrite))
{
using var installerZipStream = await WebClient.GetStreamAsync(UpdateInfo.UpdateInstallerLink);
await installerZipStream.CopyToAsync(installerZipFile);
}
// Extract update installer
var installerExtractPath = installerDirPath.CreateSubdirectory("extracted");
@@ -262,26 +243,24 @@ namespace Pilz.Updating
UpdateInstallerStarted?.Invoke();
}
public bool InstallPackage(UpdatePackageInfo package)
public async Task<bool> InstallPackage(UpdatePackageInfo package)
{
string packagePath = null;
bool hasDownloaded = dicPackagePaths.TryGetValue(package, out packagePath);
if (hasDownloaded)
if (dicPackagePaths.TryGetValue(package, out var packagePath))
{
// Download update installer
var installerPath = DownloadUpdateInstaller();
var installerPath = await DownloadUpdateInstaller();
// Start update installer
StartUpdateInstaller(packagePath, installerPath.FullName);
// Close Host Application
if (AutoCloseHostApplication)
{
Environment.Exit(Environment.ExitCode);
}
return true;
}
return hasDownloaded;
return false;
}
}
}