44using System . Diagnostics ;
55using System . IO . Compression ;
66using System . IO . Pipelines ;
7+ using System . Text . Json ;
78using Nuke . Common ;
89using Nuke . Common . CI . GitHubActions ;
910using Nuke . Common . Execution ;
1617using Nuke . Common . Tools . DotCover ;
1718using Nuke . Common . Tools . DotNet ;
1819using Nuke . Common . Tools . GitVersion ;
20+ using Nuke . Common . Tools . NerdbankGitVersioning ;
1921using Nuke . Common . Tools . ReportGenerator ;
2022using Nuke . Common . Utilities ;
2123using 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}
0 commit comments