Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing custom project properties from Visual Studio Extension during build

How can I pass a custom MsBuild property to Visual Studio build engine from Visual Studio Extension (I want to write a custom add-in) ? Just like Visual Studio itself is passing properties like $(Configuration) and $(Platform), I would like to attach to a build process and pass my own property (e.g. $(MyCustomProperty) = "foobar"). I know it can be accomplished using MsBuild command line (/p:Property), but I want to do it from an extension.

Here is an example of what I would like to accompilsh: a textbox on visual studio toolbar, where user can type in some text - a value, that I would like to pass to build engine.

Then in .csproj:

<PropertyGroup>
  <MyCustomProperty Condition=" '$(MyCustomProperty)' == '' ">DefaultValue</MyCustomProperty>
</PropertyGroup>

<Target Name="AfterBuild" Condition=" '$(MyCustomProperty)' = 'DoAfterBuild' ">
  ...
</Target>

So target AfterBuild would be exectued only if user typed DoAfterBuild into my extension's text box

like image 634
Maciej Wozniak Avatar asked Jun 04 '13 19:06

Maciej Wozniak


1 Answers

You can request service IVsBuildPropertyStorage, it has following method:

SetPropertyValue: Used by a project subtype to set an MSBuild property value.

Something like this:

EnvDTE.Project proj = ...;
var sol = Package.GetGlobalService(typeof(VsSolution)) as IVsSolution;
IVsHierarchy hier;
sol.GetProjectOfUniqueName(p.UniqueName, out hier);
var storage = hier as IVsBuildPropertyStorage;
storage.SetPropertyValue(..);
like image 78
Erti-Chris Eelmaa Avatar answered Sep 30 '22 16:09

Erti-Chris Eelmaa