MSBuild ItemGroup with condition

2019-04-26 12:33发布

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?

1条回答
狗以群分
2楼-- · 2019-04-26 12:51

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>
查看更多
登录 后发表回答