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!
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.