Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild ItemGroup with condition

I don't know if ItemGroup is the right type to use. I will get 4 different booleans that will be true or false depending on choice.

I would like to fill up an ItemGroup with this "strings" depending on the true or false. Is that possible or what should I use?

Example

Anders = true
Peter = false
Michael = false
Gustaf = true

My ItemGroup should then have Anders and Gustaf.

Is that possible or how should I solve that?

like image 293
user1540911 Avatar asked Oct 22 '12 11:10

user1540911


People also ask

What is itemgroup in MSBuild?

Identifies the ItemGroup. Defines the inputs for the build process. There may be zero or more Item elements in an ItemGroup. Required root element of an MSBuild project file. Starting with .NET Framework 3.5, the ItemGroup element can appear inside a Target element. For more information, see Targets.

How do I apply conditions to items in an itemgroup?

ItemGroups can have conditions applied by using the Condition attribute. In that case, the items are only added to the item list if the condition is satisfied. See MSBuild conditions The Label attribute is used in some build systems as a way to control build behaviors.

What happens when multiple items are included in an itemgroup?

When multiple ItemGroup elements are used, items are combined into a single ItemGroup. For example, some items might be included by a separate ItemGroup element that's defined in an imported file. ItemGroups can have conditions applied by using the Condition attribute.

What are conditions in MSBuild?

MSBuild conditions. MSBuild supports a specific set of conditions that can be applied wherever a Condition attribute is allowed.


1 Answers

Since you have a bunch of items, it would be better to store them in an ItemGroup from the start since after all that is what it is meant for and it also allows transformations etc. For example this achieves what you want:

<ItemGroup>
  <Names Include="Anders">
    <Value>True</Value>
  </Names>
  <Names Include="Peter">
    <Value>False</Value>
  </Names>
  <Names Include="Michael">
    <Value>False</Value>
  </Names>
  <Names Include="Gustaf">
    <Value>True</Value>
  </Names>
</ItemGroup>

<Target Name="GetNames">

  <ItemGroup>
    <AllNames Include="%(Names.Identity)" Condition="%(Names.Value)==true"/>
  </ItemGroup>

  <Message Text="@(AllNames)"/>  <!--AllNames contains Anders and Gustaf-->
</Target>

However if they must be properties, I do not think there is another way than enumerating them all manually like so:

<PropertyGroup>
  <Anders>True</Anders>
  <Peter>False</Peter>
  <Michael>False</Michael>
  <Gustaf>True</Gustaf>
</PropertyGroup>

<Target Name="GetNames">

  <ItemGroup>
    <AllNames Include="Anders" Condition="$(Anders)==true"/>
    <AllNames Include="Peter" Condition="$(Peter)==true"/>
    <AllNames Include="Michael" Condition="$(Michael)==true"/>
    <AllNames Include="Gustaf" Condition="$(Gustaf)==true"/>
  </ItemGroup>

  <Message Text="@(AllNames)"/>
</Target>
like image 163
stijn Avatar answered Oct 10 '22 22:10

stijn