SolutionSection GenerateProjectConfigurationSection(ISolution solution)
        {
            SolutionSection section = new SolutionSection("ProjectConfigurationPlatforms", "postSolution");

            foreach (var project in solution.AllItems.OfType <IProject>())
            {
                foreach (var configuration in solution.ConfigurationNames)
                {
                    foreach (var platform in solution.PlatformNames)
                    {
                        var    solutionConfig = new ConfigurationAndPlatform(configuration, platform);
                        var    projectConfig  = project.ConfigurationMapping.GetProjectConfiguration(solutionConfig);
                        string key            = GuidToString(project.IdGuid) + "." + solutionConfig;
                        string value          = projectConfig.Configuration + "|" + MSBuildInternals.FixPlatformNameForSolution(projectConfig.Platform);
                        section.Add(key + ".ActiveCfg", value);
                        if (project.ConfigurationMapping.IsBuildEnabled(solutionConfig))
                        {
                            section.Add(key + ".Build.0", value);
                        }
                        if (project.ConfigurationMapping.IsDeployEnabled(solutionConfig))
                        {
                            section.Add(key + ".Deploy.0", value);
                        }
                    }
                }
            }
            return(section);
        }
 public string ValidateName(string name)
 {
     if (name == null)
     {
         return(null);
     }
     name = name.Trim();
     if (!ConfigurationAndPlatform.IsValidName(name))
     {
         return(null);
     }
     if (isPlatform)
     {
         return(MSBuildInternals.FixPlatformNameForSolution(name));
     }
     else
     {
         return(name);
     }
 }
        void IConfigurationOrPlatformNameCollection.Add(string newName, string copyFrom)
        {
            SD.MainThread.VerifyAccess();
            newName = ValidateName(newName);
            if (newName == null)
            {
                throw new ArgumentException();
            }
            lock (project.SyncRoot) {
                var  projectFile           = project.MSBuildProjectFile;
                var  userProjectFile       = project.MSBuildUserProjectFile;
                bool copiedGroupInMainFile = false;
                if (copyFrom != null)
                {
                    copyFrom = MSBuildInternals.FixPlatformNameForProject(copyFrom);
                    foreach (ProjectPropertyGroupElement g in projectFile.PropertyGroups.ToList())
                    {
                        var gConfig = ConfigurationAndPlatform.FromCondition(g.Condition);
                        if (HasName(gConfig, copyFrom))
                        {
                            CopyProperties(projectFile, g, SetName(gConfig, newName));
                            copiedGroupInMainFile = true;
                        }
                    }
                    foreach (ProjectPropertyGroupElement g in userProjectFile.PropertyGroups.ToList())
                    {
                        var gConfig = ConfigurationAndPlatform.FromCondition(g.Condition);
                        if (HasName(gConfig, copyFrom))
                        {
                            CopyProperties(userProjectFile, g, SetName(gConfig, newName));
                        }
                    }
                }
                if (!copiedGroupInMainFile)
                {
                    projectFile.AddPropertyGroup().Condition = (isPlatform ? new ConfigurationAndPlatform(null, newName) : new ConfigurationAndPlatform(newName, null)).ToCondition();
                }
                project.LoadConfigurationPlatformNamesFromMSBuild();

                // Adjust mapping:
                // If the new config/platform already exists in the solution and is mapped to some old project config/platform,
                // re-map it to the new config/platform.
                var mapping = project.ConfigurationMapping;
                if (isPlatform)
                {
                    string newNameForSolution = MSBuildInternals.FixPlatformNameForSolution(newName);
                    if (project.ParentSolution.PlatformNames.Contains(newNameForSolution, ConfigurationAndPlatform.ConfigurationNameComparer))
                    {
                        foreach (string solutionConfiguration in project.ParentSolution.ConfigurationNames)
                        {
                            var solutionConfig = new ConfigurationAndPlatform(solutionConfiguration, newNameForSolution);
                            var projectConfig  = mapping.GetProjectConfiguration(solutionConfig);
                            mapping.SetProjectConfiguration(solutionConfig, SetName(projectConfig, newName));
                        }
                    }
                }
                else
                {
                    if (project.ParentSolution.ConfigurationNames.Contains(newName, ConfigurationAndPlatform.ConfigurationNameComparer))
                    {
                        foreach (string solutionPlatform in project.ParentSolution.PlatformNames)
                        {
                            var solutionConfig = new ConfigurationAndPlatform(newName, solutionPlatform);
                            var projectConfig  = mapping.GetProjectConfiguration(solutionConfig);
                            mapping.SetProjectConfiguration(solutionConfig, SetName(projectConfig, newName));
                        }
                    }
                }
                project.ActiveConfiguration = mapping.GetProjectConfiguration(project.ParentSolution.ActiveConfiguration);
            }
        }