From fa0531f2c5ee981f227a41efd3525a423925c5ef Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 4 Sep 2026 10:46:34 +1000 Subject: [PATCH] refactor(build): move BuildContext to file, add BuildSuffix and CommitHash configuration options --- build/BuildContext.cs | 50 ++++++++++++++++++++++++++++++++++++++++ build/Program.cs | 48 ++++---------------------------------- build/Tasks/BuildTask.cs | 23 ++++++++++++++---- 3 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 build/BuildContext.cs diff --git a/build/BuildContext.cs b/build/BuildContext.cs new file mode 100644 index 0000000..29634e9 --- /dev/null +++ b/build/BuildContext.cs @@ -0,0 +1,50 @@ +using Cake.Common.IO; +using Cake.Common.IO.Paths; +using Cake.Core; +using Cake.Frosting; + +namespace Build; + +public class BuildContext : FrostingContext +{ + /// + /// Base source directory + /// + public ConvertableDirectoryPath BaseSourceLocation { get; set; } + + /// + /// Powershell module project folder + /// + public ConvertableDirectoryPath PowershellModuleProjectDirectory { get; set; } + + /// + /// Powershell module csproj location + /// + public ConvertableFilePath PowershellModuleCsproj { get; set; } + + /// + /// Output directory for built DLLs and powershell module manifest files + /// + public ConvertableDirectoryPath PowershellModuleOutputDir { get; set; } + + /// + /// Path to powershell script that creates the module manifest files + /// + public ConvertableFilePath CreateModuleManifestScript { get; set; } + + public string BuildSuffix { get; set; } + public bool DisableCommitHash { get; set; } + + public BuildContext(ICakeContext context) + : base(context) + { + BaseSourceLocation = context.Directory("../src"); + PowershellModuleProjectDirectory = BaseSourceLocation + context.Directory("PowershellModule"); + PowershellModuleCsproj = PowershellModuleProjectDirectory + context.File("PowershellModule.csproj"); + PowershellModuleOutputDir = context.Directory("../") + context.Directory("output") + context.Directory("PowershellModule"); + var buildScriptDirectory = context.Directory("./Scripts"); + CreateModuleManifestScript = buildScriptDirectory + context.File("CreateModuleManifest.ps1"); + BuildSuffix = context.Configuration.GetValue(nameof(BuildSuffix)); + DisableCommitHash = context.Configuration.GetBoolValue(nameof(DisableCommitHash)); + } +} \ No newline at end of file diff --git a/build/Program.cs b/build/Program.cs index ffb3738..1c348c5 100644 --- a/build/Program.cs +++ b/build/Program.cs @@ -1,9 +1,6 @@ -using Cake.Common; -using Cake.Common.IO; -using Cake.Common.IO.Paths; -using Cake.Core; using Cake.Frosting; -using Cake.Powershell; + +namespace Build; public static class Program { @@ -11,45 +8,8 @@ public static class Program { return new CakeHost() .UseContext() + // Uncomment this if you don't want to set the suffix via the run profile program arguments + //.UseCakeSetting(nameof(BuildContext.BuildSuffix), "") .Run(args); } -} - -public class BuildContext : FrostingContext -{ - /// - /// Base source directory - /// - public ConvertableDirectoryPath BaseSourceLocation { get; set; } - - /// - /// Powershell module project folder - /// - public ConvertableDirectoryPath PowershellModuleProjectDirectory { get; set; } - - /// - /// Powershell module csproj location - /// - public ConvertableFilePath PowershellModuleCsproj { get; set; } - - /// - /// Output directory for built DLLs and powershell module manifest files - /// - public ConvertableDirectoryPath PowershellModuleOutputDir { get; set; } - - /// - /// Path to powershell script that creates the module manifest files - /// - public ConvertableFilePath CreateModuleManifestScript { get; set; } - - public BuildContext(ICakeContext context) - : base(context) - { - BaseSourceLocation = context.Directory("../src"); - PowershellModuleProjectDirectory = BaseSourceLocation + context.Directory("PowershellModule"); - PowershellModuleCsproj = PowershellModuleProjectDirectory + context.File("PowershellModule.csproj"); - PowershellModuleOutputDir = context.Directory("../") + context.Directory("output") + context.Directory("PowershellModule"); - var buildScriptDirectory = context.Directory("./Scripts"); - CreateModuleManifestScript = buildScriptDirectory + context.File("CreateModuleManifest.ps1"); - } } \ No newline at end of file diff --git a/build/Tasks/BuildTask.cs b/build/Tasks/BuildTask.cs index c731bcb..d11cc79 100644 --- a/build/Tasks/BuildTask.cs +++ b/build/Tasks/BuildTask.cs @@ -17,14 +17,27 @@ public class BuildTask : FrostingTask var buildSettings = new DotNetBuildSettings() { - MSBuildSettings = new DotNetMSBuildSettings() - { - VersionSuffix = "cake" - }, + MSBuildSettings = new DotNetMSBuildSettings(), OutputDirectory = context.PowershellModuleOutputDir, - DiagnosticOutput = true + DiagnosticOutput = true, }; + // If a build suffix is provided, use it, otherwise whatever comes from Directory.Build.props will be used. + // It's not possible to set the actual version number for a build in this project as that is purely controlled + // from individual Directory.Build.props files + if (!string.IsNullOrEmpty(context.BuildSuffix)) + { + buildSettings.MSBuildSettings.VersionSuffix = context.BuildSuffix; + } + + // Disable SourceLink automatic commit hash addition. Documentation about how that package work is garbage and + // this property is only mentioned in breaking changes https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/8.0/source-link + // which is pretty on par for shit like this. + if (context.DisableCommitHash) + { + buildSettings.MSBuildSettings.Properties.Add("IncludeSourceRevisionInInformationalVersion", ["false"]); + } + // /p:DebugSymbols=false buildSettings.MSBuildSettings.Properties.Add("DebugSymbols", ["false"]); // /p:DebugType=None