https://github.com/devlead/cake.bridge.dependencyinjection
Provides helpers for providing Cake context using Microsoft DependencyInjection, letting you use Cake Core/Common/Addins abstractions and aliases.
https://github.com/devlead/cake.bridge.dependencyinjection
Last synced: about 2 months ago
JSON representation
Provides helpers for providing Cake context using Microsoft DependencyInjection, letting you use Cake Core/Common/Addins abstractions and aliases.
- Host: GitHub
- URL: https://github.com/devlead/cake.bridge.dependencyinjection
- Owner: devlead
- License: mit
- Created: 2021-01-07T07:07:00.000Z (over 5 years ago)
- Default Branch: develop
- Last Pushed: 2025-04-09T19:39:25.000Z (about 1 year ago)
- Last Synced: 2025-04-09T20:36:06.850Z (about 1 year ago)
- Language: C#
- Size: 103 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Cake Bridge DependencyInjection
Provides helpers for providing Cake context using Microsoft DependencyInjection, letting you use Cake Core/Common/Addins abstractions and aliases.
## Usage
### Obtain
The assembly is published at [nuget.org/packages/Cake.Bridge.DependencyInjection](https://www.nuget.org/packages/Cake.Bridge.DependencyInjection).
#### .NET CLI
```bash
dotnet add package Cake.Bridge.DependencyInjection
```
#### PackageReference
```xml
```
### Register
```csharp
using Cake.Bridge.DependencyInjection;
...
serviceCollection
.AddCakeCore();
```
### Use
Once registered you can now via dependency injection access majority [Cake.Core](https://cakebuild.net/api/Cake.Core/#InterfaceTypes) interfaces with ease, i.e:
| Type | Description |
|--------------|-------------|
| [ICakeContext](https://cakebuild.net/api/Cake.Core/ICakeContext/) | Gives access to Cake built-in and addin aliases, and most Cake abstractions. |
| [IScriptHost](https://cakebuild.net/api/Cake.Core.Scripting/IScriptHost/) | Gives access to script runner. |
| [ICakeLog](https://cakebuild.net/api/Cake.Core.Diagnostics/ICakeLog/) | Cake logging implementation. |
| [IFileSystem](https://cakebuild.net/api/Cake.Core.IO/IFileSystem/) | Cake file system abstraction. |
### Example
```csharp
var serviceCollection = new ServiceCollection()
.AddCakeCore();
var serviceProvider = serviceCollection.BuildServiceProvider();
var scriptHost = serviceProvider.GetRequiredService();
scriptHost.Task("Hello")
.Does(ctx => ctx.Information("Hello"));
scriptHost.Task("World")
.IsDependentOn("Hello")
.Does(ctx => ctx.Information("World"));
await scriptHost.RunTargetAsync("World");
```
will output
```powershell
========================================
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](https://www.nuget.org/packages/Spectre.Console) demonstrating usage of both [ICakeContext](https://cakebuild.net/api/Cake.Core/ICakeContext/) and [IScriptHost](https://cakebuild.net/api/Cake.Core.Scripting/IScriptHost/) can be found in this repository at [src/Cake.Bridge.DependencyInjection.Example](src/Cake.Bridge.DependencyInjection.Example).
## Testing
Cake.Bridge.DependencyInjection.Testing provides mock implementations of Cake Core interfaces for in-memory unit tests with minimal side effects, using the same dependency injection approach as the main library.
### Register
```csharp
using Cake.Bridge.DependencyInjection.Testing;
...
serviceCollection
.AddCakeCoreFakes();
```
### Fake Implementations
The following fake implementations are provided:
| Fake Type | Original Interface | Description |
|--------------------|--------------------|----------------------------------------------|
| FakeConfiguration | ICakeConfiguration | Mock implementation of configuration |
| FakeEnvironment | ICakeEnvironment | Mock environment with configurable settings |
| FakeFileSystem | IFileSystem | In-memory file system |
| FakeLog | ICakeLog | Capture and inspect logging output |
| FakeConsole | IConsole | Mock console for testing console output |
| FakeRuntime | ICakeRuntime | Mock runtime information |
| FakePlatform | ICakePlatform | Configurable platform information |
| BridgeArguments | ICakeArguments | Test arguments collection |
| FakeProcessRunner | IProcessRunner | Mock process execution |
| FakeProcess | IProcess | Mock process with configurable exit codes |
Each fake implementation can be configured during registration:
```csharp
serviceCollection.AddCakeCoreFakes(
configureFileSystem: fileSystem => {
fileSystem.CreateFile("/temp/test.txt", "test content");
},
configureLog: log => {
log.Verbosity = Verbosity.Diagnostic;
}
);
```
### Example Tests
For practical examples of testing with these fake implementations, see the test project in this repository at [src/Cake.Bridge.DependencyInjection.Testing.Tests](src/Cake.Bridge.DependencyInjection.Testing.Tests) using [Verify](https://github.com/VerifyTests/Verify) and [xUnit](https://xunit.net/).