Skip to content

Commit

Permalink
Fix build on tag (#1454)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter authored Dec 2, 2021
1 parent 0094535 commit 7b44a91
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
6 changes: 3 additions & 3 deletions build/Build.Publish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand Down
52 changes: 45 additions & 7 deletions build/Build.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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]
Expand Down Expand Up @@ -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(() =>
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7b44a91

Please sign in to comment.