Skip to content

Commit

Permalink
Removed Print task and moved it to OnInitialized event handler. Added…
Browse files Browse the repository at this point in the history
… Global usings and code refactoring.
  • Loading branch information
DavidRogersDev committed Mar 29, 2024
1 parent 2ba30eb commit 04392cc
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 94 deletions.
5 changes: 3 additions & 2 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"LocalPack": {
"type": "boolean"
},
"LocalPackDenyRelease": {
"type": "boolean"
},
"NoLogo": {
"type": "boolean",
"description": "Disables displaying the NUKE logo"
Expand Down Expand Up @@ -99,7 +102,6 @@
"CleanAndBuild",
"Compile",
"Pack",
"Print",
"Push",
"Restore"
]
Expand All @@ -119,7 +121,6 @@
"CleanAndBuild",
"Compile",
"Pack",
"Print",
"Push",
"Restore"
]
Expand Down
86 changes: 29 additions & 57 deletions build/Build.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
using _build;
using Invariants;
using Nuke.Common;
using Nuke.Common.CI.AzurePipelines;
using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.GitVersion;
using Nuke.Common.Utilities.Collections;
using Serilog;
using System;
using System.Linq;
using static Nuke.Common.Tools.DotNet.DotNetTasks;

class Build : NukeBuild
{
public static int Main() => IsLocalBuild
? Execute<Build>(t0 => t0.Print, t1 => t1.Pack)
: Execute<Build>(t0 => t0.Print, t1 => t1.Push);
? Execute<Build>(t => t.Pack)
: Execute<Build>(t => t.Push);

protected override void OnBuildInitialized()
{
_releaseGuard = new ReleaseGuard(GitVersion);
_releaseGuard = new ReleaseGuard(GitVersion,
new PackagePublishConfig(NUGET_API_KEY, NUGET_URL),
new PackagePublishConfig(PACKAGES_GITHUB_NUGET_PAT, PACKAGES_GITHUB_NUGET_URL));
_directoryResolver = new DirectoryResolver(RootDirectory);
_buildConfigurationManager = new BuildConfigurationManager(Solution);

Log.Information(LogMessage.IsTaggedBuild, _releaseGuard.IsTaggedBuild);
Log.Information(LogMessage.MajorMinorPatch, GitVersion.MajorMinorPatch);
Log.Information(LogMessage.NuGetVersion, GitVersion.NuGetVersion);
Log.Information(LogMessage.PreReleaseLabel, GitVersion?.PreReleaseLabel ?? LogMessage.NoReleaseLabel);
Log.Information(LogMessage.PreReleaseTag, GitVersion?.PreReleaseTag ?? LogMessage.NoReleaseTag);

if (IsLocalBuild)
{
Log.Information(LogMessage.Configuration, Configuration);
Log.Information(LogMessage.ReleaseNotes, RELEASE_NOTES);
Log.Information(LogMessage.RootDirectory, RootDirectory);
Log.Information(LogMessage.SourceDirectory, _directoryResolver.SourceDirectory);
}

_directoryResolver.ArtifactsDirectory.CreateOrCleanDirectory();
}

Expand All @@ -36,6 +43,7 @@ protected override void OnBuildInitialized()
[Parameter] readonly string RELEASE_NOTES;
[Parameter] readonly bool IgnoreFailedSources = false;
[Parameter] readonly bool LocalPack = false;
[Parameter] readonly bool LocalPackDenyRelease = false;
[Parameter(Param.Description.Configuration)]
readonly Configuration Configuration = IsLocalBuild
? Configuration.Debug
Expand All @@ -50,27 +58,7 @@ protected override void OnBuildInitialized()
ReleaseGuard _releaseGuard;
BuildConfigurationManager _buildConfigurationManager;

// Delegate Tasks
Target Print => _ => _
.Description(Description.Print)
.Before(Clean, Restore)
.Executes(() =>
{
Log.Information(LogMessage.IsTaggedBuild, _releaseGuard.IsTaggedBuild);
Log.Information(LogMessage.MajorMinorPatch, GitVersion.MajorMinorPatch);
Log.Information(LogMessage.NuGetVersion, GitVersion.NuGetVersion);
Log.Information(LogMessage.PreReleaseLabel, GitVersion?.PreReleaseLabel ?? LogMessage.NoReleaseLabel);
Log.Information(LogMessage.PreReleaseTag, GitVersion?.PreReleaseTag ?? LogMessage.NoReleaseTag);
if (IsLocalBuild)
{
Log.Information(LogMessage.Configuration, Configuration);
Log.Information(LogMessage.ReleaseNotes, RELEASE_NOTES);
Log.Information(LogMessage.RootDirectory, RootDirectory);
Log.Information(LogMessage.SourceDirectory, _directoryResolver.SourceDirectory);
}
});

/********************************** Delegate Tasks **********************************/
Target Clean => _ => _
.Unlisted()
.Description(Description.Clean)
Expand Down Expand Up @@ -119,7 +107,9 @@ protected override void OnBuildInitialized()
.DependsOn(Compile)
.OnlyWhenDynamic(() => LocalPack
? _releaseGuard.BuildToBePacked(System.Configuration.OverrideMode.Allow)
: _releaseGuard.BuildToBePacked())
: LocalPackDenyRelease
? _releaseGuard.BuildToBePacked(System.Configuration.OverrideMode.Deny)
: _releaseGuard.BuildToBePacked())
.Executes(() =>
{
var packableProjects = _buildConfigurationManager.GetPackableProjects();
Expand Down Expand Up @@ -161,13 +151,13 @@ protected override void OnBuildInitialized()
var nugetFiles = _directoryResolver.ArtifactsDirectory.GlobFiles(FileSystem.GlobPattern.NugetFiles)
.Where(x => !x.Name.EndsWith(FileSystem.GlobPattern.NugetSymbolFiles));
Assert.True(nugetFiles.Any(), "There are no Nuget files");
Assert.True(nugetFiles.Any(), LogMessage.Assertion.NoNugetFilesExist);
(string Token, string Url) = ResolvePublishDestinationDetails(_releaseGuard);
(string Token, string Url) = _releaseGuard.ResolvePublishDestinationDetails();
if (string.IsNullOrEmpty(Token))
{
Log.Information("Nothing pushed");
Log.Information(LogMessage.NothingPushed);
}
else
{
Expand All @@ -182,27 +172,9 @@ protected override void OnBuildInitialized()
}
});

