gpt4 book ai didi

collections - 加入的项目组上的 MSBuild 元数据

转载 作者:行者123 更新时间:2023-12-04 17:27:59 28 4
gpt4 key购买 nike

我有两个项目组想要加入:

  <ItemGroup>
<ServerTypeA Include="ServerA;ServerB;">
<MetaDataA>A</MetaDataA>
</ServerTypeA>
</ItemGroup>

<ItemGroup>
<ServerTypeB Include="ServerB;ServerC;">
<MetaDataB>B</MetaDataB>
</ServerTypeB>
</ItemGroup>

使用常规连接会给我一个包含 4 个项目的集合:
ServerA with Metadata A;
ServerB with Metadata A;
ServerB with Metadata B;
ServerC with Metadata B;

如何创建以下集合:
ServerA with Metadata A
ServerB with Metadata A & B
ServerC with Metadata B

最佳答案

这是可能的。您必须可以手动执行连接。

以下是如何执行此操作的示例(需要 msbuild 3.5 或更高版本):

<ItemGroup>
<ServerTypeA Include="ServerA;ServerB;">
<MetaDataA>A</MetaDataA>
</ServerTypeA>

<ServerTypeB Include="ServerB;ServerC;">
<MetaDataB>B</MetaDataB>
</ServerTypeB>
</ItemGroup>

<Target Name="JoinServers" DependsOnTargets="ProcessServerTypeA;ProcessServerTypeB">
<Message Text="%(Joined.Identity) Metadata: %(Joined.MetaDataA)%(Joined.MetaDataB)"/>
</Target>

<!--Create -->
<Target Name="ProcessServerTypeA">
<ItemGroup>
<Joined Include="%(ServerTypeA.Identity)">
<MetaDataA>%(ServerTypeA.MetaDataA)</MetaDataA>
</Joined>
</ItemGroup>
</Target>

<!--Need to batch at the target level for this to work-->
<Target Name="ProcessServerTypeB" Inputs="@(ServerTypeB)" Outputs="%(ServerTypeB.Identity)'">

<PropertyGroup>
<!--Create Temporary Properties for the Item Metadata-->
<TempItemName>%(ServerTypeB.Identity)</TempItemName>
<TempMetaDataB>%(ServerTypeB.MetaDataB)</TempMetaDataB>
<!--Does the current item already exist?-->
<TempIsDuplicate Condition="'%(Joined.Identity)' == '$(TempItemName)'">True</TempIsDuplicate>
</PropertyGroup>

<ItemGroup>
<!--Update the existing item's metadata if this is a duplicate-->
<!--Don't provide the include attribute. This will allow you to update existing items metadata-->
<!--Have to reference %(Joined.Identity) in the condtion to ensure we only update the correct item-->
<!--You cannot directly reference metadata from ServerTypeB here. Hence the need for the temp Properties-->
<Joined Condition="'%(Joined.Identity)' == '$(TempItemName)'">
<MetaDataB>$(TempMetaDataB)</MetaDataB>
</Joined>

<!--Create a new item if current item is not a duplicate-->
<Joined Include="$(TempItemName)" Condition="'$(TempIsDuplicate)' != 'True'">
<MetaDataB>$(TempMetaDataB)</MetaDataB>
</Joined>
</ItemGroup>
</Target>

运行 JoinServers目标将产生以下输出:
ServerA Metadata: A
ServerB Metadata: AB
ServerC Metadata: B

更新一个更好的答案

This问题向我指出了一个更简单的解决方案。

基本上你使用 Transform modifiers%(Identity)执行连接。

您可以使用以下内容替换上面的所有 3 个目标以获得相同的输出。
<Target Name="JoinServers">
<ItemGroup>
<Joined Include="%(Identity)">
<MetaDataA>@(ServerTypeA->'%(MetaDataA)')</MetaDataA>
<MetaDataB>@(ServerTypeB->'%(MetaDataB)')</MetaDataB>
</Joined>
</ItemGroup>

<Message Text="%(Joined.Identity) Metadata: %(Joined.MetaDataA)%(Joined.MetaDataB)"/>
</Target>

关于collections - 加入的项目组上的 MSBuild 元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4721661/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com