void InsertGroup(BuildProviderGroup group, List <BuildProviderGroup> groups)
        {
            if (group.Application)
            {
                groups.Insert(groups.Count - 1, group);
                return;
            }

            int index;

            if (group.Standalone)
            {
                index = groups.FindLastIndex(SkipApplicationGroup);
            }
            else
            {
                index = groups.FindLastIndex(SkipStandaloneGroups);
            }

            if (index == -1)
            {
                groups.Add(group);
            }
            else
            {
                groups.Insert(index == 0 ? 0 : index - 1, group);
            }
        }
        static bool SkipApplicationGroup(BuildProviderGroup group)
        {
            if (group == null)
            {
                return(false);
            }

            return(group.Application);
        }
        static bool SkipStandaloneGroups(BuildProviderGroup group)
        {
            if (group == null)
            {
                return(false);
            }

            return(group.Standalone);
        }
        List <BuildProviderGroup> GetSingleBuildProviderGroup(BuildProvider bp)
        {
            var ret   = new List <BuildProviderGroup> ();
            var group = new BuildProviderGroup();

            group.AddProvider(bp);
            ret.Add(group);

            return(ret);
        }
        void AssignToGroup(BuildProvider buildProvider, List <BuildProviderGroup> groups)
        {
            if (IsDependencyCycle(buildProvider))
            {
                throw new HttpException("Dependency cycles are not suppported: " + buildProvider.VirtualPath);
            }

            BuildProviderGroup myGroup       = null;
            string             bpVirtualPath = buildProvider.VirtualPath;
            string             bpPath        = VirtualPathUtility.GetDirectory(bpVirtualPath);
            bool canAdd;

            if (BuildManager.HasCachedItemNoLock(buildProvider.VirtualPath))
            {
                return;
            }

            if (buildProvider is ApplicationFileBuildProvider || buildProvider is ThemeDirectoryBuildProvider)
            {
                // global.asax and theme directory go into their own assemblies
                myGroup            = new BuildProviderGroup();
                myGroup.Standalone = true;
                InsertGroup(myGroup, groups);
            }
            else
            {
                Type bpCodeDomType = GetBuildProviderCodeDomType(buildProvider);
                foreach (BuildProviderGroup group in groups)
                {
                    if (group.Standalone)
                    {
                        continue;
                    }

                    if (group.Count == 0)
                    {
                        myGroup = group;
                        break;
                    }

                    canAdd = true;
                    foreach (BuildProvider bp in group)
                    {
                        if (IsDependency(buildProvider, bp))
                        {
                            canAdd = false;
                            break;
                        }

                        // There should be one assembly per virtual dir
                        if (String.Compare(bpPath, VirtualPathUtility.GetDirectory(bp.VirtualPath), stringComparer) != 0)
                        {
                            canAdd = false;
                            break;
                        }

                        // Different languages go to different assemblies
                        if (bpCodeDomType != null)
                        {
                            Type type = GetBuildProviderCodeDomType(bp);
                            if (type != null)
                            {
                                if (type != bpCodeDomType)
                                {
                                    canAdd = false;
                                    break;
                                }
                            }
                        }
                    }

                    if (!canAdd)
                    {
                        continue;
                    }

                    myGroup = group;
                    break;
                }

                if (myGroup == null)
                {
                    myGroup = new BuildProviderGroup();
                    InsertGroup(myGroup, groups);
                }
            }

            myGroup.AddProvider(buildProvider);
            if (String.Compare(bpPath, virtualPathDirectory, stringComparer) == 0)
            {
                myGroup.Master = true;
            }
        }