PackagePublishConfig ResolvePublishDestinationDetails(ReleaseGuard releaseGuard)
{
if (releaseGuard.BuildToBeReleasedToNugetOrg)
{
return new PackagePublishConfig(NUGET_API_KEY, NUGET_URL);
}
else if (releaseGuard.BuildToBeReleasedToGitHub)
{
return new PackagePublishConfig(PACKAGES_GITHUB_NUGET_PAT, PACKAGES_GITHUB_NUGET_URL);
}
else
{
return new PackagePublishConfig(string.Empty, string.Empty);
}

}

// Ad-hoc Build Configs
/********************************** Ad-hoc Build Configs **********************************/
Target CleanAndBuild => _ => _
.Description(Description.CleanAndBuild)
.DependsOn(Print, Compile);
.DependsOn(Compile);
}

public record PackagePublishConfig(string Token, string Url);
5 changes: 1 addition & 4 deletions build/BuildConfigurationManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Nuke.Common.ProjectModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;

namespace _build
{
Expand Down
3 changes: 0 additions & 3 deletions build/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.ComponentModel;
using System.Linq;
using Nuke.Common.Tooling;

[TypeConverter(typeof(TypeConverter<Configuration>))]
public class Configuration : Enumeration
Expand Down
9 changes: 2 additions & 7 deletions build/DirectoryResolver.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using Invariants;
using Nuke.Common.IO;
using System;


public sealed class DirectoryResolver
public sealed class DirectoryResolver
{
readonly AbsolutePath rootDirectory;
public DirectoryResolver(AbsolutePath rootDirectory)
{
this.rootDirectory = rootDirectory;
this.rootDirectory = rootDirectory ?? throw new ArgumentNullException(nameof(rootDirectory));
}

public AbsolutePath ArtifactsDirectory => rootDirectory / FileSystem.Directory.Artifacts;
Expand Down
9 changes: 9 additions & 0 deletions build/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global using Invariants;
global using Nuke.Common.ProjectModel;
global using Nuke.Common.IO;
global using Nuke.Common.Tooling;
global using Nuke.Common.Tools.GitVersion;
global using Nuke.Common.Utilities.Collections;
global using Serilog;
global using System;
global using System.Linq;
6 changes: 6 additions & 0 deletions build/Invariants/LogMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ public static class LogMessage

public const string NoReleaseLabel = "No Release label";
public const string NoReleaseTag = "No Release Tag";
public const string NothingPushed = "Nothing pushed";

public class Assertion
{
public const string NoNugetFilesExist = "There are no Nuget files";
}
}
}
4 changes: 1 addition & 3 deletions build/Invariants/PackageDetail.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Invariants
namespace Invariants
{
public static class PackageDetail
{
Expand Down
4 changes: 1 addition & 3 deletions build/Invariants/PackageValue.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Invariants
namespace Invariants
{
public static class PackageValue
{
Expand Down
53 changes: 38 additions & 15 deletions build/ReleaseGuard.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,58 @@
using Invariants;
using Nuke.Common.Tools.GitVersion;
using Nuke.Common.Utilities.Collections;
using Serilog;
using System;
using System.Configuration;
using System.Configuration;


public class ReleaseGuard
public sealed class ReleaseGuard
{
readonly GitVersion gitVersion;

internal ReleaseGuard(GitVersion GitVersion)
readonly PackagePublishConfig nugetOrg;
readonly PackagePublishConfig githubPackages;

internal ReleaseGuard(GitVersion GitVersion, PackagePublishConfig nugetOrg, PackagePublishConfig githubPackages)
{
this.githubPackages = githubPackages;
this.nugetOrg = nugetOrg;
gitVersion = GitVersion ?? throw new ArgumentNullException(nameof(GitVersion));
}

private bool IsPreReleaseBuild =>
gitVersion.PreReleaseLabel.Equals(BuildType.Ci, StringComparison.OrdinalIgnoreCase) ||
gitVersion.PreReleaseLabel.Equals(BuildType.Rc, StringComparison.OrdinalIgnoreCase);

private bool IsReleaseOrMain =>
private bool IsReleaseOrMainBranch =>
gitVersion.BranchName.StartsWith(Branch.Release, StringComparison.OrdinalIgnoreCase) ||
gitVersion.BranchName.Equals(Branch.Main, StringComparison.OrdinalIgnoreCase);

private bool IsReleaseBranch =>
gitVersion.BranchName.StartsWith(Branch.Release, StringComparison.OrdinalIgnoreCase);

public bool IsTaggedBuild => gitVersion.PreReleaseTag.IsEmpty();

public bool BuildToBePacked(OverrideMode overrideMode = OverrideMode.Deny) => overrideMode == OverrideMode.Allow ? true : IsReleaseOrMain;
public bool BuildToBePacked(OverrideMode? overrideMode = null) => overrideMode switch
{
OverrideMode.Allow => true,
OverrideMode.Deny => false,
_ => IsReleaseOrMainBranch
};

public bool BuildToBeReleasedToNugetOrg =>
IsTaggedBuild &&
gitVersion.BranchName.StartsWith(Branch.Release, StringComparison.OrdinalIgnoreCase);
public bool BuildToBeReleasedToNugetOrg() => IsTaggedBuild && IsReleaseBranch;

public bool BuildToBeReleasedToGitHub => IsPreReleaseBuild;
public bool BuildToBeReleasedToGitHub() => IsPreReleaseBuild;

public PackagePublishConfig ResolvePublishDestinationDetails()
{
if (BuildToBeReleasedToNugetOrg())
{
return nugetOrg;
}
else if (BuildToBeReleasedToGitHub())
{
return githubPackages;
}
else
{
return new PackagePublishConfig(string.Empty, string.Empty);
}
}
}

public record PackagePublishConfig(string Token, string Url);

0 comments on commit 04392cc

Please sign in to comment.