FAKE 5 - Custom Modules

Note: This documentation is for FAKE.exe version 5.0 or later. The old documentation can be found here

"FAKE - F# Make" is intended to be an extensible build framework and therefore it should be as easy as possible to create custom tasks. This tutorial shows how to create a (very simple) custom task in C#. The same works for other .NET language like Visual Basic or F#.

Creating a custom task

Open Visual Studio and create a new C# class library called my MyCustomTask and create a class called RandomNumberTask:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
using System;
namespace MyCustomTask
{
    public class RandomNumberTask
    {
        public static int RandomNumber(int min, int max)
        {
            var random = new Random();
            return random.Next(min, max);
        }
    }
}

Now you can build, package and upload your build task to a NuGet feed. There are no special requirements, you can add dependencies to your NuGet package and define the API however you like.

Note: As FAKE 5 currently is a netcoreapp20 application you need to provide a binary in your NuGet package compatible with netcore. We suggest targetting netstandard20 or lower. as we update to newer netcore versions from time to time you should re-check and open a PR to change this text if it is outdated. (Just edit here with the pencil)

If you want to use FAKE's standard functionality (like globbing) within your CustomTask project, just reference the corresponding NuGet package and explore the FAKE namespace.

Using the custom task

Assume you pushed your custom task as MyTaskNuGetPackage to NuGet. Now you can use your CustomTask in the build script, by adding the

1: 
nuget MyTaskNuGetPackage

to your fake dependencies, see the relevant documentation for adding modules. This documentation now applies to your package too! One example would be:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
#r "paket:
nuget Fake.Core.Target
nuget MyTaskNuGetPackage //"
#load "./.fake/build.fsx/intellisense.fsx"

open Fake.Core
open MyCustomTask

Target.create "GenerateNumber" (fun _ ->
    // use custom functionality
    RandomNumberTask.RandomNumber(2,13)
        |> Trace.tracefn "RandomNumber: %d"
)

Target.runOrDefault "GenerateNumber"