views:

2480

answers:

7

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 doing this with something like CC.NET (although on ASP.NET actually), but is there any way to do it without any extra software? I've done similar things in C/C++ before using build batch scripts, but in was accomplished by reading the version number, then having the script write out a file called "ver.h" everytime with something to the effect of:

#define MAJORVER 4
#define MINORVER 23
#define SOURCEVER 965

We would then use these defines to generate the version string.

Is something like this possible for .NET?

+2  A: 

Have a look at SubWCRev - http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html

The assembly version numbers are usually in assemblyinfo.cs

Will Dean
A: 

Stackoverflow show the revision number en the footer, maybe Jeff can tell us how he do this.

Jedi Master Spooky
+2  A: 

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.

DevelopingChris
+1  A: 

If you want to update the version number in a projects AssemblyInfo.cs you may be interested in this article:

CodeProject: Use Subversion Revision numbers in your Visual Studio Projects

If you enable SVN Keywords then every time you check in the project Subversion scans your files for certain "keywords" and replaces the keywords with some information.

For example, At the top of my source files I would create a header contain the following keywords:

'$Author:$
'$Id:$
'$Rev:$

When I check this file into Subversion these keywords are replaced with the following:

'$Author: paulbetteridge $
'$Id: myfile.vb 145 2008-07-16 15:24:29Z paulbetteridge $
'$Rev: 145 $

spoon16
from that article: "The limitation of using the $Rev$ keyword is that it only gives you the revision of the file and not the entire project so using $rev$ keyword cannot work as part of my software versioning." so you would have to edit/commit assembly.cs on every revision to keep it updated?
Lucas
+3  A: 

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.

Unfortunately dotSVN is no longer maintained and one of the things that was left out was repositories on http.
Martin
+5  A: 

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.

Wim Coenen
A: 

You can use a shared Assembly Version file that you can reference in all of your projects.

UppercuT does this - http://ferventcoder.com/archive/2009/05/21/uppercut---automated-builds---versionbuilder.aspx

This will give you an idea of what you can do to get versions in your assemblies.

ferventcoder