msbuild从属性组创建项目组 [英] msbuild create itemgroup from property group
本文介绍了msbuild从属性组创建项目组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想传递用分号分隔的字符串列表.
每个字符串代表一个文件名.
I want to pass a semi-colon separated list of strings.
Each string represents a file name.
<PropertyGroup>
<FileNames>Newtonsoft.Json;Reactive</FileNames>
<PathToOutput>C:/</PathToOutput>
</PropertyGroup>
现在,我想创建一个项目组,该组应该给我所有特定文件夹中的文件,但不包括文件名列表,例如:
Now I want to create an item group which should give me all the files in particular folder excluding list of Filename, something like:
<ItemGroup>
<ReleaseFiles Include="$(PathToOutput)\**\*.*" Exclude="%(identity)-> identity.contains(%FileNames)"/>
</ItemGroup>
如果文件名变量中包含任意一个文件名,如何遍历当前文件夹的文件并匹配每个文件名.
How do I iterate through current folder's files and match each ones name if it contains any one filename in Filenames variable.
推荐答案
很确定这是一个重复项,但是我暂时找不到,所以在这里:
Pretty sure this is a duplicate but I cannot find it at the moment so here it goes:
- 将分号分隔的属性转换为项目只是使用
Include=$(Property)
-
Exclude
仅在具有完全匹配列表的情况下有效,但是您需要在此处进行更多任意过滤,因此您需要Condition
通过使ReleaseFiles项目的那些FileNames元数据,像交叉产品一样将两个ItemGroups连接在一起.然后,您可以遍历ReleaseFiles中的每个项目并同时访问FileNames
-
Contains
是属性函数(或者是System :: String方法),因此无法在元数据上使用,因此我们首先将元数据转换为字符串
- turning a semicolon-seperated property into an item is just a matter of using
Include=$(Property)
Exclude
only works if you have a list of exact matches, but you need more arbitrary filtering here so you'll needCondition
- join the two ItemGroups together like a cross-product, by making those FileNames metadata of the ReleaseFiles item. Then you can iterate over each item in ReleaseFiles and have access to the FileNames at the same time
Contains
is a property function (well, or a System::String method) so won't work as such on metadata, hence we turn metadata into a string first
在代码中:
<PropertyGroup>
<FileNames>Newtonsoft.Json;Reactive</FileNames>
<PathToOutput>C:/</PathToOutput>
</PropertyGroup>
<Target Name="FilterBasedCommaSeperatedProperty">
<ItemGroup>
<!-- property -> item -->
<Excl Include="$(FileNames)"/>
<!-- list all and add metadata list -->
<AllReleaseFiles Include="$(PathToOutput)\**\*.*">
<Excl>%(Excl.Identity)</Excl>
</AllReleaseFiles >
<!-- filter to get list of files we don't want -->
<FilesToExclude Include="@(AllReleaseFiles)"
Condition="$([System.String]::Copy('%(FileName)').Contains('%(Excl)'))"/>
<!-- all but the ones to exclude -->
<ReleaseFiles Include="@(AllReleaseFiles)" Exclude="@(FilesToExclude)"/>
</ItemGroup>
<Message Text="%(ReleaseFiles.Identity)" />
</Target>
这篇关于msbuild从属性组创建项目组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文