Search code examples
batch-filemsbuildmsbuild-itemgroup

msbuild itemgroup items


I have msbuild file which is executing batch file.

Msbuild file:

<PropertyGroup>
    <ItemAString>Green;Red;Blue</ItemAString>
    <ItemBString>Uno;Due;Tre</ItemBString>
    <ItemCString>Song;Movie;Picture</ItemCString>
</PropertyGroup>

<ItemGroup>
    <ItemsA Include="$(ItemAString.Split(';'))" />
    <ItemsB Include="$(ItemBString.Split(';'))" />
    <ItemsC Include="$(ItemCString.Split(';'))" />
</ItemGroup>

<Target Name = "CallBatch">                     
    <!-- THIS DOES NOT WORK  -->
    <Exec Command="mybatch.bat %(ItemsA.Identity) %(ItemsB.Identity) %(ItemsC.Identity)" />             
</Target>       

Batch file is very simple:

echo Params = [%1] - [%2] - [%3]

I want to get next output:

Params = Green - Uno - Song
Params = Red - Due - Movie
Params = Blue - Movie - Picture

How to achieve this?


Solution

  • I found solution:

    <Project DefaultTarget="DoTheMagic" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="12.0">
    <PropertyGroup Condition=" '$(TFP)'=='' ">
        <TFP>$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll</TFP>
        <TFP Condition=" !Exists('$(TFP)')">$(MSBuildFrameworkToolsPath)\Microsoft.Build.Tasks.v4.0.dll</TFP>
        <TFP Condition=" !Exists('$(TFP)')">$(windir)\Microsoft.NET\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll</TFP>
    </PropertyGroup>
    
    <UsingTask
        TaskName="Bukake"
        TaskFactory="CodeTaskFactory"       
        AssemblyFile="$(TFP)" >
        <ParameterGroup>
            <ItemsA Required="True" ParameterType="System.String"/>
            <ItemsB Required="True" ParameterType="System.String"/>
            <ItemsC Required="True" ParameterType="System.String"/>
            <Result ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="True"/>
        </ParameterGroup>
        <Task>                      
            <Code Type="Fragment" Language="cs">
                <![CDATA[
            string[] itemsA = ItemsA.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
            string[] itemsB = ItemsB.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
            string[] itemsC = ItemsC.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    
            List<TaskItem> items = new List<TaskItem>();
            for (int index = 0; index < itemsA.Length; index++)
            {                            
                TaskItem item = new TaskItem();
                item.ItemSpec = "item";
                item.SetMetadata("itemA", itemsA[index]);
                item.SetMetadata("itemB", itemsB[index]);
                item.SetMetadata("itemC", itemsC[index]);
                items.Add(item);
            }
            Result = items.ToArray();                         
            ]]>
            </Code>
        </Task>
    </UsingTask>
    
    <PropertyGroup>
        <ItemAString>Green;Red;Blue</ItemAString>
        <ItemBString>Uno;Due;Tre</ItemBString>
        <ItemCString>Song;Movie;Picture</ItemCString>       
    </PropertyGroup>
    
    <Target Name = "CallBatch">    
        <Message Text="$(TFS)" />   
        <Bukake ItemsA="$(ItemAString)" 
                ItemsB="$(ItemBString)"
                ItemsC="$(ItemCString)">
            <Output TaskParameter="Result" ItemName="Dundonja" />
        </Bukake>
    
        <ItemGroup>                 
            <PreparedItems Include="@(Dundonja)"/>              
        </ItemGroup>
        <!-- <Message Text="Dundonja: %(Dundonja.Identity) %(Dundonja.itemA) %(Dundonja.itemB) %(Dundonja.itemC)"/>          -->
        <Exec Command="mybatch.bat Dundonja %(Dundonja.Identity) %(Dundonja.itemA) %(Dundonja.itemB) %(Dundonja.itemC)"/>
    </Target>