Blog

Cake.Paket - An Addin and Module to Add Support for Paket in Cake

Published
Wednesday, January 25, 2017
Category
Addins
Author
Larz White

Most .NET developers are comfortable using NuGet to manage their dependencies. This is probably why Cake has such great support for it. Unfortunately, Paket which is a popular alternative has very little support. Luckily, with the help of Cake.Paket you can make Paket a first class citizen in your Cake scripts!

Paket

Paket is a dependency manager for .NET and mono projects, which is designed to work well with NuGet packages and also enables referencing files directly from Git repositories or any HTTP resource. It enables precise and predictable control over what packages the projects within your application reference. ~ Paket Website

Although this is not a tutorial on Paket, a brief discussion is necessary.

Dependencies such as NuGet packages are handled external to Visual Studio by specifying them in the paket.dependencies file. This is analogous to the packages.config file which is updated when you install NuGet packages inside Visual Studio. For example, to install xUnit

# paket.dependencies

source https://nuget.org/api/v2
nuget xunit # Required in order to run the unit tests.

and then run paket.exe install.

For the above example we used paket.exe which is analogous to nuget.exe. In fact, a lot of the Paket commands are similar to NuGet commands: paket.exe restore, paket.exe pack, and paket.exe push. For a complete list of Paket commands see Paket's website.

Cake.Paket: Cake Addin

Cake.Paket is an addin that provides support for Paket commands such as restore, pack, and push. For example,

// Required in order to run the unit tests.
#tool nuget:?package=xunit.runner.console

// Required in order to use the addin Cake.Paket because it downloads paket.exe.
#tool nuget:?package=Paket

// Required in order to use PaketRestore, PaketPack, and PaketPush.
#addin nuget:?package=Cake.Paket

// Restores xUnit dependency specified in the paket.dependencies file.
Task("Paket-Restore")
    .Does(() =>
{
    PaketRestore();
});

Task("Build")
    .IsDependentOn("Paket-Restore")
    .Does(() =>
{
    MSBuild("./src/Cake.sln");
});

Task("Run-Unit-Tests")
    .IsDependentOn("Build")
    .Does(() =>
{
    XUnit2("./src/**/bin/Release/*.Tests.dll");
});

// Creates a NuGet package.
Task("Paket-Pack")
    .IsDependentOn("Build")
    .Does(() =>
{
    PaketPack("./NuGet");
});

// Pushes the package to a NuGet feed.
Task("Paket-Push")
    .IsDependentOn("Paket-Pack")
    .Does(() =>
{
    PaketPush("./NuGet/foo.nupkg", new PaketPushSettings { ApiKey = "00000000-0000-0000-0000-000000000000" });
});

where the script can be run using the Cake teams bootstrapper scripts build.ps1 and build.sh.

Cake.Paket.Module: Cake Module

The astute reader will notice we used NuGet to manage our dependencies in the previous Cake script because we used the preprocessor directives #tool nuget:?package=foo and #addin nuget:?package=bar. Wouldn't it be great if we could use Paket instead? That's where Cake.Paket.Module comes to the rescue! The Cake script above can be rewritten using the syntax #tool paket:?package=foo and #addin paket:?package=bar

// Required in order to run the unit tests.
#tool paket:?package=xunit.runner.console

// Required in order to use PaketPack and PaketPush.
#addin paket:?package=Cake.Paket

// Paket restore is handled by the bootstrapper.

Task("Build")
    .Does(() =>
{
    MSBuild("./src/Cake.sln");
});

Task("Run-Unit-Tests")
    .IsDependentOn("Build")
    .Does(() =>
{
    XUnit2("./src/**/bin/Release/*.Tests.dll");
});

// Creates a NuGet package.
Task("Paket-Pack")
    .IsDependentOn("Build")
    .Does(() =>
{
    PaketPack("./NuGet");
});

// Pushes the package to a NuGet feed.
Task("Paket-Push")
    .IsDependentOn("Paket-Pack")
    .Does(() =>
{
    PaketPush("./NuGet/foo.nupkg", new PaketPushSettings { ApiKey = "00000000-0000-0000-0000-000000000000" });
});

