ToolTask for VSeWSS 1.3 (Feb 2009 CTP) Command-line


I went to the 2009 MVP Global Summit this year and figuring I would have a lot of time to kill aboard the airplane I brought my copy of “Inside the Microsoft Build Engine: Using MSBuild and Team Foundation Build”.

This book was fantastic, full of great examples and guides you through customizing MSBuild and Team Build.  I was initially surprised to find so many chapters dedicated to MSBuild but afterwards I realized there is just so much you can do with it, there probably could have been more.

I finished the book on the flight to Seattle, WA and wanted to experiment with Team Build and MSBuild so my first project was a ToolTask which allows you to execute one of the command-line options included in the latest VSeWSS 1.3 CTP

devenv.exe <.csproj or .sln and other usual flags> /deploy Debug
devenv.exe <.csproj or .sln and other usual flags> /deploy Debug /package
devenv.exe <.csproj or .sln and other usual flags> /deploy Debug /retract

Here is the source for my ToolTask

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Win32;
using System.IO;
using Microsoft.Build.Framework;



namespace VSeWss
{
    public class VsTask: ToolTask
    {

        private const string ExeName = "devenv.exe";

        public VsTask()
        {
            Retract = false;
            Package = false;
        }

        #region Properties

        public bool Retract 
        {
            get;
            set;
        }

        public bool Package
        {
            get;
            set;
        }

        [Required]
        public string SolutionOrProjectFile
        {
            get;
            set;
        }

        [Required]
        public string FlavorToBuild
        {
            get;
            set;
        }
        #endregion

        #region VsTask Methods

        protected override bool ValidateParameters()
        {
            base.Log.LogMessageFromText("Validating Task Arguments", MessageImportance.Low);

            if (string.IsNullOrEmpty(SolutionOrProjectFile))
            {
                base.Log.LogError("No solution or project file specified", null);
                return false;
            }

            if (string.IsNullOrEmpty(FlavorToBuild))
            {
                base.Log.LogError("Release or Debug must be specified", null);
                return false;
            }
                        
            return base.ValidateParameters();

        }

        protected override string GenerateCommandLineCommands()
        {

            // devenv.exe <Project.csproj or Solution.sln and other usual flags> /deploy Debug
            // devenv.exe <Project.csproj or Solution.sln and other usual flags> /deploy Debug /package
            // devenv.exe <Project.csproj or Solution.sln and other usual flags> /deploy Debug /retract

            
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(SolutionOrProjectFile))
            {
                sb.AppendFormat("{0} ", SolutionOrProjectFile);
            }

            sb.AppendFormat("{0} ", "/deploy");

            

            if (!string.IsNullOrEmpty(FlavorToBuild))
            {
                sb.AppendFormat("{0} ", FlavorToBuild);
            }

            if (Retract)
            {
                sb.AppendFormat("{0} ", "/retract");
            }
            
            if (Package) 
            {
                sb.AppendFormat("{0} ", "/package");
            }

            return sb.ToString();
        }

        
        protected override string GenerateFullPathToTool()
        {
            string path = ToolPath;
            // If ToolPath not given to us go and find it in the registry
            if (string.IsNullOrEmpty(path))
            {
                string regKey = @"SOFTWARE\Microsoft\VisualStudio\9.0\Setup\VS";
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regKey))
                {
                    if (key != null)
                    {
                        string keyValue = key.GetValue("EnvironmentDirectory", null).ToString();
                        path = keyValue;
                    }
                }
            }

            if (string.IsNullOrEmpty(path))
            {
                Log.LogError("Visual Studio 2008 install not found", null);
                return string.Empty;
            }

            string fullPath = Path.Combine(path, ToolName);
            return fullPath;
        }

        protected override string ToolName
        {
            get { return ExeName; }
        }
        #endregion

    }
}

 

After I built the assembly for my VSeWSS Tool Task copied in into the same folder with my tfsbuild.proj like this and referenced the new task like this:

<UsingTask AssemblyFile="VseWssTask.dll" TaskName="VsTask"/>

This is how I called my task inside my build script:

<Message Text="Starting to build SharePoint solution"/>
<VsTask SolutionOrProjectFile="&quot;$(SolutionRoot)\dev\source\samples\source\TFSExamples\wss.sln&quot;"
            Retract="false"
            FlavorToBuild="$(Configuration)"
            Package="true"/>

If you are testing your tfsbuild script using a DesktopBuild make sure you populate the Configuration property with a value. The command line would look like this:

msbuild tfsbuild.proj /p:Configuration=Debug

And that’s it, you could easily replace the Exec task that calls Visual Studio and make your build script read a bit better.

While I was at the MVP Summit this year I actually got to meet William Bartholomew who is a VSTS MVP as well.

Technorati Tags: ,

About Wes MacDonald

Wes MacDonald is a DevOps Consultant for LIKE 10 INC., a DevOps consulting firm providing premium support, guidance and services for Azure, Microsoft 365 and Azure DevOps.

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: