Cake is a free open-source task runner for writing custom cross-platform builds stages for .NET applications.

MailSlurp uses Cake to build CSharp SDKs for its free email API. In this tutorial we will show you how to create Cake files and a dotnet tool manifest in order to create custom builds and tasks in your .NET project.

What is Cake?

Cake is the Make of C#. It's a cross-platform build automation system with a C# DSL that makes it easy to compile code, copy files, run tests, build packages, collect code coverage and more on Linux, Mac, and Windows - using the same tools for each.

Cake is not intended to replace commands or your CI pipelines in Circle or TeamCity. Cake is designed for developers so they can run common tasks on any machine within an organization and get the same results. If you have ever developed a NodeJS application it is a bit like scripts.

An example of a task runner in another environment, NodeJS:

Why Cake? (Why not Make?)

Cake might seem unusual if you are used to Visual Studio or Resharper for .NET development but Cake adds the possibility of a central file for common tasks that can result in deterministic builds across different machine types. In plain english, Cake lets you write common tasks such as running tests, collecting code coverage, zipping files etc. in a automated way when you may have performed them manually using an IDE in the past. It also helps to add a common build system to cross-platform apps that may run on Windows, Linux and Mac.

Why not Make? Make is the original inspiration for Cake and is used extensively in the Linux world. Make however isn't bundled with Windows and uses a syntax that can be unfamiliar for many developers. Using Cake lets you write tasks in C# and include regular Nuget packages like you would in a DotNET project.

Writing a Cakefile

You can install Cake as a dotnet tool locally using a tool manifest. This means versions and dependencies for your tools are stored in code.

Create a tool manifest

First create a tool manifest in your DotNET project:

This will create a new directory and inside that will be a file.

Add Cake to your manifest

Next we want to install Cake as a tool:

For demonstration purposes let's also add to let use format code.

Restore dependencies

Now that we have added tools our file should look like this:

To install tools on a new machine run:

This will read the tool manifest and install any missing tools on your machine. It is good practice in an organization to add these setup steps to a file like so:

Simple example

Create a new file called in the root directory of your project. The main concepts in Cake are targets and tasks. One defined tasks in a build file and targets them in the command line. For instance here is a simple Cake file that builds a solution:

The build task can be invoked in a command line by running:

Or you can specify the target explicitly using:

Creating custom Cake tasks

We can use Cake to turn tasks that might typically involve clicking through submenus in an IDE into jobs that can be run on any platform using a command line. This includes continous integration pipelines like Jenkins and Azure. For an example let's create a Cake file that can collect code coverage using coverlet and generate a report to display the percentage of lines covered by our tests in the terminal.

Install coverlet and report generator

In your test solution add the coverlet package - this will orchestrate tests to collect code coverage. Code coverage defines how many lines of code your tests test for. Code coverage is a useful way to measure how well your application is tested.

Add tasks to your Cake file

Now we can define a Cake file to run our build, tests, code coverage and output the results in the command line with one simple command:

The Cake file looks like this (see each comment):

Task output

Running the above Cake file will print code coverage to the terminal. This is great for cross platform tooling or for CI builds. The output looks like so:

Extending Cake and next steps

Cake is a highly underated tool that lets DotNET teams create custom build tasks that run cross-platform and IDE-independent. There are many Cake extensions available for AWS, GitHub, NuGet, Azure and more. You can even create your own!

We use Cake at MailSlurp to build and deploy our Email APIs. You can use MailSlurp to unlimited free test email accounts. Send and receive emails from tests or application using intuitive CSharp SDKs. Check it out!