Before I start getting into cake usage, I want to just list some problems I had during the Getting Started process. I’m not blaming cake for these, but just wanting to put these issues out there in case others hit the same.
- It does state in the Getting Started page, “NOTE: If downloading the latest zip file, ensure that it is unblocked otherwise you will run into issues when attempting to execute Cake.” and I forgot this step initially. If you do not unblock before unzipping, you’ll end up with all sorts of Powershell security problems.
- NuGet.exe needed to be located on your machine (not a big deal) and copied to the Tools folder – I couldn’t find this mentioned in the Getting Started, so not quite sure wther this was supposed to be in the example zip or downloaded manually or whatever
- When I eventually got cake to run I kept hitting this error “Error: Failed to install tool ‘NUnit.ConsoleRunner’.”. The .cake script specifies that NUnit.ConsoleRunner version 3.4.0 is required, but this continually failed to work. I ran NuGet list NUnit.ConsoleRunner and found that the package didn’t exist. When I ran NuGet sources I noticed nuget api v2 was disabled, enabling this using NuGet.exe source Enable -Name “https://www.nuget.org/api/v2/” solved this. I’m not sure why cake was unable to use nuget.org (v3) which was enabled, but this solved the problem for me.
Make, Rake, Fake, Cake…
So first there was make, the good old (and complicated to understand) C/C++ build tool, then there were others ant/nant etc. Then along comes fake, the F# build tool and now cake the C# build tool. When I say F# or C#, I simply mean they use the syntax of F# or C# as opposed to nant’s (for example) XML, for creating the build etc. process.
The Basics
I’m going to solely be discussing running the cake executable against a .cake script here, not a PowerShell ps1 file hence you needn’t have to use PowerShell to try these commands out.
By default, if you run cake without supplying a specific .cake file (the extension can be anything you prefer if specifying the cake file name), cake will try to run build.cake.
A .cake file will have one or more Task’s (these are analogous to Target’s in nant and strangely cake has the command RunTarget to run the tasks). The task ofcourse can have some meaningful name, such as Build, Clean, Deploy (or whatever your preference). Let’s look at a simple task from Cake’s example
Task("Clean") .Does(() => { CleanDirectory(buildDir); });
In the above we have a task named Clean and this runs the code CleanDirectory, which is a DSL method to, as I’m sure you guessed, clean the supplied folder. The buildDir is declared in the example cack file as
var buildDir = Directory("./src/Example/bin") + Directory(configuration);
We can also compose multiple tasks using the DSL’s IsDependentOn method, such as
Task("Restore-NuGet-Packages") .IsDependentOn("Clean") .Does(() => { NuGetRestore("./src/Example.sln"); });
So now, when the Restore-NuGet-Packages task is run cake will first run the Clean task.
You’ll need to specify some form of command line argument to allow you to run one of these tasks, i.e. to pass a string from the command line into your cake script. This is done by declaring a line like this
var target = Argument("target", "Default");
in this case we’ve declared a target command line switch with the default value of Default (which obviously you’ll need to create a matching task name for). To run this command line using cake, you use a single hyphen such as
.\tools\Cake\Cake.exe --verbosity=Verbose -target=Clean
Note: the Cake exe command line switch uses –, whereas a command line to be passed into the cake script, uses a single -.
I’m not going to cover the DSL here, for that, go check out the Cake Reference.