From 00a48ab2586eceee089f5987899fe0be750483e8 Mon Sep 17 00:00:00 2001 From: Mattias Karlsson Date: Wed, 17 Dec 2025 10:11:27 +0100 Subject: [PATCH] Migrate from Cake.Tool to Cake.Sdk - Replace Cake.Tool with Cake.Sdk@6.0.0 - Remove .config/dotnet-tools.json (no longer needed with SDK) - Rename build.cake to cake.cs and update to SDK syntax - Convert build/helpers.cake and build/records.cake to .cs files - Update GitHub Actions workflow to use file-path instead of cake-version - Use InstallTool() for GitVersion.Tool and DPI instead of #tool directive - Update GitHub Actions integration to use static GitHubActions access - Make environment variable properties nullable in BuildData record - Update package versions: Cake.Bridge.DependencyInjection, Devlead.Console, Microsoft.Extensions.Http --- .config/dotnet-tools.json | 20 --------- .github/workflows/build.yml | 2 +- build/helpers.cake | 56 ----------------------- build/helpers.cs | 61 +++++++++++++++++++++++++ build/{records.cake => records.cs} | 24 +++++----- build.cake => cake.cs | 72 ++++++++++++++---------------- src/ARI/ARI.csproj | 6 +-- 7 files changed, 111 insertions(+), 130 deletions(-) delete mode 100644 .config/dotnet-tools.json delete mode 100644 build/helpers.cake create mode 100644 build/helpers.cs rename build/{records.cake => records.cs} (78%) rename build.cake => cake.cs (80%) diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json deleted file mode 100644 index 800d86f..0000000 --- a/.config/dotnet-tools.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "version": 1, - "isRoot": true, - "tools": { - "cake.tool": { - "version": "6.0.0", - "commands": [ - "dotnet-cake" - ], - "rollForward": false - }, - "dpi": { - "version": "2025.11.25.337", - "commands": [ - "dpi" - ], - "rollForward": false - } - } -} \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5b773ba..22986c7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -60,7 +60,7 @@ jobs: AZURE_AUTHORITY_HOST: "https://login.microsoftonline.com" AZURE_DOMAIN: ${{ secrets.AZURE_DOMAIN }} with: - cake-version: tool-manifest + file-path: cake.cs target: GitHub-Actions - name: Setup Pages diff --git a/build/helpers.cake b/build/helpers.cake deleted file mode 100644 index a3c84f4..0000000 --- a/build/helpers.cake +++ /dev/null @@ -1,56 +0,0 @@ -#addin "nuget:?package=xunit.assert&version=2.9.3" -#load "records.cake" - -// Usings -using Xunit; - -/***************************** - * Helpers - *****************************/ - -private static ExtensionHelper extensionHelper; -extensionHelper = new ExtensionHelper(Task, () => RunTarget(Argument("target", "Default"))); -public static CakeTaskBuilder Then(this CakeTaskBuilder cakeTaskBuilder, string name) - => extensionHelper - .TaskCreate(name) - .IsDependentOn(cakeTaskBuilder); - - -public static CakeReport Run(this CakeTaskBuilder cakeTaskBuilder) - => extensionHelper.Run(); - -public static CakeTaskBuilder Default(this CakeTaskBuilder cakeTaskBuilder) -{ - extensionHelper - .TaskCreate("Default") - .IsDependentOn(cakeTaskBuilder); - return cakeTaskBuilder; -} - -if (BuildSystem.GitHubActions.IsRunningOnGitHubActions) -{ - TaskSetup(context=> System.Console.WriteLine($"::group::{context.Task.Name.Quote()}")); - TaskTeardown(context=>System.Console.WriteLine("::endgroup::")); -} - -public class FilePathJsonConverter : PathJsonConverter -{ - protected override FilePath ConvertFromString(string value) => FilePath.FromString(value); -} - -public abstract class PathJsonConverter : System.Text.Json.Serialization.JsonConverter where TPath : Cake.Core.IO.Path -{ - public override TPath Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options) - { - var value = reader.GetString(); - - return value is null ? null : ConvertFromString(value); - } - - public override void Write(System.Text.Json.Utf8JsonWriter writer, TPath value, System.Text.Json.JsonSerializerOptions options) - { - writer.WriteStringValue(value.FullPath); - } - - protected abstract TPath ConvertFromString(string value); -} \ No newline at end of file diff --git a/build/helpers.cs b/build/helpers.cs new file mode 100644 index 0000000..afb8923 --- /dev/null +++ b/build/helpers.cs @@ -0,0 +1,61 @@ +/***************************** + * Helpers + *****************************/ + +public partial class Program +{ + static void Main_SetupExtensions() + { + if (BuildSystem.GitHubActions.IsRunningOnGitHubActions) + { + TaskSetup(context => BuildSystem.GitHubActions.Commands.StartGroup(context.Task.Name)); + TaskTeardown(context => BuildSystem.GitHubActions.Commands.EndGroup()); + } + } +} + + +public static partial class CakeTaskBuilderExtensions +{ + private static ExtensionHelper extensionHelper = new (Task, () => RunTarget(Argument("target", "Default"))); + + public static CakeTaskBuilder Then(this CakeTaskBuilder cakeTaskBuilder, string name) + => extensionHelper + .TaskCreate(name) + .IsDependentOn(cakeTaskBuilder); + + + public static CakeReport Run(this CakeTaskBuilder cakeTaskBuilder) + => extensionHelper.Run(); + + public static CakeTaskBuilder Default(this CakeTaskBuilder cakeTaskBuilder) + { + extensionHelper + .TaskCreate("Default") + .IsDependentOn(cakeTaskBuilder); + return cakeTaskBuilder; + } + +} + +public class FilePathJsonConverter : PathJsonConverter +{ + protected override FilePath ConvertFromString(string value) => FilePath.FromString(value); +} + +public abstract class PathJsonConverter : System.Text.Json.Serialization.JsonConverter where TPath : Cake.Core.IO.Path +{ + public override TPath Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var value = reader.GetString(); + + return value is null ? null : ConvertFromString(value); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, TPath value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.FullPath); + } + + protected abstract TPath ConvertFromString(string value); +} \ No newline at end of file diff --git a/build/records.cake b/build/records.cs similarity index 78% rename from build/records.cake rename to build/records.cs index a7b6e4f..34740bf 100644 --- a/build/records.cake +++ b/build/records.cs @@ -1,4 +1,3 @@ -#load "helpers.cake" using System.Text.Json.Serialization; /***************************** @@ -25,15 +24,15 @@ DirectoryPath OutputPath public DirectoryPath StatiqWebPath { get; } = ArtifactsPath.Combine(Web); public DirectoryPath StatiqWebOutputPath { get; } = ArtifactsPath.Combine(Web).Combine(Output); - public string GitHubNuGetSource { get; } = System.Environment.GetEnvironmentVariable("GH_PACKAGES_NUGET_SOURCE"); - public string GitHubNuGetApiKey { get; } = System.Environment.GetEnvironmentVariable("GITHUB_TOKEN"); + public string? GitHubNuGetSource { get; } = System.Environment.GetEnvironmentVariable("GH_PACKAGES_NUGET_SOURCE"); + public string? GitHubNuGetApiKey { get; } = System.Environment.GetEnvironmentVariable("GITHUB_TOKEN"); public bool ShouldPushGitHubPackages() => !ShouldNotPublish && !string.IsNullOrWhiteSpace(GitHubNuGetSource) && !string.IsNullOrWhiteSpace(GitHubNuGetApiKey); - public string NuGetSource { get; } = System.Environment.GetEnvironmentVariable("NUGET_SOURCE"); - public string NuGetApiKey { get; } = System.Environment.GetEnvironmentVariable("NUGET_APIKEY"); + public string? NuGetSource { get; } = System.Environment.GetEnvironmentVariable("NUGET_SOURCE"); + public string? NuGetApiKey { get; } = System.Environment.GetEnvironmentVariable("NUGET_APIKEY"); public bool ShouldPushNuGetPackages() => IsMainBranch && !ShouldNotPublish && !string.IsNullOrWhiteSpace(NuGetSource) && @@ -54,8 +53,8 @@ public bool ShouldPushNuGetPackages() => IsMainBranch && System.Environment.GetEnvironmentVariable("AZURE_AUTHORITY_HOST") ); - public string AzureTenantId { get; } = System.Environment.GetEnvironmentVariable("AZURE_TENANT_ID"); - public string AzureDomain { get; } = System.Environment.GetEnvironmentVariable("AZURE_DOMAIN"); + public string? AzureTenantId { get; } = System.Environment.GetEnvironmentVariable("AZURE_TENANT_ID"); + public string? AzureDomain { get; } = System.Environment.GetEnvironmentVariable("AZURE_DOMAIN"); public bool ShouldRunIntegrationTests() => !string.IsNullOrWhiteSpace(AzureDomain) && ( @@ -65,10 +64,10 @@ public bool ShouldRunIntegrationTests() => !string.IsNullOrWhiteSpace(AzureDoma } public record AzureCredentials( - string TenantId, - string ClientId, - string ClientSecret, - string AuthorityHost = "login.microsoftonline.com" + string? TenantId, + string? ClientId, + string? ClientSecret, + string? AuthorityHost = "login.microsoftonline.com" ) { public bool AzureCredentialsSpecified { get; } = !string.IsNullOrWhiteSpace(TenantId) && @@ -76,4 +75,5 @@ public record AzureCredentials( !string.IsNullOrWhiteSpace(ClientSecret) && !string.IsNullOrWhiteSpace(AuthorityHost); } -private record ExtensionHelper(Func TaskCreate, Func Run); + +internal record ExtensionHelper(Func TaskCreate, Func Run); diff --git a/build.cake b/cake.cs similarity index 80% rename from build.cake rename to cake.cs index 078363a..e739a81 100644 --- a/build.cake +++ b/cake.cs @@ -1,13 +1,14 @@ -#tool dotnet:?package=GitVersion.Tool&version=6.5.1 -#load "build/records.cake" -#load "build/helpers.cake" +#:sdk Cake.Sdk@6.0.0 +#:property IncludeAdditionalFiles=./build/*.cs /***************************** * Setup *****************************/ Setup( static context => { - var assertedVersions = context.GitVersion(new GitVersionSettings + InstallTool("dotnet:https://api.nuget.org/v3/index.json?package=GitVersion.Tool&version=6.5.1"); + InstallTool("dotnet:https://api.nuget.org/v3/index.json?package=DPI&version=2025.12.17.349"); + var assertedVersions = context.GitVersion(new GitVersionSettings { OutputType = GitVersionOutput.Json }); @@ -15,10 +16,9 @@ var branchName = assertedVersions.BranchName; var isMainBranch = StringComparer.OrdinalIgnoreCase.Equals("main", branchName); - var gh = context.GitHubActions(); var buildDate = DateTime.UtcNow; - var runNumber = gh.IsRunningOnGitHubActions - ? gh.Environment.Workflow.RunNumber + var runNumber = GitHubActions.IsRunningOnGitHubActions + ? GitHubActions.Environment.Workflow.RunNumber : (short)((buildDate - buildDate.Date).TotalSeconds/3); var version = FormattableString @@ -41,7 +41,7 @@ version, isMainBranch, !context.IsRunningOnWindows(), - context.BuildSystem().IsLocalBuild, + BuildSystem.IsLocalBuild, projectRoot, projectPath, new DotNetMSBuildSettings() @@ -54,7 +54,7 @@ .WithProperty("PackageTags", "tool;bicep;acr;azure") .WithProperty("PackageDescription", "Azure Resource Inventory .NET Tool - Inventories and documents Azure Tenant resources") .WithProperty("RepositoryUrl", "https://github.com/devlead/ARI.git") - .WithProperty("ContinuousIntegrationBuild", gh.IsRunningOnGitHubActions ? "true" : "false") + .WithProperty("ContinuousIntegrationBuild", GitHubActions.IsRunningOnGitHubActions ? "true" : "false") .WithProperty("EmbedUntrackedSources", "true"), artifactsPath, artifactsPath.Combine(version) @@ -80,27 +80,25 @@ ) .Then("DPI") .Does( - static (context, data) => context.DotNetTool( - "tool", - new DotNetToolSettings { - ArgumentCustomization = args => args - .Append("run") - .Append("dpi") - .Append("nuget") - .Append("--silent") - .AppendSwitchQuoted("--output", "table") - .Append( - ( - !string.IsNullOrWhiteSpace(context.EnvironmentVariable("NuGetReportSettings_SharedKey")) - && - !string.IsNullOrWhiteSpace(context.EnvironmentVariable("NuGetReportSettings_WorkspaceId")) - ) - ? "report" - : "analyze" - ) - .AppendSwitchQuoted("--buildversion", data.Version) - } - ) + static (context, data) => { + Command( + ["dpi", "dpi.exe"], + new ProcessArgumentBuilder() + .Append("nuget") + .Append("--silent") + .AppendSwitchQuoted("--output", "table") + .Append( + ( + !string.IsNullOrWhiteSpace(context.EnvironmentVariable("NuGetReportSettings_SharedKey")) + && + !string.IsNullOrWhiteSpace(context.EnvironmentVariable("NuGetReportSettings_WorkspaceId")) + ) + ? "report" + : "analyze" + ) + .AppendSwitchQuoted("--buildversion", data.Version) + ); + } ) .Then("Build") .Does( @@ -138,11 +136,9 @@ .Then("Upload-Artifacts") .WithCriteria(BuildSystem.IsRunningOnGitHubActions, nameof(BuildSystem.IsRunningOnGitHubActions)) .Does( - static (context, data) => context - .GitHubActions() is var gh && gh != null - ? gh.Commands - .UploadArtifact(data.ArtifactsPath, $"Artifact_{gh.Environment.Runner.ImageOS ?? gh.Environment.Runner.OS}_{context.Environment.Runtime.BuiltFramework.Identifier}_{context.Environment.Runtime.BuiltFramework.Version}") - : throw new Exception("GitHubActions not available") + static (context, data) => GitHubActions + .Commands + .UploadArtifact(data.ArtifactsPath, $"Artifact_{GitHubActions.Environment.Runner.ImageOS ?? GitHubActions.Environment.Runner.OS}_{context.Environment.Runtime.BuiltFramework.Identifier}_{context.Environment.Runtime.BuiltFramework.Version}") ) .Then("Integration-Tests-Tool-Manifest") .Does( @@ -192,9 +188,9 @@ .WithCriteria(BuildSystem.IsRunningOnGitHubActions, nameof(BuildSystem.IsRunningOnGitHubActions)) .WithCriteria((context, data) => data.ShouldRunIntegrationTests(), "ShouldRunIntegrationTests") .Does( - async (context, data) => { + static (context, data) => { var resultPath = data.IntegrationTestPath; - await GitHubActions.Commands.UploadArtifact( + GitHubActions.Commands.UploadArtifact( resultPath, $"{data.AzureDomain}_{GitHubActions.Environment.Runner.ImageOS ?? GitHubActions.Environment.Runner.OS}_{context.Environment.Runtime.BuiltFramework.Identifier}_{context.Environment.Runtime.BuiltFramework.Version}" ); @@ -206,7 +202,7 @@ await GitHubActions.Commands.UploadArtifact( .SelectMany(line => line) ) ); - } + } ) .Then("Integration-Tests") .Then("Generate-Statiq-Web") diff --git a/src/ARI/ARI.csproj b/src/ARI/ARI.csproj index f51bd2c..75153da 100644 --- a/src/ARI/ARI.csproj +++ b/src/ARI/ARI.csproj @@ -42,11 +42,11 @@ - + - + - +