Search code examples
visual-studio-2010svnrevisionassemblyinfo

Getting SVN working copy revision number into VersionInfo.cs


All our projects in our SLN shares a VersionInfo.cs which holds the project version number

[assembly: AssemblyVersion("0.0.1.0")]
[assembly: AssemblyFileVersion("0.0.1.0")]

I want to staticly define the 3 first parts of the version number and the last part i want to be the working copy SVN revision.

Step number one is to define a pre-build event in VS that triggers a cmd script, is there an easy way of getting the working copy revision from cmd?

Step number two is to insert that number into the VersionInfo.cs file

Theres probably more elegant ways of doing this, if you have one in store just keep in mind that this is a open source project and we do not have a fancy build server or anything like that. The deployment procedure is just put the project in release mode and build :D


Solution

  • hmjd:s solution was only half there, if you write to the file every time you build all projects refering to the Versionfile needs to rebuild even if nothing has changed, I altered the script to only write to the file if its a new revision number

    @ECHO off
    
    FOR /F "tokens=1,2 delims=:M" %%A in ('svnversion ../ -c') do SET PART_1=%%A&SET PART_2=%%B
    
    SET file=../VersionInfo.cs
    
    IF NOT DEFINED PART_2 (
    SET SVN_REV=%PART_1%
    )
    
    IF NOT DEFINED SVN_REV (
    SET SVN_REV=%PART_2%
    )
    
    set result=0
    
    for /f "tokens=3" %%f in ('find /c /i ".%SVN_REV%." %file%') do set result=%%f
    
    IF %result% gtr 0 (
    GOTO end
    )
    
    ECHO using System.Reflection; > %file%
    ECHO [assembly: AssemblyVersion("0.1.%SVN_REV%.0")]     >>  %file%
    ECHO [assembly: AssemblyFileVersion("0.1.%SVN_REV%.0")] >> %file%
    
    :end