Question

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

Was it helpful?

Solution 3

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

OTHER TIPS

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
  • 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)

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top