Subversion support

Feb 28, 2012 at 11:29 PM

Hi all,

I've created a plug-in for BVI to support SVN. It uses "svn info" shell command to detect revison number.
Hope you find it useful.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuildVersionIncrement.Incrementors;
using BuildVersionIncrement;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace Sample.Incrementor
{
    /// <summary>
    /// Example incrementor plugin that will reset the version number.
    /// </summary>
    /// <remarks>
    /// Instead of modifying one single version component (like major, minor, build or revision), this incrementor 
    /// will set a complete version number and notifies the addin to stop calling any other incrementors for this version increment.
    /// </remarks>
    public class SVNIncrementor : BaseIncrementor
    {
        /// <summary>
        /// Gets the name of this incrementor.
        /// </summary>
        /// <value>The name.</value>
        public override string Name
        {
            get { return "SVN Revision"; }
        }

        /// <summary>
        /// Gets the description of this incrementor.
        /// </summary>
        /// <value>The description.</value>
        public override string Description
        {
            get { return "Subversion revision number"; }
        }

        /// <summary>
        /// Executes the increment.
        /// </summary>
        /// <param name="context">The context of the increment.</param>
        /// <param name="versionComponent">The version component that needs to be incremented.</param>
        /// <remarks>
        /// Use the method <see cref="IncrementContext.SetNewVersionComponentValue"/> to set the new version component value.
        /// Set the  <see cref="IncrementContext.Continue"/> property to <c>false</c> to skip updating the other component values.
        /// </remarks>
        public override void Increment(IncrementContext context, VersionComponent versionComponent)
        {
            Logger.Write("Detecting SVN revision...", LogLevel.Debug);

            // Set all the version components
            string _svnRev = GetSVNVersion(context.ProjectFilename);
            if (_svnRev!=null)
                context.SetNewVersionComponentValue(versionComponent, _svnRev);

            // Tell the addin to stop incrementing the other version components
            // context.Continue = false;
        }

        private string GetSVNVersion(string projectfilename)
        {
            try
            {
                Process p = new Process();
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = "svn";
                p.StartInfo.Arguments = "info " + projectfilename;
                p.Start();
                string output = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                Match a = Regex.Match(output, "Revision:\\s(\\d+)", RegexOptions.Multiline);
                if (!a.Success)
                {
                    Logger.Write("Can't find revision number!", LogLevel.Error);
                    return null;
                }
                else
                    return a.Result("$1");
            }
            catch (Exception e)
            {
                Logger.Write(e.Message, LogLevel.Error);
                return null;
            }
        }
    }
}