version-controlbranchnantvisual-sourcesafenantcontrib

How to create a Visual Source Safe branch using NAnt


Summary
I currently have a NAnt build script that performs a vssget on either the latest source code, or a specific branch (using a ${branch} parameter).

Whenever we do a production build/deployment the code-tree that was built has a branch created, (so that we can continue development and still know what codebase is on production, pretty standard stuff...)

Problem
The process of creation of that branch is still a manual one, performed by someone going into Visual Source Safe Explorer and performing the branching procedure. I was wondering if there is any way in NAnt of creating a VSS branch.

Current Plan
I already know about using <exec program="ss"> and am trying to avoid that, but in the absence of any better solutions, that is the most probable route I will take.

Does anyone know if there is a NAnt or NAntContrib target for this, or if anyone has a script task that they have used to do this in the past and could provide the code for that, that would be very much appreciated.

Disclaimer
I know about cvs, svn, git and all the other Source Control solutions, and to change the tool is not an option at present


Solution

  • We actually need this where I work. I whipped together a small task called 'vssbranch' (not particularly creative but here is the code...an example build file and the output of its execution:

    CODE:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    
    using SourceSafeTypeLib;
    
    using NAnt.Core;
    using NAnt.Core.Attributes;
    
    namespace NAnt.Contrib.Tasks.SourceSafe
    {
        [TaskName("vssbranch")]
        public sealed class BranchTask : BaseTask
        {
            /// <summary>
            /// The label comment.
            /// </summary>
            [TaskAttribute("comment")]
            public String Comment { get; set; }
    
            /// <summary>
            /// Determines whether to perform the branch recursively.
            /// The default is <see langword="true"/>
            /// </summary>
            [TaskAttribute("recursive"),
            BooleanValidator()]
            public Boolean Recursive { get; set; }
    
            [TaskAttribute("branchname", Required = true)]
            public String BranchName { get; set; }
    
    
            protected override void ExecuteTask()
            {
                this.Open();
                try
                {
                    if (VSSItemType.VSSITEM_PROJECT != (VSSItemType)this.Item.Type)
                        throw new BuildException("Only vss projects can be branched", this.Location);
    
                    IVSSItem newShare = null;
                    this.Comment = String.IsNullOrEmpty(this.Comment) ? String.Empty : this.Comment;
                    if (null != this.Item.Parent)
                        newShare = this.Item.Parent.NewSubproject(this.BranchName, this.Comment);
    
                    if (null != newShare)
                    {
                        newShare.Share(this.Item as VSSItem, this.Comment,
                            (this.Recursive) ?
                                (int)VSSFlags.VSSFLAG_RECURSYES : 0);
                        foreach (IVSSItem item in newShare.get_Items(false))
                            this.BranchItem(item, this.Recursive);
                    }
                }
                catch (Exception ex)
                {
                    throw new BuildException(String.Format("Failed to branch '{0}' to '{1}'", this.Item.Name, this.BranchName), this.Location, ex);
                }
            }
    
            private void BranchItem(IVSSItem itemToBranch, Boolean recursive)
            {
                if (null == itemToBranch) return;
    
                if (this.Verbose)
                    this.Log(Level.Info, String.Format("Branching {0} path: {1}", itemToBranch.Name, itemToBranch.Spec));
    
                if (VSSItemType.VSSITEM_FILE == (VSSItemType)itemToBranch.Type)
                    itemToBranch.Branch(this.Comment, 0);
                else if (recursive)
                {
                    foreach (IVSSItem item in itemToBranch.get_Items(false))
                        this.BranchItem(item, recursive);
                }
            }
        }
    }
    

    BUILD FILE:

            <echo message="About to execute: VSS Branch" />
    
            <echo message="Source Safe Path: ${SourceSafeRootPath}/${CURRENT_FILE}" />
    
            <vssbranch
                  username="my_user_name"
                  password="my_password"
                  recursive="true"
                  comment="attempt to make a branch"
                  branchname="test-branch"
                  dbpath="${SourceSafeDBPath}"
                  path="${SourceSafeRootPath}/${CURRENT_FILE}"
                  verbose="true"
                />
    
        </foreach>
    </target>
    

    OUTPUT:

    NAnt 0.85 (Build 0.85.2478.0; release; 10/14/2006) Copyright (C) 2001-2006 Gerry Shaw http://nant.sourceforge.net

    Buildfile: file:///C:/scm/custom/src/VssBranch/bin/Debug/test.build Target framework: Microsoft .NET Framework 2.0 Target(s) specified: run

    run:

    [loadtasks] Scanning assembly "NAnt.Contrib.Tasks" for extensions. [loadtasks] Scanning assembly "VssBranch" for extensions. [echo] About to execute: VSS Branch

    ....

    [vssbranch] Branching SecurityProto path: $/VSS/Endur's Source/C#/DailyLive/proto/test-branch/SecurityProto

    ....

    BUILD SUCCEEDED

    Total time: 12.9 seconds.

    Obviously the output would vary, I was pulling in the items to branch from a text file named 'params.txt'. This task performs what is known in the VSS world as 'Share and Branch' (Branching immediately after Sharing)...other source control systems do not need to share before branching, eh...that's for another day