Share via


MSBuild Batching - Generate a Cross-Product (continued)

As promised, here is the code for the ItemGroupCrossProduct task presented in a previous post:

 using System;
using System.Collections.Generic;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace CustomTasks
{
    public class ItemGroupCrossProduct : Task
    {
        [Required]
        public ITaskItem[] ItemGroup1
        {
            get
            {
                return m_itemGroup1;
            }
            set
            {
                m_itemGroup1 = value;
            }
        }

        [Required]
        public string MetaData1
        {
            get
            {
                return m_metaData1;
            }
            set
            {
                m_metaData1 = value;
            }
        }

        [Required]
        public ITaskItem[] ItemGroup2
        {
            get
            {
                return m_itemGroup2;
            }
            set
            {
                m_itemGroup2 = value;
            }
        }

        [Required]
        public string MetaData2
        {
            get
            {
                return m_metaData2;
            }
            set
            {
                m_metaData2 = value;
            }
        }

        [Output]
        public ITaskItem[] CombinedItemGroup
        {
            get
            {
                return m_combinedItemGroup;
            }
        }

        /// <summary>
        /// Task implementation - Execute method.
        /// </summary>
        /// <returns>
        /// True if the task succeeded, false otherwise.
        /// </returns>
        public override bool Execute()
        {
            List<TaskItem> newItems = new List<TaskItem>();

            foreach (ITaskItem item1 in ItemGroup1)
            {
                foreach (ITaskItem item2 in ItemGroup2)
                {
                    TaskItem newItem = new TaskItem(item2.ItemSpec);
                    newItem.SetMetadata(MetaData1, item1.GetMetadata(MetaData1));
                    newItem.SetMetadata(MetaData2, item2.GetMetadata(MetaData2));
                    newItems.Add(newItem);
                }
            }

            m_combinedItemGroup = newItems.ToArray();

            return true;
        }

        private ITaskItem[] m_itemGroup1;
        private string m_metaData1;
        private ITaskItem[] m_itemGroup2;
        private string m_metaData2;
        private ITaskItem[] m_combinedItemGroup;
    }
}

The idea of the task is to take in two collections of Items and two corresponding metadata property names and produce a new Item collection which contains the cross product of the two inputs.  This is a cross product in a rather limited sense - the resulting Items will contain only the two metadata properties specified in the input properties.  Any other metadata properties in either of the input Item collections will be ignored.  Additionally, the Identity values of the resulting Items are determined by the ItemGroup2 Items.

Comments

  • Anonymous
    September 08, 2006
    Aaron Hallberg on Visual Studio Team System Chat and MSBuild Batching - Generate a Cross-Product (continued)....