Skip to content

Commit

Permalink
Switch to getting the product version
Browse files Browse the repository at this point in the history
  • Loading branch information
dalyIsaac committed Aug 13, 2024
1 parent 5aacacd commit 4539d28
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/Whim.Updater.Tests/VersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class VersionTests
public void Parse(string tagName, int major, int minor, int patch, ReleaseChannel releaseChannel, string commit)
{
// Given
Version? version = Version.Parse(tagName);
Version? version = Version.ParseSemver(tagName);

// Then
Assert.NotNull(version);
Expand All @@ -31,7 +31,7 @@ public void Parse(string tagName, int major, int minor, int patch, ReleaseChanne
public void Parse_Invalid(string tagName)
{
// Given
Version? version = Version.Parse(tagName);
Version? version = Version.ParseSemver(tagName);

// Then
Assert.Null(version);
Expand All @@ -47,8 +47,8 @@ public void Parse_Invalid(string tagName)
public void IsNewerVersion(string tagName, string otherTagName, bool expected)
{
// Arrange
Version version = Version.Parse(tagName)!;
Version otherVersion = Version.Parse(otherTagName)!;
Version version = Version.ParseSemver(tagName)!;
Version otherVersion = Version.ParseSemver(otherTagName)!;

// Given
bool isNewer = version.IsNewerVersion(otherVersion);
Expand Down
4 changes: 2 additions & 2 deletions src/Whim.Updater/ReleaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ReleaseManager(IContext context, UpdaterPlugin plugin)
{
_ctx = context;
_plugin = plugin;
CurrentVersion = Version.Parse(_ctx.NativeManager.GetWhimVersion())!;
CurrentVersion = Version.ParseProductVersion(_ctx.NativeManager.GetWhimVersion())!;
}

/// <summary>
Expand Down Expand Up @@ -136,7 +136,7 @@ public async Task<List<ReleaseInfo>> GetNotInstalledReleases()
List<ReleaseInfo> sortedReleases = [];
foreach (Release r in releases)
{
Version? version = Version.Parse(r.TagName);
Version? version = Version.ParseSemver(r.TagName);
if (version == null)
{
Logger.Debug($"Invalid release tag: {r.TagName}");
Expand Down
6 changes: 6 additions & 0 deletions src/Whim.Updater/UpdaterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public enum ReleaseChannel
/// Stable releases are specific releases in a release branch <c>release/v*</c>.
/// </summary>
Stable = 2,

/// <summary>
/// Unknown release channel. This is typically used when we parse the version from
/// <see cref="INativeManager.GetWhimVersion"/>.
/// </summary>
Unknown = 999,
}

/// <summary>
Expand Down
40 changes: 35 additions & 5 deletions src/Whim.Updater/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,42 @@ private Version(int major, int minor, int patch, ReleaseChannel releaseChannel,
/// <returns>
/// A new <see cref="Version"/> if the tag name is valid, otherwise null.
/// </returns>
public static Version? Parse(string tagName)
public static Version? ParseSemver(string tagName)
{
Match match = ReleaseTagRegex().Match(tagName);
Match match = SemverRegex().Match(tagName);
if (!match.Success || match.Groups.Count != 6)
{
return null;
}

return GetVersionFromMatch(match);
}

[GeneratedRegex(@"^v(\d+).(\d+).(\d+)-(alpha|beta|stable)\+([a-z0-9]{8})$")]
private static partial Regex SemverRegex();

/// <summary>
/// Parses a version string in the form of "v{major}.{minor}.{patch}.{build}".
/// This is the format returned by <see cref="INativeManager.GetWhimVersion"/>
/// </summary>
/// <param name="version"></param>
/// <returns></returns>
public static Version? ParseProductVersion(string version)
{
Match match = ProductVersionRegex().Match(version);
if (!match.Success || match.Groups.Count != 2)
{
return null;
}

return GetVersionFromMatch(match);
}

[GeneratedRegex(@"^v(\d+).(\d+).(\d+)-(alpha|beta|stable)\+([a-z\d]{8}).[a-z\d]*$")]
private static partial Regex ProductVersionRegex();

private static Version GetVersionFromMatch(Match match)
{
int major = int.Parse(match.Groups[1].Value);
int minor = int.Parse(match.Groups[2].Value);
int patch = int.Parse(match.Groups[3].Value);
Expand All @@ -71,9 +99,6 @@ private Version(int major, int minor, int patch, ReleaseChannel releaseChannel,
return new Version(major, minor, patch, releaseChannel, commit);
}

[GeneratedRegex(@"^v(\d+).(\d+).(\d+)-(alpha|beta|stable)\+([a-z0-9]{8})$")]
private static partial Regex ReleaseTagRegex();

/// <summary>
/// Returns true if this is a newer release than the <paramref name="other"/> release.
/// </summary>
Expand Down Expand Up @@ -108,6 +133,11 @@ public bool IsNewerVersion(Version other)
return false;
}

if (ReleaseChannel == ReleaseChannel.Unknown || other.ReleaseChannel == ReleaseChannel.Unknown)
{
return false;
}

// The releases are the same version, so compare the channels
return ReleaseChannel > other.ReleaseChannel;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Whim/Native/NativeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,11 @@ public string GetWhimVersion()
{
#if DEBUG
// An arbitrary version number for debugging.
return "v0.1.263-alpha+bc5c56c4";
return "0.1.263.0";
#else
return Assembly.GetExecutingAssembly().GetName().Version!.ToString();
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
return fileVersionInfo.ProductVersion!;
#endif
}
}

0 comments on commit 4539d28

Please sign in to comment.