Sunday, April 19, 2009

Creating a Subversion Changelog via MSBuild …

The other day, I thought it would be a good idea to provide a generated ChangeLog for our integration builds. We are using TeamCity and MSBuild projects.
After searching for a while, I found only one solutions – svn2cl by Arthur de Jong. It is pretty straight forward, using the xml output of the svn log command and applying a XSL Stylesheet. It supports the standard GNU Changelog text format and a basic html formated one. The tool comes standard as a korn-shell script for Unix only, but luckily there is a windows port by Iwasa Kazmi using csript.
I could have just called the script via a <Exec> task, but this would have been no fun. The script uses the MSBuild Community Extensions.
In addition to just the Changelog I wanted to include
  1. Creation time
  2. The Subversion Repository root and revision
  3. HTML format with revision links back to our trac – changeset browser
  4. Embed the style sheet, so the generated html file is self sufficient
So – let’s get on with it. In your MSBuild project, import the “svn2cl.targets” project, set some properties and call the target or set a dependency.

How to use it

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

 <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
 <Import Project="./Build/svn2cl/svn2cl.targets"/>

 <PropertyGroup>
   <!-- pick up the workspace repository root or overwrite here -->
   <!--<Svn2clRepositoryPath> ... </Svn2clRepositoryPath>-->
   <!-- the marker ## will be replaced with the revision number -->
   <Svn2clRevisionLink>https://trac-location/trac/project/changeset/##/</Svn2clRevisionLink>
   <Svn2clLimit>100</Svn2clLimit>
   <Svn2clChangelogFile>./Changelog.html</Svn2clChangelogFile>
   <!-- by default take the current workspace revision -->
   <!--<Svn2clTopRevision>HEAD</Svn2clTopRevision>-->
 </PropertyGroup>

 <Target Name="BuildChangeLog" DependsOnTargets="SvnChangeLog"/>
</Project>

Repository information

I am using the SvnInfo task from the Community Extensions to determine information about the workspace.
    <!-- If Svn2clRepositoryPath is not set, get the information from the current working directory -->
   <SvnInfo LocalPath="$(MSBuildProjectDirectory)" Condition="$(Svn2clRepositoryPath) == ''">
     <Output TaskParameter="RepositoryPath" PropertyName="Svn2clRepositoryPath"/>
     <Output TaskParameter="Revision" PropertyName="Svn2clTopRevision"/>
   </SvnInfo>

   <Error Condition="$(Svn2clRepositoryPath) == ''" Text="Could not determine the Svn2clRepositoryPath"/>

Embed the CSS Style Sheet

I didn’t like the fact that the CSS Style sheet was just referenced, so I changed the XSLT to include it.
<!DOCTYPE xsl:stylesheet [
<!ENTITY newl "&#38;#xA;">
<!ENTITY space "&#32;">
<!ENTITY css SYSTEM "svn2html.css">
]>
...
<xsl:template match="log">
 <html>
  <head>
   <title><xsl:value-of select="string($title)" /></title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
       &css;
    </style>      
  </head>
  <body>

Subversion UID’s to real user names with email

The XSL can also read author information from a xml file and include them in the Changelog.
<?xml version="1.0" encoding="utf-8"?>
  <authors xmlns:html="http://www.w3.org/1999/xhtml">
   <author uid="bscholze">
     Bernd Scholze &lt;<html:a href="mailto:bscholze@gmail.com">bscholze@gmail.com</html:a>&gt;
  </author>
  <author uid="nobody">
     Who's this &lt;nobody@example.com&gt;
  </author>
</authors>
Download the MSBuild task.

4 comments: