Microsoft.VisualStudio.Project.ConfigProvider.ClonePlatform C# (CSharp) Method

ClonePlatform() protected method

protected ClonePlatform ( Microsoft.Build.Evaluation project, string existingPlatformName, string newPlatformName ) : int
project Microsoft.Build.Evaluation
existingPlatformName string
newPlatformName string
return int
        protected virtual int ClonePlatform(MSBuild.Project project, string existingPlatformName, string newPlatformName)
        {
            if (project == null)
                throw new ArgumentNullException("project");
            if (existingPlatformName == null)
                throw new ArgumentNullException("existingPlatformName");
            if (newPlatformName == null)
                throw new ArgumentNullException("newPlatformName");
            if (string.IsNullOrEmpty(existingPlatformName))
                throw new ArgumentException("existingPlatformName cannot be null or empty");
            if (string.IsNullOrEmpty(newPlatformName))
                throw new ArgumentException("newPlatformName cannot be null or empty");

            string[] configurationNames = GetPropertiesConditionedOn(ProjectFileConstants.Configuration);

            string existingPlatformCondition = GetPlatformCondition(existingPlatformName).Trim();
            string newPlatformCondition = GetPlatformCondition(existingPlatformName);

            string[] existingConfigurationPlatformConditions = new string[configurationNames.Length];
            string[] newConfigurationPlatformConditions = new string[configurationNames.Length];

            for (int i = 0; i < existingConfigurationPlatformConditions.Length; i++)
            {
                existingConfigurationPlatformConditions[i] = GetConfigurationPlatformCondition(configurationNames[i], existingPlatformName).Trim();
                newConfigurationPlatformConditions[i] = GetConfigurationPlatformCondition(configurationNames[i], newPlatformName);
            }

            foreach (var group in project.Xml.PropertyGroups.ToArray())
            {
                if (string.IsNullOrEmpty(group.Condition))
                    continue;

                if (string.Equals(group.Condition.Trim(), existingPlatformCondition, StringComparison.OrdinalIgnoreCase))
                {
                    ProjectPropertyGroupElement clonedGroup = ClonePropertyGroup(project, group);
                    clonedGroup.Condition = newPlatformCondition;
                }
                else
                {
                    int index = Array.FindIndex(existingConfigurationPlatformConditions, i => StringComparer.OrdinalIgnoreCase.Equals(i, group.Condition.Trim()));
                    if (index < 0)
                        continue;

                    ProjectPropertyGroupElement clonedGroup = ClonePropertyGroup(project, group);
                    clonedGroup.Condition = newConfigurationPlatformConditions[index];

                    // update the OutputPath property if it exists and isn't conditioned on $(Platform)
                    foreach (var property in clonedGroup.Properties)
                    {
                        if (!string.Equals(property.Name, "OutputPath", StringComparison.OrdinalIgnoreCase))
                            continue;

                        if (!string.IsNullOrEmpty(property.Condition))
                            continue;

                        if (property.Value.IndexOf("$(Platform)", StringComparison.OrdinalIgnoreCase) >= 0)
                            continue;

                        // update the output path
                        property.Value = GetDefaultOutputPath(configurationNames[index], newPlatformName);
                    }
                }
            }

            return VSConstants.S_OK;
        }