Use svn version number in application version

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

at the moment we do not use make files, but only the settings of the VS / VS project.

I want to get the revision number of the working copy at compile time, save it in a variable so that it can be used later in the code to display the version number.

So far, I have successfully used svnversion to get the latest working copy as a pre-generated event.

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

During build, I see that the correct version number is returned to the output console.

Now the question is how to store this value in a variable that can be used in code?

I tried to define the variable pre compiler ( _SVNREV ) and use it to save the result from the above cmd, directly from the pre-build event field, but this does not 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 them work.

Solution : I have never tried to update a variable from VS env anywhere. so I took another route, calling the script as a pre-build step, which gets the svn revision of the working copy, and then creates a header file with this information.

Here is svnrev.bat for everyone who is 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% 
+7
source share
4 answers

The best guess I came across is to create a header file in the pre-build event that contains svnversion.

eg. 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 svnversion, I just #include "svnversion.h"

+5
source

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

One of Jenkins' neat tricks is inserting into some useful environment variables for your use:

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

Once you start using Jenkins for your builds, the last two environment variables are likely to become more valuable than the Subversion revision itself, since the last two will link your code with the project and all kinds of good information.

Jenkins is a continuous assembly engine. You can set up Jenkins' task to create a team when there is a check or at certain times of the day. You can even send a built-in command via wget to a URL.

Using the build server, you eliminate the good possibility that the configuration of a particular system has some kind of side effect for the build. The build server is a known configuration and will create your official builds .

This is a little more than you probably wanted to know. (In particular, you could use the svnversion command to get the revision number, and then go from there). But this will greatly improve the assembly process.

+3
source

You can run the command wrapped in a batch file. A batch file can create a header file, for example version.h, containing a version string:

 #define SVN_REV 12345 

"# define SVN_REV" is called by the script package. SVN rev is the command you have.

Now you can include this header in the code and use #define as you wish.

+1
source

I saw people parse svn information for version number and write it to a file, and then configure compilation to cut the file into compiled code.

The tricky thing about SVN: in older versions (where there is a .svn file in each subdirectory), the version number is only guaranteed for the project root directory. Any other directory will have a version number for the last commit in this subtree. I donโ€™t know which version you are using, but watch this if you are a couple back!

0
source

All Articles