FAKE - F# Make - A DSL for build tasks and more FAKE
6.1.3
Edit this page

Lookig for older versions of the documentation, pre FAKE v6? See 

v5.fake.build

Generating AssemblyInfo Files

In this article the AssemblyInfo task is used in order to set specific version information to .NET assemblies.

If you succeeded with the Getting Started tutorial, then you just have to modify your BuildApp target to the following:

#r "paket:
nuget Fake.DotNet.AssemblyInfoFile
nuget Fake.DotNet.MSBuild
nuget Fake.Core.Target //"
open Fake.Core
open Fake.DotNet

Target.create "BuildApp" (fun _ ->
    AssemblyInfoFile.createCSharp "./src/app/Calculator/Properties/AssemblyInfo.cs"
        [   AssemblyInfo.Title "Calculator Command line tool"
            AssemblyInfo.Description "Sample project for FAKE - F# MAKE"
            AssemblyInfo.Guid "A539B42C-CB9F-4a23-8E57-AF4E7CEE5BAA"
            AssemblyInfo.Product "Calculator"
            AssemblyInfo.Version version
            AssemblyInfo.FileVersion version]

    AssemblyInfoFile.createFSharp "./src/app/CalculatorLib/Properties/AssemblyInfo.fs"
        [   AssemblyInfo.Title "Calculator library"
            AssemblyInfo.Description "Sample project for FAKE - F# MAKE"
            AssemblyInfo.Guid "EE5621DB-B86B-44eb-987F-9C94BCC98441"
            AssemblyInfo.Product "Calculator"
            AssemblyInfo.Version version
            AssemblyInfo.FileVersion version]

    MSBuild.runRelease id buildDir "Build" appReferences
        |> Trace.logItems "AppBuild-Output: "
)

As you can see generating an AssemblyInfo.fs file is pretty easy with FAKE. You can read more about the C# and F# AssemblyInfo tasks in the API docs.

Setting the version Number

The version parameter can be declared as a property or fetched from a build server like TeamCity:

let version =
  match BuildServer.buildServer with
  | TeamCity -> buildVersion
  | _ -> "0.2"

The file version is set by FAKE

Storing the githash in the AssemblyInfo File

Storing the githash with the assembly can make it easier to identify exactly what code is running. There isn't an attribute that directly fits with doing this, but one way is by storing it as Metadata (warning: this attribute is only available in .NET 4.5 and above)

If your solution is inside a git repository you can get the git hash like this (remember to add Git module and add open Fake.Git):

let commitHash = Information.getCurrentHash()

And set like this:

AssemblyInfo.Metadata("githash", commitHash)

One of the easiest ways to retrieve this hash is to load and use a reflector program, like ILSpy:

Checking the git hash of an assembly

Using the SolutionInfo Approach

Some companies split their AssemblyInfo into a SolutionInfo.cs which is shared by all projects and a specific AssemblyInfo.cs per project which contains the product data. All versioning data is generated by the AssemblyInfo task into the SolutionInfo.cs and the AssemblyInfo.cs files are edited manually. This could look like this:

SolutionInfo.cs is shared between projects

The above picture and code within is from FAKE v4 (but the concept is very similar with fake 5 and above)

The generated SolutionInfo.cs looks like this:

Generated SolutionInfo.cs