Sorting Targets for Execution

Sample MSBuild target dependencies

<target Name=”ReBuild” DependsUponTargets=”Clean;Build”/>

<target Name=”Build” DependsUponTargets=”BeforeBuild;CoreBuild;AfterBuild”/>

<target Name=”CoreBuild” DependsUponTargets=”Compile”/>

<target Name=”Compile” DependsUponTargets=””/>

<target Name=”BeforeBuild” DependsUponTargets=””/>

<target Name=”AfterBuild” DependsUponTargets=””/>

<target Name=”Clean” DependsUponTargets=””/>

 

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        public struct Target

        {

            public String Name;

            public List<Target> DependsOn;

        }

 

        static void Main(string[] args)

        {

            //<target Name=”ReBuild” DependsUponTargets=”Clean;Build”/>

            //<target Name=”Build” DependsUponTargets=”BeforeBuild;CoreBuild;AfterBuild”/>

            //<target Name=”CoreBuild” DependsUponTargets=”Compile”/>

            //<target Name=”Compile” DependsUponTargets=””/>

            //<target Name=”BeforeBuild” DependsUponTargets=””/>

            //<target Name=”AfterBuild” DependsUponTargets=””/>

            //<target Name=”Clean” DependsUponTargets=””/>

 

            Target[] tg = new Target[7];

            tg[0].Name = "ReBuild";

            tg[0].DependsOn = new List<Target>();

            tg[1].Name = "Build";

            tg[1].DependsOn = new List<Target>();

            tg[2].Name = "CoreBuild";

            tg[2].DependsOn = new List<Target>();

            tg[3].Name = "Compile";

            tg[3].DependsOn = new List<Target>();

            tg[4].Name = "BeforeBuild";

            tg[4].DependsOn = new List<Target>();

            tg[5].Name = "AfterBuild";

            tg[5].DependsOn = new List<Target>();

            tg[6].Name = "Clean";

            tg[6].DependsOn = new List<Target>();

 

            tg[0].DependsOn.Add(tg[6]);

            tg[0].DependsOn.Add(tg[1]);

            tg[1].DependsOn.Add(tg[4]);

            tg[1].DependsOn.Add(tg[2]);

            tg[1].DependsOn.Add(tg[5]);

            tg[2].DependsOn.Add(tg[3]);

            SortTargets(tg);

        }

        public static void SortTargets(Target[] tg)

        {

            string sortedTargets = "";

            Hashtable ht = new Hashtable();

            Stack aStack = new Stack();

            for (int i = 0; i < tg.Length; i++)

            {

                if (!ht.Contains(i))

                {

                    ht.Add(i, null);

                    int index = sortedTargets.Length;

                    sortedTargets = sortedTargets + tg[i].Name + ";";

                    for (int j = 0; j < tg[i].DependsOn.Count; j++)

                    {

                        aStack.Push(tg[i].DependsOn[j].Name);

                    }

                    if (aStack.Count > 0)

                    {

                        string dependStr = "";

                        while (aStack.Count != 0)

                        {

                            string tempStr = (string)aStack.Pop();

                            dependStr = tempStr + ";" + dependStr;

                            ht.Add(GetTargetRow(tg, tempStr), null);

                            if (tg[GetTargetRow(tg, tempStr)].DependsOn.Count != 0)

                            {

                                for (int k = 0; k < tg[GetTargetRow(tg, tempStr)].DependsOn.Count; k++)

                                {

                                    aStack.Push(tg[GetTargetRow(tg, tempStr)].DependsOn[k].Name);

                                }

                            }

                        }

                        sortedTargets = sortedTargets.Insert(index, dependStr);

                    }

                }

            }

            Console.WriteLine(sortedTargets);

        }

        public static int GetTargetRow(Target[] tg, string name)

        {

            for (int i = 0; i < tg.Length; i++)

            {

                if (tg[i].Name == name)

                {

                    return (i);

                }

            }

            return (-1);

        }

    }

}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

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