Tuesday, June 17, 2008

YALT: Adding Headers to C# Source Files with Subversion Integration & Auto Fields

That's "Yet Another Little Tool"... In an endless series of diversions to keep myself somewhat challenged, last night I wrote a little tool that applies a header to C# source files. 

See the screen shot below. 


The header text box is directly editable and recognizes tokens opened by "[" and closed by "]".  When detected, a row is added (or removed if an existing field is removed from the header) to the Replaceable Fields grid view.  The grid view displays an image in the first column coupled the the feedback column (which in turn indicates whether the field is supported, properly constructed and a value provided for if not an auto field or a Subversion field--we will talk about this in a minute).  The second column is the name of the replaceable field, the third column is the value to replace the field name with in the header text (if the field is auto field or a Subversion field, the value is not supplied manually).

So, what does this mean?  I can create (and save and load, incidentally) header templates which will automatically be appended to any C# source file.  Furthermore, if a Subversion URL is provided that matches the source directory, the tool will query Subversion and automatically populate the SVN:AUTHOR field and the SVN:CREATED_DATE field and optionally append Subversion log messages to the end of the source file as comments.

Also, the tool supports a limited vocabulary of AUTO:* fields, such as the FILE (file name) auto field.  Auto fields are, as you might expect, automatically filled in for you.

The rest of the fields such as COMPANY NAME, PRODUCT NAME, and COPYRIGHT use name/value pairs supplied by the user.  See the screen shot below.  Any number of fields may be added to the header.


Probably the most interesting thing about this little tool is the use of SharpSVN to query Subversion for information about the source files.  Either I am missing something, or the documentation is really quite lacking for SharpSVN.  I was able to figure out how to do the following:

  1. Connect to an Subversion repository
  2. Get the revision list for a particular file
  3. Get additional information about a file for the first revision (in order to retrieve the AUTHOR and CREATED_DATE fields)
  4. Get the list of log messages supplied during check in (as I mentioned, these are optionally appended to the end of the source file as comments).

Here is the code snippet:

   1: string rootPath = Path.GetFileName(_SvnTarget);
   3: SvnClient svnClient = new SvnClient();
   5: SvnTarget svnTarget;
   7: SvnInfoArgs svnInfoArgs = new SvnInfoArgs();
   9: string svnPath = _SvnTarget + sourceFile.Substring(sourceFile.IndexOf(@"\" + rootPath) + (@"\" + rootPath).Length);
  11: svnPath = svnPath.Replace(@"\", "/");
  13: if (SvnTarget.TryParse(svnPath, out svnTarget))
  14: {
  16:     // Get the revision list
  17:     Collection<SvnLogEventArgs> outSvnLogEventArgs;
  18:     svnClient.GetLog(new Uri(svnTarget.ToString()), out outSvnLogEventArgs);
  20:     svnInfoArgs.Revision = outSvnLogEventArgs[0].Revision;
  22:     // Get the first revision
  23:     Collection<SvnInfoEventArgs> outSvnInfoEventArgs;
  24:     svnClient.GetInfo(svnTarget, svnInfoArgs, out outSvnInfoEventArgs);
  26:     // Save the author and created date
  27:     string author = outSvnInfoEventArgs[0].LastChangeAuthor;
  28:     string dateCreated = outSvnInfoEventArgs[0].LastChangeTime.ToLongDateString();
  30:     // Retrieve the log messages 
  31:     if (_AppendLogMessagesAsFooter)
  32:     {
  33:         StringBuilder comments = new StringBuilder();
  35:         foreach (SvnLogEventArgs logEventArg in outSvnLogEventArgs)
  36:         {
  37:             if (!string.IsNullOrEmpty(logEventArg.LogMessage))
  38:             {
  39:                 comments.Append("\n");
  40:                 comments.Append(@"//");
  41:                 comments.Append(logEventArg.Time.ToShortDateString());
  42:                 comments.Append(" : ");
  43:                 comments.Append(logEventArg.Author);
  44:                 comments.Append(" - ");
  45:                 comments.Append(logEventArg.LogMessage);
  46:             }
  47:         }
  49:         footer = comments.ToString();
  50:     }
  51: }
The files to apply the header to are selected via a source browser:

1 comment:

Sander Rijken said...

Did you check the svn:keywords property? See http://svnbook.red-bean.com/en/1.0/ch07s02.html#svn-ch-7-sect-2.3.4

it involves setting svn:keywords to the value(s) you want replaced, and then you can use $Author $, which will be replaced to read $Author: John Doe $


Content on this site is provided "AS IS" with no warranties and confers no rights. Additionally, all content on this site is my own personal opinion and does not represent my employer's view in any way.