How to programmatically override the build and launch actions?
Asked Answered
D

1

5

I created a custom project template associated with a custom project type. The solution depends heavily on MPF for Projects - Visual Studio 2012 framework.

What i would like to do next is override the default "Build" (F6) and "Start without debugging" (ctrl + F6) events for this custom project type. The solution itself will be deployed as a VSIX package.

Any help is appreciated.

Dora answered 9/4, 2013 at 17:34 Comment(5)
What do you want those keys to do instead? There might be a way to integrate what you want them to do into the regular paths.Seve
@JasonMalinowski I want to use an external compiler alongside with some custom logic depending on the types of files added to the project.Dora
Should that customization be done via msbuild instead of any VS project types? What do you expect to happen if the user builds from the command line or their favorite continuous integration build process?Seve
@JasonMalinowski I don't quite understand the first question. I want to use an external compiler(s) to build my project based on the file extensions in the custom project type.Dora
Ah, maybe I misunderstood. Are you creating a project type for a fundamentally new language, or implementing some fancy tricks atop C#/VB/C++/etc.?Seve
M
9

You can intercept any command coming from the Visual Studio UI in VSPackage. To do this, you should subscribe to the desired event of DTE.Events.CommandEvents. You have to pass GUID and Id to CommandEvents.

private void Initialize()
{
  var dte = GetService(typeof(SDTE)) as EnvDTE.DTE;
  _startCommandEvents = dte.Events.CommandEvents["{5EFC7975-14BC-11CF-9B2B-00AA00573819}", 295];
  _startCommandEvents.BeforeExecute += OnLeaveBreakMode;
}

private void OnBeforeStartCommand(string guid, int id, object customIn, object customOut, ref bool cancelDefault)
{
  //your event handler this command
}

Your event handler has ref bool cancelDefault parameter, passing in cancelDefault the TRUE you cancel the VS command, thereby replacing VS behavior by their.

To get GUID and Id command you can use VSIP Logging feature. To enable this feature, set the value of registry key:

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\<version>\General] "EnableVSIPLogging"=dword:00000001 

and restart Visual Studio IDE. Then using Ctrl-Shift, click on a menu item and you'll get a message like this:

Guid and Id command

Guid and CmdID from message are a required parameter for CommandEvents.

If you are implementing a new language (create a new type of project), it is more correct to add the custom Debug Engine and MSBuild integration. You can see examples of such implementation in IronPython or Nemerle projects.

Melancon answered 14/4, 2013 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.