This project is read-only.

VS integration with MSBuild

Nov 26, 2009 at 8:14 PM

Hi Nicholas, Your works is great. I have started using it in my project and also for reference to something i have to build.

I am building a build and dev package as part of my work for VS. We have our build system wwritten around msbuild and are also writing targets for several other tasks (like reference handling, code release, distribution etc).

Here are a few things I need to achieve.

1. We need full visual studio integration and also the facility to do all the functions with command line. So, i thought i would write all the functionalities as msbuild targets,which will give me command line flexiblity for free.

2. Now, i need to get GUI options as a visual studio add-in that will have all these targets as menu options for user to trigger. Any parameters required will have to be passed from the GUI. How do i achieve GUI interaction?

3. Since we are doing everything in Visual Studio, we need make change to the project files etc. I somehow would like to avoid reloading of the project. Can this be done? I found from MSDN that making changes to the cached sln/proj file would not relaoad project, but will still not pick up the values. How can i do this?

4. How can i get these targets work in process in visual studio? I don't want to shell out the commands to a command line and run them. How do i integrate into VS and get it working? This would be very useful and less annoying for developers.

5. I know VS uses the in process compiler for c# and VB projects, is there someway i can utilize thsi to get what i need done? The option you have mentioned as annoying in your write up is probably what i should be using.

6. My idea for the add-in menu for the target is to keep it completely dynamic. I want the add0in to read some property in all my targets (like "IsVSRunnable") and pick those and add it to the menu.

I know this is a lot for a clarification. ANy help




Nov 28, 2009 at 10:57 AM

As I've responded you on Codeproject:

Hi dudbolt, good to see you use it, stay tuned, I'm coding an extensibility point ! :p

2. GUI interaction can be achieved by creating Visual Studio Addin, the best place to document yourself is here
I don't really like the API to extend visual studio, so I try to not use it.
You can do what you want with it, but the API is difficult to understand and not well documented sometimes.
I've never used it except for a really really simple addin.

So another solution is to use the "external tools" feature from Visual Studio in Tools/External Tools.
For example, the command C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe with arguments "$(ProjectDir)$(ProjectFileName)" will build the current selected project. Once you add a new Tool (command + parameters), it will appear in the Tools menu item of Visual Studio. You can also configure shortcut keys on them.

For your project you can create one new tool for each target you want to be able to fire on your project for example 
Command : C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe
Arguments : $(ProjectDir)$(ProjectFileName) /t:YourTarget

If you don't want to add your target definition in every projects, MSBuild provide an extensibility point at the machine level.
just add :
C:/program files/Msbuild/v3.5/Custom.After.Microsoft.Common.targets and/or 
C:/program files/Msbuild/v3.5/Custom.Before.Microsoft.Common.targets, and write your targets definition here, these file are automatically imported in every project your build on your machine. 
(You can see why in C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets in line 27 and line 4068).

This extensibility point + external tools and you can do crazy things without one line of code.

3. I have no idea on how you can modify a project file without reloading your project, if you find let me know.
A workaround would be to create a separate target file to isolate changes and import it in the csproj.
This way you just modify the target file, and not your project file. But I think Visual Studio also puts in cache the target file.

4. Well, do what I said in 2., and check Use Output Windows in the External Tools dialog, it's awesome. (you can also prompt for arguments)

5. Indeed; VS use an ip process compile, and I've found how to disable it with themsbuild property UseHostCompilerIfAvailable to false.
I have no idea on what you can do with it. I think it's just to speed up the compilation because the intellisense as already "pre parsed" the source code, but I'm not sure.

6. If you want to make it dynamic, external tools is not the right option. Generally speaking, you don't add or delete targets everyday, maybe a simpler approach with external tools is better.
Nevertheless, the only solution I see if you really want to do that is a visual studio Addin with a FileSystemWatcher which monitor your project files for changes, and add menu dynamically. (sorry I'm not really good with the VS API, so I can't really help you more than that)

Hope it will help,