views:

808

answers:

7

I'm using TortoiseSVN and Visual Studio 2008. Is there any way to update my project's assemblyinfo.cs with svn's version in every build?

For example, 1.0.0.[svn's version] -> 1.0.0.12

+2  A: 

How do you feel about a Visual Studio addin doing it?

popester
Heh, didn't know this one, looks interesting.
RedGlyph
Doesn't look like that addin allows using the Subversion revision number as the value though.
Lasse V. Karlsen
I created an SVN version plug-in (http://happyturtle.codeplex.com/) for the Build Version Increment project. This SVN plug-in will pull the latest change revision number from your working copy and allow you to use that in your version number, which should accomplish exactly what you're trying to do.
grimus
+1  A: 

Yes, you can add a pre-build event that calls a script which

  • calls svn info to extract the current revision number (if you do an update before, you can directly include the keyword $Revision$ in a file, check also this post);
  • modifes your Properties\AssemblyInfo.cs file accordingly.

What I usually do is transform a AssemblyInfo.cs template when the project is built. The script is necessary to adapt the form of $Revision$ to the syntax of this file, unfortunately.

The interesting properties are (where the template strings are between '$'):

[assembly: AssemblyVersion("$v$.$build$.$Last Changed Rev$")]
[assembly: AssemblyFileVersion("$v$.$build$.$Last Changed Rev$")]

Edit: svn info is part of the standard SVN client, not TortoiseSVN as pointed out in another post. Easy to install though. However, TortoiseSVN comes with SubWCRev.exe which transforms a file with keyword substitution, so it would do the trick if you update your local copy.

RedGlyph
+1  A: 

In Visual Studio 2008, you can define a pre-build event that can launch a script or small program. Within this script or program, you can use the svnversion command to get the repository revision, then parse your AssemblyInfo.cs and modify the version number to what you desire.

I'm not sure if TortoiseSVN comes with svnversion, but it does come with the Windows SVN build provided by CollabNet

Jeras
+1  A: 

You could use the $Rev$ svn keyword but that will give you the last revision of the file, I think you want to get the HEAD revision number.

Give a look to this question:

CMS
+5  A: 

You could use the SubWCRev tool which comes with TortoiseSVN (also available separately).

Either run it from a command line or use the COM-Object it offers.

The SubWCRev command line tool replaces keywords inside a file with information from your svn working copy. An example is shown in the docs.

Stefan
But if AssembyInfo.cs is modified it appears in the working copy as a modification. If you commit it and rebuild it changes again. How can this be done without modifying files that are under source control?
Assaf Lavie
@Assaf: That's why one should not check in the AssemblyInfo.cs file if it is automatically generated.
chiccodoro
@chiccodoro, I think you missed the point of my question. Part of the version string stays constant and part is auto-generated (the last number).
Assaf Lavie
@Assaf: You keep an AssemblyInfo.template.cs commited that is and the AssemblyInfo.cs that is generated every build from your msbuild you keep it out of subversion by ignoring it.
VirtualBlackFox
+2  A: 

I do this in my build script:

<SvnInfo LocalPath=".">
  <Output TaskParameter="Revision" PropertyName="BuildRev" />
</SvnInfo>
<FileUpdate Files="protobuf-net\Properties\AssemblyInfo.cs"
  Regex='(\[\s*assembly:\s*AssemblyVersion\(\s*"[^\.]+\.[^\.]+)\.([^\.]+)(\.)([^\.]+)("\)\s*\])'
  ReplacementText='$1.$2.$(BuildRev)$5' />

using the community build tasks. This essentially applies a regex to the AssemblyInfo.cs, replacing the current revision with the svn revision.

Marc Gravell
Any way to use these build tasks without installing them? In other words is it possible to have them versioned along with the source code so developers don't have to be aware of them or worry about installing them?
jpierson
@jpierson - I honestly haven't tried, so I don't know
Marc Gravell
Yes, it is possible. You have to have the dll versionned (you will find the code here : http://msbuildtasks.tigris.org/ ) and write the following line : <UsingTask AssemblyFile="NameOfTheDll.dll" TaskName="SvnInfo" />. You will have to write one line for each task you want to use.
Benjamin Baumann
A: 

I created an SVN version plug-in for the Build Version Increment project (which is named in popester's answer). This SVN plug-in will pull the latest change revision number from your working copy and allow you to use that in your version number, which should accomplish exactly what you're trying to do.

grimus