Hooking into build process in Visual Studio

c#, visual-studio, visual-studio-2013, vs-extensibility, vsix

Solution

You can use `EnvDTE.Events.BuildEvents.OnBuildBegin` and `OnBuildDone`.

Note that Every time you say `dte.Events.BuildEvents` you're creating a new COM object behind the scenes that is garbage collected even if you still have an event listener on it. So save the BuildEvents object into a member variable somewhere before attaching your event handlers to it (so that it doesn't get garbage collected while you're using it).

You could also implement `Microsoft.VisualStudio.Shell.Interop.IVsBuildStatusCallback` (and hook it into VS via `AdviseBuildStatusCallback`) if you're sick of EnvDTE :P

Edit: Both of those run on the UI thread, but upon further reflection I think it might be too late at that point to modify the build itself (MSBuild may have already been sent the files and started building asynchronously). I'm not sure.

Problem

I'm writing a Visual Studio extension, which allows editing specific type of files in the project. This file serves as a description for further automated code generation (similarly to, say, Entity Framework). I need the code generation to be performed prior to building of the project, which contains the description file. Code generation algorithm is (currently) placed inside the editor of the description file. Is there a way to hook the building process to automatically perform some additional steps before actually building the project?

Original source