获取msbuild中的子目录列表
给定目录列表:
<ItemGroup>
<Dirs Include="Foo\Dir1" />
<Dirs Include="Foo\Dir2" />
</ItemGroup>
如何获取所有子目录的列表。
使用 "$(Identity)\**"
转换此列表不会匹配任何内容,使用 "$(Identity)\**\*"
转换,然后使用 RelativeDir
仅生成包含文件的目录。
目前我必须求助于 C#:
<UsingTask TaskName="GetSubdirectories" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Directories ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<SubDirectories ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs"><![CDATA[
var result = new List<ITaskItem>();
foreach (var dirItem in Directories) {
foreach (var dir in Directory.GetDirectories(dirItem.ItemSpec, "*", SearchOption.AllDirectories)) {
if (dir.Contains(@"\.svn\") || dir.EndsWith(@"\.svn")) continue;
result.Add(new TaskItem(dir));
}
}
SubDirectories = result.ToArray();
]]></Code>
</Task>
</UsingTask>
<GetSubdirectories Directories="@(Dirs)">
<Output TaskParameter="SubDirectories" ItemName="SubDirs" />
</GetSubdirectories>
但我想知道是否有更简单的方法。
Given a list of directories:
<ItemGroup>
<Dirs Include="Foo\Dir1" />
<Dirs Include="Foo\Dir2" />
</ItemGroup>
How can I get a list of all subdirectories.
Transforming this list with "$(Identity)\**"
does not match anything and transforming with "$(Identity)\**\*"
and then with RelativeDir
yields only directories that contain files.
Currently I have to resort to C#:
<UsingTask TaskName="GetSubdirectories" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Directories ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<SubDirectories ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs"><![CDATA[
var result = new List<ITaskItem>();
foreach (var dirItem in Directories) {
foreach (var dir in Directory.GetDirectories(dirItem.ItemSpec, "*", SearchOption.AllDirectories)) {
if (dir.Contains(@"\.svn\") || dir.EndsWith(@"\.svn")) continue;
result.Add(new TaskItem(dir));
}
}
SubDirectories = result.ToArray();
]]></Code>
</Task>
</UsingTask>
<GetSubdirectories Directories="@(Dirs)">
<Output TaskParameter="SubDirectories" ItemName="SubDirs" />
</GetSubdirectories>
But I would like to know if there is an easier way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
摘自《MSBuild Trickery》一书:
您需要首先通过确保环境变量 MSBuildEnableAllPropertyFunctions 设置为值 1 来启用扩展属性函数集(这就是导入的 .tasks 文件通过内联任务完成的任务) 。
设置 @(Dirs) 后,您可以使用“删除”属性对其进行过滤以删除 Subversion 文件夹。
Excerpted from the book "MSBuild Trickery":
You'll need to first enable the extended property function set by ensuring that the environment variable MSBuildEnableAllPropertyFunctions is set to the value 1 (that is what the imported .tasks file accomplishes, with an inline task).
Once @(Dirs) is set up, you can filter it with the Remove attribute to get rid of the Subversion folders.
这会将所有直接子文件夹路径放入@(SubDirs)。如果将
Include="$(OutputFolder)\*\*.*"
更改为Include="$(OutputFolder)\**\*.*"
,则将递归地包含所有子文件夹。This will put all the immediate subfolder paths into @(SubDirs). If you change
Include="$(OutputFolder)\*\*.*"
toInclude="$(OutputFolder)\**\*.*"
, it'll include all subfolders recursively.为了用一个完全独立的示例来扩展 Brian 的答案:
我从 this 获取了UsingTask 示例回答。
To expand on Brian's answer with a fully self-contained example:
I got the UsingTask example from this answer.