torsdag 2 april 2009

Script to add svn version to your code

In the project I'm currently in, I found it very convenient to include the SVN version number of the release in the application's GUI. If a customer has a problem, we then knows for sure what version they are using, and we don't have to rely on our sometimes shaky release management being correct.

A bash script puts the current SVN version number in a #define in a C++ header file, which is then included in the project and displayed in the 'About box', next to the official version number.

I have included the script below that extracts the version number from the top level .svn/entries database in the project, and puts it in a file VERSION_CPP_FILEPATH that contains the row "#define SVN_VERSION = "

This bash script is then executed as a pre-build action in our IDE.

#/!bin/sh
VERSION_CPP_FILEPATH=../inc/version.hpp
SVN_ENTRIES_PATH=../../.svn/entries

VERS=`cat $SVN_ENTRIES_PATH | grep dir -A 2 | sed -n '2p'`
sed "s/#define SVN_VERSION .*/#define SVN_VERSION $VERS/g" $VERSION_CPP_FILEPATH > tmp.hpp
rm $VERSION_CPP_FILEPATH
mv tmp.hpp $VERSION_CPP_FILEPATH

2 kommentarer:

  1. Grymt! Känns absolut som det bästa sättet att ange vilken version som körs.

    SvaraRadera
  2. cat $SVN_ENTRIES_PATH | grep dir -A 2 | sed -n '2p'

    verkar rätt krångligt och väldigt beroende av att formatet på .svn/entries inte ändras i framtiden. varför inte använda "svn info"?

    svn info | awk '/Revision/ { print $2 }'

    SvaraRadera