theKindOfMe

April 27, 2009

Automate adding/using of the SVN revision to/as the version of a .net project, through a build target

Filed under: Uncategorized — Tags: , , — yasi8h @ 11:16 am

a bit too complex title eh? sorry about that. i am finally blogging about something after a long while. its a good idea to use the revision number of your source tree in thee release versions of your software. because it will enable you to ‘connect and identify’ your release versions with your source tree’s different revisions. this is how i went about ‘automating’ it. note that I use svn as my source control system, and VS(2008) as my IDE at work.

What we are trying to do

Read the AssemblyInfo.cs file in a project and replace the(/a part of the) version number for that project with it’s svn revision number.

What you need

Code

<!-- Import of the MSBuildCommunityTask targets -->
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />

  <!-- to AssemblyInfo to include svn revision number -->
<Target Name="BeforeBuild">
	<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(ProgramFiles)\VisualSVN\bin">
	   <Output TaskParameter="Revision" PropertyName="Revision" />
        </SvnVersion>

	<FileUpdate Files="Properties\AssemblyInfo.cs"
                Regex="(\d+)\.(\d+)\.(\d+)\.(\d+)"
                ReplacementText="$1.$2.$3.$(Revision)" />
</Target>

Steps

  • Locate and open your project file. ex:- exampleProject.csproj in a editor.
  • Edit(SvnVersion node’s ToolPath attribute) the path of the svn bin folder, where you find the executables(ex: svn.exe, svnversion.exe…).
  • go the very end and add the following code to the file. be sure to add this code before the </project> tag closing. so that your code is still inside the <project> node.
  • Save the exampleProject.csproj file.
  • it’s done! now every time you build your project, it’s AssemblyInfo.cs file’s version will contain the svn revision number as it’s last part(x.x.x.svn-revison)

How

Let me start by saying that I am no expert in MSBuild(reffer MSDN for more info). MSBuild is the build system used by VS to build your projects. when you create a project file it is basically a MSBuild file. the build file tell MSBuild how your project should be build/compiled and about any other actions that needs to taken to insure a succesful build. when we needed to include the svn revision number of our project in the AssemblyInfo.cs file we simply added a build target called AssenblyInfo. inside that we uses two MSBuild CommunityTasks(“The MSBuild Community Tasks Project is an open source project for MSBuild tasks. The goal of the project is to provide a collection of open source tasks for MSBuild.“).

  • SvnVersion – “Summarize the local revision(s) of a working copy.” – we use this to get the svn revision of our project. the result(revision) is stroed in a property called Revision.
  • FileUpdate – “Replace text in file(s) using a Regular Expression.” – we use this task to replace the revision portion of the version string(x.x.x.revision) with the content of the Revision property.

and that’s it. pretty simple eh!

References

Advertisements

8 Comments »

  1. Thanks for the simple but effective post!

    Here is a slight mod for those .sln (solutions) with more than 1 project in it.

    I’m using CollabNet instead of VisualSVN, but same difference of course.

    Thanks again.

    Comment by granadaCoder — October 23, 2009 @ 7:55 pm

  2. Shoot, I always forget alot of “comment” areas strike out any xml.
    Here is the code.
    Key: { = Left Bracket
    } = Right Bracket

    {Target Name=”BeforeBuild”}

    {!– Find all those AssemblyInfo files in multiple projects –}
    {ItemGroup}
    {AssemblyInfoFiles Include=”$(MSBuildProjectDirectory)\**\*AssemblyInfo.cs” /}
    {/ItemGroup}

    {SvnVersion LocalPath=”$(MSBuildProjectDirectory)” ToolPath=”$(ProgramFiles)\CollabNet\Subversion Client”}
    {Output TaskParameter=”Revision” PropertyName=”SubVersionRevision” /}
    {/SvnVersion}

    {FileUpdate Files=”@(AssemblyInfoFiles)”
    Regex=”(\d+)\.(\d+)\.(\d+)\.(\d+)”
    ReplacementText=”$1.$2.$3.$(SubVersionRevision)” /}
    {/Target}

    Comment by granadaCoder — October 23, 2009 @ 7:57 pm

    • Thanks for your input 🙂

      Comment by yasi8h — October 24, 2009 @ 1:12 am

  3. […] update: As given in Markus comment, this code is a much better option: 1 2 3 4 5 6 7 8 9 10 11 12 13 <!– Import […]

    Pingback by Insert SVN version and Build number in your C# AssemblyInfo file - Florent Clairambault — July 2, 2011 @ 4:08 pm

  4. […] zu bauen. Was man mit MSBuild nicht hinbekommt, bekommt man auch nicht anderen Scripten hin. 2.) https://thekindofme.wordpress.com/200…-build-target/ Das ganze baut auf den MSBuild Community Tasks auf, die es auf Codeplex gibt. Diese Tasks sind […]

    Pingback by Subversion Revision in die Compilierung aufnehmen - Delphi-PRAXiS — June 1, 2012 @ 5:48 am

  5. I found every time I did a check in it updated the Revision – which means the next build updates the number again, which is a change that needs checking in – this emans that the project always got a pending change even when I’ve not changed anything

    One solution to this is to add to this is to put a AfterBuild section on it that will put it back to 0 at the end of the build. This way you don’t get a pending change to the AssemblyInfo.cs file every time you build (but the binaries still have the right version in them)

    {Target Name=”AfterBuild”}
    {FileUpdate Files=”Properties\AssemblyInfo.cs”
    Regex=”(\d+)\.(\d+)\.(\d+)\.(\d+)”
    ReplacementText=”$1.$2.$3.0″ /}
    {/Target}

    Comment by Pete McPhearson — November 26, 2012 @ 12:01 pm

  6. What I have found with his is that AssemblyInfo.cs must be in a “pending checkin” state to actually get this update. If this file itself has been commited, it will not update. Not sure why this is happening. Any thoughts?

    Comment by russdawgbass — December 3, 2014 @ 1:42 pm

  7. i have a revision Number > 65535 then Revision part of Version validation is failing, it only supports up to 65535 ?

    Comment by Nagaraju V — August 28, 2017 @ 12:09 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: