Skip to content

Commit 5a2d4cb

Browse files
authored
feat: Add advanced multi-file Cake build example with dependency injection (#6)
- Add multifile-build-advanced/ directory with organized structure - Implement service pattern with IMyService/MyService for GitVersion logic - Add dependency injection configuration in IoC.cs - Organize files into Models/ and Services/ subdirectories - Add PreClean task with IsDependeeOf relationship - Update BuildData model to include Rebuild property - Add GitHub Actions workflow for advanced multi-file build - Update README.md with comprehensive documentation This example demonstrates advanced Cake build patterns including: - Multi-file organization with proper separation of concerns - Dependency injection and service registration - Partial methods for extensibility - Advanced task dependency management - Clean architecture with Models and Services folders
1 parent d7b25f3 commit 5a2d4cb

File tree

8 files changed

+204
-4
lines changed

8 files changed

+204
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Build using Cake.Sdk and Multi-File based Cake (Advanced)
2+
on:
3+
push:
4+
branches:
5+
- develop
6+
- main
7+
pull_request:
8+
jobs:
9+
build:
10+
name: Build and Test
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Get the sources
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
18+
- name: Install .NET Core SDK (global.json)
19+
uses: actions/setup-dotnet@v4
20+
with:
21+
global-json-file: global.json
22+
23+
- name: Run Cake File
24+
uses: cake-build/cake-action@master
25+
with:
26+
file-path: multifile-build-advanced/build.cs
27+
target: GitHubActions

README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
 
33
[![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)
44
 
5+
[![Build using Cake.Sdk and Multi-File Advanced Cake](https://github.com/cake-build/cakesdk-example/actions/workflows/Cake_Sdk-MultiFile-Advanced.yml/badge.svg?branch=main)](https://github.com/cake-build/cakesdk-example/actions/workflows/Cake_Sdk-MultiFile-Advanced.yml)
6+
 
57
[![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)
68

79

810
# Cake.Sdk Example Repository
911

10-
This repository demonstrates minimal, modern usage of [Cake.Sdk](https://www.nuget.org/packages/Cake.Sdk/) for .NET build automation. It showcases both file-based and project-based approaches for defining Cake build scripts, and includes a minimal .NET class library.
12+
This repository demonstrates minimal, modern usage of [Cake.Sdk](https://www.nuget.org/packages/Cake.Sdk/) for .NET build automation. It showcases file-based, multi-file-based, and project-based approaches for defining Cake build scripts, and includes a minimal .NET class library.
1113

1214
## Features
1315

1416
- **File-based build script**: Standalone `build.cs` using Cake Sdk directives.
17+
- **Multi-file-based build script**: `multifile-build/build.cs` with additional files in build folder.
18+
- **Advanced multi-file-based build script**: `multifile-build-advanced/build.cs` with organized structure, dependency injection, and service patterns.
1519
- **Project-based build script**: `build/build.csproj` referencing Cake.Sdk.
1620
- **Pinned versions**: .NET SDK and Cake.Sdk versions are pinned via `global.json`.
17-
- **CI examples**: Example GitHub Actions workflows for both approaches.
21+
- **CI examples**: Example GitHub Actions workflows for all approaches.
1822

1923
## Repository Structure
2024

@@ -30,6 +34,17 @@ This repository demonstrates minimal, modern usage of [Cake.Sdk](https://www.nug
3034
│ └── build/
3135
│ └── BuildData.cs # BuildData model for Multi-file build script
3236
|
37+
├── multifile-build-advanced/
38+
│ ├── build.cs # Advanced multi-file based Cake build script
39+
│ └── build/
40+
│ ├── Models/
41+
│ │ └── BuildData.cs # BuildData model with Rebuild property
42+
│ ├── Services/
43+
│ │ ├── IMyService.cs # Service interface
44+
│ │ └── MyService.cs # Service implementation with GitVersion logic
45+
│ ├── IoC.cs # Dependency injection configuration
46+
│ └── Task.cs # Additional task definitions
47+
|
3348
├── src/
3449
│ └── Example/
3550
│ └── Example.csproj # Minimal .NET class library (net10.0)
@@ -39,7 +54,8 @@ This repository demonstrates minimal, modern usage of [Cake.Sdk](https://www.nug
3954
│ └── workflows/
4055
│ ├── CakeFile.yml # GitHub Actions: file-based build
4156
│ ├── CakeProj.yml # GitHub Actions: project-based build
42-
│ └── Cake_Sdk-MultiFile.yml # GitHub Actions: multi-file-based build
57+
│ ├── Cake_Sdk-MultiFile.yml # GitHub Actions: multi-file-based build
58+
│ └── Cake_Sdk-MultiFile-Advanced.yml # GitHub Actions: advanced multi-file-based build
4359
|
4460
└── README.md # This file
4561
```
@@ -61,14 +77,20 @@ This repository demonstrates minimal, modern usage of [Cake.Sdk](https://www.nug
6177
```sh
6278
dotnet cake multifile-build/build.cs
6379
```
80+
- **Advanced multi-file-based build**:
81+
Run with:
82+
```sh
83+
dotnet cake multifile-build-advanced/build.cs
84+
```
6485

6586
## Continuous Integration
6687

6788
- **.github/workflows/CakeFile.yml**: Runs the file-based build script on push/PR.
6889
- **.github/workflows/CakeProj.yml**: Runs the project-based build script on push/PR.
6990
- **.github/workflows/Cake_Sdk-MultiFile.yml**: Runs the multi-file-based build script on push/PR.
91+
- **.github/workflows/Cake_Sdk-MultiFile-Advanced.yml**: Runs the advanced multi-file-based build script on push/PR.
7092

71-
Both workflows use the pinned .NET SDK and Cake.Sdk versions from `global.json`.
93+
All workflows use the pinned .NET SDK and Cake.Sdk versions from `global.json`.
7294

7395
## About Cake.Sdk
7496

multifile-build-advanced/build.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#:sdk Cake.Sdk
2+
#:property IncludeAdditionalFiles=build/**/*.cs
3+
#:property RunWorkingDirectory=$(MSBuildProjectDirectory)/..
4+
#:package Cake.BuildSystems.Module@8.0.0
5+
6+
var target = Argument("target", "Pack");
7+
8+
//////////////////////////////////////////////////////////////////////
9+
// TASKS
10+
//////////////////////////////////////////////////////////////////////
11+
12+
Setup(context =>
13+
{
14+
var configuration = Argument("configuration", "Release");
15+
16+
var myService = ServiceProvider.GetRequiredService<IMyService>();
17+
var version = myService.GetVersion();
18+
19+
Information(
20+
"Building Version: {0}, Configuration: {1}",
21+
version,
22+
configuration);
23+
24+
return new BuildData(
25+
Version: version,
26+
Configuration: configuration,
27+
SolutionFile: MakeAbsolute(File("./src/Example.sln")),
28+
ArtifactsDirectory: MakeAbsolute(Directory("./artifacts")),
29+
MSBuildSettings: new DotNetMSBuildSettings()
30+
.SetVersion(version)
31+
.SetConfiguration(configuration)
32+
.WithProperty("TreatWarningsAsErrors", "true"),
33+
Rebuild: HasArgument("rebuild"));
34+
});
35+
36+
Task("Clean")
37+
.WithCriteria<BuildData>(c => c.Rebuild, nameof(BuildData.Rebuild))
38+
.Does<BuildData>((ctx, data) =>
39+
{
40+
CleanDirectories(data.DirectoriesToClean);
41+
});
42+
43+
Task("Build")
44+
.IsDependentOn("Clean")
45+
.Does<BuildData>((ctx, data) =>
46+
{
47+
DotNetBuild(
48+
data.SolutionFile.FullPath,
49+
new DotNetBuildSettings
50+
{
51+
MSBuildSettings = data.MSBuildSettings
52+
});
53+
});
54+
55+
Task("Test")
56+
.IsDependentOn("Build")
57+
.Does<BuildData>((ctx, data) =>
58+
{
59+
DotNetTest(
60+
data.SolutionFile.FullPath,
61+
new DotNetTestSettings
62+
{
63+
MSBuildSettings = data.MSBuildSettings,
64+
NoRestore = true,
65+
NoBuild = true
66+
});
67+
});
68+
69+
Task("Pack")
70+
.IsDependentOn("Test")
71+
.Does<BuildData>((ctx, data) =>
72+
{
73+
DotNetPack(
74+
data.SolutionFile.FullPath,
75+
new DotNetPackSettings
76+
{
77+
MSBuildSettings = data.MSBuildSettings,
78+
NoRestore = true,
79+
NoBuild = true,
80+
OutputDirectory = data.ArtifactsDirectory
81+
});
82+
});
83+
84+
Task("UploadArtifacts")
85+
.IsDependentOn("Pack")
86+
.Does<BuildData>((ctx, data) =>
87+
GitHubActions.Commands.UploadArtifact(
88+
data.ArtifactsDirectory,
89+
"ExampleArtifacts"));
90+
91+
Task("GitHubActions")
92+
.IsDependentOn("UploadArtifacts");
93+
94+
//////////////////////////////////////////////////////////////////////
95+
// EXECUTION
96+
//////////////////////////////////////////////////////////////////////
97+
98+
RunTarget(target);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public static partial class Program
2+
{
3+
static partial void RegisterServices(IServiceCollection services)
4+
{
5+
// Register MyService
6+
services.AddSingleton<IMyService, MyService>();
7+
8+
// Injects IOC-Task as an dependency of Build task
9+
services.AddSingleton(new Action<IScriptHost>(
10+
host => host.Task("IOC-Task")
11+
.IsDependeeOf("Build")
12+
.Does(() => Information("Hello from IOC-Task"))));
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public record BuildData(
2+
string Version,
3+
string Configuration,
4+
FilePath SolutionFile,
5+
DirectoryPath ArtifactsDirectory,
6+
DotNetMSBuildSettings MSBuildSettings,
7+
bool Rebuild
8+
)
9+
{
10+
public DirectoryPath[] DirectoriesToClean { get; init; } =
11+
[
12+
$"./src/Example/bin/{Configuration}",
13+
$"./src/Example/obj/{Configuration}",
14+
ArtifactsDirectory
15+
];
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface IMyService
2+
{
3+
string GetVersion();
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class MyService : IMyService
2+
{
3+
public string GetVersion()
4+
{
5+
InstallTool("dotnet:https://api.nuget.org/v3/index.json?package=GitVersion.Tool&version=6.3.0");
6+
var version = GitVersion();
7+
return version.FullSemVer;
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public static partial class Program
2+
{
3+
static void Main_PreClean()
4+
{
5+
Task("PreClean")
6+
.IsDependeeOf("Clean")
7+
.WithCriteria<BuildData>(c => c.Rebuild, nameof(BuildData.Rebuild))
8+
.Does(() => Information("Preparing for clean operation"));
9+
}
10+
}

0 commit comments

Comments
 (0)