Thursday, February 07, 2013

Including TFS Info In Visual Studio Project Build Output

I wanted to know what build version is in which environment and whether there were pending local changes when a project was built so I put a few things together to recreate what I'd done before with ant and subversion.  This requires a visual studio version with TFS support in the first place (so that you have the tf.exe command available) and may or may not work on a build server.  If you try it on a build server, please leave a comment about whether it works or not.  Also, it assumes you already got the latest code from the server.  Otherwise, the tf history info might list changesets which you haven't pulled down locally.  Personally, I think that's a bug in tf.exe.
  1.  In the properties for your project, under build events, add the following as either a pre or post build step:
    set PATH=%PATH%;$(DevEnvDir)
    $(ProjectDir)\tfsVersionScript.bat $(ProjectName) $(ProjectDir)
  2. Create a file in your project root directory called tfsVersionScript.bat with the following contents (You'll have to adjust the number of times you put "..\" in the tf command arguments based on your own TFS folder structure so that you get the changeset history and pending changes for the right folder):
    @echo off
    del /Q %2\version.htm
    echo ^<html^>^<head^>^<title^>%1^</title^>^</head^>^<body^> > %2\version.htm
    echo ^<p^>Built project %~1 in path %~2 ^</p^> >> %2\version.htm
    time /T >> %2\version.htm
    date /T >> %2\version.htm
    echo ^<pre^>Most recent changes from tfs: >> %2\version.htm
    tf history ..\..\* /recursive /stopafter:10 /noprompt >> %2\version.htm
    echo ^<br\^> >> %2\version.htm
    echo ^<b^>Pending local changes:^</b^>^<br\^> >> %2\version.htm
    tf status ..\..\* /recursive /format:brief >> %2\version.htm
    echo ^</pre^>^</body^>^</html^> >> %2\version.htm
  3. Run the build once.
  4. Add version.htm to your project so it will be included when you package the output
  5. Save and check-in your pending changes.  Add the .bat file to tfs, but not the project and don't bother checking in version.htm.  The .bat file doesn't even need to be inside the project directory.
Not perfect, but a lot better than relying on somebody manually updating a config file.

No comments: