Cake

Johan Vergeer

This page contains some snippets and links I refer to when I'm creating Cake scripts. Most of this information can be found on the Cake website, but this should give a better overview of some things I personally use.

.gitignore

The .gitignore file should contain the following lines:

tools/*
!tools/packages.config

build.cake content

Arguments

This defines a -target argument which, when omitted, defaults to Default

var target = Argument("target", "Default")

This defines a -configuration argument which, when omitted, defaults to Release

var configuration = Argument("configuration", "Release")

Tasks

A Cake file contains Tasks that we want to execute:

Task("Default")
    .IsDependentOn("Dotnet-Core-Package")

CleanDirectory / CleanDirectories

These aliases make sure that the directory or directories exist, and that they are empty.

Task("Clean")
    .Does(() => {
        CleanDirectory("./BuildArtifacts");
    });

DotNetCoreRestore

In the example below is DotNetCoreRestore an alias, which is running the dotnet cli with the restore command.

Task("Dotnet-Core-Package")
    .Does(() => {
        DotNetCoreRestore(
            "./src/project/project.csproj",
            new DotNetCoreRestoreSettings {
                PackagesDirectory = "./src/packages"
            }
        );
    });

DotNetCoreBuild

In the next example the DotNetCoreBuild is an alias, which is running the dotnet cli with the build command.

Task("Build")
    .IsDependentOn("Dotnet-Core-Build")
    .Does(() => {
        var settings = new DotNetCoreBuildSettings {
            Configuration = configuration,
            NoRestore = true
        };

        DotNetCoreBuild("./src/project/project.csproj");
    });

DotNetCorePublish

Publish to the local file system.

Task("Publish")
    .IsDependentOn("Build")
    .Does(() => {
        var settings = new DotNetCorePublishSettings {
            Configuration = configuration,
            OutputDirectory = "./BuildArtifacts"
        };

        DotNetCorePublish("./src/project/project.csproj", settings);
    });

Aliases

Cake supports something called script aliases. Script aliases are convenience methods that are easily accessible directly from a Cake script. Every single DSL method in Cake is implemented like an alias method.

RunTarget

The end of build.cake should contain the following line to run:

RunTarget(target);

This will make sure the tools directory is not included in source control, except for packages.config, which contains the Cake version we're using.

What is a build?

Typical build workflow

  1. Package restore
  2. Clean
  3. Unit Tests
  4. Test Coverage (NCover, OpenCover)
  5. Static analysis
    1. StyleCop
    2. FxCop
    3. DupFinder
    4. InspectCode
  6. Package
  7. Publish

Intellisense

To have Intellisense in VS Code for Cake, just run the Cake: Install Intellisense Support and OmniSharp: Restart OmniSharp commands in VS Code.