If you heavily work with branches, one of the most frustrating error you can do is modify the wrong branch. I have a project composed by several solution each one containing different UI Projects, each one using WCF as back end but released as separate software. Whenever we do a release of a new version of one of the UI we create a branch, so we can support hotfix, SP, etc.

Figure 1: A branching strategy taken from the branchingguidance

With this scenario I’m prone to this error:

Someone call me telling to create an hotfix for the UI XYZ, I open the solution from BranchReleasesXYZR3_0 to open the third (is the latest) release of the XYZ UI, I do the hotfix, test it, run all the test etc etc, then other people of the team call me asking for modification in other part, so I open the same solution from a different branch (say the trunk or the Feature BLABLA) and do some modification. Now I have two Visual Studio opened, with the very same solution, sometimes I got confused and I modify the wrong line of code… too bad, because this can cause a really bad problem. The problem arise from having more than one instance of Visual Studio opened with solutions of same name, just from different branch Sad smile and you can become confused.

This is expecially true when you use HG or subversion or any other VCS tools that are not integrated in Visual Studio.

A simple solution is creating a Visual Studio Macro that uses a Regex to parse the fullpath of the solution file, searching for a specific folder structure that identify a Branch. Here is the full code

   1: Declare Auto Function SetWindowText Lib "user32" (ByVal hWnd As System.IntPtr, _

   2: ByVal lpstring As String) As Boolean

   3:  

   4:  

   5: Private Sub showTitle(ByVal title As String)

   6:     SetWindowText(New System.IntPtr(DTE.MainWindow.HWnd), title & " - " & DTE.Name)

   7: End Sub

   8:  

   9:  

  10: Private Sub SolutionEvents_Opened() Handles SolutionEvents.Opened

  11:     Dim m As Match = Regex.Match( _

  12:         DTE.Solution.FullName, _

  13:         "Branch.*\(?<project>.*)\(?<branch>.*)\(?<sln>.*).sln", _

  14:         RegexOptions.IgnoreCase)

  15:     If (m.Success) Then

  16:         Dim project As String = m.Groups("project").Value

  17:         Dim version As String = m.Groups("branch").Value

  18:         Dim sln As String = m.Groups("sln").Value

  19:         showTitle(String.Format("BRANCH [{0}] - Project {1} - {2}", _

  20:                     version, project, sln))

  21:  

  22:     End If

  23: End Sub

You need to paste this code in the Macros editor, opened from Tools –> Macros –> Macros IDE

SNAGHTML1889083

Figure 2: Open the Macro editor

From the opened editor you just double click on MyMacros, expand the EnvironmentEvents, and you can add your code to every handler supported from Visual Studio.

SNAGHTML18c25a1

Figure 3: You can handle standard events from the IDE, such as when a solution is opened.

If you look at the code, I’ve simply put a regex that permits me to parse the typical branch path structure I have on my projects, where I have Branchsomeothertextnameoftheprojectbranchnumbersolutionfile.sln. When I open the trunk version I got only the solution name on the title bar.

image

Figure 4: the solution is opened from the trunk (main) because only the name of the solution is shown in title bar

I’ve opened the same solution from a release of a specific UI and I got

image

Figure 5: The same solution is opened from a branch folder, now title bar shows me that I’m working on branch

I immediately visualize that this solution is a branch, the version number is R3_0 (Release three point zero), the project released is ZZZZManager that resides on the XXXXXWeb-Vs2010 solution ;) now it is really difficult for me and other people to write code on a wrong branch, because all the informations I need are on the title bar.

Using title of the windows gives also the exceptional advantage of having these information in Windows 7 tray bar

image

Figure 6: Using the title bar, I got the very same information when I need to maximize a windows from Windows 7 tray bar

So you can immediately know witch is the right windows to use. Moreover developing such a macro is a matter of minutes, because you can simply add the handler and some code inside the macro editor, Then save the macro, close and reopen visual studio, open again the macro editor and activate the debugger

image 

Figure 7: I can even debug the macro, an invaluable option when you are experimenting on your code.

Now you can put breakpoint around the code, and debugging your macro to tune it.

Alk.

Tags: , ,

5 Responses to “Visual studio Macro to the rescue”

  1. Thanks you!

    I was looking for just this functionality.

    All I had to do was update the regex and it works :)

  2. Visual Studio Macro is probably the quickest way to adapt the IDE to your specific needs :) and since you have a reference to the DTE object you can really do any operation you want :)

  3. One drawback with this macro is that title won’t show state of debugger anymore (e.g. running).
    You may want to try latest release of VSCommands 2010 from Visual Studio Gallery which works around this issue (plus it does it automatically so no need for macros any more). See http://vscommands.com/releasenotes/3.6.9.0 for details

  4. Yes, you are right, but I do not care for this, because I’m used to understand if the instance is in debug mode looking at the debug toolbar. For vscommands they are greats, but I’d like to be able to use my custom regex to show branch and project information, even for project that run on subversion, or HG, or any other VCS.

  5. Sure, and you can use regex with vscommands too!
    You can set one global for all solutions but you also can override it individually for each solution.