Add versioning using GitVersion
This commit is contained in:
Родитель
a101582c0a
Коммит
08228a3c9c
|
@ -0,0 +1,13 @@
|
|||
next-version: 3.9.0
|
||||
mode: ContinuousDelivery
|
||||
legacy-semver-padding: 5
|
||||
build-metadata-padding: 5
|
||||
commits-since-version-source-padding: 5
|
||||
branches:
|
||||
master:
|
||||
regex: ^main$
|
||||
tag: dev
|
||||
release:
|
||||
tag: pre
|
||||
pull-request:
|
||||
tag: pr
|
60
build.cake
60
build.cake
|
@ -1,3 +1,4 @@
|
|||
#tool nuget:?package=GitVersion.CommandLine&version=5.0.0
|
||||
#tool nuget:?package=NUnit.ConsoleRunner&version=3.7.0
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
@ -11,61 +12,6 @@
|
|||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
var target = Argument("target", "Default");
|
||||
var configuration = Argument("configuration", DEFAULT_CONFIGURATION);
|
||||
|
||||
// Special (optional) arguments for the script. You pass these
|
||||
// through the Cake bootscrap script via the -ScriptArgs argument
|
||||
// for example:
|
||||
// ./build.ps1 -t RePackageNuget -ScriptArgs --nugetVersion="3.9.9"
|
||||
// ./build.ps1 -t RePackageNuget -ScriptArgs '--binaries="rel3.9.9" --nugetVersion="3.9.9"'
|
||||
var nugetVersion = Argument("nugetVersion", (string)null);
|
||||
var chocoVersion = Argument("chocoVersion", (string)null);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// SET PACKAGE VERSION
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
var packageVersion = DEFAULT_VERSION;
|
||||
|
||||
if (BuildSystem.IsRunningOnAppVeyor)
|
||||
{
|
||||
var tag = AppVeyor.Environment.Repository.Tag;
|
||||
|
||||
if (tag.IsTag)
|
||||
{
|
||||
packageVersion = tag.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
var buildNumber = AppVeyor.Environment.Build.Number.ToString("00000");
|
||||
var branch = AppVeyor.Environment.Repository.Branch;
|
||||
var isPullRequest = AppVeyor.Environment.PullRequest.IsPullRequest;
|
||||
|
||||
if (branch == "main" && !isPullRequest)
|
||||
{
|
||||
packageVersion = DEFAULT_VERSION + "-dev-" + buildNumber;
|
||||
}
|
||||
else
|
||||
{
|
||||
var suffix = "-ci-" + buildNumber;
|
||||
|
||||
if (isPullRequest)
|
||||
suffix += "-pr-" + AppVeyor.Environment.PullRequest.Number;
|
||||
else if (AppVeyor.Environment.Repository.Branch.StartsWith("release", StringComparison.OrdinalIgnoreCase))
|
||||
suffix += "-pre-" + buildNumber;
|
||||
else
|
||||
suffix += "-" + System.Text.RegularExpressions.Regex.Replace(branch, "[^0-9A-Za-z-]+", "-");
|
||||
|
||||
// Nuget limits "special version part" to 20 chars. Add one for the hyphen.
|
||||
if (suffix.Length > 21)
|
||||
suffix = suffix.Substring(0, 21);
|
||||
|
||||
packageVersion = DEFAULT_VERSION + suffix;
|
||||
}
|
||||
}
|
||||
|
||||
AppVeyor.UpdateBuildVersion(packageVersion);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// SETUP AND TEARDOWN
|
||||
|
@ -168,7 +114,7 @@ Task("BuildNuGetPackage")
|
|||
NuGetPack(new NuGetPackSettings()
|
||||
{
|
||||
Id = NUGET_ID,
|
||||
Version = nugetVersion ?? packageVersion,
|
||||
Version = parameters.PackageVersion,
|
||||
Title = TITLE,
|
||||
Authors = AUTHORS,
|
||||
Owners = OWNERS,
|
||||
|
@ -204,7 +150,7 @@ Task("BuildNuGetPackage")
|
|||
new ChocolateyPackSettings()
|
||||
{
|
||||
Id = CHOCO_ID,
|
||||
Version = chocoVersion ?? packageVersion,
|
||||
Version = parameters.PackageVersion,
|
||||
Title = TITLE,
|
||||
Authors = AUTHORS,
|
||||
Owners = OWNERS,
|
||||
|
|
|
@ -59,7 +59,6 @@ var MAILING_LIST_URL = new Uri("https://groups.google.com/forum/#!forum/nunit-di
|
|||
|
||||
// Directories
|
||||
var PROJECT_DIR = Context.Environment.WorkingDirectory.FullPath + "/";
|
||||
var BIN_DIR = PROJECT_DIR + "bin/" + configuration + "/";
|
||||
var OUTPUT_DIR = PROJECT_DIR + "output/";
|
||||
|
||||
// Package sources for nuget restore
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#load "./constants.cake"
|
||||
// #load "./versioning.cake"
|
||||
#load "./versioning.cake"
|
||||
// #load "./packaging.cake"
|
||||
// #load "./package-checks.cake"
|
||||
// #load "./package-tests.cake"
|
||||
|
@ -38,7 +38,7 @@ public class BuildParameters
|
|||
ChocolateyApiKey = _context.EnvironmentVariable(CHOCO_API_KEY);
|
||||
GitHubAccessToken = _context.EnvironmentVariable(GITHUB_ACCESS_TOKEN);
|
||||
|
||||
// BuildVersion = new BuildVersion(context, this);
|
||||
BuildVersion = new BuildVersion(context, this);
|
||||
}
|
||||
|
||||
public string Target { get; }
|
||||
|
@ -48,14 +48,13 @@ public class BuildParameters
|
|||
|
||||
public string Configuration { get; }
|
||||
|
||||
// public BuildVersion BuildVersion { get; }
|
||||
// public string PackageVersion => BuildVersion.PackageVersion;
|
||||
public string PackageVersion => DEFAULT_VERSION;
|
||||
// public string AssemblyVersion => BuildVersion.AssemblyVersion;
|
||||
// public string AssemblyFileVersion => BuildVersion.AssemblyFileVersion;
|
||||
// public string AssemblyInformationalVersion => BuildVersion.AssemblyInformationalVersion;
|
||||
public BuildVersion BuildVersion { get; }
|
||||
public string PackageVersion => BuildVersion.PackageVersion;
|
||||
public string AssemblyVersion => BuildVersion.AssemblyVersion;
|
||||
public string AssemblyFileVersion => BuildVersion.AssemblyFileVersion;
|
||||
public string AssemblyInformationalVersion => BuildVersion.AssemblyInformationalVersion;
|
||||
|
||||
// public int PackageTestLevel { get; }
|
||||
public int PackageTestLevel { get; }
|
||||
|
||||
public bool IsLocalBuild => _buildSystem.IsLocalBuild;
|
||||
public bool IsRunningOnUnix => _context.IsRunningOnUnix();
|
||||
|
@ -86,18 +85,18 @@ public class BuildParameters
|
|||
public string ChocolateyApiKey { get; }
|
||||
public string GitHubAccessToken { get; }
|
||||
|
||||
// public string BranchName => BuildVersion.BranchName;
|
||||
// public bool IsReleaseBranch => BuildVersion.IsReleaseBranch;
|
||||
public string BranchName => BuildVersion.BranchName;
|
||||
public bool IsReleaseBranch => BuildVersion.IsReleaseBranch;
|
||||
|
||||
// public bool IsPreRelease => BuildVersion.IsPreRelease;
|
||||
// public bool ShouldPublishToMyGet =>
|
||||
// !IsPreRelease || LABELS_WE_PUBLISH_ON_MYGET.Contains(BuildVersion.PreReleaseLabel);
|
||||
// public bool ShouldPublishToNuGet =>
|
||||
// !IsPreRelease || LABELS_WE_PUBLISH_ON_NUGET.Contains(BuildVersion.PreReleaseLabel);
|
||||
// public bool ShouldPublishToChocolatey =>
|
||||
// !IsPreRelease || LABELS_WE_PUBLISH_ON_CHOCOLATEY.Contains(BuildVersion.PreReleaseLabel);
|
||||
// public bool IsProductionRelease =>
|
||||
// !IsPreRelease || LABELS_WE_RELEASE_ON_GITHUB.Contains(BuildVersion.PreReleaseLabel);
|
||||
public bool IsPreRelease => BuildVersion.IsPreRelease;
|
||||
public bool ShouldPublishToMyGet =>
|
||||
!IsPreRelease || LABELS_WE_PUBLISH_ON_MYGET.Contains(BuildVersion.PreReleaseLabel);
|
||||
public bool ShouldPublishToNuGet =>
|
||||
!IsPreRelease || LABELS_WE_PUBLISH_ON_NUGET.Contains(BuildVersion.PreReleaseLabel);
|
||||
public bool ShouldPublishToChocolatey =>
|
||||
!IsPreRelease || LABELS_WE_PUBLISH_ON_CHOCOLATEY.Contains(BuildVersion.PreReleaseLabel);
|
||||
public bool IsProductionRelease =>
|
||||
!IsPreRelease || LABELS_WE_RELEASE_ON_GITHUB.Contains(BuildVersion.PreReleaseLabel);
|
||||
|
||||
public string GetPathToConsoleRunner(string version)
|
||||
{
|
||||
|
@ -108,21 +107,21 @@ public class BuildParameters
|
|||
{
|
||||
var validationErrors = new List<string>();
|
||||
|
||||
// if (TasksToExecute.Contains("PublishPackages"))
|
||||
// {
|
||||
// if (ShouldPublishToMyGet && string.IsNullOrEmpty(MyGetApiKey))
|
||||
// validationErrors.Add("MyGet ApiKey was not set.");
|
||||
// if (ShouldPublishToNuGet && string.IsNullOrEmpty(NuGetApiKey))
|
||||
// validationErrors.Add("NuGet ApiKey was not set.");
|
||||
// if (ShouldPublishToChocolatey && string.IsNullOrEmpty(ChocolateyApiKey))
|
||||
// validationErrors.Add("Chocolatey ApiKey was not set.");
|
||||
// }
|
||||
if (TasksToExecute.Contains("PublishPackages"))
|
||||
{
|
||||
if (ShouldPublishToMyGet && string.IsNullOrEmpty(MyGetApiKey))
|
||||
validationErrors.Add("MyGet ApiKey was not set.");
|
||||
if (ShouldPublishToNuGet && string.IsNullOrEmpty(NuGetApiKey))
|
||||
validationErrors.Add("NuGet ApiKey was not set.");
|
||||
if (ShouldPublishToChocolatey && string.IsNullOrEmpty(ChocolateyApiKey))
|
||||
validationErrors.Add("Chocolatey ApiKey was not set.");
|
||||
}
|
||||
|
||||
// if (TasksToExecute.Contains("CreateDraftRelease") && (IsReleaseBranch || IsProductionRelease))
|
||||
// {
|
||||
// if (string.IsNullOrEmpty(GitHubAccessToken))
|
||||
// validationErrors.Add("GitHub Access Token was not set.");
|
||||
// }
|
||||
if (TasksToExecute.Contains("CreateDraftRelease") && (IsReleaseBranch || IsProductionRelease))
|
||||
{
|
||||
if (string.IsNullOrEmpty(GitHubAccessToken))
|
||||
validationErrors.Add("GitHub Access Token was not set.");
|
||||
}
|
||||
|
||||
if (validationErrors.Count > 0)
|
||||
{
|
||||
|
@ -150,18 +149,18 @@ public class BuildParameters
|
|||
|
||||
Console.WriteLine("\nVERSIONING");
|
||||
Console.WriteLine("PackageVersion: " + PackageVersion);
|
||||
// Console.WriteLine("AssemblyVersion: " + AssemblyVersion);
|
||||
// Console.WriteLine("AssemblyFileVersion: " + AssemblyFileVersion);
|
||||
// Console.WriteLine("AssemblyInformationalVersion: " + AssemblyInformationalVersion);
|
||||
// Console.WriteLine("SemVer: " + BuildVersion.SemVer);
|
||||
// Console.WriteLine("IsPreRelease: " + BuildVersion.IsPreRelease);
|
||||
// Console.WriteLine("PreReleaseLabel: " + BuildVersion.PreReleaseLabel);
|
||||
// Console.WriteLine("PreReleaseSuffix: " + BuildVersion.PreReleaseSuffix);
|
||||
Console.WriteLine("AssemblyVersion: " + AssemblyVersion);
|
||||
Console.WriteLine("AssemblyFileVersion: " + AssemblyFileVersion);
|
||||
Console.WriteLine("AssemblyInformationalVersion: " + AssemblyInformationalVersion);
|
||||
Console.WriteLine("SemVer: " + BuildVersion.SemVer);
|
||||
Console.WriteLine("IsPreRelease: " + BuildVersion.IsPreRelease);
|
||||
Console.WriteLine("PreReleaseLabel: " + BuildVersion.PreReleaseLabel);
|
||||
Console.WriteLine("PreReleaseSuffix: " + BuildVersion.PreReleaseSuffix);
|
||||
|
||||
Console.WriteLine("\nDIRECTORIES");
|
||||
Console.WriteLine("Project: " + ProjectDirectory);
|
||||
Console.WriteLine("Output: " + OutputDirectory);
|
||||
//Console.WriteLine("Source: " + SourceDirectory);
|
||||
//Console.WriteLine("Source: " + SourceDirectory);
|
||||
//Console.WriteLine("NuGet: " + NuGetDirectory);
|
||||
//Console.WriteLine("Choco: " + ChocoDirectory);
|
||||
Console.WriteLine("Package: " + PackageDirectory);
|
||||
|
@ -182,14 +181,14 @@ public class BuildParameters
|
|||
Console.WriteLine("NuGetApiKey: " + (!string.IsNullOrEmpty(NuGetApiKey) ? "AVAILABLE" : "NOT AVAILABLE"));
|
||||
Console.WriteLine("ChocolateyApiKey: " + (!string.IsNullOrEmpty(ChocolateyApiKey) ? "AVAILABLE" : "NOT AVAILABLE"));
|
||||
|
||||
// Console.WriteLine("\nPUBLISHING");
|
||||
// Console.WriteLine("ShouldPublishToMyGet: " + ShouldPublishToMyGet);
|
||||
// Console.WriteLine("ShouldPublishToNuGet: " + ShouldPublishToNuGet);
|
||||
// Console.WriteLine("ShouldPublishToChocolatey: " + ShouldPublishToChocolatey);
|
||||
Console.WriteLine("\nPUBLISHING");
|
||||
Console.WriteLine("ShouldPublishToMyGet: " + ShouldPublishToMyGet);
|
||||
Console.WriteLine("ShouldPublishToNuGet: " + ShouldPublishToNuGet);
|
||||
Console.WriteLine("ShouldPublishToChocolatey: " + ShouldPublishToChocolatey);
|
||||
|
||||
// Console.WriteLine("\nRELEASING");
|
||||
// Console.WriteLine("BranchName: " + BranchName);
|
||||
// Console.WriteLine("IsReleaseBranch: " + IsReleaseBranch);
|
||||
// Console.WriteLine("IsProductionRelease: " + IsProductionRelease);
|
||||
Console.WriteLine("\nRELEASING");
|
||||
Console.WriteLine("BranchName: " + BranchName);
|
||||
Console.WriteLine("IsReleaseBranch: " + IsReleaseBranch);
|
||||
Console.WriteLine("IsProductionRelease: " + IsProductionRelease);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
using System.Text.RegularExpressions;
|
||||
|
||||
public class BuildVersion
|
||||
{
|
||||
private ISetupContext _context;
|
||||
private BuildParameters _parameters;
|
||||
private GitVersion _gitVersion;
|
||||
|
||||
// NOTE: This is complicated because (1) the user may have specified
|
||||
// the package version on the command-line and (2) GitVersion may
|
||||
// or may not be available. We'll work on solving (2) by getting
|
||||
// GitVersion to run for us on Linux, but (1) will alwas remain.
|
||||
//
|
||||
// We simplify things a by figuring out the full package version and
|
||||
// then parsing it to provide information that is used in the build.
|
||||
public BuildVersion(ISetupContext context, BuildParameters parameters)
|
||||
{
|
||||
_context = context;
|
||||
_parameters = parameters;
|
||||
_gitVersion = context.GitVersion();
|
||||
|
||||
BranchName = _gitVersion.BranchName;
|
||||
IsReleaseBranch = BranchName.StartsWith("release-");
|
||||
|
||||
// TODO: Get GitVersion to work on Linux
|
||||
string packageVersion = context.HasArgument("version")
|
||||
? context.Argument("version", DEFAULT_VERSION)
|
||||
: CalculatePackageVersion();
|
||||
|
||||
int dash = packageVersion.IndexOf('-');
|
||||
IsPreRelease = dash > 0;
|
||||
|
||||
string versionPart = packageVersion;
|
||||
string suffix = "";
|
||||
string label = "";
|
||||
|
||||
if (IsPreRelease)
|
||||
{
|
||||
versionPart = packageVersion.Substring(0, dash);
|
||||
suffix = packageVersion.Substring(dash+1);
|
||||
foreach (char c in suffix)
|
||||
{
|
||||
if (!char.IsLetter(c))
|
||||
break;
|
||||
label += c;
|
||||
}
|
||||
}
|
||||
|
||||
Version version = new Version(versionPart);
|
||||
SemVer = version.ToString(3);
|
||||
PreReleaseLabel = label;
|
||||
PreReleaseSuffix = suffix;
|
||||
|
||||
PackageVersion = packageVersion;
|
||||
AssemblyVersion = SemVer + ".0";
|
||||
AssemblyFileVersion = SemVer;
|
||||
AssemblyInformationalVersion = packageVersion;
|
||||
}
|
||||
|
||||
public string BranchName { get; }
|
||||
public bool IsReleaseBranch { get; }
|
||||
|
||||
public string PackageVersion { get; }
|
||||
public string AssemblyVersion { get; }
|
||||
public string AssemblyFileVersion { get; }
|
||||
public string AssemblyInformationalVersion { get; }
|
||||
|
||||
public string SemVer { get; }
|
||||
public bool IsPreRelease { get; }
|
||||
public string PreReleaseLabel { get; }
|
||||
public string PreReleaseSuffix { get; }
|
||||
|
||||
private string CalculatePackageVersion()
|
||||
{
|
||||
string label = _gitVersion.PreReleaseLabel;
|
||||
|
||||
// Non pre-release is easy
|
||||
if (string.IsNullOrEmpty(label))
|
||||
return _gitVersion.MajorMinorPatch;
|
||||
|
||||
string branchName = _gitVersion.BranchName;
|
||||
// We don't currently use this pattern, but check in case we do later.
|
||||
if (branchName.StartsWith ("feature/"))
|
||||
branchName = branchName.Substring(8);
|
||||
|
||||
// Arbitrary branch names are ci builds
|
||||
if (label == branchName)
|
||||
label = "ci";
|
||||
|
||||
string suffix = "-" + label + _gitVersion.CommitsSinceVersionSourcePadded;
|
||||
|
||||
switch(label)
|
||||
{
|
||||
case "ci":
|
||||
branchName = Regex.Replace(branchName, "[^0-9A-Za-z-]+", "-");
|
||||
suffix += "-" + branchName;
|
||||
// Nuget limits "special version part" to 20 chars. Add one for the hyphen.
|
||||
if (suffix.Length > 21)
|
||||
suffix = suffix.Substring(0, 21);
|
||||
return _gitVersion.MajorMinorPatch + suffix;
|
||||
|
||||
case "dev":
|
||||
case "pre":
|
||||
return _gitVersion.MajorMinorPatch + suffix;
|
||||
|
||||
case "pr":
|
||||
return _gitVersion.LegacySemVerPadded;
|
||||
|
||||
case "rc":
|
||||
case "alpha":
|
||||
case "beta":
|
||||
default:
|
||||
return _gitVersion.LegacySemVer;
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче