3

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Anders
  • 17,306
  • 10
  • 76
  • 144

5 Answers5

2

Is there an easy way of getting the working copy revision from cmd?

There is an executable called svnversion.exe which prints on standard output the revision. If you ensure this is in your PATH you could call this.

To insert that number into the VersionInfo.cs file

You could generate the VesionInfo.cs file completely, or partially, from a batch file:

@echo off

FOR /F %%A in ('svnversion') do SET SVN_REV=%%A

echo [assembly: AssemblyVersion("0.0.1.%SVN_REV%")]     >  VersionInfo.cs
echo [assembly: AssemblyFileVersion("0.0.1.%SVN_REV%")] >> VersionInfo.cs

EDIT:

Updated batch file to cope with revision numbers of the format RR, NN:RR and NN:RRM where NN is an integer and RR is the revision:

@ECHO off

FOR /F "tokens=1,2 delims=:M" %%A in ('svnversion') do SET PART_1=%%A&SET PART_2=%%B

IF NOT DEFINED PART_2 (
SET SVN_REV=%PART_1%
)

IF NOT DEFINED SVN_REV (
SET SVN_REV=%PART_2%
)

ECHO [assembly: AssemblyVersion("0.0.1.%SVN_REV%")]     >  VersionInfo.cs
ECHO [assembly: AssemblyFileVersion("0.0.1.%SVN_REV%")] >> VersionInfo.cs
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Thanks for feedback, I downloaded svnversion.exe and it outputs in this format 33:37M where 37 is the revision, so your batch script will create an invalid Version number, I'm thinking of creating a little C# console program and do the parsing of revision and creating of file there, but maybe that is overkill, are there better ways? – Anders Feb 27 '12 at 14:20
  • @Anders, updated answer after some fiddling with DOS to extract the revision number. – hmjd Feb 27 '12 at 14:54
  • @Anders - you have mixed working copy. Is it intended to be so? – Lazy Badger Feb 28 '12 at 08:52
  • @Anders - you have mixed working copy. Is it intended to be so? – Lazy Badger Feb 28 '12 at 08:52
  • I've just done a checkout from sourceforge SVN with TortoiseSVN, I'm no SVN expert :D This is the repo svn://svn.code.sf.net/p/freepie/code/trunk – Anders Feb 28 '12 at 13:10
2

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
Anders
  • 17,306
  • 10
  • 76
  • 144
1

Download MSBuild Community Task and install it.

Open your .csproj and at the end (before closure </project> tag)

Paste the following code (don't change the <Import> tag):

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" /> 

After the <Import> tag paste the following:

  <Target Name="BeforeBuild">
      <SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(ProgramFiles)\TortoiseSVN\bin">
          <Output TaskParameter="Revision" PropertyName="Revision" />
      </SvnVersion>
      <FileUpdate Files="Properties\AssemblyInfo.cs" Regex="(\d+)\.(\d+)\.(\d+)\.(\d+)" ReplacementText="$1.$2.$3.$(Revision)" />
  </Target>

Look at the Attribute ToolPath inside SvnVersion tag, there is where you must identify the location where in your machine is the svnversion.exe binary file.

Assuming that you have the TortoiseSVN software installed, the Path to it is: C:\ProgramFiles\TortoiseSVN\bin\ You could also use the VisualSVN binaries (in this case, binary file is located at C:\ProgramFiles\VisualSVN\bin\)

With this modification in your .csproj, in every build project, the MSBuild will first call the svnversion.exe (with argument, the current solution directory) and the svnversion will return the revision number for that repository. In the FileUpdate tag, MSBuild will lookup for the regex pattern and then replace with the current values for Major, Minor and Build ($1, $2 and $3 respectively) and update the Revision with the variable Revision value

1
  • If it's VisualStudio, it's Windows
  • If it's Windows, you can use SubWCRev from TortoiseSVN

    1. Write template of VersionInfo.cs into repository instead of final file, there changeable part of data replaced by (appropriate) SubWCRev-keyword
    2. On every build-process run SubWCRev, which write final file with actual data from template and WC-data (full builder can be /started/ as simply as svn export + subwcrev wc-path VersionInfo.cs.tpl VersionInfo.cs)
Lazy Badger
  • 94,711
  • 9
  • 78
  • 110
0

You could try using Keyword Substitutes to replace the number as you're committing. They have this as an example:

$Rev$:     Revision of last commit
$Author$:  Author of last commit
$Date$:    Date of last commit
jprofitt
  • 10,874
  • 4
  • 36
  • 46
  • For this to work you'll need to update the VersionFile.cs each time to get a new revison this the $Rev$ only reflects when the actual file changed... – Anders Feb 27 '12 at 13:40
  • $Rev$ keywords expands to bad (for this task) string, like this `$Revision: 1028 $` – Lazy Badger Feb 28 '12 at 08:56