Mar 18

Using Cassette.MSBuild with Mono

  • c#
  • mono
  • cassette

Cassette.MSBuild lets you pre-build your asset bundles. This is a key step if you want to upload your bundles to a CDN for release builds instead of serving them from your web server. Unfortunately, Cassette.MSBuild is - as the name indicates - intended for use with MSBuild, not mono-based build processes. Here’s how you can use it in a non-MSBuild environment, like a NancyFX application built in Xamarin Studio on OS X.

Upon installing the Cassette.MSBuild package, you’ll get the first hint that it’s not going to work out of the box:

Package install warning

Package install message

Since the PowerShell script didn’t run, we’ll look at the source and see what it was supposed to do. The PowerShell script adds a post-build step to your project to build the asset bundles after a successful release build:

$target = $buildProject.Xml.AddTarget("Bundle")
$target.AfterTargets = "Build"
$task = $target.AddTask("Exec")
$task.SetParameter("Command", '"$(msbuildtoolspath)\msbuild.exe"
  $(ProjectDirectory)cassette.targets /p:OutputPath=$(OutputPath)
  /t:Bundle /nr:false')

However, there’s another stumbling block here: the after-build task uses msbuild.exe. While there is a (roughly) corresponding xbuild command on mono platforms, it’s not apparent to me how it can be used to build a .targets file. Instead, we can add the targets file to our project and call the CreateBundles task manually.

In your project’s .csproj, add the following line somewhere inside the <Project></Project> node (you probably have other <Import Project=... /> nodes - it can go after one of those):

<Import Project="$(ProjectDirectory)cassette.targets" />

Immediately after this added line, we’ll add a post-build target:

  Condition="'$(Configuration)' == 'Release'">
  <CreateBundles Output="cassette-cache" />

This will create your asset bundles after a successful release build. Note that the bundles will be created in the cassete-cache directory, relative to your project file. You can customize this - along with a handful of additional settings listed in the Cassette.targets file - by editing the <CreateBundles> node.

Reload your project, switch to a release configuration, and build to confirm that everything is working. You should have something like the following in your build output:

Target Bundle:
  Source directory = /Users/jordan/src/recipequant/RecipeQuant
  Bin directory = /Users/jordan/src/recipequant/RecipeQuant/bin
  Output directory = /Users/jordan/src/recipequant/RecipeQuant/cassette-cache
  App virtual path = /
  Include other files = False
  Starting bundling
  Finished bundling - (took 327ms)

… and your bundles should be ready to use in the cassette-cache directory.

blog comments powered by Disqus