Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Using SubVersion Revision Numbers in Build Versions

Rob Rastelli

Having used Team Foundation Systems (TFS) for many years I grew accustom to having an automated build process and the ability to have the Revision number in my binary file versions.  Recently I began work for a client that used SubVersion as their source control system.  This removed much of the niceties I was used to having in other TFS based projects.  I set out on a search of the internet and found a couple methods to provide similar functionality of tying a deployed package of code back to a revision in my source control.

Both of these methods require that you use the Tortoise SVN tool and have it installed on your local system.  Since both of these methods do not require you to actually log into SVN to get the revision number, it is important to note that the revision number displayed will reflect the Revision Number of the source code on your local file system.  That is to say, that if you don’t “Update” prior to building, you will have the Revision Number from the last time you updated.

Method 1: Embed the Revision Number on your page

    1. Create a file in the project root called CreateSvnRevFile.bat and add the following contents
@echo off

echo Checking Input
if /I ["%~1"]==[] goto usage else goto start
:start
echo Creating svn_rev.txt

echo "$WCREV$" > "%~2svn_rev.txt"

if exist "%~2svn_rev.txt" (
echo Success
) else (
goto createFailed
)

echo Getting revision number
C:\Progra~1\TortoiseSVN\bin\SubWCRev.exe "%~1" "%~2svn_rev.txt" "%~2svn_rev.txt" -q
echo Success

goto end

:createFailed
echo Failed to create svn_rev.txt
exit 2

:usage
echo Usage: %0 SvnWorkingCopyDir
exit 1

:end
exit 0
    1. Save the file.
    2. In the project properties for your site, under the Build Events section add the following line to the Post-build events.
call "$(ProjectDir)CreateSvnRevFile.bat" "$(SolutionDir)." "$(ProjectDir)"
    1. Save the project and close the properties window.

At this point, after each build you will have a file name svn_rev.txt that will be created on each build.  This will contain the latest SVN Revision number of the project folder being built.

    1. In your project create a helper class or extension method (In my case, I was using MVC, so I added an HTML Helper method) as follows:
        public static string SVNRevision(this HtmlHelper html)
        {
            StreamReader svnFile;
            string svnRev = "";

            try
            {
                svnFile = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "svn_rev.txt");
                svnRev = svnFile.ReadToEnd();
                svnRev = svnRev.Replace("\"", "");
                svnFile.Close();
            }
            catch (Exception)
            {

            }
            finally
            {
                svnRev = svnRev.Length == 0 ? "-" : svnRev;
            }

            return svnRev;
        }
    1. Then in your BaseLayout or MasterPage insert something along the lines of the following:
<p style="color: #f6f0e1;">Revision: @Html.SVNRevision()</p>
  1. Save and build everything.
  2. Once the build is completed include the svn_rev.txt file in your web project with a Build Action of Content.
  3. Save and build once more.

With this method you do not need to add the svn_rev.txt file to SubVersion, just include the file in the project so it will be part of any deployment packages.  I’d also recommend adding the Batch file created in step 1 so other developers will be able to build the solution.

Method 2: Set the Version Number of binary files

For this method I used the blog of Andrew Freemantle and his program What The Version (WTV).  The blog post and referenced executable can be found here:

http://www.fatlemon.co.uk/2011/11/wtv-automatic-date-based-version-numbering-for-net-with-whentheversion/

    1. Create a folder in your Solution Root named WTV_SVNLookup.
    2. Download WTV from GitHub.
      1. Binary: https://github.com/AndrewFreemantle/When-The-Version/downloads
      2. Source: https://github.com/AndrewFreemantle/When-The-Version
    3. Whichever path you choose above, place the resulting Executable in the folder created in step 1.
    4. Open the project in question.  This can be an MVC project that compiles to a DLL, or just a class library that is under SVN version control.
    5. Open the Properties of the project and navigate to the Build Events tab.
    6. Add the following to your Pre-build event command line
"$(SolutionDir)WTV_SVNLookup\wtv.exe" "$(ProjectDir)Properties\AssemblyInfo.Template.cs" "$(ProjectDir)Properties\AssemblyInfo.cs" "C:\Program Files\TortoiseSVN\bin\SubWCRev.exe" "$(SolutionDir)."
    1. Save and close the project properties window.
    2. Expand the Properties node in the Solution Explorer of the project you are working with.
    3. Copy and Paste the AssemblyInfo.cs file and rename it AssemblyInfo.Template.cs
    4. Open the Template file, and set the Version lines as follows:
[assembly: AssemblyVersion(“{YYYY}.{MM}.{DD}.{SVN}”)]
[assembly: AssemblyFileVersion(“{YYYY}.{MM}.{DD}.{SVN}”)]
    1. Save the template file.
    2. Build the project.

If you navigate to the output directory for your project and view the Properties Dialog for your newly built DLL/EXE you will see on the Details tab, the Version numbers are now set to something like “2014.11.10.20403”.

  1. You can now Add the AssemblyInfo.Template.cs file to source control so other developers can build to project.
  2. You can also Remove the AssemblyInfo.cs file, as it is now generated before the project is built.  If you keep this file under source control, each developer will end up committing the file whenever they do any work, and it could produce misleading SVN Logs.

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

\\\