protected virtual int AddConfiguration(MSBuild.Project project, string configurationName)
{
if (project == null)
throw new ArgumentNullException("project");
if (configurationName == null)
throw new ArgumentNullException("configurationName");
if (string.IsNullOrEmpty(configurationName))
throw new ArgumentException("configurationName cannot be null or empty");
MSBuildConstruction.ProjectElement lastRelevantElement = null;
foreach (ProjectPropertyGroupElement group in project.Xml.PropertyGroupsReversed)
{
if (string.IsNullOrEmpty(group.Condition))
continue;
if (group.Condition.IndexOf("'$(Configuration)'", StringComparison.OrdinalIgnoreCase) >= 0
|| group.Condition.IndexOf("'$(Platform)'", StringComparison.OrdinalIgnoreCase) >= 0
|| group.Condition.IndexOf("'$(Configuration)|$(Platform)'", StringComparison.OrdinalIgnoreCase) >= 0)
{
lastRelevantElement = group;
break;
}
}
if (lastRelevantElement == null)
lastRelevantElement = project.Xml.PropertyGroupsReversed.First();
string[] platformNames = GetPlatformsFromProject();
foreach (var platformName in platformNames)
{
ProjectPropertyGroupElement element = project.Xml.CreatePropertyGroupElement();
project.Xml.InsertAfterChild(element, lastRelevantElement);
lastRelevantElement = element;
element.Condition = GetConfigurationPlatformCondition(configurationName, platformName);
/* TODO:
* 1. No need to create a property group if there are no properties to insert into in *and* this is a user build project.
* If this is the main project, we still need to insert the group because the conditions are used to determine which
* configurations/platforms are included in the project. For now, it's fine to always add the group.
* 2. Need a way to specify NewConfigProperties for each type of build project (main, user, etc).
*/
if (project == this._project.BuildProject)
{
// Get the list of property name, condition value from the config provider
IEnumerable<KeyValuePair<KeyValuePair<string, string>, string>> propVals = this.NewConfigProperties;
foreach (KeyValuePair<KeyValuePair<string, string>, string> data in propVals)
{
KeyValuePair<string, string> propData = data.Key;
string value = data.Value;
ProjectPropertyElement newProperty = element.AddProperty(propData.Key, value);
if (!string.IsNullOrEmpty(propData.Value))
newProperty.Condition = propData.Value;
}
//add the output path
element.AddProperty("OutputPath", GetDefaultOutputPath(configurationName, platformName));
}
}
return VSConstants.S_OK;
}