MakeMSBuildCondition() public static method

public static MakeMSBuildCondition ( string configName, string platform ) : string
configName string
platform string
return string
        /// <summary>
        /// Copies an existing platform name or creates a new one.
        /// </summary>
        /// <param name="platformName">The name of the new platform.</param>
        /// <param name="clonePlatformName">The name of the platform to copy, or a null reference, indicating that AddCfgsOfPlatformName should create a new platform.</param>
        /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns an error code.</returns>
        public virtual int AddCfgsOfPlatformName(string platformName, string clonePlatformName)
        {
            var msbuildPlatform = ProjectConfig.ToMSBuildPlatform(platformName);

            clonePlatformName = ProjectConfig.ToMSBuildPlatform(clonePlatformName);

            if (!this.ProjectMgr.QueryEditProjectFile(false))
            {
                throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED);                 //0x8004000c
            }
            ProjectMgr.BuildProject.ReevaluateIfNecessary();
            var propertyGroups = new List <ProjectPropertyGroupElement>(this.project.BuildProject.Xml.PropertyGroups);
            var dictionary     = new Dictionary <string, ProjectPropertyGroupElement>(StringComparer.Ordinal);

            if (clonePlatformName != null)
            {
                foreach (ProjectPropertyGroupElement propertyGroup in propertyGroups)
                {
                    if (!string.IsNullOrEmpty(propertyGroup.Condition))
                    {
                        var cfgNameAndPlatform = ProjectConfig.ConfigAndPlatformOfCondition(propertyGroup.Condition);
                        var cfgNme             = ProjectConfig.GetConfigName(cfgNameAndPlatform);
                        if (ProjectConfig.EqPlatform(cfgNameAndPlatform.Item2, clonePlatformName) && !dictionary.ContainsKey(cfgNme))
                        {
                            dictionary.Add(cfgNme, propertyGroup);
                        }
                    }
                }
            }

            string[] propertiesConditionedOn = this.GetPropertiesConditionedOn("Configuration");

            if (propertiesConditionedOn.Length == 0)
            {
                return(VSConstants.E_FAIL);
            }

            foreach (string configName in propertiesConditionedOn)
            {
                if (dictionary.Count <= 0 || dictionary.ContainsKey(configName))
                {
                    ProjectPropertyGroupElement newConfig = null;
                    if (dictionary.ContainsKey(configName))
                    {
                        newConfig = this.project.ClonePropertyGroup(dictionary[configName]);

                        foreach (ProjectPropertyElement property in newConfig.Properties)
                        {
                            if (ProjectConfig.Eq(property.Name, "PlatformTarget") || ProjectConfig.Eq(property.Name, "Platform"))
                            {
                                property.Parent.RemoveChild(property);
                            }
                        }
                    }
                    else
                    {
                        this.PopulateEmptyConfig(ref newConfig);
                        this.AddOutputPath(newConfig, configName);
                    }
                    newConfig.AddProperty("PlatformTarget", msbuildPlatform);
                    newConfig.AddProperty("Platform", msbuildPlatform);
                    newConfig.Condition = ProjectConfig.MakeMSBuildCondition(configName, msbuildPlatform);
                }
            }

            NotifyOnPlatformNameAdded(platformName);

            return(VSConstants.S_OK);
        }
        /// <summary>
        /// Assigns a new name to a configuration.
        /// </summary>
        /// <param name="old">The old name of the target configuration.</param>
        /// <param name="newname">The new name of the target configuration.</param>
        /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns an error code.</returns>
        public virtual int RenameCfgsOfCfgName(string old, string newname)
        {
            this.project.BuildProject.ReevaluateIfNecessary();
            foreach (ProjectPropertyGroupElement current in this.project.BuildProject.Xml.PropertyGroups)
            {
                if (string.IsNullOrEmpty(current.Condition))
                {
                    continue;
                }

                var cfgNameAndPlatform = ProjectConfig.ConfigAndPlatformOfCondition(current.Condition);

                if (ProjectConfig.Eq(ProjectConfig.GetConfigName(cfgNameAndPlatform), old))
                {
                    //ConfigCanonicalName key2 = new ConfigCanonicalName(newname, key.Platform);
                    current.Condition = ProjectConfig.MakeMSBuildCondition(newname, cfgNameAndPlatform.Item2);

                    var outputPath = current.Properties.Where(p => p.Name == "OutputPath").FirstOrDefault();

                    if (outputPath != null && outputPath.Value != null)
                    {
                        string path = this.ProjectMgr.OutputBaseRelativePath;
                        if (path.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
                        {
                            path = Path.GetDirectoryName(path);
                        }

                        if (string.Equals(Path.Combine(path, old), outputPath.Value, StringComparison.OrdinalIgnoreCase))
                        {
                            current.SetProperty("OutputPath", Path.Combine(path, newname));
                        }
                    }

                    var oldKey = ProjectConfig.MakeConfigKey(cfgNameAndPlatform);

                    if (this.configurationsList.ContainsKey(oldKey))
                    {
                        ProjectConfig projectConfig = this.configurationsList[oldKey];
                        this.configurationsList.Remove(oldKey);
                        this.configurationsList.Add(ProjectConfig.MakeConfigKey(newname, cfgNameAndPlatform.Item2), projectConfig);
                        projectConfig.ConfigurationName = newname;
                    }
                }
            }

            this.NotifyOnCfgNameRenamed(old, newname);
            //// First create the condition that represent the configuration we want to rename
            //string condition = String.Format(CultureInfo.InvariantCulture, configString, old).Trim();

            //foreach (ProjectPropertyGroupElement config in this.project.BuildProject.Xml.PropertyGroups)
            //{
            //  // Only care about conditional property groups
            //  if (config.Condition == null || config.Condition.Length == 0)
            //    continue;

            //  // Skip if it isn't the group we want
            //  if (String.Compare(config.Condition.Trim(), condition, StringComparison.OrdinalIgnoreCase) != 0)
            //    continue;

            //  // Change the name
            //  config.Condition = String.Format(CultureInfo.InvariantCulture, configString, newname);
            //  // Update the name in our config list
            //  if (configurationsList.ContainsKey(old))
            //  {
            //    ProjectConfig configuration = configurationsList[old];
            //    configurationsList.Remove(old);
            //    configurationsList.Add(newname, configuration);
            //    // notify the configuration of its new name
            //    configuration.ConfigurationName = newname;
            //  }

            //  NotifyOnCfgNameRenamed(old, newname);
            //}

            return(VSConstants.S_OK);
        }
        /// <summary>
        /// Copies an existing configuration name or creates a new one.
        /// </summary>
        /// <param name="name">The name of the new configuration.</param>
        /// <param name="cloneName">the name of the configuration to copy, or a null reference, indicating that AddCfgsOfCfgName should create a new configuration.</param>
        /// <param name="fPrivate">Flag indicating whether or not the new configuration is private. If fPrivate is set to true, the configuration is private. If set to false, the configuration is public. This flag can be ignored.</param>
        /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns an error code. </returns>
        public virtual int AddCfgsOfCfgName(string name, string cloneName, int fPrivate)
        {
            // We need to QE/QS the project file
            if (!this.ProjectMgr.QueryEditProjectFile(false))
            {
                throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED);
            }

            ProjectMgr.BuildProject.ReevaluateIfNecessary();

            var list       = new List <ProjectPropertyGroupElement>(this.project.BuildProject.Xml.PropertyGroups);
            var dictionary = new Dictionary <string, ProjectPropertyGroupElement>(StringComparer.Ordinal);

            if (cloneName != null)
            {
                foreach (ProjectPropertyGroupElement element in list)
                {
                    if (!string.IsNullOrEmpty(element.Condition))
                    {
                        var cfgNameAndPlatform = ProjectConfig.ConfigAndPlatformOfCondition(element.Condition);
                        //ConfigCanonicalName name2 = ConfigCanonicalName.OfCondition(element.Condition);
                        var platformName = ProjectConfig.GetPlatformName(cfgNameAndPlatform);
                        if ((string.Compare(ProjectConfig.GetConfigName(cfgNameAndPlatform), cloneName, StringComparison.OrdinalIgnoreCase) == 0) && !dictionary.ContainsKey(platformName))
                        {
                            dictionary.Add(platformName, element);
                        }
                    }
                }
            }
            string[] platformsFromProject = this.GetPlatformsFromProject();
            if (platformsFromProject.Length == 0)
            {
                platformsFromProject = new [] { string.Empty }
            }
            ;

            foreach (string latform in platformsFromProject)
            {
                if (dictionary.Count <= 0 || dictionary.ContainsKey(latform))
                {
                    //ConfigCanonicalName name3 = new ConfigCanonicalName(name, latform);
                    ProjectPropertyGroupElement newConfig = null;
                    if (dictionary.ContainsKey(latform))
                    {
                        newConfig = this.project.ClonePropertyGroup(dictionary[latform]);
                        foreach (ProjectPropertyElement element3 in newConfig.Properties)
                        {
                            if (element3.Name.Equals("OutputPath", StringComparison.OrdinalIgnoreCase))
                            {
                                element3.Parent.RemoveChild(element3);
                            }
                        }
                    }
                    else
                    {
                        var msbuildPlatform = ProjectConfig.ToMSBuildPlatform(latform);
                        this.PopulateEmptyConfig(ref newConfig);
                        if (!string.IsNullOrEmpty(msbuildPlatform))
                        {
                            newConfig.AddProperty("PlatformTarget", msbuildPlatform);
                        }
                    }

                    this.AddOutputPath(newConfig, name);
                    newConfig.Condition = ProjectConfig.MakeMSBuildCondition(name, latform);
                }
            }

            NotifyOnCfgNameAdded(name);
            return(VSConstants.S_OK);
        }