Skip to content

Commit

Permalink
Remove use of Cake.Git (#13)
Browse files Browse the repository at this point in the history
* Remove use of Cake.Git

Cake.Git does not work on MacOS (cake-contrib/Cake_Git#79)

We only used it to get the current branch name so we can just call git directly to get it instead.

* Update version
  • Loading branch information
wazzamatazz authored Nov 28, 2024
1 parent afb5fb2 commit dec6560
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Jaahas.Cake.Extensions/Jaahas.Cake.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<PropertyGroup>
<Description>Extensions for the Cake build system.</Description>
<Version>2.2.1</Version>
<Version>2.3.0</Version>
<Authors>Graham Watts</Authors>
<CopyrightStartYear>2021</CopyrightStartYear>
<PackageProjectUrl>https://github.com/wazzamatazz/cake-recipes</PackageProjectUrl>
Expand Down
48 changes: 25 additions & 23 deletions src/Jaahas.Cake.Extensions/content/build-utilities.cake
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ using System.Text.Json;

using Spectre.Console;

#addin nuget:?package=Cake.Git&version=4.0.0

#tool dotnet:?package=CycloneDX&version=3.0.8
#tool dotnet:?package=CycloneDX&version=4.1.0

#load "build-state.cake"
#load "task-definitions.cake"
Expand All @@ -21,7 +19,25 @@ public void Bootstrap(string solutionFilePath, string versionFilePath, IEnumerab


private string GetBranchName() {
return Argument("branch", "");
// For Git repositories, we always use the name of the current branch, regardless of what was
// specified as the branch argument.
var currentDir = DirectoryPath.FromString(".");
return GetGitBranchName(currentDir) ?? Argument("branch", "main");
}


private string GetGitBranchName(DirectoryPath dir) {
var settings = new ProcessSettings() {
RedirectStandardOutput = true,
Arguments = "rev-parse --abbrev-ref HEAD"
};

using (var process = StartAndReturnProcess("git", settings)) {
process.WaitForExit();
return process.GetExitCode() == 0
? process.GetStandardOutput().FirstOrDefault()
: null;
}
}


Expand Down Expand Up @@ -104,20 +120,6 @@ private void ConfigureBuildState(string solutionFilePath, string versionFilePath
var buildCounter = Argument("build-counter", -1);
var buildMetadata = Argument("build-metadata", "");

// Set branch name. For Git repositories, we always use the friendly name of the
// current branch, regardless of what was specified in the branchName parameter.
string branch;

var currentDir = DirectoryPath.FromString(".");
if (GitIsValidRepository(currentDir)) {
branch = GitBranchCurrent(currentDir).FriendlyName;
}
else {
branch = string.IsNullOrEmpty(branchName)
? "main"
: branchName;
}

// Assembly version:
// MAJOR.MINOR.0.0
state.AssemblyVersion = $"{majorVersion}.{minorVersion}.0.0";
Expand All @@ -138,7 +140,7 @@ private void ConfigureBuildState(string solutionFilePath, string versionFilePath
if (buildCounter >= 0) {
informationalVersionBuilder.Append($".{buildCounter}");
}
informationalVersionBuilder.Append($"+{NormaliseMetadata(branch)}");
informationalVersionBuilder.Append($"+{NormaliseMetadata(branchName)}");
if (!string.IsNullOrWhiteSpace(buildMetadata)) {
informationalVersionBuilder.Append($".{NormaliseMetadata(buildMetadata)}");
}
Expand All @@ -158,11 +160,11 @@ private void ConfigureBuildState(string solutionFilePath, string versionFilePath
// MAJOR.MINOR.PATCH[-SUFFIX]+BRANCH (build counter < 0)
state.BuildNumber = string.IsNullOrWhiteSpace(versionSuffix)
? buildCounter >= 0
? $"{majorVersion}.{minorVersion}.{patchVersion}.{buildCounter}+{branch}"
: $"{majorVersion}.{minorVersion}.{patchVersion}+{branch}"
? $"{majorVersion}.{minorVersion}.{patchVersion}.{buildCounter}+{branchName}"
: $"{majorVersion}.{minorVersion}.{patchVersion}+{branchName}"
: buildCounter >= 0
? $"{majorVersion}.{minorVersion}.{patchVersion}-{versionSuffix}.{buildCounter}+{branch}"
: $"{majorVersion}.{minorVersion}.{patchVersion}-{versionSuffix}+{branch}";
? $"{majorVersion}.{minorVersion}.{patchVersion}-{versionSuffix}.{buildCounter}+{branchName}"
: $"{majorVersion}.{minorVersion}.{patchVersion}-{versionSuffix}+{branchName}";

var setBuildNumber = !string.Equals(state.Target, "Clean", StringComparison.OrdinalIgnoreCase) && !string.Equals(state.Target, "BillOfMaterials", StringComparison.OrdinalIgnoreCase);
if (setBuildNumber) {
Expand Down

0 comments on commit dec6560

Please sign in to comment.