From 833df1e3542b33ce1cb80ff01640b846b2883ed3 Mon Sep 17 00:00:00 2001 From: Mattias Karlsson Date: Sun, 22 Jun 2025 23:30:04 +0200 Subject: [PATCH] feat: Add support for multi-file Cake builds This commit introduces an example of a multi-file Cake build using Cake.Sdk. This approach allows for better organization of build logic by splitting it across multiple C# files. Key changes: - Added a multifile-build directory containing the new build script (uild.cs) and a BuildData.cs model. - Introduced a new GitHub Actions workflow to run the multi-file build. - Updated README.md with instructions, a build badge, and a diagram for the new build type. - Bumped the Cake.Sdk version in global.json. --- .github/workflows/Cake_Sdk-MultiFile.yml | 27 +++++++ .gitignore | 1 + README.md | 20 ++++- build.cs | 2 +- build/build.cs | 2 +- global.json | 2 +- multifile-build/build.cs | 97 ++++++++++++++++++++++++ multifile-build/build/BuildData.cs | 15 ++++ 8 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/Cake_Sdk-MultiFile.yml create mode 100644 multifile-build/build.cs create mode 100644 multifile-build/build/BuildData.cs diff --git a/.github/workflows/Cake_Sdk-MultiFile.yml b/.github/workflows/Cake_Sdk-MultiFile.yml new file mode 100644 index 0000000..ed87dc9 --- /dev/null +++ b/.github/workflows/Cake_Sdk-MultiFile.yml @@ -0,0 +1,27 @@ +name: Build using Cake.Sdk and Multi-File based Cake +on: + push: + branches: + - develop + - main + pull_request: +jobs: + build: + name: Build and Test + runs-on: ubuntu-latest + steps: + - name: Get the sources + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install .NET Core SDK (global.json) + uses: actions/setup-dotnet@v4 + with: + global-json-file: global.json + + - name: Run Cake File + uses: cake-build/cake-action@master + with: + file-path: multifile-build/build.cs + target: GitHubActions \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6edd9a4..e3ba009 100644 --- a/.gitignore +++ b/.gitignore @@ -335,6 +335,7 @@ __pycache__/ # Cake - Uncomment if you are using it tools/** +multifile-build/** # !tools/packages.config # Tabs Studio diff --git a/README.md b/README.md index ccb55e5..6beb5ff 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ [![Build using Cake.Sdk and File based Cake](https://github.com/cake-build/cakesdk-example/actions/workflows/Cake_Sdk-File.yml/badge.svg?branch=main)](https://github.com/cake-build/cakesdk-example/actions/workflows/Cake_Sdk-File.yml)   +[![Build using Cake.Sdk and Multi-File based Cake](https://github.com/cake-build/cakesdk-example/actions/workflows/Cake_Sdk-MultiFile.yml/badge.svg?branch=main)](https://github.com/cake-build/cakesdk-example/actions/workflows/Cake_Sdk-MultiFile.yml) +  [![Build using Cake.Sdk and Project based Cake](https://github.com/cake-build/cakesdk-example/actions/workflows/Cake_Sdk-Proj.yml/badge.svg)](https://github.com/cake-build/cakesdk-example/actions/workflows/Cake_Sdk-Proj.yml) @@ -19,16 +21,26 @@ This repository demonstrates minimal, modern usage of [Cake.Sdk](https://www.nug ``` / ├── build.cs # File-based Cake build script +| ├── build/ │ └── build.csproj # Project-based Cake build script using Cake.Sdk +| +├── multifile-build/ +│ ├── build.cs # Multi-file based Cake build script +│ └── build/ +│ └── BuildData.cs # BuildData model for Multi-file build script +| ├── src/ │ └── Example/ │ └── Example.csproj # Minimal .NET class library (net10.0) +| ├── global.json # Pins .NET SDK and Cake.Sdk versions ├── .github/ │ └── workflows/ │ ├── CakeFile.yml # GitHub Actions: file-based build -│ └── CakeProj.yml # GitHub Actions: project-based build +│ ├── CakeProj.yml # GitHub Actions: project-based build +│ └── Cake_Sdk-MultiFile.yml # GitHub Actions: multi-file-based build +| └── README.md # This file ``` @@ -44,11 +56,17 @@ This repository demonstrates minimal, modern usage of [Cake.Sdk](https://www.nug ```sh dotnet run --project build/build.csproj ``` +- **Multi-file-based build**: + Run with: + ```sh + dotnet cake multifile-build/build.cs + ``` ## Continuous Integration - **.github/workflows/CakeFile.yml**: Runs the file-based build script on push/PR. - **.github/workflows/CakeProj.yml**: Runs the project-based build script on push/PR. +- **.github/workflows/Cake_Sdk-MultiFile.yml**: Runs the multi-file-based build script on push/PR. Both workflows use the pinned .NET SDK and Cake.Sdk versions from `global.json`. diff --git a/build.cs b/build.cs index 02cbddc..3b524de 100644 --- a/build.cs +++ b/build.cs @@ -74,7 +74,7 @@ MSBuildSettings = data.MSBuildSettings, NoRestore = true, NoBuild = true, - OutputDirectory = "./artifacts" + OutputDirectory = data.ArtifactsDirectory }); }); diff --git a/build/build.cs b/build/build.cs index 42b78f7..2ccda4e 100644 --- a/build/build.cs +++ b/build/build.cs @@ -73,7 +73,7 @@ MSBuildSettings = data.MSBuildSettings, NoRestore = true, NoBuild = true, - OutputDirectory = "./artifacts" + OutputDirectory = data.ArtifactsDirectory }); }); diff --git a/global.json b/global.json index 37d3fe5..cf87fa5 100644 --- a/global.json +++ b/global.json @@ -3,6 +3,6 @@ "version": "10.0.100-preview.5.25277.114" }, "msbuild-sdks": { - "Cake.Sdk": "5.0.25167.12-alpha" + "Cake.Sdk": "5.0.25173.18-alpha" } } \ No newline at end of file diff --git a/multifile-build/build.cs b/multifile-build/build.cs new file mode 100644 index 0000000..716663d --- /dev/null +++ b/multifile-build/build.cs @@ -0,0 +1,97 @@ +#:sdk Cake.Sdk +#:property IncludeAdditionalFiles build/**/*.cs +#:property RunWorkingDirectory $(MSBuildProjectDirectory)/.. +#:package Cake.BuildSystems.Module@7.1.0 + +var target = Argument("target", "Pack"); + +////////////////////////////////////////////////////////////////////// +// TASKS +////////////////////////////////////////////////////////////////////// + +Setup(context => +{ + var configuration = Argument("configuration", "Release"); + + InstallTool("dotnet:https://api.nuget.org/v3/index.json?package=GitVersion.Tool&version=6.3.0"); + var version = GitVersion(); + + Information( + "Building Version: {0}, Configuration: {1}", + version.FullSemVer, + configuration); + + return new BuildData( + Version: version.FullSemVer, + Configuration: configuration, + SolutionFile: MakeAbsolute(File("./src/Example.sln")), + ArtifactsDirectory: MakeAbsolute(Directory("./artifacts")), + MSBuildSettings: new DotNetMSBuildSettings() + .SetVersion(version.FullSemVer) + .SetConfiguration(configuration) + .WithProperty("WarnAsError", "true")); +}); + +Task("Clean") + .WithCriteria(c => HasArgument("rebuild")) + .Does((ctx, data) => +{ + CleanDirectories(data.DirectoriesToClean); +}); + +Task("Build") + .IsDependentOn("Clean") + .Does((ctx, data) => +{ + DotNetBuild( + data.SolutionFile.FullPath, + new DotNetBuildSettings + { + MSBuildSettings = data.MSBuildSettings + }); +}); + +Task("Test") + .IsDependentOn("Build") + .Does((ctx, data) => +{ + DotNetTest( + data.SolutionFile.FullPath, + new DotNetTestSettings + { + MSBuildSettings = data.MSBuildSettings, + NoRestore = true, + NoBuild = true + }); +}); + +Task("Pack") + .IsDependentOn("Test") + .Does((ctx, data) => +{ + DotNetPack( + data.SolutionFile.FullPath, + new DotNetPackSettings + { + MSBuildSettings = data.MSBuildSettings, + NoRestore = true, + NoBuild = true, + OutputDirectory = data.ArtifactsDirectory + }); +}); + +Task("UploadArtifacts") + .IsDependentOn("Pack") + .Does((ctx, data) => + GitHubActions.Commands.UploadArtifact( + data.ArtifactsDirectory, + "ExampleArtifacts")); + +Task("GitHubActions") + .IsDependentOn("UploadArtifacts"); + +////////////////////////////////////////////////////////////////////// +// EXECUTION +////////////////////////////////////////////////////////////////////// + +RunTarget(target); \ No newline at end of file diff --git a/multifile-build/build/BuildData.cs b/multifile-build/build/BuildData.cs new file mode 100644 index 0000000..9c13fc7 --- /dev/null +++ b/multifile-build/build/BuildData.cs @@ -0,0 +1,15 @@ +public record BuildData( + string Version, + string Configuration, + FilePath SolutionFile, + DirectoryPath ArtifactsDirectory, + DotNetMSBuildSettings MSBuildSettings +) +{ + public DirectoryPath[] DirectoriesToClean { get; init; } = + [ + $"./src/Example/bin/{Configuration}", + $"./src/Example/obj/{Configuration}", + ArtifactsDirectory + ]; +} \ No newline at end of file