From 5194fbe87b978608c4bbfb945e6464a1208f4b89 Mon Sep 17 00:00:00 2001 From: Mattias Karlsson Date: Fri, 19 Dec 2025 16:21:05 +0100 Subject: [PATCH] Migrate build scripts from Cake script to Cake C# SDK - Convert build.cake to cake.cs using Cake.Sdk@6.0.0 - Replace dotnet-tools.json with inline tool installation via InstallTool - Convert build/helpers.cake to build/helpers.cs (removed xunit.assert and JSON converters) - Convert build/records.cake to build/records.cs (nullable strings, internal visibility) - Update GitHub Actions workflow: - Add .NET SDK 9.0.x installation - Use file-path instead of tool-manifest - Simplify version calculation (remove GitVersion dependency) - Modernize Cake API usage (direct GitHubActions/BuildSystem access, Command API) --- .config/dotnet-tools.json | 20 ------- .github/workflows/build.yml | 5 +- build/helpers.cake | 56 -------------------- build/helpers.cs | 39 ++++++++++++++ build/{records.cake => records.cs} | 23 ++++---- build.cake => cake.cs | 85 +++++++++++++++--------------- 6 files changed, 95 insertions(+), 133 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} (76%) rename build.cake => cake.cs (74%) diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json deleted file mode 100644 index 83ee37d..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.12.17.349", - "commands": [ - "dpi" - ], - "rollForward": false - } - } -} \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d4df31c..b7ca840 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,11 +26,12 @@ jobs: with: fetch-depth: 0 - - name: Install .NET SDK 8.0.x + - name: Install .NET SDK 8.0.x - 9.0.x uses: actions/setup-dotnet@v5 with: dotnet-version: | 8.0.x + 9.0.x - name: Install .NET SDK uses: actions/setup-dotnet@v5 @@ -60,5 +61,5 @@ jobs: AZURE_STORAGE_ACCOUNT: ${{ secrets.AZURE_STORAGE_ACCOUNT }} AZURE_STORAGE_ACCOUNT_CONTAINER: ${{ secrets.AZURE_STORAGE_ACCOUNT_CONTAINER }}${{ matrix.os }} with: - cake-version: tool-manifest + file-path: cake.cs target: GitHub-Actions 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..6029c27 --- /dev/null +++ b/build/helpers.cs @@ -0,0 +1,39 @@ +/***************************** + * 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; + } + +} \ No newline at end of file diff --git a/build/records.cake b/build/records.cs similarity index 76% rename from build/records.cake rename to build/records.cs index 51554d7..0792069 100644 --- a/build/records.cake +++ b/build/records.cs @@ -1,4 +1,3 @@ -#load "helpers.cake" using System.Text.Json.Serialization; /***************************** @@ -22,15 +21,15 @@ DirectoryPath OutputPath public DirectoryPath BinaryOutputPath { get; } = OutputPath.Combine("bin"); public DirectoryPath IntegrationTestPath { get; } = OutputPath.Combine(IntegrationTest); - 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) && @@ -49,9 +48,9 @@ public bool ShouldPushNuGetPackages() => IsMainBranch && System.Environment.GetEnvironmentVariable("AZURE_AUTHORITY_HOST") ); - public string AzureStorageAccount { get; } = System.Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT"); + public string? AzureStorageAccount { get; } = System.Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT"); - public string AzureStorageAccountContainer { get; } = System.Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT_CONTAINER"); + public string? AzureStorageAccountContainer { get; } = System.Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT_CONTAINER"); public bool ShouldRunIntegrationTests() => !string.IsNullOrWhiteSpace(AzureStorageAccount) && !string.IsNullOrWhiteSpace(AzureStorageAccountContainer) && @@ -62,10 +61,10 @@ public bool ShouldRunIntegrationTests() => !string.IsNullOrWhiteSpace(AzureStor } 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) && @@ -73,4 +72,4 @@ 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 74% rename from build.cake rename to cake.cs index a194d04..1dc55d6 100644 --- a/build.cake +++ b/cake.cs @@ -1,28 +1,28 @@ -#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 - { - OutputType = GitVersionOutput.Json - }); - - var branchName = assertedVersions.BranchName; - var isMainBranch = StringComparer.OrdinalIgnoreCase.Equals("main", branchName); - - var gh = context.GitHubActions(); + 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 buildDate = DateTime.UtcNow; - var runNumber = gh.IsRunningOnGitHubActions - ? gh.Environment.Workflow.RunNumber - : (short)((buildDate - buildDate.Date).TotalSeconds/3); + var runNumber = GitHubActions.IsRunningOnGitHubActions + ? GitHubActions.Environment.Workflow.RunNumber + : 0; + + var suffix = runNumber == 0 + ? $"-{(short)((buildDate - buildDate.Date).TotalSeconds/3)}" + : string.Empty; var version = FormattableString - .Invariant($"{buildDate:yyyy.M.d}.{runNumber}"); + .Invariant($"{buildDate:yyyy.M.d}.{runNumber}{suffix}"); + + var branchName = GitHubActions.Environment.Workflow.RefName; + var isMainBranch = StringComparer.OrdinalIgnoreCase.Equals("main", branchName); context.Information("Building version {0} (Branch: {1}, IsMain: {2})", version, @@ -41,7 +41,7 @@ version, isMainBranch, !context.IsRunningOnWindows(), - context.BuildSystem().IsLocalBuild, + BuildSystem.IsLocalBuild, projectRoot, projectPath, new DotNetMSBuildSettings() @@ -54,7 +54,7 @@ .WithProperty("PackageTags", "tool;blob;storage;azure") .WithProperty("PackageDescription", ".NET Tool that archives local files to Azure Blob Storage") .WithProperty("RepositoryUrl", "https://github.com/devlead/Blobify.git") - .WithProperty("ContinuousIntegrationBuild", gh.IsRunningOnGitHubActions ? "true" : "false") + .WithProperty("ContinuousIntegrationBuild", GitHubActions.IsRunningOnGitHubActions ? "true" : "false") .WithProperty("EmbedUntrackedSources", "true"), artifactsPath, artifactsPath.Combine(version) @@ -80,26 +80,23 @@ ) .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") @@ -138,11 +135,13 @@ .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(