Package | Latest Release |
---|---|
Cake.Generator | |
Cake.Sdk | |
Cake.Template |
A .NET source generator that creates proxy methods for Cake build system extensions, eliminating the need to pass ICakeContext
explicitly.
This source generator scans referenced assemblies for static extension methods that have:
System.Runtime.CompilerServices.ExtensionAttribute
Cake.Core.Annotations.CakeMethodAliasAttribute
orCake.Core.Annotations.CakePropertyAliasAttribute
- First parameter of type
Cake.Core.ICakeContext
It then generates proxy methods in a partial static class named Program
that:
- Remove the
ICakeContext
parameter - Use a static
Context
property instead - Preserve all other parameters, documentation, and method signatures
/// <summary>
/// Writes an error message to the log using the specified format information.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="format">The format.</param>
/// <param name="args">The arguments.</param>
/// <example>
/// <code>
/// Error("Hello {0}! Today is an {1:dddd}", "World", DateTime.Now);
/// </code>
/// </example>
[CakeMethodAlias]
public static void Error(this ICakeContext context, string format, params object[] args)
/// <summary>
/// Writes an error message to the log using the specified format information.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="args">The arguments.</param>
/// <example>
/// <code>
/// Error("Hello {0}! Today is an {1:dddd}", "World", DateTime.Now);
/// </code>
/// </example>
public static void Error(string format, params object[] args)
=> Context.Error(format, args);
Cake.Generator/
├── src/
│ ├── Cake.Generator.Core/ # Source generator implementation
│ │ ├── CakeGenerator.cs
│ │ ├── CakeGenerator.*.cs # Partial generator classes
│ │ └── Cake.Generator.Core.csproj
│ ├── Cake.Generator/ # Meta project with default dependencies
│ │ └── Cake.Generator.csproj
│ ├── Cake.Generator.TestApp/ # Test application (used to build project)
│ │ ├── Program.cs
│ │ └── Cake.Generator.TestApp.csproj
│ ├── Cake.Generator.Core.Tests/ # Unit tests
│ │ └── Cake.Generator.Core.Tests.csproj
│ ├── Cake.Sdk/ # .NET SDK for Cake
│ │ └── Cake.Sdk.csproj
│ ├── Cake.Template/ # Project template for Cake
│ │ └── Cake.Template.csproj
│ └── Cake.Generator.slnx # Solution file (slnx format)
└── README.md
- Add the Cake.Generator package to your project:
<ItemGroup>
<PackageReference Include="Cake.Generator" Version="1.0.0" />
</ItemGroup>
This will automatically include:
- The source generator (Cake.Generator.Core)
- Default Cake dependencies:
- Cake.Core
- Cake.Cli
- Cake.Common
- Microsoft.Extensions.DependencyInjection
- Build your project - the generator will automatically create proxy methods for all discovered Cake method aliases.
The generator creates a partial static Program
class with generated methods that use an internal Context
property, eliminating the need to pass ICakeContext
explicitly.
With the generator, your Program.cs
can be simplified significantly, leveraging top-level statements and the automatically generated alias methods:
Task("Build")
.Does(() => Information("Build"));
Task("Default")
.IsDependentOn("Build");
await RunTargetAsync(target);
Any Cake addin can be added as a PackageReference
and its alias proxies will be generated automatically, example:
<ItemGroup>
<PackageReference Include="Cake.Twitter" Version="5.0.0.0" />
</ItemGroup>
The generator will scan the addin assembly and create proxy methods for all discovered Cake method and property aliases, making them available as static methods without requiring explicit ICakeContext
parameters.
The generator now supports Cake modules with automatic registration. Modules referenced in your project will have their method and property aliases automatically discovered and proxy methods generated, just like regular addins. This includes both NuGet package modules and local module assemblies, example:
<ItemGroup>
<PackageReference Include="Cake.BuildSystems.Module" Version="7.1.0" />
</ItemGroup>
The generator creates a partial Program
class that allows you to register your own services to the IoC container. Simply implement the RegisterServices
partial method:
public static partial class Program
{
static partial void RegisterServices(IServiceCollection services)
{
// Register your services here
services.AddSingleton<IMyService, MyService>();
services.AddTransient<IAnotherService, AnotherService>();
services.AddScoped<IScopedService, ScopedService>();
}
}
Services can be resolved from the IoC container using the static ServiceProvider
property. Here's how to use it in your tasks:
Task("MyTask")
.Does(() => {
// Resolve a service
var myService = ServiceProvider.GetRequiredService<IMyService>();
// Use the service
myService.DoSomething();
// You can also resolve multiple services
var anotherService = ServiceProvider.GetRequiredService<IAnotherService>();
anotherService.Process();
});
The ServiceProvider
is available throughout your build script, making it easy to access your registered services wherever needed.
The following constants are automatically generated and available in your Cake script:
Constant | Description |
---|---|
CakeGeneratorDate |
The UTC date and time when the aliases were generated (format: yyyy-MM-dd HH:mm:ssZ ) |
CakeGeneratorVersion |
The version of Cake.Generator.Core used to generate the aliases |
CakeGeneratorInformationalVersion |
The full informational version of Cake.Generator.Core, including build metadata |
CakeGeneratorNuGetVersion |
The NuGet package version of Cake.Generator.Core |
These constants are useful for version tracking and debugging purposes in your Cake scripts.
Here's an example of how to use these constants in your Cake script:
Task("Version-Info")
.Does(() =>
{
Information("Generated with Cake.Generator.Core version: {0}", CakeGeneratorVersion);
Information("Generation date: {0}", CakeGeneratorDate);
});
This will output the version information when you run the Version-Info
task.
The generator provides methods to install tools that can be used in your Cake script. Here are examples of how to use them:
// Install a single tool using a package reference string
InstallTool("dotnet:https://api.nuget.org/v3/index.json?package=GitVersion.Tool&version=5.12.0");
// Install multiple tools at once
InstallTools(
"dotnet:https://api.nuget.org/v3/index.json?package=GitVersion.Tool&version=5.12.0",
"dotnet:https://api.nuget.org/v3/index.json?package=GitReleaseManager.Tool&version=0.20.0"
);
The InstallTool
and InstallTools
methods return the paths where the tools were installed, which can be useful for verification or direct tool execution.
Tools installed using these methods are automatically registered with Cake's tool resolution system, meaning you can use them directly in your tasks without specifying their full paths. For example:
Task("Use-Registered-Tools")
.Does(() =>
{
// GitVersion is automatically available in PATH
var version = GitVersion();
// GitReleaseManager is automatically available
GitReleaseManagerCreate("token", "owner", "repo");
});
- Scans all referenced assemblies for Cake method aliases
- Preserves method signatures, including generics and constraints
- Handles parameter modifiers (
ref
,out
,in
,params
) - Preserves default parameter values
- Copies XML documentation (excluding context parameter)
- Supports both
CakeMethodAlias
andCakePropertyAlias
attributes - Supports
CakeNamespaceImport
attribute for importing global static namespaces - Supports Cake modules with automatic registration
- Generates code in a partial static
Program
class with no namespace - Provides helper methods for installing .NET / NuGet tools
# Build the source generator
dotnet build src/Cake.Generator.Core/Cake.Generator.Core.csproj
# Build the meta package
dotnet build src/Cake.Generator/Cake.Generator.csproj
# Build and test the complete solution
dotnet test src/Cake.Generator.slnx
# Run the test application
dotnet run --project src/Cake.Generator.TestApp/Cake.Generator.TestApp.csproj
- .NET 10 SDK
- .NET Standard 2.0+ (for the source generator)
- .NET 8.0+ (for the test application)
- Cake.Core package for ICakeContext and annotations
MIT License - see LICENSE file for details.