Question

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?

Was it helpful?

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.

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top