Skip to content

Instantly share code, notes, and snippets.

@chrisoldwood
Last active May 18, 2016 10:42
Show Gist options
  • Save chrisoldwood/7576895756bcbd0d3c62 to your computer and use it in GitHub Desktop.
Save chrisoldwood/7576895756bcbd0d3c62 to your computer and use it in GitHub Desktop.
Visual C# pre-build step to generate a common AssemblyVersionInfo.cs from a version number defined by a couple of environment variables.
<PreBuildEvent>
if not defined PRODUCT_VERSION set PRODUCT_VERSION=0.0.0
if not defined BUILD_NUMBER set BUILD_NUMBER=1
echo using System.Reflection;&gt;"$(SolutionDir)Source\AssemblyVersionInfo.cs"
echo [assembly: AssemblyVersion("%25PRODUCT_VERSION%25.%25BUILD_NUMBER%25")]&gt;&gt;"$(SolutionDir)Source\AssemblyVersionInfo.cs"
</PreBuildEvent>
@chrisoldwood
Copy link
Author

chrisoldwood commented Jan 14, 2016

This is a technique for injecting a build number into a global file called AssemblyVersionInfo.cs. The product-common assembly properties are split into two files stored near the root of the source tree - AssemblyProductInfo.cs & AssemblyVersionInfo.cs - with the latter generated by a pre-build step in the first assembly to be compiled. These two files are linked into each assembly project. Only the static properties file AssemblyProductInfo.cs is checked into the VCS.

On the CI server you create two environment variables, one for the fixed part of the product version and the other comes from the build number counter - PRODUCT_VERSION and BUILD_NUMBER. These are concatenated above to make the full, 4-part version number.

Notes:

The reason we use 0.0.0.1 for the default build number is to avoid the default (1.0.0.0) so we can easily see when we've forgotten to link the global ones in, and because FX Copy complains if you use 0.0.0.0 as it thinks you haven't set one.

Be careful when pasting the snippet into the Build Events edit box in the Visual Studio UI. The snippet above was taken from the .csproj file and so has the >, >> and % characters escaped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment