Extreme CI Part 3: The Visual Studio Macro.

How I run a personal CI build every time I build my code in Visual Studio.


This week we’ll finish the series by creating the Visual Studio macro that automatically runs the Personal CI process every time we build the code.

The Series

In this series I show you how to take continuous integration to the extreme by building a personal CI server that runs all your tests and adds a commit to source control every time you build in Visual Studio.

Setting Up the External Command in Visual Studio

Last week we created a batch file to commit and push our changes to the CI server. Now we need to execute that batch file from within Visual Studio. We’ll do it using an external command. To set up the external command in Visual Studio:

  1. Select Tools –> External Tools… from within Visual Studio. I am using Visual Studio 2008, for 2010, it may be a bit different.
  2. When the “External Tools” dialog shows, click “Add” and fill in the following:
    • Title: Commit and Push to Personal CI
    • Command: $(SolutionDir)\..\CommitAndPushToPersonalCI.bat
    • Initial directory: $(SolutionDir)\..\
    • Use Output window: checked

When you are done, the dialog should look like this:


Let me clarify a few things.

Why did we use $(SolutionDir)\..\?

The $(SolutionDir) is the directory containing the current solution, so when I am specifying $(SolutionDir)\..\, I am specifying the parent directory of the solution’s directory. This is because I have the following folder structure.

  +-MyProject  <-- Root of Git Repository
    +-src      <-- Solution Location

I want to commit and push from the root of my repository so that anything I have added to the lib or any other folders will be commited. However, my solution is in the src folder, which is one level deeper than I want to commit from. You may need to adjust your settings based on your project structure.

What’s up with the Use Output window setting?

Checking the “Use Output window” option redirects the console output of the batch file to the output window in Visual Studio.

Ok, let’s move on.

Now your tools menu should contain the new command and look like this.


And when running your custom command, its results appear in the output window.


Now lets take the final step and create the Visual Studio macro that runs this every time we build.

Create the Visual Studio Macro

To create the macro, we need to open up the Macros IDE.

Select Tools –> Macros –> Macros IDE… from within Visual Studio, or press Alt+F11. The Macros IDE will launch.


Open the EnvironmentEvents module under the MyMacros project. It should look like the screenshot above. Take the following code and insert it after the automatically generated code, and before the End Module.

Dim BuildWasSuccessful As Boolean
Dim BuildingSolution As Boolean
Dim RestrictToSolutionName As String
Dim PersonalCICommandName As String

Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin

    'Initialize variables when the build starts.
    BuildWasSuccessful = True
    BuildingSolution = True

    'Only commit to personal CI for the specified solution name.
    RestrictToSolutionName = "MyProject"

    'Name of the external Personal CI command.
    PersonalCICommandName = "Tools.ExternalCommand3"

End Sub

Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone

    'Don't run it if we just did a clean, since its not a build.
    If Action = vsBuildAction.vsBuildActionClean Then
        BuildingSolution = False
        Exit Sub
    End If

    'Only run autocommit if the build succeeded.
    If BuildWasSuccessful And DTE.Solution.FullName.Contains(RestrictToSolutionName) Then
        'Run the command to autocommit to git.
    End If

    BuildingSolution = False

End Sub

