MSBuild Filtering ItemGroup of files with condition

后端 未结 2 455
春和景丽
春和景丽 2021-01-27 04:12

This feels like it\'s so simple, but I cannot get it to work.

All I\'m trying to achieve is a filtered list of the embedded resources. I\'ve tried various approaches bu

相关标签:
2条回答
  • 2021-01-27 04:22

    Inside a target, this can be done using the batching syntax for items and using the System.String.Copy method to be able to call instance functions on the string:

    <Target Name="ListAllEmbeddedResources">
      <ItemGroup>
        <AllEmbeddedResources Include="@(EmbeddedResource)" Condition="$([System.String]::Copy(%(FullPath)).Contains('Change'))" />
      </ItemGroup>
      <Message Importance="high" Text="AllEmbeddedResources: %(AllEmbeddedResources.Identity)" />
    </Target>
    

    Note that this syntax only works inside a target and not during static evaluation (item group directly under the <Project> node).

    0 讨论(0)
  • 2021-01-27 04:43

    The Condition Attribute must return a boolean, and it operates on each element of the itemgroup. You can access each element using %(Identity). Say you have some unfiltered itemgroup called UnfilteredItems, and you want to filter those into a group called MyFilteredItems, using some regex pattern.

    <ItemGroup>
      <MyFilteredItems Include="@(UnfilteredItems)" Condition="$([System.Text.RegularExpressions.Regex]::Match(%(Identity),'.*\\bin\\.*').Success)"/>
    </ItemGroup> 
    
    0 讨论(0)
提交回复
热议问题