Click here to Skip to main content
15,868,419 members
Articles / Web Development / ASP.NET
Article

MSBuild Task Tutorial: Label SourceSafe Project

Rate me:
Please Sign up or sign in to vote.
4.52/5 (14 votes)
28 Feb 2006CPOL3 min read 102.9K   42   5
Create your own MSBuild Task to label SourceSafe projects

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.

Sample image

Step 2: Add the Label Project Class

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

Sample image

Add the following code to the LabelProject.cs file:

C#
using Microsoft.Build.Framework;
using SourceSafeTypeLib;

namespace VSSTask
{
    public class LabelProject : ITask
    {

#region Properties
        private IBuildEngine engine;
        public IBuildEngine BuildEngine
        {
            get { return engine; }
            set { engine = value; }
        }

        private ITaskHost host;
        public ITaskHost HostObject
        {
            get { return host; }
            set { host = value; }
        }

        private string vssINIPath;
        [Required]
        public string VSSINIPath
        {
            get { return vssINIPath; }
            set { vssINIPath = value; }
        }

        private string vssUserName;
        public string VSSUserName
        {
            get { return vssUserName; }
            set { vssUserName = value; }
        }

        private string vssPassword;
        public string VSSPassword
        {
            get { return vssPassword; }
            set { vssPassword = value; }
        }

        private string vssProjectPath;
        [Required]
        public string VSSProjectPath
        {
            get { return vssProjectPath; }
            set { vssProjectPath = value; }
        }

        private string vssLabelText = "";
        /// <SUMMARY>
        /// Provide a string up to 31 characters long.
        /// </SUMMARY>
        [Required]
        public string VSSLabelText
        {
            get { return vssLabelText; }
            set
            {
                // only get the first 31 characters
                if (value.Length > 0)
                    vssLabelText = value.Substring(0, 31);
            }
        }

        private string vssCommentText =
           "Label generated automatically by MSBuild VSSTask";
        public string VSSCommentText
        {
            get { return vssCommentText; }
            set { vssCommentText = value; }
        }

#endregion Properties

        public bool Execute()
        {
            // Create SourceSafe ActiveX object
            SourceSafeTypeLib.VSSDatabase sourceSafeObject =
                   new SourceSafeTypeLib.VSSDatabase();

            // Open a connection to the SourceSafe database=
            // using the srcsafe.ini file and a  valid
            // userid/password combination.
            sourceSafeObject.Open(vssINIPath, vssUserName, vssPassword);

            // Get reference to project we wish to label
            VSSItem vssProjectItem =
               sourceSafeObject.get_VSSItem(vssProjectPath, false);

            vssProjectItem.Label(vssLabelText, vssCommentText);

            string message = "VSS Label Successfully Set to " + vssLabelText;
            BuildMessageEventArgs args = new BuildMessageEventArgs(
                message, string.Empty, "SetVSSLabel", MessageImportance.Normal);
            engine.LogMessageEvent(args);

            sourceSafeObject = null;
            vssProjectItem = null;

            return true;
        }
    }
}

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.

Sample image

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:

plain
C:\Program Files\Microsoft Visual Studio\VSS\win32\SSAPI.DLL 

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.

XML
<USINGTASK TaskName="VSSTask.LabelProject"
           AssemblyFile="C:\dev\VSSTasks\VSSTasks\bin\Debug\VSSTasks.dll" />

Then add the following PropertyGroup element:

XML
<PROPERTYGROUP>
   <VSSINIPATH>C:\Program Files\Microsoft Visual Studio\VSS\srcsafe.ini</VSSINIPATH>
   <VSSUSERNAME>IPFreely</VSSUSERNAME>
   <VSSPASSWORD>unGuessablePassword</VSSPASSWORD>
   <VSSPROJECTPATH>$/MyWebProject</VSSPROJECTPATH>
   <VSSLABELTEXT>Testing Label v1</VSSLABELTEXT>
   <VSSCOMMENTTEXT>Label generated by MSBuild using LableProject task.</VSSCOMMENTTEXT>
</PROPERTYGROUP>

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

XML
<Target Name="AfterBuild">
   <LabelProject VSSINIPath="$(VSSINIPath)"
                 VSSUserName="$(VSSUserName)"
                 VSSPassword="$(VSSPassword)"
                 VSSProjectPath="$(VSSProjectPath)"
                 VSSLabelText="$(VSSLabelText)"
                 VSSCommentText="$(VSSCommentText)" />
</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:

plain
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 Labeled

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.

Sample image

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

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

Sample image

History

  • 28th February, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncan i make exe using MSBuild in my desktop application? Pin
Irfan Kothari3-Nov-06 20:21
Irfan Kothari3-Nov-06 20:21 
QuestionSobaka Pin
Yustos27-Aug-06 23:45
Yustos27-Aug-06 23:45 
AnswerRe: Sobaka Pin
tod1d.net28-Aug-06 4:19
tod1d.net28-Aug-06 4:19 
Generalssapi Get with label Pin
NigelAston24-May-06 5:10
NigelAston24-May-06 5:10 
GeneralBuild Tasks Pin
Shaun Wilde6-Mar-06 19:35
Shaun Wilde6-Mar-06 19:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.