English 中文(简体)
get布
原标题:get list of subdirectories in msbuild

根据名录:

<ItemGroup>
  <Dirs Include="FooDir1" />
  <Dirs Include="FooDir2" />
</ItemGroup>

我如何能够得到一份所有分局的名单。

将这一清单与<代码>(实体)**”改为“,与任何内容不相符,并与<代码>(实体)***”改为“,然后与<代码>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>

但我要知道,有没有一种更为容易的办法。

最佳回答

摘自“MSBuild Trickery”一书:

<Import Project="EnableAllPropertyFunctions.tasks" />

<Target Name="GetSubdirectories">
   <ItemGroup>
      <Dirs Include="$([System.IO.Directory]::
         EnumerateDirectories(
            `.Foo`,
            `*`,
            System.IO.SearchOption.AllDirectories))"
            />
   </ItemGroup>

   <Message Text="%(Dirs.Identity)" />
</Target>

你们需要首先通过确保环境变异的MSBuild PropertyFunctions被设定为价值1(即进口品的Ctasks文档所完成,并承担在线任务)来实现扩大的财产功能。

@(Dirs)一旦设立,你就可以用“消除”的特性加以过滤,以摆脱颠覆的夹子。

问题回答
<CreateItem Include="$(OutputFolder)**.*">
  <Output TaskParameter="Include" ItemName="FilesInSubFolders" />
</CreateItem>

<RemoveDuplicates Inputs="@(FilesInSubFolders-> %(RelativeDir) )">
  <Output TaskParameter="Filtered" ItemName="SubDirs"/>
</RemoveDuplicates>

<Message Text="@(SubDirs)"/>

这将把所有直接的次重通道带入@(分流)。 如果您改动Include=“$(OutputFolder)**.*>Include=“$(Output Folder)***.*,则复数包括所有分数。

以完全自足的例子扩大布赖恩的答复:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="GetSubdirectories">
  <UsingTask TaskName="SetEnvironmentVariable"
             TaskFactory="CodeTaskFactory"
             AssemblyFile="$(MSBuildToolsPath)Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll">
    <ParameterGroup>
      <Name ParameterType="System.String" Required="true" />
      <Value ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Using Namespace="System" />
      <Code Type="Fragment" Language="cs"><![CDATA[
        Environment.SetEnvironmentVariable(Name, Value);
      ]]></Code>
    </Task>
  </UsingTask>

  <Target Name="GetSubdirectories">
    <SetEnvironmentVariable Name="MSBuildEnableAllPropertyFunctions" Value="1" />
    <ItemGroup>
      <Dirs Include="$([System.IO.Directory]::EnumerateFiles( .Stuff ,  * , System.IO.SearchOption.AllDirectories))"/>
    </ItemGroup>
    <Message Text="%(Dirs.Identity)" />
  </Target>
</Project>

I got the UsingTask example from 页: 1





相关问题
How can I overwrite file web.config with web.other.config?

I have a msbuild script with custom logic to deploy my service to the qa server automatically. I have to overwrite the default config with a dedicated one, but when I use <Copy SourceFiles="web....

How do I use MSBuild on a C# project with no compilable code

I have a C# web app project which actually has no ASP.Net or C# in it. It s just a single html page with some Javascript, CSS, and a couple of images. I want to use MSBuild to deploy a version of ...

您能否防止MSBuild.exe经营活动?

我用文字建造一些项目,偶尔利用习俗制造事件,给建筑系统造成了很大困难。 如果有可能,我想援引MSBuild.exe。

building .net applications without Visual Studio

I m interested to hear about people working with building .net applications using MSBuild, NAnt or similar tools. What are you using, why are you using it instead of the VS IDE? I like to use ...

How long does my Visual Studio C# compile take?

It seems like it should be possible to configure Visual Studio to print out how long the compile takes. Perhaps somewhere in the .sln file. If the sln is compiled from the command line using ...

热门标签