Provides helpers for providing Cake context using Microsoft DependencyInjection, letting you use Cake Core/Common/Addins abstractions and aliases.
The assembly is published at nuget.org/packages/Cake.Bridge.DependencyInjection.
dotnet add package Cake.Bridge.DependencyInjection
<PackageReference Include="Cake.Bridge.DependencyInjection" Version="0.4.0" />
using Cake.Bridge.DependencyInjection;
...
serviceCollection
.AddCakeCore();
Once registered you can now via dependency injection access majority Cake.Core interfaces with ease, i.e:
Type | Description |
---|---|
ICakeContext | Gives access to Cake built-in and addin aliases, and most Cake abstractions. |
IScriptHost | Gives access to script runner. |
ICakeLog | Cake logging implementation. |
IFileSystem | Cake file system abstraction. |
var serviceCollection = new ServiceCollection()
.AddCakeCore();
var serviceProvider = serviceCollection.BuildServiceProvider();
var scriptHost = serviceProvider.GetRequiredService<IScriptHost>();
scriptHost.Task("Hello")
.Does(ctx => ctx.Information("Hello"));
scriptHost.Task("World")
.IsDependentOn("Hello")
.Does(ctx => ctx.Information("World"));
await scriptHost.RunTargetAsync("World");
will output
========================================
Hello
========================================
Hello
========================================
World
========================================
World
Task Duration
--------------------------------------------------
Hello 00:00:00.0226275
World 00:00:00.0002682
--------------------------------------------------
Total: 00:00:00.0228957
A full example console application using Spectre.Console demonstrating usage of both ICakeContext and IScriptHost can be found in this repository at src/Cake.Bridge.DependencyInjection.Example.