PeteAC
PeteAC

Reputation: 799

MSBUILD operating on repeated Items in an ItemGroup

I have an ItemGroup that contains some Items that are duplicates of one another. I would like to execute a task on a modified version of all of the Items in the ItemGroup. But, so far, I have not been able to find a way that does not remove the duplicated Items.

Does such exist?

<ItemGroup>
  <MyMessage Include="foo"  />
  <MyMessage Include="bar"  />
  <MyMessage Include="baz"  />
  <MyMessage Include="foo"  />
  <MyMessage Include="baz"  />
  <MyMessage Include="baz"  />
</ItemGroup>

<Target Name="DemoBug">
  <Message Importance="High" Text="FIRST VERSION USING ItemGroup ..."/>
  <Message Importance="High" Text="@(MyMessage)"/>
  <Message Importance="High" Text="SECOND VERSION USING Batching and Metadata ..."/>
  <Message Importance="High" Text="someprefix;%(MyMessage.Identity);somesuffix"/>
</Target>

In my example above, the FIRST VERSION output has all the Items in it, but has only executed Message task once on the concatenated values of the Items. So this isn't what I want, at all.

5>  FIRST VERSION USING ItemGroup ...
5>  foo;bar;baz;foo;baz;baz

The SECOND VERSION is executing Message task several times, like I want, but it is stripping the duplicates, which I don't want it to do.

5>  SECOND VERSION USING Batching and Metadata ...
5>  someprefix;foo;somesuffix
5>  someprefix;bar;somesuffix
5>  someprefix;baz;somesuffix

Is what I want to do fundamentally impossible (in which case it's maybe time to switch to PowerShell for my task), or is there some reasonable way to do it?

(In my real task, the Items came from ReadLinesFromFile and, after various processing, will eventually end up in WriteLinesToFile. It works fine, except when duplicated lines are encountered)

Upvotes: 1

Views: 1196

Answers (1)

fsimonazzi
fsimonazzi

Reputation: 3013

Msbuild task batching is not looping. It does what it is supposed to do, grouping the items in batches that have the same metadata values. In your example it's grouping by %(Identity).

Would item transformation work in your case? Something like

<Message Importance="High" Text="@(MyMessage->'someprefix;%(Identity);somesuffix')"/>

Upvotes: 4

Related Questions