Introducing Cake.Chocolatey.Module

Or as I like to call it...

What is it?

Back in version 0.13.0 of Cake a feature known as Module Support was released. This provided the ability to both extend and replace some of the built in functionality of Cake.

One of these extension points is the registration of IPackageInstaller's, which deal with installing tools and addins during the Cake Script execution. The Chocolatey Cake Module provide the extension point to allow tools to be resolved using Chocolatey.

NOTE: Chocolatey deals with the installation of applications. As a result, it only makes sense for this module to deal with tools, and not addins.

Why is this needed?

Out of the box, Cake has the ability to install tools and addins from any NuGet source. This is documented on the Cake Website. Recently, Larz Whiteshowed how it was possible to use an alternative package manager, namely Paket to install those tools and addins.

However, not all tools that you require for your build may be available as NuGet Packages. This leaves you with two options (there are likely more, but for the purposes of this discussion, let's stick to two), you could either pre-install the required tools onto the build machine that you are using, or look to install those tools through a custom script, perhaps through something like Chocolatey.

When I joined the Cake Team, one of the first things that I did was to add Chocolatey Aliases to Cake, so you have always been able to install Chocolatey packages as part of your build process, using something like this:

ChocolateyInstall("MyChocolateyPackage");

For me, that removes an element of clarity over what tooling is required for the build. I would much rather see all of the dependencies in one place, i.e. here:

Installation

Due to the fact that Cake Modules are extending and altering the internals of the Cake, the Module Assembly needs to be loaded prior to the main Cake execution. As a result, the only place that this can really happen is during the bootstrapping phase. If you use the latest version of the default bootstrapper this process is made very easy. All you need to do is the following.

Add a modules folder into your tools folder

Add a packages.config file into the newly created modules folder

Add the name and version of the Module that you want to use. NOTE: At this point, the assumption is that you are hosting the Module on NuGet.org. If this is not the case, then additional steps would be required.

Run the build as normal. During the Cake Execution, it will recognise the Module Assembly which as been restored into the tools/modules folder, and load it.

NOTE: Similar to the recommendation here regarding only checking in your packages.config and not the entire contents of the Cake tools folder, the same recommendation is applied here. Only check-in the packages.config file in the modules folder, and not the entire contents.

Usage

As mentioned above, installing a tool using the Chocolatey Cake Module is as simple as:

#tool choco:?package=nodejs.install&version=7.7.3

If the tool in question comes from a different source, you can change that as follows:

In this version 0.1.0 release of the Chocolatey Cake Module you can also specify whether the package in question is a pre-release or not:

#tool choco:?package=packageA&prerelease

Future versions of the Chocolatey Cake Module will include additional options for installing the package, including things like --forcex86, --install-arguments, --package-parameters, etc. This initial release includes just the very basic of installation functionality.