How to create a Cake Addin

Who doesn’t love cake?  There are a ton of existing addins for Cake, but you may run into a situation where you want to create your own cake add in.  Here’s how!

Note: If you have no idea what Cake is, check out my intro to Automating Builds with Cake (C# Make) post first.

AppSettings Replacer

In my case I wanted to replace some values in the appSettings of an app.config.  There are already a couple existing ways to do this, such as the XdtTransform , however I wanted to give it a shot and explore how to create a cake addin.

Turns out it’s crazy simple to create an addin.   The minimum you need to know are the ICakeContext and the CakeMethodAlias attribute.

ICakeContext and CakeMethodAlias

First you need to reference the Cake.Core nuget package.

Install-Package Cake.Core

Next thing is to create an extension method off of ICakeContext with the CakeMethodAlias as an attribute.

For my example, the body of the extension method will be replacing a value for a specified key in the <appSettings> section of an app/web.config.

Build.cake

Once we have built our project, we can take our assembly and reference it within our cake.build script.  You can do this with using #r at the top of your file.  From here you can now use your extension method.  Here is an example of my build.cake

Results

Once we run our cake build script, here are the outputs with our log.

Next

Want to run this under .NET Core?  You can do that!  I’ll take a look at a bootstrapper that uses the Cake.CoreCLR package. As well as how you can debug and unit test your addins.

Have you written a addin?  If so, I’d love to hear about it in the comments or on Twitter.