From 7b44a91d7333b331ef569e5f55260fd4ef7a0dd0 Mon Sep 17 00:00:00 2001 From: Rico Suter Date: Thu, 2 Dec 2021 20:25:56 +0100 Subject: [PATCH] Fix build on tag (#1454) --- build/Build.Publish.cs | 6 ++--- build/Build.cs | 52 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/build/Build.Publish.cs b/build/Build.Publish.cs index 94b7d625a..84964ff86 100644 --- a/build/Build.Publish.cs +++ b/build/Build.Publish.cs @@ -16,11 +16,11 @@ public partial class Build string MyGetGetSource => "https://www.myget.org/F/njsonschema/api/v2/package"; [Parameter] [Secret] string MyGetApiKey; - string ApiKeyToUse => !string.IsNullOrWhiteSpace(TagVersion) ? NuGetApiKey : MyGetApiKey; - string SourceToUse => !string.IsNullOrWhiteSpace(TagVersion) ? NuGetSource : MyGetGetSource; + string ApiKeyToUse => IsTaggedBuild ? NuGetApiKey : MyGetApiKey; + string SourceToUse => IsTaggedBuild ? NuGetSource : MyGetGetSource; Target Publish => _ => _ - .OnlyWhenDynamic(() => IsRunningOnWindows && (GitRepository.IsOnMainOrMasterBranch())) + .OnlyWhenDynamic(() => IsRunningOnWindows && (GitRepository.IsOnMainOrMasterBranch() || IsTaggedBuild)) .DependsOn(Pack) .Requires(() => NuGetApiKey, () => MyGetApiKey) .Executes(() => diff --git a/build/Build.cs b/build/Build.cs index 9edf9e3fc..9d9684bda 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Runtime.InteropServices; +using System.Xml.Linq; using Nuke.Common; using Nuke.Common.CI; using Nuke.Common.Git; @@ -11,6 +12,7 @@ using Nuke.Common.Utilities.Collections; using static Nuke.Common.IO.FileSystemTasks; +using static Nuke.Common.Logger; using static Nuke.Common.Tools.DotNet.DotNetTasks; [ShutdownDotNetAfterServerBuild] @@ -38,13 +40,48 @@ partial class Build : NukeBuild static bool IsRunningOnWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); - string TagVersion => GitRepository.Tags.SingleOrDefault(x => x.StartsWith("v"))?[1..]; + bool IsTaggedBuild; + string VersionPrefix; + string VersionSuffix; - string VersionSuffix => - string.IsNullOrWhiteSpace(TagVersion) - ? "preview-" + DateTimeSuffix + string DetermineVersionPrefix() + { + var versionPrefix = GitRepository.Tags.SingleOrDefault(x => x.StartsWith("v"))?[1..]; + if (!string.IsNullOrWhiteSpace(versionPrefix)) + { + IsTaggedBuild = true; + Info($"Tag version {versionPrefix} from Git found, using it as version prefix"); + } + else + { + var propsDocument = XDocument.Parse(TextTasks.ReadAllText(SourceDirectory / "Directory.Build.props")); + versionPrefix = propsDocument.Element("Project").Element("PropertyGroup").Element("VersionPrefix").Value; + Info($"Version prefix {versionPrefix} read from Directory.Build.props"); + } + + return versionPrefix; + } + + protected override void OnBuildInitialized() + { + VersionPrefix = DetermineVersionPrefix(); + + VersionSuffix = !IsTaggedBuild + ? $"preview-{DateTime.UtcNow:yyyyMMdd-HHmm}" : ""; + if (IsLocalBuild) + { + VersionSuffix = $"dev-{DateTime.UtcNow:yyyyMMdd-HHmm}"; + } + + using var _ = Block("BUILD SETUP"); + Info("Configuration:\t" + Configuration); + Info("Version prefix:\t" + VersionPrefix); + Info("Version suffix:\t" + VersionSuffix); + Info("Tagged build:\t" + IsTaggedBuild); + } + Target Clean => _ => _ .Before(Restore) .Executes(() => @@ -102,9 +139,10 @@ partial class Build : NukeBuild DotNetPack(s => s .SetProcessWorkingDirectory(SourceDirectory) - .SetAssemblyVersion(TagVersion) - .SetFileVersion(TagVersion) - .SetInformationalVersion(TagVersion) + .SetAssemblyVersion(VersionPrefix) + .SetFileVersion(VersionPrefix) + .SetInformationalVersion(VersionPrefix) + .SetVersion(VersionPrefix) .SetVersionSuffix(VersionSuffix) .SetConfiguration(Configuration) .SetOutputDirectory(ArtifactsDirectory)