Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I automatically set assembly version during nightly build?

We have a nightly build process that automatically versions all C++ projecs. Here's how it works. There's a common header file VersionNumber.h that has a specific #define for the version number. The nighly build checks this file out, increments the integer behind that #define and checks it in. All Visual C++ projects #include that header into their resource files and use that define for specifying the version (version is smth like 1.0.3.ThatNumber).

So far so good. Now I'd like to have the same for the C# class libraries built in the same daily build. Currently they all have

[assembly: AssemblyVersion("1.0.*")]

in the AssemblyInfo.cs files and libraries end up with 1.0.HorribleNumber.AnotherHorribleNumber as the version and the two numbers don't correlate to the number used by C++ projects.

How do I have the same determenistic automatic version numbering in my C# projects with minimal effort?

like image 270
sharptooth Avatar asked Jul 27 '10 08:07

sharptooth


2 Answers

Firstly you can specify the full version as follows:

[assembly: AssemblyVersion("1.0.9.10")]

Secondly, a common approach to make this a little more straightforward (and echoes your C++ approach) is to have a single Version.cs file (name unimportant) that sits in a common location that has the version attributes in it. You can then add this file as a link to all your cs projects, remembering to remove the version attributes from your AssemblyInfo.cs files. In this way you only have a single file to update (before running your build). You can also put other common assembly attributes in your Version.cs file such as: NeutralResourcesLanguage or CLSCompliant.

If you do not use a single "Version.cs" approach, then you can work recursively through your source code directory structure, and update AssemblyInfo files individually (before running your build).

It may not be relevant to you, but the version numbers (in AssemblyVersion) have a maximum range of 16bit. I have seen this become an issue where dates have been used for these numbers. If you wish to have more latitude then AssemblyFileVersion does not have these restriction, but is purely for informational purposes only in .Net, rather than part of the assembly's identity. It is common to set AssemblyVersion and AssemblyFileVersion to the same values as some tools display combinations of these.

See the following for more on AssemblyVersion vs AssemblyFileVersion:

What are differences between AssemblyVersion, AssemblyFileVersion and AssemblyInformationalVersion?

like image 88
Tim Lloyd Avatar answered Nov 01 '22 06:11

Tim Lloyd


We use FinalBuilder for automated builds (we have TeamCity call it), and it does this sort of stuff automatically (i.e. it can get a build number from somewhere else (ini file, environment variable, command line, whatever) and then update all the assembly versions for you with the build number.)

Obviously not the only way to do it, but if you haven't used something like FinalBuilder, then give it a try - our experience is you start to wonder why you previously spent so long being clever with Makefiles and batch files...

But if you don't want to do that, can you get the same process which generates/modifies the VersionNumber.h file to also spit out VersionNumber.cs, with an AssemblyVersion line in it? Then you can just include that file in your project.

The AssemblyVersion directive doesn't need to be in the same file as all the rest of your AssemblyInfo stuff.

like image 45
Will Dean Avatar answered Nov 01 '22 07:11

Will Dean