Free, open-source Windows optimization tool for performance, privacy, and simplicity.
C# csharpdebloatdotnetgamingopen-sourceoptimizationoptimizeroptimizerduckperformanceprivacytweakertweakswindowswinui
fix(updater): return the newer version or null instead of a tuple
fatDuck114d1468e5e7
diff --git a/optimizerDuck.Test/Services/UpdaterServiceTests.cs b/optimizerDuck.Test/Services/UpdaterServiceTests.csnew file mode 100644index 0000000..0c72009--- /dev/null+++ b/optimizerDuck.Test/Services/UpdaterServiceTests.cs@@ -0,0 +1,74 @@+using optimizerDuck.Common.Helpers;+using optimizerDuck.Services.System;++namespace optimizerDuck.Test.Services;++/// <summary>+/// Pins how the update check identifies itself and when a release counts as an update.+/// </summary>+public class UpdaterServiceTests+{+ [Fact]+ public void BuildUserAgentHeader_NamesTheRunningBuild()+ {+ var header = UpdaterService.BuildUserAgentHeader();++ Assert.Equal("optimizerDuck", header.Product?.Name);+ Assert.Equal(Shared.FileVersion, header.Product?.Version);+ }++ [Theory]+ [InlineData("v1.2.3", "1.2.3")]+ [InlineData("1.2.3", "1.2.3")]+ [InlineData("1.2.3-fix", "1.2.3")]+ [InlineData("v2.0", "2.0")]+ public void ParseReleaseTag_ReadsTheVersionItNames(string tag, string expected)+ {+ Assert.Equal(Version.Parse(expected), UpdaterService.ParseReleaseTag(tag));+ }++ [Theory]+ [InlineData(null)]+ [InlineData("")]+ [InlineData(" ")]+ [InlineData("latest")]+ [InlineData("v")]+ public void ParseReleaseTag_WithoutAVersion_IsNothing(string? tag)+ {+ Assert.Null(UpdaterService.ParseReleaseTag(tag));+ }++ [Fact]+ public void FindExecutableAsset_WithoutTheExecutable_IsNothing()+ {+ Assert.Null(UpdaterService.FindExecutableAsset(null));+ Assert.Null(UpdaterService.FindExecutableAsset(Release("optimizerDuck.zip")));+ Assert.Null(UpdaterService.FindExecutableAsset(Release("notes.txt")));+ }++ [Fact]+ public void FindExecutableAsset_WithTheExecutable_FindsIt()+ {+ var asset = UpdaterService.FindExecutableAsset(Release("optimizerDuck-v1.2.3-win.exe"));++ Assert.NotNull(asset);+ Assert.Equal("optimizerDuck-v1.2.3-win.exe", asset!.Name);+ }++ private static GitHubRelease Release(params string[] assetNames)+ {+ return new GitHubRelease+ {+ TagName = "v1.2.3",+ Body = string.Empty,+ Assets =+ [+ .. assetNames.Select(name => new GitHubAsset+ {+ Name = name,+ BrowserDownloadUrl = $"https://example.invalid/{name}",+ }),+ ],+ };+ }+}diff --git a/optimizerDuck/Services/System/UpdaterService.cs b/optimizerDuck/Services/System/UpdaterService.csindex 649298f..3303e1f 100644--- a/optimizerDuck/Services/System/UpdaterService.cs+++ b/optimizerDuck/Services/System/UpdaterService.cs@@ -22,18 +22,55 @@ public class UpdaterService : IDisposablepublic UpdaterService(ILogger<UpdaterService> logger){_httpClient = HttpClientFactory.CreateClient(logger: logger);- _httpClient.DefaultRequestHeaders.UserAgent.Add(- new ProductInfoHeaderValue("optimizerDuck", "1.0")- );+ _httpClient.DefaultRequestHeaders.UserAgent.Add(BuildUserAgentHeader());_logger = logger;}+ /// <summary>+ /// Builds the user agent header that identifies this build to the release host.+ /// </summary>+ /// <returns>The user agent header.</returns>+ internal static ProductInfoHeaderValue BuildUserAgentHeader()+ {+ return new ProductInfoHeaderValue("optimizerDuck", Shared.FileVersion);+ }++ /// <summary>Parses the version a release tag names.</summary>+ /// <param name="tagName">The release tag, for example "v1.2.3".</param>+ /// <returns>The version the tag names, or <see langword="null" /> when it names none.</returns>+ internal static Version? ParseReleaseTag(string? tagName)+ {+ if (string.IsNullOrWhiteSpace(tagName))+ return null;++ // "v1.1.0" becomes "1.1.0", then "1.1.0-fix" becomes "1.1.0".+ var text = tagName.TrimStart('v');+ var preReleaseSeparatorIndex = text.IndexOf('-');+ if (preReleaseSeparatorIndex != -1)+ text = text[..preReleaseSeparatorIndex];++ return Version.TryParse(text, out var version) ? version : null;+ }++ /// <summary>Finds the installable executable in a release.</summary>+ /// <param name="release">The release as the host reported it.</param>+ /// <returns>+ /// The executable asset, or <see langword="null" /> when the release carries none.+ /// </returns>+ internal static GitHubAsset? FindExecutableAsset(GitHubRelease? release)+ {+ return release?.Assets.FirstOrDefault(asset =>+ asset.Name.StartsWith("optimizerDuck", StringComparison.OrdinalIgnoreCase)+ && asset.Name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)+ );+ }+/// <summary>Checks the GitHub API for a newer version of the application.</summary>/// <returns>- /// A tuple where <c>Result</c> is <see langword="true" /> if a newer version exists, and- /// <c>Version</c> is the latest version string.+ /// The newer version string, or <see langword="null" /> when the running version is current or+ /// the check could not determine a newer one./// </returns>- public async Task<(bool Result, string? Version)> CheckForUpdatesAsync()+ public async Task<string?> CheckForUpdatesAsync(){_logger.LogInformation("Checking for updates (Current version: {CurrentVersion})",@@ -50,62 +87,50 @@ public class UpdaterService : IDisposableif (latestRelease == null || string.IsNullOrEmpty(latestRelease.TagName)){_logger.LogWarning("Could not retrieve latest release information");- return (false, null);+ return null;}- // "v1.1.0" becomes "1.1.0"- var latestVersionStr = latestRelease.TagName.TrimStart('v');-- // "1.1.0-fix" becomes "1.1.0"- var preReleaseSeparatorIndex = latestVersionStr.IndexOf('-');- if (preReleaseSeparatorIndex != -1)- latestVersionStr = latestVersionStr[..preReleaseSeparatorIndex];-_logger.LogDebug("Latest release version: {LatestReleaseTagName}",latestRelease.TagName);- // Parse version- if (!Version.TryParse(latestVersionStr, out var latestVersion))+ var latestVersion = ParseReleaseTag(latestRelease.TagName);+ if (latestVersion is null){_logger.LogWarning("Could not parse latest release version: {LatestReleaseTagName}",latestRelease.TagName);- return (false, null);+ return null;}var currentVersion = Version.Parse(Shared.FileVersion);if (latestVersion > currentVersion){- var updateExecutableAsset = latestRelease.Assets.FirstOrDefault(a =>- a.Name.StartsWith("optimizerDuck", StringComparison.OrdinalIgnoreCase)- && a.Name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)- );-- if (updateExecutableAsset == null)+ if (FindExecutableAsset(latestRelease) == null){_logger.LogWarning("No update executable (.exe) found in the latest release");- return (false, null);+ return null;}+ var latestVersionStr = latestVersion.ToString();_logger.LogInformation("A new version ({LatestVersion}) is available!",latestVersionStr);- return (true, latestVersionStr);+ return latestVersionStr;}_logger.LogInformation("You are running the latest version");- return (false, null);+ return null;}catch (Exception ex){_logger.LogError(ex, "Error checking for updates");- return (false, null);+ return null;}}diff --git a/optimizerDuck/UI/ViewModels/Pages/DashboardViewModel.cs b/optimizerDuck/UI/ViewModels/Pages/DashboardViewModel.csindex 5445800..3e6a09e 100644--- a/optimizerDuck/UI/ViewModels/Pages/DashboardViewModel.cs+++ b/optimizerDuck/UI/ViewModels/Pages/DashboardViewModel.cs@@ -104,8 +104,8 @@ public partial class DashboardViewModel : ViewModel{await LoadSystemInfoAsync();_systemInfoService.LogSummary();- var (result, version) = await _updaterService.CheckForUpdatesAsync();- if (result)+ var version = await _updaterService.CheckForUpdatesAsync();+ if (version is not null){_updateNotified = true;IsUpdateInfoOpen = true;