Skip to content

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.

using System;
using System.Xml;
using Cake.Core;
using Cake.Core.Annotations;
using LogLevel = Cake.Core.Diagnostics.LogLevel;
using Verbosity = Cake.Core.Diagnostics.Verbosity;
namespace Cake.AppSettingsReplacer
{
public static class Replacer
{
[CakeMethodAlias]
public static void ReplaceAppSetting(this ICakeContext context, string filename, string key, string newValue)
{
var xml = new XmlDocument();
xml.Load(filename);
var node = xml.SelectSingleNode($"/configuration/appSettings/add[@key='{key}']");
if (node == null)
{
throw new InvalidOperationException($"ApplicationSetting Key ({key}) does not exist.");
}
var valueAttribute = node.Attributes["value"];
if (valueAttribute == null)
{
valueAttribute = xml.CreateAttribute("value");
node.Attributes.SetNamedItem(valueAttribute);
}
valueAttribute.Value = newValue;
xml.Save(filename);
context?.Log.Write(Verbosity.Normal, LogLevel.Debug, $"Replacing {filename} appSetting key={key} with value={newValue}");
}
}
}
view raw Replacer.cs hosted with ❤ by GitHub

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

#r "Cake.AppSettingsReplacer.dll"
var target = Argument("target", "Default");
Task("Default")
.Does(() =>
{
ReplaceAppSetting("App.config", "Website", "CodeOpinion.com");
});
RunTarget(target);
view raw cake.build hosted with ❤ by GitHub

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.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *