Cake Build Script - Node, Gulp, .NET Core MSBuild

The following is a basic guide for setting up a Cake build script to work with a project that utilizes .NET Core, Node.JS, and Gulp.

This build script can be used with various continuous integration setups to assist with automated builds and deployments of code.

Requirements:

Node.js Requirements:

  • Install the latest version of npm 3.x
  • Install the gulp-cli package globally

    npm install -g npm@3
    npm install -g gulp-cli
    

Project Configuration

Project Directory Structure

  • ./ - Repository root
    • ./src/ - Project source code (.net core, etc.)
      • ./src/project.csproj - .NET Core project file
      • ./src/package.json - Node.js package file
      • ./src/gulpfile.js - Gulp file
    • ./publish/ - Publish staging directory
  • ./build.sh - Cake build script for Linux
  • ./build.ps1 - Cake build script for Windows
  • ./build.cake - Cake build file

Example build.cake:

#addin "Cake.Gulp"
#addin "Cake.Npm"

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

Task("Clean")  
  .Does(() =>
{
  if (DirectoryExists("./src/bin/"))
  {
    CleanDirectory("./src/bin/");
  }
  if (DirectoryExists("./src/obj/"))
  {
    CleanDirectory("./src/obj/");
  }
  if (DirectoryExists("./publish/"))
  {
    CleanDirectory("./publish/");
  }

});

Task("Restore")  
  .Does(() => 
{
  // Reads ./src/package.json and installs node.js packages
  Npm.FromPath("./src/").Install();
  // Restores NuGet packages from project.csproj
  DotNetCoreRestore("./src/");
});

Task("Build")  
  .IsDependentOn("Restore")
  .Does(() => 
{
  // Executes the gulpfile.js script
  Gulp.Global.Execute(settings => settings.WithGulpFile("./src/gulpfile.js"));
  // Performs a dotnet core build (MSBuild) using project.csproj
  DotNetCoreBuild("./src/", new DotNetCoreBuildSettings
  {
      Configuration = "Release"
  });
});

Task("Publish")  
  .IsDependentOn("Build")
  .Does(() => 
{
  if (DirectoryExists("./publish/"))
  {
    CleanDirectory("./publish/");
  }
  // Performs a dotnet core (msbuild) publish
  DotNetCorePublish("./src/", new DotNetCorePublishSettings
  {
    Configuration = "Release",
    OutputDirectory = "./publish/"
  });

});

Task("Default")  
  .Does(() =>
{
  Information("Please specify a task of Restore, Build, or Publish");
});

RunTarget(target);  

Usage

Windows

powershell -ExecutionPolicy ByPass -File build.ps1 -target "Clean"  
powershell -ExecutionPolicy ByPass -File build.ps1 -target "Build"  
powershell -ExecutionPolicy ByPass -File build.ps1 -target "Publish"  

Linux

Note: Linux may require an installation of Mono for the MSBuild components of the .NET Core build scripts to work correctly.

./build.sh --target "Clean"
./build.sh --target "Build"
./build.sh --target "Publish"