Private Sub BuildEvents_OnBuildProjConfigDone(ByVal Project As String, ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String, ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone

    If Success = False Then
        'Set the build to failed.
        BuildWasSuccessful = False

        'The build failed...cancel any further builds.
    End If

    'Only commit project level builds if we are not building the whole solution
    If Success = True And BuildingSolution = False And DTE.Solution.FullName.Contains(RestrictToSolutionName) Then
        'Run the command to autocommit to git.
    End If

End Sub

To get it to work with your project, you need to change two variables: RestrictToSolutionName and PersonalCICommandName.


This value is used to restrict the personal CI process to one solution. If it executes on a solution that is not set up properly, it will fail. If you want to use this script for multiple solutions, you will need to change it. If you do, send me a copy and I’ll update this post and give you the credit.


This is the name of the ExternalCommand you created in visual studio. It is tricky to get the right name because it depends upon the number of external commands you have. It does not use the name you gave it. Pity.

The command names are in the form “Tools.ExternalCommandN” where N is a number. One way to determine the name is to look at your Tools menu and see what position that command is in. In the case of our example, it is the third custom tool in the tools menu, so it is “Tools.ExternalCommand3”. If that doesn’t work, you can create a custom toolbar and add the external commands to it one at a time. When you view the toolbar, you will see the real name of the command and can determine which is yours.

One More Thing

Once you set those properties and save the script, try building your project. It should automatically commit the code and push it up to the CI repository.

Now all you have to do it hook up a CI Server to it. I leave that as an exercise to you. It is beyond the scope of this article. But, you’re a good developer. You’ve setup a CI server before, haven’t you?


I pray this series has helped you tighten up your feedback loop so that when tests fail, you know…fast. My challenge to you is, how can we tighten it up even further? I am open to suggestions.


Posted in .NET, Agile, Continuous Integration, Testing, Visual Studio | Tagged , , , , | 2 Responses

Extreme CI Part 2: Setting up Git for a Personal CI

How I run a personal CI build every time I build my code in Visual Studio.


This week we setup Git so it commits your changes and pushes them up to the personal CI server. We’ll also create a batch file to automate this process. In part 3 we’ll create the Visual Studio macro that automatically runs the Personal CI process every time you build your code.

The Series

In this series I show you how to take continuous integration to the extreme by building a personal CI server that runs all your tests and adds a commit to source control every time you build in Visual Studio.

Lets get to it.

Create the Personal CI Git Repository

The CI Server will get code changes from a Git repository. It will monitor this repository, and when changes are committed, it will pull them, build the code, and run the tests. Let’s create the repository.

For this example we’ll keep it simple and create the repository in a new folder our local machine. In reality, you may want to put this on another machine.

C:\Projects>md MyProjectPersonalCI.git

C:\Projects>cd MyProjectPersonalCI.git

Since this repository will only be accessed remotely, it doesn’t need a working directory—so we will use the --bare switch when initalizing it to tell Git not to create one.

C:\Projects\MyProjectPersonalCI.git>git --bare init

Initialized empty Git repository in C:\Projects\MyProjectPersonalCI.git\

Add the Personal CI Repository as a Remote

To push changes to the personal CI repository we need to add it as a remote to our local working repository. To avoid conflicts with developers using “origin”, we’ll give it the name “personalci”. How descriptive.

C:\Projects\MyProjectPersonalCI.git>cd ..

C:\Projects>cd MyProject

C:\Projects\MyProject>git remote add personalci C:\Projects\MyProjectPersonalCI.git

C:\Projects\MyProject>git remote



All we have to do now is push our changes to the personalci remote and they will be visible to the CI server. Let’s do that now (if there is any code in your project to commit).

C:\Projects\MyProject>git push personalci master

Counting objects: 899, done.

Delta compression using up to 2 threads.

Compressing objects: 100% (554/554), done.

Writing objects: 100% (899/899), 13.93 MiB | 3.15 MiB/s, done.

Total 899 (delta 422), reused 691 (delta 332)

To C:\Projects\MyProjectPersonalCI.git

* [new branch] master –> master

Your output may differ depending on how many files you have uncommitted in your project folde. I am adding this to an existing project of mine, so there are quite a few files.

The command git push personalci master is telling Git to push the changes from the master branch on my local repository to the master branch on the personalci remote repository. The remote master branch was automatically created for us since it didn’t already exist.

If you are having trouble understanding what Git is doing here, I encourage you to read the documentation. I don’t want to dive too deep into Git since it’s beyond the scope of this article.

Now we can push our changes up to the CI Server. Lets automate it.

Creating the Batch File

This batch file will do three things:

  1. Add any untracked files.
  2. Commit all changes with a custom commit message.
  3. Push the changes to the personalci remote repository.

Create a file in the root of your git repository called “CommitAndPushToPersonalCI.bat”. Yeah, I know…I’m a sucker for long descriptive names.

@REM CommitAndPushToPersonalCI.bat
@REM This script adds any untracked files and commits them and any changes to git.
@REM It then pushes the git repository changes up to the "personalci" remote repository.
@REM From there, a CI Server should pick up and run the build.

@ECHO on
@ECHO Auto-committing to Git Repository and push to PersonalCI after Successful Build.

@REM Add files that are outstanding to the local git repository.
call git add .
@if errorlevel 1 goto :error

@REM Commit all files to the repository with an autogenerated commit message.
call git commit -a -m "Auto-commit from successful build on %COMPUTERNAME% by %USERNAME% at %TIME% on %DATE%"
@if errorlevel 1 goto :error

@REM Push to the personalci remote repository.
call git push personalci master
@if errorlevel 1 goto :error

@ECHO SUCCESS: Pushed to Personal CI
@goto :exit

@ECHO ERROR! -- ErrorLevel: %errorLevel%


The code batch file is simple and should pretty much explain itself.

Run the batch file and you should get output similar to–but not exactly the same as the following:


We have everything ready, all we need now is to get Visual Studio to fire it off when we build.

Extreme CI Part 3: The Visual Studio Macro


Posted in .NET, Agile, Continuous Integration, Testing, Visual Studio | Tagged , , , , | 2 Responses

Structure and Interpretation of Computer Programs pdf is online

image Many of you probably already knew this, but I just found out. I heard Uncle Bob mention this book in a podcast and have wanted to read it for quite a while.

It’s not a cheap book, but I just found out its available online! You can get the pdf here on Scribd.



Posted in Books, Continuous Learning | Tagged , | Leave a comment

Extreme Continuous Integration: A Personal CI Build For Visual Studio – Part 1

How I run a personal CI build every time I build my code in Visual Studio.


In this series I show you how to take continuous integration to the extreme by building a personal CI server that runs all your tests and adds a commit to source control every time you build in Visual Studio.

This week, we look at the overview of the problem and the proposed solution.

The Problem

As more and more tests are written for a project, they take longer and longer to run. Even after optimizing tests I find that I spend far too much time waiting for them to run. So what do I do? I stop running all the tests, and only run part of them. But then how do I know when I have broken a test if I am not running it? And there lies the rub. It’s a catch-22. I try to save time by running fewer tests, only to lose time fixing errors those tests were written to protect me from. What to do?

The Idea

I thought, “If only I could keep working while the tests were running…But that would mean they would need to run on another machine…How would I kick that off…” Then it hit me, “I do this all the time, I just need a CI server.” Then the ideas just started flowing…


Continuous Integration:

Continuous Integration is an Agile/XP practice where developers check their code into a source code repository very often (at least once a day) and an automated process builds the code and runs all the tests against it to ensure quality.

To run this automated process, we a Continuous Integration Server application. This software can be configured to check the source control repository for changes, and kick off your build scripts (that you write) to compile your code, and run your tests. You can run unit, integration, and ever UI-tests. Its common to build installers and gather code metrics, etc., all automatically.

The top three CI Servers for .NET projects are CruiseControl.NET, TeamCity, and Hudson

If I could get a CI Server just for me, a “Personal CI”, and have it kick off every time I run my tests (even if I don’t run them all), then I could get out of that catch-22 and save time running tests because they will run automatically on another server while I continue working.

As an added bonus, I could have the CI Server tag the versions of the code that passed all the tests so I could roll back to the most recent good state if I ever needed to.

How It Works

So I schemed an planned, and tried, failed, and tried again, and here is what I came up with.


  • I build my code in Visual Studio.
  • A custom Visual Studio macro commits all my changes to a local Git repository.
  • The macro then pushes the changes to the Git repository that the CI Server is watching.
  • The CI Server sees the changes, pulls them down, builds the code, and runs all the tests.
  • When all the tests pass, the CI server can tag that version of the code so we can roll back to it if necessary.
  • On failure, the CI notifies me.

Lets walk through the major parts of this process.

The Visual Studio Macro

I chose to trigger the process when building rather than running the tests.  I did this for two reasons: (1) I wanted to run the tests as often as possible, (I build my code more often than I run the tests.) and (2) it was much easier than triggering off the test runner.

When code is built in Visual Studio, it fires of several events that can be captured by custom macros. When the macro fires off, it will execute a batch file that calls git on the command line to commit our code to the git repository and push it to the CI’s repository. The batch file is executed as an “External Tool” since we cant just shell out to the command prompt from the macro.

The Git Repositories

This process involves two repositories. The first is on the developer’s machine. It’s the developer’s local working repository. The second is the CI Server’s repository. This is set up as a remote on the developer’s repository so the changes can be pushed to the CI server.

I chose to use Git because it can work side by side with other source control packages.. If you are using SubVersion, you can just tell subversion to ignore the .Git folder and use them both.. If you are using Git, you can push to the CI Server’s repository without pushing to other developer repositories or your main repository. Its just another remote.

The CI Server

The CI server will detect that the new code has been committed to the repository, check it out, and run your build script against it to build the code and run the tests. What CI you choose to use is up to you. Hopefully, if you are setting up a personal CI Server, you already have a team CI Server that you are using. If not, you need to set up one of those first. So you would likely use the same CI Server software. I have done this with Hudson and TeamCity, though CCNet would work as well.

I set my CI Server up on my personal laptop and had it running next to my dev box. That way I could watch it run the tests and know immediately if it fails (using email or a CI monitor for notification works just as well).  Alternatively, you could also set up another box or VM somewhere on the network. I wouldn’t recommend running it on your dev machine (either in a VM, or directly on the machine) because that would really slow your machine down and defeat the purpose. Whatever machine you use, it doesn’t have to be a really fast one. After all, its running on its own so you don’t have to wait for it, so speed isn’t that big of a deal.

The Build Script

This is the script that builds your code and runs your tests. Use MSBuild, NAnt, PowerShell, Rake, or whatever floats our boat. If you have a team CI Server running, you can usually just use its script.

You can set this script up to tag the code when the tests pass if you like. Then you can roll back to the last known state where the tests passed.

Next week we’ll look at how to set up the Git repositories and automate pushing files from the developer’s working repository up to the CI Server.

Extreme CI Part 2: Setting Up Git


Posted in .NET, Agile, Continuous Integration, Testing, Visual Studio | Tagged , , , , | 2 Responses

Ultimate .NET Developer Tools Roundup

Over the last year, I have been compiling a list of all the tools I use on a regular basis. I was amazed how many there are. Below are the tools I find useful to do my job as a .NET developer. I hope you find them useful as well.

Development Tools

Version Control

Great SCM tool that is probably the most popular open source SCM around.

A distributed version control system (DVCS) that is gaining popularity.

Currently, the most popular open source Distributed Version Control System (DVCS).

Open Source Subversion plugin for Visual Studio.

Windows explorer extensions for Git.

GitEtensions Visual Studio Plugin
Visual Studio Plugin to manage git.

Windows Explorer extensions for SubVersion.

Windows Explorer extensions for Git, functions similar to TortoiseSVN.


Great Mocking Framework for .NET.

Test runner that can run NUnit, MSTest, MBUnit, etc, all in one. Really simplifies Continuous Integration when dealing with multiple test frameworks.

Visual Studio plugin that really simplifies running tests. Its the fastest test runner I have used.

The standard Unit Testing framework for .NET

Extension to NUnit to help promote BDD (Behaviour Driven Development)-style tests.

Machine.Specifications (MSpec)
Testing framework for .NET that promotes BDD style tests, but also seeks to minimize test size by the smart use of lambdas.

Fluency (previously called FluentObjectBuilder)
C# library I wrote to help create Fluent interfaces (DSLs) in C#. Works well to build configurations or dynamically build objects for testing. I’ll be adding blog posts detailing its use soon.

Data Access

SSMS Tools Pack
Plugin for SSMS that adds a good deal of functionality. Includes the ability to script out data, see versions of scripts that have run, etc.

The most popular ORM for the .NET platform.

.NET library that exposes fluent intefaces to configure NHibernate and define mappings.

Profiler for NHibernate written by Ayende Rahien. An absolute must-have for serious NHibernate development.

Coding Support

Power Commands
Plugin for Visual Studio that allows you to collapse to projects and many other functions.
For Visual Studio 2010: http://visualstudiogallery.msdn.microsoft.com/en-us/e5f41ad9-4edc-4912-bca3-91147db95b99
For Visual Studio 2008: http://visualstudiogallery.msdn.microsoft.com/en-us/df3f0c30-3d37-4e06-9ef8-3bff3508be31

The absolute MUST HAVE tool for any C# developer. If you haven’t used it, you really are missing out. I refuse to work without it.

.NET Profiler by the folks that brought us ReSharper. Will pinpoint hotspots in your code that are bottlenecks in performance.

Generates documentation comments in .NET from your method and variable names.

Excellent tool to help improve code readability within Visual Studio. Augments syntax highlighting. This is a commercial tool.

Windows utility that allows custom macros to be used. I use JP Boodhoo’s script to automatically add underscores when I press space while typing test names in Visual Studio.
AutoHotKey: http://www.autohotkey.com/
JPBoodhoo AutoHotKey Test Naming Script: http://blog.jpboodhoo.com/BDDAutoHotKeyScriptUpdateTake2.aspx

A source code analysis tool from Microsoft. Very similar to FXCop, but focused more on the readability. Works great paired with ReSharper.

StyleCop for ReSharper
Visual Studio plugin to show StyleCop violations inline in the editor. Integrates with ReSharper such that pressing alt-enter will fix many issues automatically.

Library that simplifies developent of T4 code generation templates.
T4Toolbox: http://t4toolbox.codeplex.com/
Oleg Sych’s blog (has many T4 Tutorials):http://www.olegsych.com/2009/10/t4-toolbox-support-for-visual-studio-2010/

Tangible T4 Editor
Visual Studio Plugin to allow editing of T4 templates with full syntax coloring.

Jimmy Bogard’s ReSharper TDD Productivity Plugin
This plugin allows you to jump from code, the the tests that cover it. However it is only supported thru ReSharper 4.5.


Sparx Systems: Enterprise Architect
The best UML modeling tool I have been able to find.

Code coverage too by JetBrains. Finally, a good alternative to NCover!

A very comprehensive code analyzer that generates many code metrics reports.

.NET code coverage tool. There is a free version, but support is lacking.

.NET Reflector
Formerly "Lutz Roeder’s .NET Reflector". Disassembles .NET code so you can view the source of dlls. Has now been purchased by RedGate.

Excellent diff viewer and 3-way merge tool. Beats the pants off TortoiseMerge. Free for personal use.

Microsoft’s build tool. Works well for automating builds and test runs. All things CI. Similar to NAnt.

MsBuild Community Tools
A collection of community-created tasks for MsBuild. Contains utilities to process zip files, execute sql, and much much more.

MSBuild Extension Pack
Another set of tasks for MsBuild.

Continuous Integration

Excellent CI server. I have used CCNet and Hudson, and really like TeamCity better than both. Allows full configuration of the build without editing tedious XML configuration files. Its free (with some limitations) and is from JetBrains. Great tool!

The de-facto standard CI server for the last several years. Works great, but can be difficult to configure.

Java-based CI server that is so easy to install, it is almost unbelievable. Like TeamCity, it allows full configuration of the build without editing tedious XML configuration files.


This is what I use to write blog entries. Haven’t found another tool that compares yet.

Syntax Highlighter 2.0
This used to be called the "Google Syntax Highlighter" and is a Javascript-based syntax highlighter. It has a wordpress plugin as well.

Screencasting & Presenting

Free, full-featured screencast recording software.

Most popular commercial screencasting tool around.

Allows you to dynamically zoom in to portions of your screen. Works great to point out code details during presentations.

Keyboard Jedi
Tool to show hotkey combinations as you type them. Great to use during screencasts or presentations so the users can learn from seeing what keys you are pressing.


Process Explorer
Really nice tool to replace Task Manager. Shows much more detail.

A remote desktop client that supports tabs across to top for simultaneous connections, and scaling to view higher resolutions on a lower resolution screen.

Screen sharing software that works well in remote pairing. I use a dual monitor system with one monitor being mine, and the other, the monitor of the person I am pairing with. Works really well.

Screenshot Captor
Free utility to capture full screenshots or portions of the screen. Very similar to SnagIt.

MultiMon Taskbar
Extends your taskbar to your second monitor in XP or Vista.

Windows defrag utility that optimizes boot and often-accessed files to the outer edges of the spindle so they have faster access.

A windows clipboard tool that remembers the last several thousand snippets that were in your clipboard and maked them available to you via a simple dropdown menu.

Great utility that allows you to scroll a window using your mouse wheel when the mouse is over it, without having to give the window the focus. Once you use it, you will wonder how you ever did without it.

Control another computer’s mouse and keyboard simply by moving your mouse from your screen onto the adjacent computer’s screen. Makes two computers (or 3 or 4 or…) feel like one. I use it across 2 Windows boxes and my MacBook. The clipboard is shared and all!

SynergyKM (for Mac OSX)
Mac OSX version of Synergy. Much easier than trying to get the standard command line build to work.


Nice twitter app. They even have a version for Windows Mobile.

I used Twhirl until I found TweetDeck. Very nice application takes it to the next level. Integrates Twitter, Facebook, and now Google Buzz. It also has an iPhone app (which is great for me since I recently switched to an iPhone).

Great chat client, but also has high quality voice chat built in. I use this and VNC when pairing remotely. (I have used skype as well).

Voice chat online. Great for remote pairing.

Nice scrolling ticker for your RSS feeds. Automatically syncs with Google Reader and works on Mac and Windows. I keep this and TweetDeck running on my MacBook at work to keep me plugged in to the collective knowledge of the development community.

Documents & Notes

E Text Editor
TextMate for Windows….need I say more?

Open source, cross platform mind-mapping tool. If you haven’t tried a mind mapping tool before, please do. It takes note-taking and idea organization to a whole new level.

Visualization of directed graphs. Describe the graph in simple text, and generate the graphics from it. Simple, but powerful. Great for visualizing code dependencies and relationships programmatically.
Mac Gui (+ iPhone): http://www.pixelglow.com/graphviz/

Simple, solid text editor that allows you to specify your own syntax highlighting (to a certain degree).

Video Training Sites

I found the following sites very useful when trying to learn new technologies quickly.

Very reasonably priced, high quality training videos on new development topics.

Free, 10-minute training screencasts on new .NET technolgies. Perfect for the ADD inflicted developer like myself.

Posted in .NET, Tools, Utilities | Tagged , , , | 7 Responses