MSBuild Task Tutorial: Label SourceSafe Project

Introduction


While attempting to automate my build process using MSBuild, I have been unable to find a MSBuild Task to label projects in Visual SourceSafe. So, I decided to roll my own using C#. Since you are reading this, you are probably aware that MSBuild is the new build platform from Microsoft. If you are unfamiliar with MSBuild, check out the list of resources at the bottom of this page for an introduction.

Tools Involved


During the course of this tutorial I use the following tools:
  • Visual Studio 2005
  • Microsoft Visual Studio 2005 Web Deployment Project
  • Microsoft Visual SourceSafe 6.0
  • MSBuild

Step 1: Create your MSBuild task project


Start by opening Visual Studio 2005 and creating a class library project. Name it something clever, like VSSTasks.

Create new Visual SourceSafe Label project

Step 2: Add the Label Project class


Create a new class file and name it LabelProject.cs.

Visual SourceSafe Label Project class added

Add the following code to the LabelProject.cs file:

    1 using Microsoft.Build.Framework;
    2 using SourceSafeTypeLib;
    3 
    4 namespace VSSTask
    5 {
    6     public class LabelProject : ITask
    7     {
    8 
    9 #region Properties
   10         private IBuildEngine engine;
   11         public IBuildEngine BuildEngine
   12         {
   13             get { return engine; }
   14             set { engine = value; }
   15         }
   16 
   17         private ITaskHost host;
   18         public ITaskHost HostObject
   19         {
   20             get { return host; }
   21             set { host = value; }
   22         }
   23 
   24         private string vssINIPath;
   25         [Required]
   26         public string VSSINIPath
   27         {
   28             get { return vssINIPath; }
   29             set { vssINIPath = value; }
   30         }
   31 
   32         private string vssUserName;
   33         public string VSSUserName
   34         {
   35             get { return vssUserName; }
   36             set { vssUserName = value; }
   37         }
   38 
   39         private string vssPassword;
   40         public string VSSPassword
   41         {
   42             get { return vssPassword; }
   43             set { vssPassword = value; }
   44         }
   45 
   46         private string vssProjectPath;
   47         [Required]
   48         public string VSSProjectPath
   49         {
   50             get { return vssProjectPath; }
   51             set { vssProjectPath = value; }
   52         }
   53 
   54         private string vssLabelText = "";
   55         /// <summary>
   56         /// Provide a string up to 31 characters long.
   57         /// </summary>
   58         [Required]
   59         public string VSSLabelText
   60         {
   61             get { return vssLabelText; }
   62             set
   63             { 
   64                 // only get the first 31 characters
   65                 if (value.Length > 0)
   66                     vssLabelText = value.Substring(0, 31);
   67             }
   68         }
   69 
   70         private string vssCommentText = "Label generated automatically by MSBuild VSSTask";
   71         public string VSSCommentText
   72         {
   73             get { return vssCommentText; }
   74             set { vssCommentText = value; }
   75         }
   76 
   77 #endregion Properties
   78 
   79         public bool Execute()
   80         {
   81             // Create SourceSafe ActiveX object
   82             SourceSafeTypeLib.VSSDatabase sourceSafeObject = new SourceSafeTypeLib.VSSDatabase();
   83 
   84             // Open a connection to the SourceSafe database=
   85             // using the srcsafe.ini file and a  valid
   86             // userid/password combination.
   87             sourceSafeObject.Open(vssINIPath, vssUserName, vssPassword);
   88 
   89             // Get reference to project we wish to label
   90             VSSItem vssProjectItem = sourceSafeObject.get_VSSItem(vssProjectPath, false);
   91 
   92             vssProjectItem.Label(vssLabelText, vssCommentText);
   93 
   94             string message = "VSS Label Successfully Set to " + vssLabelText;
   95             BuildMessageEventArgs args = new BuildMessageEventArgs(
   96                 message, string.Empty, "SetVSSLabel", MessageImportance.Normal);
   97             engine.LogMessageEvent(args);
   98 
   99             sourceSafeObject = null;
  100             vssProjectItem = null;
  101 
  102             return true;
  103         }
  104     }
  105 }

Step 3: Add necessary references


You will need to add two references to your VSSTasks project. First, add a reference to the Microsoft.Build.Framework. It should be located under the .NET tab on the Add Reference screen.

Visual Studio Add Reference screen

Adding the reference to the Microsoft SourceSafe 6.0 Type Library (SSAPI.DLL), which is installed along with SourceSafe, is slightly more complicated. I assume that you have a local copy of Visual SourceSafe installed. You will need to browse to the SSAPI.DLL file via the Add Reference screen.

In my case, the path to the SSAPI.DLL is:
C:\Program Files\Microsoft Visual Studio\VSS\win32\SSAPI.DLL

You should now see both the Microsoft.Build.Framework and the SourceSafeTypeLib in your list of references.

SourceSafe Type Library reference added

Step 4: Implementation


Compile your new VSSTasks project. Atta boy!

I use Microsoft Visual Studio 2005 Web Deployment Project to create my MSBuild project files. I highly recommend it, if you are automating the build process of an ASP.NET project. Check out the list of resources at the bottom of this page for examples of how to get started with the Web Deployment Project. For the sake of this article, I will assume that you have already added a Web Deployment Project to your own ASP.NET project in Visual Studio 2005.

Inside of your web deployment project file (.wdproj) you will add a reference to your newly created LabelProject task.

   54 <UsingTask AssemblyFile="C:\dev\VSSTasks\VSSTasks\bin\Debug\VSSTasks.dll"
   55              TaskName="VSSTask.LabelProject" />

Then add the following PropertyGroup element.

   34 <PropertyGroup>
   35       <VSSINIPath>C:\Program Files\Microsoft Visual Studio\VSS\srcsafe.ini</VSSINIPath>
   36       <VSSUserName>IPFreely</VSSUserName>
   37       <VSSPassword>unGuessablePassword</VSSPassword>
   38       <VSSProjectPath>$/MyWebProject</VSSProjectPath>
   39       <VSSLabelText>Testing Label v1</VSSLabelText>
   40       <VSSCommentText>Label generated by MSBuild using LableProject task.</VSSCommentText>
   41    </PropertyGroup>

Finally, add the task you just created to the After Build target element.

   50 <Target Name="AfterBuild">
   51       <LabelProject VSSINIPath="$(VSSINIPath)"
   52                 VSSUserName="$(VSSUserName)"
   53                 VSSPassword="$(VSSPassword)"
   54                 VSSProjectPath="$(VSSProjectPath)"
   55                 VSSLabelText="$(VSSLabelText)"
   56                 VSSCommentText="$(VSSCommentText)" />
   57   </Target>


Step 5: It's run time, baby!


Build your Web Deployment project either via the Visual Studio 2005 IDE, or you can do so using MSBuild and the command line.

Example:
C:\MyProjects\MyWeb>msbuild MyWeb.sln /p:Configuration=Debug

The output from the build process should include the following text:
VSS Label Successfully Set to Testing LabelProject Class

Step 6: Verify that VSS project was labled


Open Visual SourceSafe. Right click your project and select Properties. Under the General tab, you should see the Last Label text, followed by the text you specified in the VSSLabelText property in your Web Deployment Project file.

SourceSafe project successfully labeled using MSBuild

Congratulations, you have just labeled a Visual SourceSafe project using a MSBuild task, created with C# and Visual Studio 2005.

Conclusion


Don't stop now. You are only getting started. Why not create a task to get the latest version of your project from SourceSafe?

Resources

Content copyright ©2003-2006 Tod Birdsall