Building C# projects with Cake Build

I’ve been helping out the nunit team the last few weeks in my spare time, one thing that I was interested to check out is cake, which they are using for building.

Off the bat I don’t recommend using visual studio to edit the cake files, I was unable to get the IDE to work correctly, it just made a mess of the file. I’ve been using visual studio code instead which has an addon for cake.

There is also a VSTS addon available for a cake build step, or you can just run a powershell command line if your build server doesn’t have a build step for it OOTB.

Cake chains tasks together with dependencies and criteria.


Task("BuildProject")
.Does(() =>
{
// code to build your project ehre
});

You can then use the IsDependant and WithCriteria methods to chain dependencies like below.

Task("BuildProject")
.IsDependentOn("InitializeBuild")
.WithCriteria(IsRunningOnWindows)
.Does(() =>
{
// code to build your project here
});

Task("InitializeBuild")
.Does(() =>
{
// code to do stuff before building your project here
});

When you run cake from the command line you need to specify the Task name which is your entry point. With nunit what the guys have done is defined a number of tasks that are the main target entry points at the end of the cake file an example would be something lie the below when you specify a “Release Build” target it does the steps to Build, Test and then package in that order.


Task("ReleaseBuild")
.IsDependentOn("Build")
.IsDependentOn("TestAll")
.IsDependentOn("Package");

Task("Build")
.Does(() =>
{
// code to Build your project here
});

Task("TestAll")
.Does(() =>
{
// code to run your tests here
});

Task("Package")
.Does(() =>
{
// code to package up your project here
});

Arguments from the command line can be picked up from within the script using the arguments method.


// if not specified at the command line the value will be Debug
var configuration = Argument("configuration", "Debug");

Building projects from within cake is pretty straight forward, you just need to call the built in MSBuild method


// Use MSBuild
MSBuild("MySolution/MyProject.csproj", new MSBuildSettings()
.SetConfiguration("Release")
.SetMSBuildPlatform(MSBuildPlatform.Automatic)
.SetVerbosity(Verbosity.Minimal)
);

If you want to use Travis CI you’ll need to use XBuild instead of MSBuild, and also get a build.sh file in your project to kick off the cake script.

Here is a ref to their cake file for nunit that we have been working on

I’m not “blown away” with cake so far, the good parts are:

  • It’s multi-platform so you can run free Travis CI builds with it if you are working on an opensource project like nunit.
  • Its easy to get a consistent experience if you are using multiple build servers (appveyor, Travis, TFS, etc).
  • The scripting language is in C# so for some developers who might struggle with PowerShell or possible node.js/javascript in the case of gulp, its a lot more familiar
  • While there isn’t a “huge” amount of libraries out there its got all the core stuff you need (nuget, git, msbuild, etc.)

The bad parts that I don’t like are:

  • The documentation is pretty light, and its hard to find sites out there with examples
  • If you actually want to support builds on Travis there is language limitations to be aware of, so you end up with functioning cake script on a windows box that will fail when run on Travis the below example code will fail on cake in Travis but work on windows

 


var MyString ="A string";

void WriteStringConsole()
{
Console.WriteLine(MyString);
}

Task("MyTargetTask")
.Does(() =>
{
WriteStringConsole();
});

  • While the ability to have a consistent build experience across multiple build servers is good, who actually uses multiple build servers to build a single product? I think this will be useful for open source products where another company may want to fork and start building a version for their use, but I don’t see this as advantageous for private code.
  • I haven’t found an effective way to do templates with it yet, happy to be corrected on this as the documentation is light. The build templating system we use in TeamCity is awesome, the ability to use one central template then tweak it slightly for each project is second to none, and TFS seem to be following suite with this too, using a single script file means this becomes a lot more hard, sure you can toggle things on and off with code, but in TeamCity there is a “GUI” to do this.

Overall I think cake is cute. It defiantly has its place in open source projects, but I’m not going to be moving projects over to it en masse.

 

 

 

One thought on “Building C# projects with Cake Build

  1. Thanks for your blog post, always great to get user feedback.
    Any XPlat differences are because Cake uses the Roslyn on Windows and Mono, when Roslyn scripting goes xplat Cake will unify around one scripting engine.
    In general delegates agree better with mono than methods, a little different syntax using funcs&actions but you can achieve the same goal xplat at least.

    That said the DSL methods are in general xplat as long as the tools exist on the platform, i.e. MSBuild isn’t yet xplat ready so XBuild is currently the alternative there.

    Also for outputting data to the console you should use the available logging DSL methods
    http://cakebuild.net/dsl/logging
    Those work xplat in same way.

    Feel free to raise an issue if you have any suggestions of improvements or ping us on our gitter:
    https://github.com/cake-build/cake

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s