Skip to content

Commit 71d3b36

Browse files
committed
Switch to NerdBank.GitVersioning for versioning
1 parent 9470c7f commit 71d3b36

File tree

10 files changed

+113
-108
lines changed

10 files changed

+113
-108
lines changed

.config/dotnet-tools.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
"nuke"
99
],
1010
"rollForward": false
11+
},
12+
"nbgv": {
13+
"version": "3.9.50",
14+
"commands": [
15+
"nbgv"
16+
],
17+
"rollForward": false
1118
}
1219
}
1320
}

.nuke/build.schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
"Clean",
2929
"Compile",
3030
"Coverage",
31-
"FinishFeature",
3231
"Full",
32+
"GitFetch",
33+
"PrintVersion",
3334
"Publish",
34-
"Release",
3535
"Restore",
3636
"Test",
3737
"UploadCoveralls",

DemoEngine.slnx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
<Folder Name="/solution items/">
1919
<File Path=".config/dotnet-tools.json" />
2020
<File Path=".editorconfig" />
21+
<File Path="CHANGELOG.md" />
2122
<File Path="Directory.Build.props" />
2223
<File Path="Directory.Packages.props" />
2324
<File Path="global.json" />
25+
<File Path="version.json" />
2426
</Folder>
2527
<Folder Name="/src/">
2628
<Project Path="src/Demo.Engine.Core/Demo.Engine.Core.csproj">

Directory.Build.props

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@
1313
<NuGetAudit>true</NuGetAudit>
1414
<NuGetAuditLevel>low</NuGetAuditLevel>
1515
</PropertyGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="Nerdbank.GitVersioning" Condition="!Exists('packages.config')" >
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
</ItemGroup>
1623
</Project>

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.2" />
2525
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.2" />
2626
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.2" />
27+
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.9.50" />
2728
<PackageVersion Include="System.Text.Json" Version="9.0.9" />
2829
<PackageVersion Include="TUnit" Version="1.11.45" />
2930
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0" />

GitVersion.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
mode: ContinuousDeployment
2-
assembly-versioning-scheme: MajorMinor
3-
assembly-file-versioning-scheme: MajorMinorPatch
4-
assembly-versioning-format: '{Major}.{Minor}'
5-
assembly-file-versioning-format: '{Major}.{Minor}.{Patch}'
6-
assembly-informational-format: '{Major}.{Minor}.{Patch}-{PreReleaseTag}'
7-
branches: {}
8-
ignore:
9-
sha: []
1+
workflow: TrunkBased/preview1

build/Build.GitFlow.cs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,31 @@
22
// Distributed under MIT license. See LICENSE file in the root for more information.
33

44
using Nuke.Common;
5-
using Nuke.Common.Git;
6-
using Serilog;
75
using static Nuke.Common.ChangeLog.ChangelogTasks;
8-
using static Nuke.Common.Tools.Git.GitTasks;
96

107
namespace BuildScript;
118

129
internal partial class Build
1310
{
11+
private const string DEVELOP_BRANCH = "develop";
12+
1413
private readonly string _changelogFile = RootDirectory / "CHANGELOG.MD";
1514

1615
public Target Changelog => _ => _
17-
.OnlyWhenStatic(() =>
18-
_gitRepository.IsOnReleaseBranch()
19-
|| _gitRepository.IsOnHotfixBranch())
16+
//.OnlyWhenStatic(() =>
17+
// _gitRepository.IsOnReleaseBranch()
18+
// || _gitRepository.IsOnHotfixBranch())
2019
//.WhenSkipped(DependencyBehavior.Skip)
2120
.Executes(() =>
2221
{
2322
FinalizeChangelog(
2423
_changelogFile,
25-
_gitVersion.MajorMinorPatch,
24+
_gitVersioning.SemVer2,
2625
_gitRepository);
2726
//if (false) //TODO one day, maybe
2827
//{
2928
// Git($"add {_changelogFile}");
3029
// Git($"commit -m \"Finalize {Path.GetFileName(_changelogFile)} for {_gitVersion.MajorMinorPatch}\"");
3130
//}
3231
});
33-
34-
public Target Release => _ => _
35-
.DependsOn(Changelog)
36-
.Requires(() =>
37-
!_gitRepository.IsOnReleaseBranch()
38-
|| GitHasCleanWorkingCopy())
39-
.Executes(() =>
40-
{
41-
var isRelease = _gitRepository.IsOnReleaseBranch();
42-
var clean = GitHasCleanWorkingCopy();
43-
Log.Information($"isRelease {isRelease}, clean {clean}");
44-
});
45-
46-
public Target FinishFeature => _ => _
47-
.DependsOn(Clean, Restore, Compile, Test)
48-
.Executes(() =>
49-
{
50-
var currentBranch = _gitRepository.Branch;
51-
if (!GitHasCleanWorkingCopy())
52-
{
53-
Git("add .");
54-
Git("commit -m \"automatic commit\"");
55-
}
56-
Git($"checkout {DEVELOP_BRANCH}");
57-
Git($"merge --no-ff --no-edit {currentBranch}");
58-
});
5932
}

