Is there any way to include the SVN repository revision number in the version string of a .NET assembly? Something like Major.Minor.SVNRev
I\'ve seen mention of doi
svn info, tells you the version you are on, you can make a "pre-build" event in VS on your project to generate the assemblyinfo.cs by running svn info and parsing its results with a home grown command line app.
I have done this before, but quickly switched to just having ccnet pass it as a variable to nant.
Here's and C# example for updating the revision info in the assembly automatically. It is based on the answer by Will Dean, which is not very elaborate.
Example :
Modify the line with the AssemblyFileVersion to:
[assembly: AssemblyFileVersion("1.0.0.$WCREV$")]
Consider adding:
[assembly: AssemblyInformationalVersion("Build date: $WCNOW=%Y-%m-%d %H:%M:%S$; Revision date: $WCDATE=%Y-%m-%d %H:%M:%S$; Revision(s) in working copy: $WCRANGE$$WCMODS?; WARNING working copy had uncommitted modifications:$.")]
,
which will give details about the revision status of the source the assembly was build from.
Add the following Pre-build event to the project file properties:
subwcrev "$(SolutionDir)." "$(ProjectDir)Properties\AssemblyInfoTemplate.cs" "$(ProjectDir)Properties\AssemblyInfo.cs" -f
Consider adding AssemblyInfo.cs to the svn ignore list. Substituted revision numbers and dates will modify the file, which results in insignificant changes and revisions and $WCMODS$ will evaluate to true. AssemblyInfo.cs must, of course, be included in the project.
In response to the objections by Wim Coenen, I noticed that, in contrast to what was suggested by Darryl, the AssemblyFileVersion also does not support numbers above 2^16. The build will complete, but the property File Version in the actual assembly will be AssemblyFileVersion modulo 65536. Thus, 1.0.0.65536 as well as 1.0.0.131072 will yield 1.0.0.0, etc. In this example, there is always the true revision number in the AssemblyInformationalVersion property. You could leave out step 3, if you consider this a significant issue.
Edit: some additional info after having used this solution for a while.
I've added two tests to my AssemblyInfo.cst files:
#if(!DEBUG)
$WCMODS?#error Working copy has uncommitted modifications, please commit all modifications before creating a release build.:$
#endif
#if(!DEBUG)
$WCMIXED?#error Working copy has multiple revisions, please update to the latest revision before creating a release build.:$
#endif
Using this, you will normally have to perform a complete SVN Update, after a commit and before you can do a successful release build. Otherwise, $WCMIXED will be true. This seems to be caused by the fact that the committed files re at head revision after the commit, but other files not.
Addition To answer the comment by @tommylux.
SubWcRev can be used for any file in you project. If you want to display revision info in a web page, you could use this VersionInfo template:
public class VersionInfo
{
public const int RevisionNumber = $WCREV$;
public const string BuildDate = "$WCNOW=%Y-%m-%d %H:%M:%S$";
public const string RevisionDate = "$WCDATE=%Y-%m-%d %H:%M:%S$";
public const string RevisionsInWorkingCopy = "$WCRANGE$";
public const bool UncommitedModification = $WCMODS?true:false$;
}
Add a pre-build event just like the one for AssemblyInfo.cst and you will have easy access to all relevant SubVersion info.
Have a look at SubWCRev - http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html
The assembly version numbers are usually in assemblyinfo.cs
Read/skim these docs:
Accessing the Subversion repository from .NET using DotSVN
How to: Write a Task
Insert SVN version and Build number in your C# AssemblyInfo file
Compiling Apps With Custom Tasks For The Microsoft Build Engine
The MSBuildCommunityTasks svnversion mentioned in third reference would not perform with svn on Mac 10.5.6 and VS2008 C# project build inside Parallels hosting Vista (ie., across OS).
Write your own task to retrieve revision from repository using DotSVN:
using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using DotSVN.Common;
using DotSVN.Common.Entities;
using DotSVN.Common.Util;
using DotSVN.Server.RepositoryAccess;
namespace GetSVNVersion
{
public class GetRevision : Task
{
[Required]
public string Repository { get; set; }
[Output]
public string Revision { get; set; }
public override bool Execute()
{
ISVNRepository repo;
bool connected = true;
try
{
repo = SVNRepositoryFactory.Create(new SVNURL(Repository));
repo.OpenRepository();
Revision = repo.GetLatestRevision().ToString();
Log.LogCommandLine(Repository + " is revision " + Revision);
repo.CloseRepository();
}
catch(Exception e)
{
Log.LogError("Error retrieving revision number for " + Repository + ": " + e.Message);
connected = false;
}
return connected;
}
}
}
This way allows the repository path to be "file:///Y:/repo" where Y: is a Mac directory mapped into Vista.
It is possible but you shouldn't: the components of the assembly version string are limited to 16-bit numbers (max 65535). Subversion revision numbers can easily become bigger than that so at some point the compiler is suddenly going to complain.
Another answer mentioned that SVN revision number might not be a good idea because of the limit on the size of the number.
The following link provides not only an SNV revision number, but also a date version info template.
Adding this to a .NET project is simple - very little work needs to be done.
Here is a github project that addresses this https://github.com/AndrewFreemantle/When-The-Version/downloads
The following url may load slowly but is a step-by-step explanation of how to make this work (easy and short 3 or 4 steps)
http://www.fatlemon.co.uk/2011/11/wtv-automatic-date-based-version-numbering-for-net-with-whentheversion/