where the Cake script can be run using the modified bootstrapper scripts build.ps1 and/or build.sh and the following paket.dependencies file

# paket.dependencies

source https://nuget.org/api/v2
nuget xunit # Required in order to run the unit tests.

group tools
    source https://nuget.org/api/v2
    nuget Cake # Required in order to download Cake.exe.
    nuget xunit.runner.console # Required in order to run the unit tests.

group addins
    source https://nuget.org/api/v2
    nuget Cake.Paket # Required in order to use PaketPack and PaketPush.

group modules
    source https://nuget.org/api/v2
    nuget Cake.Paket # Required in order to use paket in the preprocessor directives.

In the above paket.dependencies file we used Dependency Groups to organize the file. It's important to note that you can organize the file however you want! For example, you don't need to use tools, addins and modules for the default group names. You can set them to whatever you want. See the command line arguments in the modified bootstrapper scripts build.ps1 and/or build.sh for more detail. Additionally, the dependencies don't have to be in the same group! If you need to specify a dependency in another group just include it in the URI #tool paket:?package=foo&group=bar.

A few side notes are in order. First, if you need to specify a package without a group (Paket calls it the main group) use #tool paket:?package=foo&group=main. Second, a version number in the URI is not allowed. This is because version's are handled in the paket.dependencies file.

Conclusion

The Cake.Paket addin and module make Paket a first class citizen in your Cake scripts. The addin provides support for common Paket commands such as restore, pack, and push while the module allows you to use Paket for the dependency management system in your Cake scripts. Checkout Cake.Paket.Example for a working example of how to use the addin and module as well as the API documentation on Cake's website.


My name is Larz White. My educational background is in computational nuclear physics. This is how I developed a passion for computing. For the past few years I've been working in industry as a C# developer. You can find me on GitHub as larzw.


New Static Cake Website

Published
Sunday, January 22, 2017
Category
Announcement
Author
daveaglick

Hi! My name is Dave Glick and I run the Wyam static site engine project. I'm also a big Cake fanboy, and over the last couple months I've been privileged to work with the rest of the Cake team on transitioning the Cake website from a dynamic ASP.NET website to a static one built by Wyam. This effort was more than mere window dressing; the new site goes beyond the old one in some interesting ways:

Read more...

Icing on the Cake – reviewing the recipe for 2016

Published
Saturday, December 31, 2016
Category
Announcement
Author
devlead

As 2016 is just about to be fully baked, the year has been packed with many ingredients, with this post we would like to take the opportunity to reflect on a few of the essences of what's been a truly amazing and eventful Cake year.

Read more...

Installing prerelease versions of Cake for Visual Studio

Published
Tuesday, December 13, 2016
Category
How To's
Author
agc93

We released the Cake for Visual Studio extension back in September and have been thrilled with the response from the whole community. There's been plenty of activity over on the GitHub repo with new issues, bug reports, features and even PRs. Now, you can get the benefits of all the brand new features as they arrive, right in Visual Studio.

Read more...

Cake v0.17.0 released

Published
Wednesday, November 9, 2016
Category
Release Notes
Author
gep13

Version 0.17.0 of Cake has been released.

This release of Cake sees a number of new features added, as well as some bug fixes, however, most importantly, there was a breaking change.

This breaking change centred around the VSTestSettings class, where the decision was wait to allow custom loggers to be passed into the VSTestRunner. As a result, the previous enumeration named VSTestLogger has been removed, and replaced with a fully customizable string. If you are using this enumeration in your scripts then you will see errors being generated if you update to the latest version of Cake. You should be able to make use of the new extension methods called WithVisualStudioLogger and WithAppVeyorLogger on the VSTestSettings to replace the enumeration values.

Please let us know if you run into any issues with this latest release.

Once again, the Cake Team have been overwhelmed by the number of contributions from our Community Members. Simply put... THANK YOU!

Contributions were included from:

Full details of everything that was included in this release can be seen below.

Read more...