build/Build.cs

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.IO.Compression;
66
using System.IO.Pipelines;
7+
using System.Text.Json;
78
using Nuke.Common;
89
using Nuke.Common.CI.GitHubActions;
910
using Nuke.Common.Execution;
@@ -16,6 +17,7 @@
1617
using Nuke.Common.Tools.DotCover;
1718
using Nuke.Common.Tools.DotNet;
1819
using Nuke.Common.Tools.GitVersion;
20+
using Nuke.Common.Tools.NerdbankGitVersioning;
1921
using Nuke.Common.Tools.ReportGenerator;
2022
using Nuke.Common.Utilities;
2123
using Nuke.Common.Utilities.Collections;
@@ -76,9 +78,6 @@ internal partial class Build : NukeBuild
7678
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
7779
public readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
7880

79-
//[Parameter("Self contained application rids")]
80-
//public readonly string[] RIDs = [];
81-
8281
[Parameter("Coveralls token")]
8382
public readonly string? CoverallsToken = null;
8483

@@ -93,40 +92,14 @@ var nuke when nuke.TryGetValue("NUKE_RUN_ID", out var nukeid) => nukeid,
9392
[Solution(GenerateProjects = true)] public readonly Solution Solution = default!;
9493
[GitRepository] private readonly GitRepository _gitRepository = default!;
9594

96-
private GitVersion _gitVersion = default!;
95+
[NerdbankGitVersioning]
96+
private NerdbankGitVersioning _gitVersioning = default!;
9797

9898
private static AbsolutePath SourceDirectory => RootDirectory / "src";
9999
private static AbsolutePath TestDirectory => RootDirectory / "test";
100100
private static AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts";
101101
private Project[] TestProjects => [.. Solution.test.Projects];
102102

103-
//private const string MASTER_BRANCH = "master";
104-
private const string DEVELOP_BRANCH = "develop";
105-
106-
//private const string RELEASE_BRANCH_PREFIX = "release";
107-
//private const string HOTFIX_BRANCH_PREFIX = "hotfix";
108-
109-
protected override void OnBuildInitialized()
110-
{
111-
base.OnBuildInitialized();
112-
var resp = Git("rev-parse --is-shallow-repository");
113-
if (bool.TryParse(resp.FirstOrDefault().Text, out var isShallow) && isShallow)
114-
{
115-
Log.Information("Unshallowing the repository");
116-
_ = Git("fetch origin +refs/heads/*:refs/remotes/origin/* --unshallow --quiet");
117-
}
118-
119-
_ = Git("fetch --all --tags --quiet");
120-
Environment.SetEnvironmentVariable("IGNORE_NORMALISATION_GIT_HEAD_MOVE", "1");
121-
_gitVersion = GitVersionTasks
122-
.GitVersion(s => s
123-
.SetNoFetch(false)
124-
.SetNoCache(true)
125-
.DisableProcessOutputLogging()
126-
)
127-
.Result;
128-
}
129-
130103
public Target Clean => t => t
131104
.Before(Restore, Compile, Test, Publish)
132105
.Executes(() =>
@@ -168,10 +141,6 @@ protected override void OnBuildInitialized()
168141
.SetProjectFile(Solution)
169142
.SetNoRestore(InvokedTargets.Contains(Restore))
170143
.SetConfiguration(Configuration)
171-
//.SetProperty("Platform", "x64")
172-
.SetAssemblyVersion(_gitVersion.AssemblySemVer)
173-
.SetFileVersion(_gitVersion.AssemblySemFileVer)
174-
.SetInformationalVersion(_gitVersion.InformationalVersion)
175144
.EnableNoRestore()));
176145

177146
public Target Test => t => t
@@ -209,27 +178,6 @@ protected override void OnBuildInitialized()
209178
);
210179
}
211180
}
212-
213-
//return DotNetTest(t => t
214-
// .SetNoRestore(InvokedTargets.Contains(Restore))
215-
// .SetNoBuild(InvokedTargets.Contains(Compile))
216-
// .SetConfiguration(Configuration)
217-
// //.SetProperty("Platform", "x64")
218-
// .SetProperty("CollectCoverage", true)
219-
// .SetProperty("CoverletOutputFormat", "opencover")
220-
// //.SetProperty("ExcludeByFile", "*.Generated.cs")
221-
// .SetResultsDirectory(ArtifactsDirectory)
222-
// .CombineWith(TestProjects, (oo, testProj) => oo
223-
// .SetProjectFile(testProj)
224-
// .AddLoggers($"trx;LogFileName={testProj.Name}.trx")
225-
// .SetProperty("CoverletOutput", ArtifactsDirectory / $"{testProj.Name}.xml")),
226-
// /* For now the parallel execution seems to be broken due to new code coverage collection
227-
// * when running dotnet test.
228-
// * dotnet test seems to be trying to restore projects that are currently being tested in parallel,
229-
// * In future consider moving to dotnet test *.sln and let that handle parallelism
230-
// */
231-
// degreeOfParallelism: 1,//TestProjects.Length,
232-
// completeOnFailure: true);
233181
});
234182

