Skip to content

Commit

Permalink
feat(common): use regex pattern for LatestGitHubReleaseAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
matkoch committed Jun 13, 2024
1 parent 5d4b16b commit 6d02194
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build/Build.ReleaseImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

partial class Build
{
[LatestGitHubRelease("JetBrains/JetBrainsMono", TrimPrefix = true)]
[LatestGitHubRelease("JetBrains/JetBrainsMono")]
readonly string JetBrainsMonoVersion;

string[] FontDownloadUrls =>
Expand Down
24 changes: 22 additions & 2 deletions source/Nuke.Common/Attributes/LatestGitHubReleaseAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
using System;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using NuGet.Versioning;
using Nuke.Common.Git;
using Nuke.Common.Tools.GitHub;
using Nuke.Common.Utilities;
using Nuke.Common.ValueInjection;

namespace Nuke.Common.Tooling;
Expand All @@ -23,11 +26,28 @@ public LatestGitHubReleaseAttribute(string identifier)
}

public bool IncludePrerelease { get; set; }
public bool TrimPrefix { get; set; }
public bool UseTagName { get; set; }

[RegexPattern]
public string Pattern { get; set; } = @"v?(?<version>\d+\.\d+(?:\.\d+)?(?:\.\d+)?(?:-\w+)?)";

public override object GetValue(MemberInfo member, object instance)
{
var repository = GitRepository.FromUrl($"https://github.com/{_identifier}");
return repository.GetLatestRelease(IncludePrerelease, TrimPrefix).GetAwaiter().GetResult();
var releases = GitHubTasks.GitHubClient.Repository.Release
.GetAll(repository.GetGitHubOwner(), repository.GetGitHubName()).GetAwaiter().GetResult();
var versions = releases
.Select(x => Regex.Match(!UseTagName ? x.Name : x.TagName, Pattern))
.Select(x => x.Groups["version"].Value)
.Select(NuGetVersion.Parse)
.OrderByDescending(x => x);

if (member.GetMemberType() == typeof(NuGetVersion[]))
return versions.ToArray();

var latestVersion = versions.FirstOrDefault(x => !x.IsPrerelease || IncludePrerelease);
return member.GetMemberType() == typeof(NuGetVersion)
? latestVersion
: latestVersion?.ToString();
}
}

0 comments on commit 6d02194

Please sign in to comment.