Passing custom project properties from Visual Studio Extension during build
Asked Answered
C

1

7

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

Clearstory answered 4/6, 2013 at 19:23 Comment(8)
How Visual Studio Extension invoked MSBUILD?Pithecanthropus
@NickCarlson - I do not want to invoke MsBuild, but .csproj files ARE MsBuild scripts and Visual Studio is passing actual values of $(Platform) and $(Configuration) variables during build. I want to have $(MyOwnProperty) value passed to this script, so I can use it (e.g. <Target Name="SomeTarget" Condition="$(MyOwnProperty) == 'foobar'">)Clearstory
In your original post you said you want to be able to define your own property via a Visual Studio Extension, but now you're saying that you don't want to invoke MSBUILD. Are you writing your own extension and want to define your own custom properties? Perhaps a code sample would help illustrate the problem.Pithecanthropus
* A Visual Studio Extension is an add-on program that extends the functionality of Visual Studio. (visualstudiogallery.msdn.microsoft.com). Did you write a Visual Studio Extension and are somehow wanting to pass properties to MSBUILD, or by "Visual Studio Extension" are you referring to a C# project file who's file EXTENSION is associated with Visual Studio?Pithecanthropus
@NickCarlson - I meant an add-on for Visual Studio (which is commonly referred to as Visual Studio extension - VSX for short) and I said that I do not want to invoke MSBUILD because Visual Studio is not internally using MSBUILD command line but has an integrated build system (based or related to MSBUILD - it is not important). I updated the question with an example of what I would like to accomplish.Clearstory
did you ever find out? it's a good question.Anybody
@Erti-ChrisEelmaa unfortunately notClearstory
@MaciejWozniak: I have been developing vs extensions for the past last 6 months or so, and I just noticed this thread again, now a little smarter :-)Anybody
A
4

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(..);
Anybody answered 14/8, 2014 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.