[MSBuild] How to implement nested loops

Let’s say we have nested loops through two list aa={1; 22; 333} and bb={a; bb; ccc} like:

foreach (i in aa) {
  foreach (j in bb) {
    print "i=? j=??";
  }
}

The solution is quite complicated and as below.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="MAIN" >
  <!--<Import Project="$(MSBuildExtensionsPath)\MSBuildContrib\MSBuildContrib.Tasks"/>-->
  <!--<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks"/>-->

  <Target Name="MAIN" >
    <CallTarget Targets="t" />
    <CallTarget Targets="tt" />
  </Target>

  <Target Name="t">
    <ItemGroup>
      <aa Include="1;22;333" />
    </ItemGroup>
  </Target>

  <Target Name="tt" Outputs="%(aa.Identity)">
    <Message Text="aa %(aa.Identity)" />

    <MSBuild Projects="$(MSBuildProjectFile)" Targets="ttt" Properties="aa=%(aa.Identity)" />
  </Target>

  <Target Name="ttt">
    <CallTarget Targets="ttt_1" />
    <CallTarget Targets="tttt" />
  </Target>

  <Target Name="ttt_1">
    <ItemGroup>
      <bb Include="a;bb;ccc" />
    </ItemGroup>
  </Target>

  <Target Name="tttt" Outputs="%(bb.Identity)">
    <Message Text="aa $(aa)" />
    <Message Text="bb %(bb.Identity)" />
  </Target>

</Project>

And for an even more complex nested loops (3 nested), see sample script below.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="MAIN" >
  <Target Name="MAIN">
    <CallTarget Targets="t"/>
    <CallTarget Targets="tt"/>
  </Target>

  <Target Name="t" Outputs="%(aaList.Identity)">
    <ItemGroup>
      <aaList Include="1; 22; 333;" />
    </ItemGroup>
    <Message Text="Foreach aa in aaList:" />
  </Target>

  <Target Name="tt">
    <MSBuild Projects="$(MSBuildProjectFile)"
             Targets="ttt"
             Properties="aa=%(aaList.Identity)"
    />
  </Target>

  <Target Name="ttt">
    <CallTarget Targets="tttt" />
    <CallTarget Targets="ttttt" />
  </Target>

  <Target Name="tttt">
    <ItemGroup>
      <bbList Include="a; bb; ccc;" />
    </ItemGroup>
  </Target>

  <Target Name="ttttt" Outputs="%(bbList.Identity)">
    <Message Text="aa:$(aa)" />
    <MSBuild Projects="$(MSBuildProjectFile)"
             Targets="t2"
             Properties="bb=%(bbList.Identity); aa=$(aa)"
    />
  </Target>
  <Target Name="t2">
    <CallTarget Targets="tt2" />
    <CallTarget Targets="ttt2" />
  </Target>

  <Target Name="tt2">
    <ItemGroup>
      <ccList Include="x; yy; zzz;" />
    </ItemGroup>
  </Target>
  <Target Name="ttt2" Outputs="%(ccList.Identity)">
    <Message Text="aa:$(aa) bb:$(bb)" />
    <Message Text="cc:%(ccList.Identity)" />
  </Target>

</Project>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: