Reputation: 17564
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
Upvotes: 3
Views: 3295
Reputation: 299
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
Upvotes: 1
Reputation: 17564
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
Upvotes: 2
Reputation: 97365
If it's Windows, you can use SubWCRev from TortoiseSVN
svn export
+ subwcrev wc-path VersionInfo.cs.tpl VersionInfo.cs
)Upvotes: 2
Reputation: 122011
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
Upvotes: 2
Reputation: 10964
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
Upvotes: 0