This project is read-only.

Assembly and Setup (MSI) Version Management using Subversion and MSBuild

Requirements:
Instructions
The default download on the right is the x86 installer and is already integrated with the Visual Studio default paths. If you do need to install 64-bit version due to other path reasons you can click the downloads link on the top and download the x64 version.

Some people have reported problems after following the above steps (not
everyone!). If you did everything above correctly, and it still does not work,
try adding the path (C:\Program Files\MSBuild\SvnTools.Targets\) to your PATH
Environment Variable. To do this,
  • Open the control panel
  • Open system
  • go to the advanced tab
  • click on the Environment Variables
  • find the Variable Path in the System variables
  • edit that and add C:\Program Files\MSBuild\SvnTools.Targets\
(Note: all paths are seperated by a semi-colon ; )

Adding to a Visual Studio Project
When you want a project to have the version management built into it. Simply edit the project file and add:

<Import Project="$(MSBuildExtensionsPath)\SvnTools.Targets\SvnTools.Tasks.VersionManagement.Tasks" />
<Target Name="BeforeCompile">
<UpdateVersion AssemblyInfoFiles="Properties\AssemblyInfo.cs" />
</Target>
<Target Name="AfterCompile">
<RevertVersionChange AssemblyInfoFiles="Properties\AssemblyInfo.cs" />
</Target>

Version Management Numbers

The SvnTools.Tasks.VersionManagement library contains two new MSBuild tasks. The first task, UpdateVersion, runs in the BeforeCompile target, and updates the files containing version number attributes. The second task, RevertVersionChange, reverts the change(s) made to the files so that no local modifications will show up.

The UpdateVersion task performs two duties:
  • In the case that the working copy is "clean", meaning it is not mixed-revision, and it has no local modifications, it updates the AssemblyVersion attribute's version number to be the revision of the working copy. In the case that the working copy isn't "clean", it sets the version number to "0.0.0.0".
  • It removes any "AssemblyFileVersion" attributes it finds. This attribute was considered dangerous, because if this attribute differs from "AssemblyVersion", then the .NET assembly version will be different than the version that the file reports, and we couldn't think of any circumstances where this wouldn't cause confusion.

The RevertVersionChange task simply reverts the change, causing the file to return to exactly what it looked like before.

Note that the version can only contain unsigned 16-bit numbers, so we're limited to 65,535 unique revision numbers. We will place the final 4 digits of the revision number into the final component, and the rest of the digits into the 3rd component.

Several examples of the possible changes to version numbers follow (assume a clean working copy at revision 31886):

"1.0.0.0" -> "1.0.3.1886"
"1.0.*" -> "1.0.3.1886"
"1.0" -> "1.0.3.1886"
"1.0.0.*" -> "1.0.3.1886"

If the working copy is not clean, of course, all resulting version numbers will be "0.0.0.0".

NEW Formatting is now possible on the version numbers. If you would like to change the version on your automation by adding the attribute Format to your msbuild updateversion task. For instance if you wanted to format your version according to the date and revision, use:

<Import Project="$(MSBuildExtensionsPath)\SvnTools.Targets\SvnTools.Tasks.VersionManagement.Tasks" />
<Target Name="BeforeCompile">
<UpdateVersion AssemblyInfoFiles="Properties\AssemblyInfo.cs" Format="yyyy.mm.dd.rev" />
</Target>
<Target Name="AfterCompile">
<RevertVersionChange AssemblyInfoFiles="Properties\AssemblyInfo.cs" />
</Target>

Or simply insert your own numbers as appropriate for major, minor and other numbers.

This is the default format (Though the Format attribute is not required)
<Import Project="$(MSBuildExtensionsPath)\SvnTools.Targets\SvnTools.Tasks.VersionManagement.Tasks" />
<Target Name="BeforeCompile">
<UpdateVersion AssemblyInfoFiles="Properties\AssemblyInfo.cs" Format="major.minor.rev_m.rev_l />
</Target>
<Target Name="AfterCompile">
<RevertVersionChange AssemblyInfoFiles="Properties\AssemblyInfo.cs" />
</Target>

MSBuild Process without modifying any Visual Studio Project

What if you don't want to modify your projects as it interferes with your Visual Studio Environment. This is usually a natural choice. All you need to do is create a new xml file that msbuild can run against and insert the following.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="build">
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\SvnTools.Targets\SvnTools.Tasks.VersionManagement.Tasks" />
<Target Name="build">
<CreateItem Include="**/Properties/AssemblyInfo.cs;**/AssemblyInfo.cs">
<Output TaskParameter="Include" ItemName="AssemblyInfoFiles" />
</CreateItem>
<UpdateVersion AssemblyInfoFiles="@(AssemblyInfoFiles)" />
<MSBuild Projects="MySolution.sln" Properties="Configuration=Debug" />
<RevertVersionChange AssemblyInfoFiles="@(AssemblyInfoFiles)" />
</Target>
</Project>

There you go! Now you have all assembly info files updating in a directory, the project building and the everything reverting.

Versioning Deployment Projects

If you have a vdproj in your solution and wish to update the version number on it, you will need to create a new msbuild.proj file and call msbuild on it while referring to the vdproj file like so:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="build">
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\SvnTools.Targets\SvnTools.Tasks.VersionManagement.Tasks" />
<Target Name="build">
<UpdateVersion DeploymentProjectFiles="MyProjectSetup.vdproj" DeployFormat="major.rev_m.rev_l" />
<Exec Command="&quot;$(DevEnvDir)\devenv&quot; MyProjectSetup.vdproj /rebuild" />
<RevertVersionChange DeploymentProjectFiles="MyProjectSetup.vdproj" />
</Target>
</Project>

By default this will update the version according to your format specified as well as generating a new ProductCode and PackageCodes. If you want to specify those to not change, simply use the ProductCodes, PackageCodes and UpgradeCodes attributes (set these to a valid set of Guids (separated by semi-colon or through itaskitems)

Combining MSBuilds for both Assemblies and Setup Projects

And if you are looking to do both your assemblies and your vdproj files. Then simply create an msbuild file like so:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="build">
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\SvnTools.Targets\SvnTools.Tasks.VersionManagement.Tasks" />
<Target Name="build">
<CreateItem Include="**/Properties/AssemblyInfo.cs;**/AssemblyInfo.cs">
<Output TaskParameter="Include" ItemName="AssemblyInfoFiles" />
</CreateItem>
<CreateItem Include="**/*.vdproj;*.vdproj">
<Output TaskParameter="Include" ItemName="DeploymentProjectFiles" />
</CreateItem>
<UpdateVersion AssemblyInfoFiles="@(AssemblyInfoFiles)" DeploymentProjectFiles="@(DeploymentProjectFiles)" />
<Exec Command="&quot;$(VS90COMNTOOLS)..\IDE\devenv&quot; MySolution.sln /build" />
<RevertVersionChange AssemblyInfoFiles="@(AssemblyInfoFiles)" DeploymentProjectFiles="@(DeploymentProjectFiles)" />
</Target>
</Project>

Enjoy :)

Last edited Nov 20, 2009 at 7:25 PM by nyxtom, version 23