From 7469252d507e1a9b471d2b9d781c2c883eb45db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Thu, 5 Sep 2024 23:58:50 +0100 Subject: [PATCH] NUKE fixes. --- Directory.Build.props | 4 +- build/_build/Build.cs | 104 +++++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 45 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 590a99a..caa13b5 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -30,9 +30,7 @@ 0.2.0 - -build.$(APPVEYOR_BUILD_NUMBER)+$(APPVEYOR_REPO_COMMIT.Substring(0, 7)) - -localbuild$([System.DateTime]::Now.ToString("yyyyMMddHHmmss")) - + -localbuild$([System.DateTime]::Now.ToString("yyyyMMddHHmmss")) diff --git a/build/_build/Build.cs b/build/_build/Build.cs index c5f5691..4e95b7a 100644 --- a/build/_build/Build.cs +++ b/build/_build/Build.cs @@ -97,8 +97,8 @@ TagName is not null IsLocalBuild ? "-local" : ( - TagName is not null - ? "" + TagVersion is not null + ? $"-{TagVersion.Release}" : $"-build2.{BuildNumber}+{GitHubActions.Sha[0..7]}" ); @@ -133,7 +133,7 @@ TagName is not null ) ); - private Target PushMyGet => _ => _ + private Target PushToMyGet => _ => _ .TriggeredBy(Pack) .OnlyWhenStatic( () => @@ -141,21 +141,9 @@ TagName is not null && Configuration == Configuration.Debug ) .Requires(() => MyGetApiKey) - .Executes( - () => - NupkgArtifactsPath - .GetFiles() - .ForEach( - nupkg => DotNetTasks.DotNetNuGetPush( - x => x - .SetSource(MyGetFeedUrl) - .SetApiKey(MyGetApiKey) - .SetTargetPath(nupkg) - ) - ) - ); + .Executes(() => PushToNuGetFeed(MyGetFeedUrl, MyGetApiKey!)); - private Target PushNuGet => _ => _ + private Target PushToNuGet => _ => _ .TriggeredBy(Pack) .OnlyWhenStatic( () => @@ -164,21 +152,11 @@ TagVersion is not null ) .Requires(() => NuGetApiKey) .Executes( - () => - NupkgArtifactsPath - .GetFiles() - .ForEach( - nupkg => DotNetTasks.DotNetNuGetPush( - x => x - .SetSource(NuGetConstants.V3FeedUrl) - .SetApiKey(NuGetApiKey) - .SetTargetPath(nupkg) - ) - ) + () => PushToNuGetFeed(NuGetConstants.V3FeedUrl, NuGetApiKey!) ); - private Target ReleaseGitHub => _ => _ - .TriggeredBy(Pack, PushNuGet) + private Target ReleaseToGitHub => _ => _ + .TriggeredBy(Pack, PushToNuGet) .OnlyWhenStatic( () => TagVersion is not null @@ -197,24 +175,53 @@ TagVersion is not null ) ); - var release = await client - .Repository - .Release - .Create( - repositoryId, - new NewRelease(TagName!) - { - Draft = true, - Name = TagName!, - Prerelease = TagVersion!.IsPrerelease - } - ); + Release release; + + try + { + release = await client + .Repository + .Release + .Get(repositoryId, TagName!); + + if (!release.Draft) + { + return; + } + } + catch (ApiException) + { + release = await client + .Repository + .Release + .Create( + repositoryId, + new NewRelease(TagName!) + { + Draft = true, + Name = TagName!, + Prerelease = TagVersion!.IsPrerelease + } + ); + } // preserve order foreach (var nupkg in NupkgArtifactsPath.GetFiles()) { using var ms = new MemoryStream(nupkg.ReadAllBytes()); + if ( + release.Assets.Any( + x => StringEqualsOrdinalIgnoreCase( + x.Name, + nupkg.Name + ) + ) + ) + { + continue; + } + await client .Repository .Release @@ -237,6 +244,19 @@ await client.Repository.Release.Edit( } ); + private void PushToNuGetFeed(string feedUrl, string apiKey) => + NupkgArtifactsPath + .GetFiles() + .ForEach( + nupkg => DotNetTasks.DotNetNuGetPush( + x => x + .SetApiKey(apiKey) + .SetSkipDuplicate(true) + .SetSource(feedUrl) + .SetTargetPath(nupkg) + ) + ); + private static bool StringEqualsOrdinalIgnoreCase(string? x, string? y) => String.Equals(x, y, StringComparison.OrdinalIgnoreCase); }