19

I'm developing a program in C# .net in visual studio and version controlling it with tortoise SVN.

at present I'm creating the assembly version based on the build number.

Is there a way I can link the final part of a projects assembly version to the revision number in tortoise SVN instead, eg:

pseudo code:

[assembly: AssemblyVersion("1.0.0."+SvnRevisionNumber.ToString())]

This would ensure that my assemblies were named after, not their build number, but after the last revision number committed to the repository.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Sigurd V
  • 5,077
  • 4
  • 18
  • 27
  • 1
    Here is a solution that seems well-thought-out, but I haven't had the opportunity to test it yet: http://www.diogonunes.com/blog/embed-svns-revision-into-assemblyinfos-version-number/ – kmote Apr 24 '18 at 14:05

4 Answers4

10

What I use is calling a cmd file at the post build event:

@echo off

if %1x==x goto ERROR

SET ProjectDir=%1
SET SubWCRev="C:\Program Files\TortoiseSVN\bin\SubWCRev.exe"
if exist %SubWCRev% goto USESUBWCREV

REM Default to copying a default version
copy %ProjectDir%\Properties\AssemblyInfo.default.cs %ProjectDir%\Properties\AssemblyInfo.cs
echo default
goto END

REM We don't want to modify AssemblyInfo.cs every time, only when a revision
REM changes. Thus, we want to first compare the last compiled revision with
REM the current revision, and only update if they've changed.
:USESUBWCREV
%SubWCRev% %ProjectDir% %ProjectDir%\Properties\rev.subwcrev-template %ProjectDir%\Properties\rev.current.tmp
if exist %ProjectDir%\Properties\rev.last-build.tmp goto CHECKREV
goto NEWREV

REM Fetch the current revision and compare to last-build revision
:CHECKREV
fc %ProjectDir%\Properties\rev.last-build.tmp %ProjectDir%\Properties\rev.current.tmp > NUL
REM Only update if it's a new revision
if errorlevel 1 goto NEWREV
goto END

REM Current revision doesn't match last-build revision. Update!
:NEWREV
echo newRev
if exist %ProjectDir%\Properties\rev.last-build.tmp del %ProjectDir%\Properties\rev.last-build.tmp
copy %ProjectDir%\Properties\rev.current.tmp rev.last-build.tmp
echo use template
%SubWCRev% %ProjectDir% %ProjectDir%\Properties\AssemblyInfo.subwcrev-template.cs %ProjectDir%\Properties\AssemblyInfo.cs
echo done
goto END

:ERROR
echo Usage: %0 project_dir
echo.
echo For example:
echo    %0 C:\projects\MyProjectDir
echo.
goto END

:END
Avi Turner
  • 10,234
  • 7
  • 48
  • 75
  • 2
    Nice script, I used it but found a problem when it's called from a build event like this: UpdateSvnVerToAssembly.bat "$(ProjectDir)" $(ProjectDir) contains a trailing slash, but SubWCRev has a bug that fails on paths that contain spaces and trailing slash. The workaround is to insert a "." after the trailing slash as well as quotes on all the paths, so it would look like this in the bat file: %SubWCRev% "%ProjectDir%." "%ProjectDir%Properties\AssemblyInfo.subwcrev-template.cs" "%ProjectDir%Properties\AssemblyInfo.cs" – Grant Oct 07 '13 at 02:08
  • One question: *"we want to first compare the last compiled revision with the current revision, and only update if they've changed"* - where does your script do this check? – vgru Feb 13 '14 at 09:51
  • @Groo You are right. when I wrote this answer, I have edited my script for omitting private parts. This some how did not come through. I have edited the answer. – Avi Turner Feb 13 '14 at 11:24
  • I've recently started at a firm that uses the SVN number as the revision value - [assembly: AssemblyFileVersion("1.2.3.$WCREV$")] - Once we get to 65535, what can we do then as the max value is a ushort. – Harag May 05 '17 at 08:13
2

I'd be looking into the MSbuild Extension Pack.

There is a Subversion extension which will retrieve the SVN stuff for you automatically. Link here: MSBuild Subversion Extension Help

Secondly you could then use that with other extensions to set the version of a directory programatically. Depending on your layout in your SCM system, you might be able to version each directory in it's own repository like this.

Spence
  • 28,526
  • 15
  • 68
  • 103
2

Check the spaces and quotes in C#Project file.

<PreBuildEvent>
        if exist "C:\Program Files\TortoiseSVN\bin\SubWCRev.exe" "C:\Program Files\TortoiseSVN\bin\SubWCRev.exe" "$(ProjectDir)." "$(ProjectDir)Properties\AssemblyInfo.Base.cs" "$(ProjectDir)Properties\AssemblyInfo.cs"
</PreBuildEvent>
Nalan Madheswaran
  • 10,136
  • 1
  • 57
  • 42
  • 1
    This is a great answer. Solved my problem completely. To see how to modify the AssemblyInfo.Base.cs file see https://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev-keywords.html. Also be sure to remove your AssemblyInfo.cs file from SVN, or every time you run this it will make a change you need to save to SVN and it'll make a loop. – BoredBsee Apr 14 '20 at 15:57
1

You can use something like this to get the svn revision number.

<echo message="Retrieving Subversion command line: ${rvsnCommandLine} into ${deployment.SourceDir}"/>
     <exec program="svn.exe" workingdir="${deployment.SourceDir}" commandline='update ${rvsnCommandLine}' failonerror="false"/>

     <echo message="Retrieving Subversion revision number ${svn.revision}"/>
     <exec
       program="svn.exe"
       commandline='log "${deployment.SourceDir}" ${rvsnCommandLine} --xml --limit 1'
       output="${deployment.SourceDir}\_revision.xml"
       failonerror="false"/>
     <xmlpeek
       file="${deployment.SourceDir}\_revision.xml"
       xpath="/log/logentry/@revision"
       property="svn.revision"
       failonerror="false"/>
     <echo message="Using Subversion revision number: ${svn.revision}"/>

It pretty much outputs the svn revision to a xml file then xml peeks to get the revision number.

You could perhaps use this as a pre-build event and then update your assemblyinfo with the new version number.

Also check this thread for more info SVN Revision Version in .NET Assembly w/ out CC.NET

Community
  • 1
  • 1
garethb
  • 3,951
  • 6
  • 32
  • 52