235183
public Target Coverage => t => t
@@ -364,14 +312,8 @@ private void PublishApp(Project project, string? rid = null, bool zipProject = t
364312
.SetProject(
365313
v: project)
366314
.SetConfiguration(Configuration)
367-
//.SetProperty("Platform", platform)
368-
.SetAssemblyVersion(_gitVersion.AssemblySemVer)
369-
.SetFileVersion(_gitVersion.AssemblySemFileVer)
370-
.SetInformationalVersion(_gitVersion.InformationalVersion)
371315
.SetOutput(outputDir)
372316
.When(string.IsNullOrEmpty(rid), t => t
373-
// .SetNoRestore(InvokedTargets.Contains(Restore))
374-
// .SetNoBuild(InvokedTargets.Contains(Compile)))
375317
.SetNoRestore(false)
376318
.SetNoBuild(false))
377319
.When(!string.IsNullOrEmpty(rid), t => t
@@ -400,11 +342,72 @@ private void PublishApp(Project project, string? rid = null, bool zipProject = t
400342
}
401343
}
402344

345+
public Target GitFetch => t => t
346+
.Executes(() =>
347+
{
348+
Log.Information("Current branch {branchName}", GitCurrentBranch());
349+
350+
_ = Git("fetch --tags --verbose",
351+
logOutput: true,
352+
logInvocation: true)
353+
;
354+
});
355+
356+
public Target PrintVersion => t => t
357+
.TriggeredBy(Publish)
358+
.Executes(async () =>
359+
{
360+
Log.Information("Generated SemVer {semVer}", _gitVersioning.SemVer2);
361+
if (GitHubActions.Instance is not null)
362+
{
363+
await using var githubSummary = File.Open(
364+
EnvironmentInfo.GetVariable("GITHUB_STEP_SUMMARY"),
365+
FileMode.Append);
366+
await using var streamWriter = new StreamWriter(githubSummary);
367+
368+
await streamWriter.WriteLineAsync();
369+
await streamWriter.WriteLineAsync(
370+
"""
371+
# Version
372+
373+
Generated SemVer:
374+
375+
```json
376+
"""
377+
);
378+
379+
await streamWriter.FlushAsync();
380+
381+
await JsonSerializer.SerializeAsync(utf8Json: streamWriter.BaseStream, _gitVersioning, JsonSerializerOptions.Indented);
382+
await streamWriter.BaseStream.FlushAsync();
383+
384+
await streamWriter.WriteLineAsync();
385+
await streamWriter.WriteLineAsync("```");
386+
}
387+
else
388+
{
389+
Console.WriteLine(JsonSerializer.Serialize(_gitVersioning, JsonSerializerOptions.Indented));
390+
}
391+
});
392+
403393
public Target Full => t => t
404394
.DependsOn(
405395
Clean,
406396
Compile,
407397
Test,
408398
Publish)
409399
.Unlisted();
400+
}
401+
402+
static file class BuildExtensions
403+
{
404+
private static readonly JsonSerializerOptions _indented = new()
405+
{
406+
WriteIndented = true,
407+
};
408+
409+
extension(JsonSerializerOptions)
410+
{
411+
public static JsonSerializerOptions Indented => _indented;
412+
}
410413
}

build/Build.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</ItemGroup>
2222

2323
<ItemGroup>
24-
<PackageDownload Include="GitVersion.Tool" Version="[6.5.0]" />
24+
<PackageDownload Include="nbgv" Version="[3.9.50]" />
2525
</ItemGroup>
26-
26+
2727
</Project>

version.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
3+
"version": "0.1-alpha",
4+
"pathFilters": [
5+
"src/",
6+
"Directory.Packages.props",
7+
"Directory.Build.props"
8+
],
9+
"publicReleaseRefSpec": [
10+
"^refs/heads/main$",
11+
"^refs/heads/release/v\\d+(?:\\.\\d+)?$"
12+
],
13+
"release": {
14+
"branchName": "release/v{version}"
15+
},
16+
"nugetPackageVersion": {
17+
"semVer": 2,
18+
"precision": "build"
19+
}
20+
}

0 commit comments

Comments
 (0)