Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hooking into build process in Visual Studio

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?

like image 761
Spook Avatar asked Jan 11 '23 18:01

Spook


2 Answers

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.

like image 136
Cameron Avatar answered Jan 17 '23 22:01

Cameron


I believe if you implement IVsUpdateSolutionEvents2 interface,

and the method

public int UpdateSolution_Begin(ref int pfCancelUpdate)

you then have a method that will notify you once the build is started, and allows you to cancel it.

Ps, take a look at PyTools, it has implemented the interfaces already, (you'd also need to implement IVsSolutionBuildManager3 and call necessary methods, such as AdviseUpdateSolutionEvents, etc).

like image 43
Erti-Chris Eelmaa Avatar answered Jan 17 '23 22:01

Erti-Chris Eelmaa