Hooking into build process in Visual Studio
Asked Answered
E

2

5

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?

Ela answered 23/4, 2014 at 11:38 Comment(4)
What about Roslyn?Babyblueeyes
How would it help me hooking into Visual Studio building process?Ela
I think Roslyn won´t help; but an additional build target (hooked into BeforeBuild) and/or a custom build task that does the preprocessing might be a possible solution. Your extension could modify the project file to enable the functionality, like PostSharp.Bakki
Go learn about MSBUILD, which is how the builds are done.Holohedral
R
5

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.

Religion answered 6/5, 2014 at 21:19 Comment(3)
I need to do something similar (generated code before a Build). Here's how I hooked into the event system: github.com/ppittle/pMixins/blob/master/…Irrespirable
Any idea of how to detect the Visual Studio Compile command (not Build)?Kirkuk
When compiling a single file, the underlying MSBuild machinery is still invoked end to end, just with different parameters. You might find some events that still fire. It probably depends on the project system.Religion
H
5

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

Hiroko answered 1/5, 2014 at 10:0 Comment(1)
+1, after reading the docs this seems to be the way to go -- despite the odd naming of the method, it's supposed to be fired before the build starts (though I think it's in the IVsUpdateSolutionEvents2 interface).Religion
R
5

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.

Religion answered 6/5, 2014 at 21:19 Comment(3)
I need to do something similar (generated code before a Build). Here's how I hooked into the event system: github.com/ppittle/pMixins/blob/master/…Irrespirable
Any idea of how to detect the Visual Studio Compile command (not Build)?Kirkuk
When compiling a single file, the underlying MSBuild machinery is still invoked end to end, just with different parameters. You might find some events that still fire. It probably depends on the project system.Religion

© 2022 - 2024 — McMap. All rights reserved.