Label Cloud

Thursday, June 07, 2007

Making your assemblies describe themselves

One of the biggest hurdles of the release process is to make sure you know exactly what you are releasing. To make that job a little easier, I've modified my project files to generate and include build related information in the assembly properties.

I am using a great open source project MSBuild Community Tasks.

Three of the tasks included are Time, Version and AssemblyInfo. Here's the process to incorporate them into the project.

Add a new project to the visual studio solution. I made this a C# project to make it easier to integrate with Visual Studio. Then open the project in your favorite text editor. Scroll down to the <Target Name="Build"> line. Now make the contents of the target the following

<Time Format="yyyy/MM/dd HH:mm:ss">
<Output TaskParameter="FormattedTime" PropertyName="buildDate" />
</Time>
<AssemblyInfo CodeLanguage="CS" OutputFile="GlobalInfo.cs"
AssemblyDescription="Build Date: $(buildDate)
Configuration: $(Configuration)$(Platform)" />
<Version VersionFile="version.txt">
<Output TaskParameter="Major" PropertyName="Major" />
<Output TaskParameter="Minor" PropertyName="Minor" />
<Output TaskParameter="Build" PropertyName="Build" />
<Output TaskParameter="Revision" PropertyName="Revision" />
</Version>
<AssemblyInfo CodeLanguage="CS" OutputFile="AssemblyVersion.cs" AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)" AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />

Now add a new text file to the project called version.txt and edit to have a single line "1.0.0.0"

That is almost it. After compiling the above project, you will receive two new files: GlobalInfo.cs and AssemblyVersion.cs. GlobalInfo.cs will contain the BuildDate and Configuration used during compilation. AssemblyVersion.cs will contain the version information based on the version.txt file. See help for AssemblyVersion task for how to make it increment the version number during the build.

Another task is to add the two new files as a replacement to the AssemblyInfo.cs that's usually a part of every solution. I do it with a text editor to make sure that they point to the file outside of the local project but rather to the newly generated files. That makes files read-only. The last task is to make sure that build dependency is properly set and the "GenerateVersion" project will be built first.

What you achieve after doing all of the above is that all compiled assemblies (.DLL and .EXE) will have a shared version number across multiple assemblies. They will also have in their properties tab information on when they where built and the configuration used during built. That can be used to troubleshoot and to easy production deployments.


Share/Save/Bookmark

No comments: