Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use svn revision number in application version

In a VS2010 solution (not .NET), I'm looking to include the svn revision number as part of the application version.

we do not use makefiles at the moment, only the VS solution/project settings.

I'd like to get the working copy revision number at compile time, store it into a variable, so it can be used later on in the code to display the version number.

So far, I've successfully used svnversion to get the latest version of the working copy as a pre-built event.

"C:\Program Files\CollabNet\Subversion Client\svnversion.exe" -n $(SolutionDir)

At build time, I can see the correct revision number being returned into the output console.

Now, the question is, how can store this value into a variable that can be used within the code?

I've tried defining a pre compiler variable (_SVNREV) and using it to save the result from the above cmd, directly from the pre-build event box, but that doesn't work.

_SVNREV="C:\Program Files\CollabNet\Subversion Client\svnversion.exe" -n $(SolutionDir)
%_SVNREV%="C:\Program Files\CollabNet\Subversion Client\svnversion.exe" -n $(SolutionDir)
%_SVNREV="C:\Program Files\CollabNet\Subversion Client\svnversion.exe" -n $(SolutionDir)
$(_SVNREV)="C:\Program Files\CollabNet\Subversion Client\svnversion.exe" -n $(SolutionDir)

none of these actually work.

RESOLUTION: I was heading nowhere trying to update a variable from within the VS env. so I took another route, calling a script as a pre-build step, that gets the svn revision of the working copy and then creates a header file with that info.

Here's the svnrev.bat for anyone interested:

@echo off
set cmd="C:\"Program Files\CollabNet\Subversion Client"\svnversion.exe -n %1 "
set versionfile=%1/version.h
FOR /F %%i IN ('%cmd%') DO SET SVNVER=%%i

echo Detected program revision %SVNVER% in %1
echo #pragma once > %versionfile%
echo #define _SVNVER "%SVNVER%" >> %versionfile%
like image 957
fduff Avatar asked Aug 21 '12 14:08

fduff


2 Answers

the best guess i came up with so far is to generate a header file in the pre-build event that contains the svnversion.

e.g. i create a batch-script version.bat:

@echo off
FOR /F "tokens=*" %%i IN ('call svnversion') DO echo #define SVNVERSION "%%i" > svnversion.h

and wherever i want to get the svnversion, i simply #include "svnversion.h"

like image 156
umläute Avatar answered Oct 22 '22 09:10

umläute


If you're doing builds, you should have a standard build system. I recommend using Jenkins. It's open source, and easy to use.

One of the neat tricks Jenkins has is inserting in some nice environment variables for your use:

  • SVN_REVISION - The Subversion revision number
  • BUILD_NUMBER - The build number in Jenkins
  • JOB_NAME - The job name in Jenkins

Once you start using Jenkins for your builds, the latter two environment variables will probably become more valuable than the Subversion revision itself since the latter two will link your code back to the project, and all sorts of good information.

Jenkins is a continuous build engine. You can set up a Jenkins job to build on command, whenever there's a checkout, or at certain times of the day. You can even send a build-on-command via a wget on a URL.

By using a build server, you eliminate the good possibility that the configuration of a particular system has some sort of side effect on the build. The build server is a known configuration and will produce your official builds.

This is a bit more than you probably wanted to know. (Specifically, you could have used the svnversion command to retrieve the revision number, and then proceed from there). But, it will greatly improve your build process.

like image 26
David W. Avatar answered Oct 22 '22 11:10

David W.