feat: add Cake build to produce powershell module (#2)

- Add Cake build project
- Change to CPM
- Package bumps for security bumps
- Reorganise solution structure to group harness projects
This commit is contained in:
Scott 2026-07-31 02:15:16 +00:00
commit ecb0a20cbb
27 changed files with 267 additions and 58 deletions

13
build/Build.csproj Normal file
View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Cake.Frosting"/>
<PackageReference Include="Cake.Powershell"/>
<PackageReference Include="System.IO.Packaging" />
<PackageReference Include="System.Security.Cryptography.Xml" />
</ItemGroup>
</Project>

55
build/Program.cs Normal file
View file

@ -0,0 +1,55 @@
using Cake.Common;
using Cake.Common.IO;
using Cake.Common.IO.Paths;
using Cake.Core;
using Cake.Frosting;
using Cake.Powershell;
public static class Program
{
public static int Main(string[] args)
{
return new CakeHost()
.UseContext<BuildContext>()
.Run(args);
}
}
public class BuildContext : FrostingContext
{
/// <summary>
/// Base source directory
/// </summary>
public ConvertableDirectoryPath BaseSourceLocation { get; set; }
/// <summary>
/// Powershell module project folder
/// </summary>
public ConvertableDirectoryPath PowershellModuleProjectDirectory { get; set; }
/// <summary>
/// Powershell module csproj location
/// </summary>
public ConvertableFilePath PowershellModuleCsproj { get; set; }
/// <summary>
/// Output directory for built DLLs and powershell module manifest files
/// </summary>
public ConvertableDirectoryPath PowershellModuleOutputDir { get; set; }
/// <summary>
/// Path to powershell script that creates the module manifest files
/// </summary>
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");
}
}

View file

@ -0,0 +1,24 @@
param (
[string]$path,
[string]$guid,
[string]$author,
[string[]]$nestedModules,
[string]$rootModule,
[string[]]$cmdletsToExport,
[string]$manifestFileLocation
)
$manifestSplat = @{
Path = "$path"
GUID = "$guid"
Author = "$author"
NestedModules = @($nestedModules)
RootModule = "$rootModule"
CmdletsToExport = @($cmdletsToExport)
FunctionsToExport = @()
VariablesToExport = @()
AliasesToExport = @()
}
New-ModuleManifest @manifestSplat
New-Item "$manifestFileLocation" -ItemType File

36
build/Tasks/BuildTask.cs Normal file
View file

@ -0,0 +1,36 @@
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Build;
using Cake.Common.Tools.DotNet.MSBuild;
using Cake.Core.Diagnostics;
using Cake.Frosting;
namespace Build.Tasks;
[TaskName("Build")]
[IsDependentOn(typeof(CleanTask))]
[IsDependeeOf(typeof(CopyOutputTask))]
public class BuildTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.Log.Information($"Building: {context.PowershellModuleCsproj}");
var buildSettings = new DotNetBuildSettings()
{
MSBuildSettings = new DotNetMSBuildSettings()
{
VersionSuffix = "cake"
},
OutputDirectory = context.PowershellModuleOutputDir,
DiagnosticOutput = true
};
// /p:DebugSymbols=false
buildSettings.MSBuildSettings.Properties.Add("DebugSymbols", ["false"]);
// /p:DebugType=None
buildSettings.MSBuildSettings.Properties.Add("DebugType", ["None"]);
context.DotNetBuild(context.PowershellModuleCsproj, buildSettings);
base.Run(context);
}
}

23
build/Tasks/CleanTask.cs Normal file
View file

@ -0,0 +1,23 @@
using Cake.Common;
using Cake.Common.Tools.DotNet;
using Cake.Core.Diagnostics;
using Cake.Frosting;
namespace Build.Tasks;
[TaskName("Clean")]
public class CleanTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.DotNetClean(context.PowershellModuleProjectDirectory);
var outDir = context.FileSystem.GetDirectory(context.PowershellModuleOutputDir);
if (outDir.Exists)
{
outDir.Delete(true);
}
base.Run(context);
}
}

View file

@ -0,0 +1,49 @@
using System;
using System.Linq;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Frosting;
using Cake.Powershell;
namespace Build.Tasks;
[TaskName("CopyOutput")]
public class CopyOutputTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var powershellModuleName = "PowershellModule";
var scriptParams = new
{
Path = $"{context.PowershellModuleOutputDir}/{powershellModuleName}.psd1",
Guid = Guid.Parse("5cdf4635-edb0-428c-8d9b-92d0bcd47443"),
Author = "Me",
NestedModules = new[] { $"{powershellModuleName}.dll" },
RootModule = $"{powershellModuleName}.psm1",
CmdletsToExport = new[] { "Get-Calendar" },
ManifestFileLocation = $"{context.PowershellModuleOutputDir}/{powershellModuleName}.psm1"
};
var psSettings = new PowershellSettings()
{
Arguments = new ProcessArgumentBuilder(),
};
// This feels a bit ugly/redundant seeing as I've defined the scriptParams above, but I'm leaving it as is
// until I want to come back and clean this up properly
psSettings.Arguments.Append("path", ToPowershellSafeString(scriptParams.Path));
psSettings.Arguments.Append("guid", ToPowershellSafeString(scriptParams.Guid.ToString()));
psSettings.Arguments.Append("author", ToPowershellSafeString(scriptParams.Author));
psSettings.Arguments.Append("nestedModules", $"@({string.Join(",", scriptParams.NestedModules.Select(ToPowershellSafeString))})");
psSettings.Arguments.Append("rootModule", ToPowershellSafeString(scriptParams.RootModule));
psSettings.Arguments.Append("cmdletsToExport", $"@({string.Join(",", scriptParams.CmdletsToExport.Select(ToPowershellSafeString))})");
psSettings.Arguments.Append("manifestFileLocation", ToPowershellSafeString(scriptParams.ManifestFileLocation));
context.StartPowershellFile(context.CreateModuleManifestScript, psSettings);
context.Log.Information($"Module output to: {context.PowershellModuleOutputDir.Path.MakeAbsolute(context.Environment)}");
base.Run(context);
}
private static string ToPowershellSafeString(string unescapedString) => $"'{unescapedString}'";
}

View file

@ -0,0 +1,16 @@
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Frosting;
namespace Build.Tasks;
// Consider this the "entry" point for builds, task order is defined by a chain of IsDependentOn
[TaskName("Default")]
[IsDependentOn(typeof(CopyOutputTask))]
public class DefaultTask : FrostingTask
{
public override void Run(ICakeContext context)
{
base.Run(context);
}
}