“FAKE – F# Make” is intended to be an extensible build framework. That’s why I tried to make it as easy as possible to create custom tasks. This posts shows how we can create a (very simple) custom task in C# which gives a random number.
Open Visual Studio and create a new C# class library called my MyCustomTask and create a class called RandomNumberTask:
Now compile this project and put the generated assembly into the tools/FAKE path of your project. Now you can use your CustomTask in the build script:
You can use every .Net class with FAKE. Just put the assembly in the right folder (tools/FAKE) and include it with the #r command.
If you want to use FAKE’s standard functionality (like globbing) within your CustomTask project, just reference FakeLib.dll and explore the FAKE namespace.
Tags:
F#,
F-sharp Make,
Fake,
MSBuild,
NAnt
This post addresses the No. 1 Feature request for MSBuild: Debugging.
With FAKE builds scripts debugging is no issue. Just write System.Diagnostics.Debugger.Break() somewhere into your build script and start the build. It is nearly as easy as pressing F9 in Visual Studio.
If the build script reaches your Breakpoint it will ask you which Debugger you want to use:
If you are editing your build script in Visual Studio you can select that instance otherwise choose a new instance and you are ready to debug:

Tags:
Debugging,
Debugging build scripts,
F#,
F-sharp Make,
MSBuild
In the last two articles I showed how we can set up FAKE to make an automated build and how we can use it with FxCop. This time we will use FAKE’s AssemblyInfo task in order to set a specific version info to our assemblies.
I assume you have succeeded the CaculatorSample tutorial. If so then just modify your “BuildApp” target to the following:
As you can see modifying your AssemblyInfo.cs file is pretty easy with FAKE. The AssemblyInfo task works for C#, VB.Net and F#. The version parameter can be declared as a property or fetched from a build server like TeamCity:

Tags:
assemblyinfo,
F#,
F-sharp Make,
MSBuild,
NAnt,
TeamCity
In the last article I showed how we can use “FAKE – F# Make” to set up a build script which
- Cleans up old build outputs
- Compiles our main projects
- Compiles test projects
- Uses NUnit to test our assembly
- Zips the assemblies to a deploy folder.
This time we will improve the same Calculator sample (download here) with a task for FxCop, so please make sure you succeeded with the last article.
“FxCop is a free static code analysis tool from Microsoft that checks .NET managed code assemblies for conformance to Microsoft’s .NET Framework Design Guidelines.”
[Wikipedia]
Setting up FxCop
Open build.fsx from your Calculator sample folder and add a new target “FxCop” to the targets section:
In the dependencies section modify the build order to:
That’s it. If you run your build script you will get new *.xml file in the .\test\-folder:

There are a lot of parameters for the FxCop task. Some are described on the project page.
Letting the build fail
If you were using MSBuild before you might know how hard it is to let MSBuild fail your build if FxCop reports any errors or warnings.
With FAKE the only thing you have to do is setting the “FailOnError” parameter:

If you activate this option FxCop errors will cause your build to fail. Possible values are:
- FxCopErrorLevel.Warning
- FxCopErrorLevel.CriticalWarning
- FxCopErrorLevel.Error
- FxCopErrorLevel.CriticalError
- FxCopErrorLevel.ToolError
- FxCopErrorLevel.DontFailBuild
The values are cummulative. If you choose FxCopErrorLevel.CriticalWarning the build will fail for critical warnings, errors, critical errors and FxCop tool errors but not for simple warnings. The default is FxCopErrorLevel.DontFailBuild.
Tags:
Code Quality,
F#,
F-sharp Make,
Fake,
FxCop,
MSBuild,
NAnt
In this tutorial I will describe how you can set up a complete build infrastructure with “FAKE – F# Make”. You will learn:
- How to automatically compile your C# or F# projects
- How to automatically run NUnit UnitTests on your projects
- How to zip the output to a deployment folder
Install F#
“FAKE – F# Make” is completely written in F# and all build scripts will also be written in F#, but this doesn’t imply that you have to learn programming in F#. In fact the “FAKE – F# Make” syntax is hopefully very easy to learn. But if you need to you can use the complete power of F# and the .NET Framework.
But in order to get things working we need to install the F# environment. You can download the F# April 2011 CTP from the Microsoft F# Developer Center or install Visual Studio 2010.
Download Calculator Sample
Now download the latest CalculatorSample-*.zip from the FAKE Download site. This sample includes 3 tiny projects and basically the following structure:
- src\app
- Calculator (Command line)
- CalculatorLib (Class library)
- src\test
- Test.CalculatorLib (NUnit test library)
- xUnit.Test.CalculatorLib (xUnit test library)
- tools
- build.bat
- build.fsx
- completeBuild.bat
- completeBuild.fsx
- Calculator.sln
Getting “FAKE – F# Make” started
If you run the build.bat from the command line then your first FAKE script (build.fsx) will be executed. If everything works fine you will get the following output:

Now open the build.fsx with Visual Studio. It should look like this:
As you can see the code is really simple. The first lines include the FAKE library and are vital in all FAKE build scripts.
After this header the Default target is defined. A target definition contains of two important parts. The first is the name of the target (here “Default”) and the second is an action (here a simple trace of “Hello world”). The Action can be defined as simple lambda expressions or as methods.
The last line runs the “Default” target – which means it executes the defined action of the target.
Cleaning the last build output
A typical first step in most every build scenario is to clean the output of the last build. We can achieve this by modifying the build.fsx to the following:
We introduced some new concepts in this snippet. At first we defined a global property called “buildDir” with the relative path of a temporary build folder.
In the Clean target we use the CleanDir task to clean up this build directory. This simply deletes all files in the folder and creates the directory if necessary.
In the dependencies section we say that the Default target is dependent of the Clean target. In other words Clean is a prerequisite of Default and will be run before the execution of Default:

Building the application
In the next step we want to compile our application libraries, which means we want to compile all projects under /src/app with MSBuild.
We defined a new build target named “BuildApp” which compiles all project files given in the property appReferences with the MSBuild task and the build output will be copied to buildDir.
In order to find the right project files “FAKE – F# Make” scans the folder src/app/ and all subfolders with the given pattern. Therefore a similar FileSet definition like in NAnt or MSBuild (see project page for details) is used.
In addition the target dependencies are modified again. Now Default is dependent of BuildApp and BuildApp needs Clean as a prerequisite.
This means the execution order is: Clean ==> BuildApp ==> Default.

Building Test projects
Now our main application will be built automatically and it’s time to build the test application. We use the same concepts as before:
This time we defined a new target “BuildTest”, which compiles all C# projects below src/test/ in Debug mode and we put the target into our build order.
Testing the test assemblies with NUnit
Now all our projects will be compiled and we can use the NUnit task in order to let NUnit test our assembly:
Our new target “Test” scans the test directory for test assemblies and runs them with NUnit. The mysterious part (fun p –> …) simply overrides the default parameters of the NUnit task and allows to specify concrete parameters..

Deploying a zip file
Now we want to deploy a *.zip file containing our application:
The new target “Deploy” scans the build directory for all files. The result will be zipped to \deploy\Calculator.zip via the Zip task.
What’s next?
Now you are ready to write your own “FAKE – F# Make” build scripts. If you have any questions or suggestions feel free to comment on this post.
In the next article I will show how we can add FxCop to our build in order to check specific naming rules.
Tags:
F#,
F-sharp Make,
Fake,
MSBuild,
NAnt,
nunit,
Rake