Thursday, June 06, 2013

Hijacking the Visual Studio Build Process

Have you ever wanted to use Visual Studio to manage project artifacts but wanted to have a fully custom build process? The recommend way to do this is to build a Custom Visual Studio Project System, but there is a much easier way for lightweight needs. In this post I'll show you how to take an existing project and "replace" the build process used? For example, wouldn't it be cool if you could develop a Chrome Extension with VS? When you do a build it would be great to generate the .zip file to for the Chrome Gallery in the output folder. Doing this is way easier than you might think.

Before I go over the steps to do this let me explain the "contract" between Visual Studio and MSBuild. The primary interactions around build in Visual Studio include the following actions in VS. Including what VS does when the action is invoked.

  • Build – VS invokes the DefaultTarget for the project file
  • Rebuild – VS invokes the "Rebuild" target
  • Clean – VS invokes the "Clean" target
For more details you can read Visual Studio Integration (MSBuild). The easiest way to completely customize the VS build process is to do the following:

  • Create the correct project based on the artifacts you plan on using (i.e. if you're going to be editing .js files then create a web project)
  • Edit the project file to not import any of the .targets files
  • Define the following targets; Build, Rebuild and Clean
After that when you invoke the actions inside of VS the correct action will be executed in your project.

Let's look at a concrete example. I've been working on a Chrome extension with Mads Kristensen the past few days. The entire project is available on github at https://github.com/ligershark/BestPracticesChromeExtension. When it came time to try out the extension or to publish it to the Chrome store we'd have to manually create it, which was annoying. We were using a standard web project to begin with. Here is how we made the workflow simple. Edited the project file to disable the Import statements. See below.

QR: Inline image 1