public override bool Execute() { LoadConfiguration(); var supportedProjectConfigurations = new HashSet <Configuration>( SupportedConfigurations.Where(c => !string.IsNullOrWhiteSpace(c)).Select(c => ConfigurationFactory.ParseConfiguration(c)), Configuration.CompatibleComparer); var bestConfigurations = new List <ITaskItem>(); foreach (var configurationItem in Configurations) { var buildConfiguration = ConfigurationFactory.ParseConfiguration(configurationItem.ItemSpec); var compatibleConfigurations = ConfigurationFactory.GetCompatibleConfigurations(buildConfiguration, DoNotAllowCompatibleValues); var bestConfiguration = compatibleConfigurations.FirstOrDefault(c => supportedProjectConfigurations.Contains(c)); if (bestConfiguration == null) { Log.LogMessage(MessageImportance.Low, $"Could not find any applicable configuration for '{buildConfiguration}' among projectConfigurations {string.Join(", ", supportedProjectConfigurations.Select(c => c.ToString()))}"); Log.LogMessage(MessageImportance.Low, $"Compatible configurations: {string.Join(", ", compatibleConfigurations.Select(c => c.ToString()))}"); } else { Log.LogMessage(MessageImportance.Low, $"Chose configuration {bestConfiguration}"); var bestConfigurationItem = new TaskItem(bestConfiguration.ToString(), (IDictionary)bestConfiguration.GetProperties()); // preserve metadata on the configuration that selected this configurationItem.CopyMetadataTo(bestConfigurationItem); // preserve the configuration that selected this bestConfigurationItem.SetMetadata("BuildConfiguration", configurationItem.ItemSpec); foreach (var additionalProperty in buildConfiguration.GetProperties()) { bestConfigurationItem.SetMetadata("BuildConfiguration_" + additionalProperty.Key, additionalProperty.Value); } bestConfigurations.Add(bestConfigurationItem); } } BestConfigurations = bestConfigurations.ToArray(); return(!Log.HasLoggedErrors); }
/// <summary> /// Generates choose/when statements to parse values from configuration string /// </summary> /// <param name="propertyName">name of property to parse</param> /// <param name="project">project to update</param> private void ParseProperties(string propertyName, ProjectRootElement project, bool includeAdditionalProperites, Func <PropertyInfo, bool> configurationSelector, string parsedValuePrefix = null) { var parseConfigurationPropertyGroup = project.LastChild as ProjectPropertyGroupElement; if (parseConfigurationPropertyGroup == null || !string.IsNullOrEmpty(project.LastChild.Condition)) { parseConfigurationPropertyGroup = project.CreatePropertyGroupElement(); project.AppendChild(parseConfigurationPropertyGroup); } // delimit property for parsing, this gaurntees that every property value is surrounded in delimiters var parsePropertyName = $"_parse_{propertyName}"; var parseConfigurationValue = $"{ConfigurationFactory.PropertySeparator}$({propertyName}){ConfigurationFactory.PropertySeparator}"; parseConfigurationPropertyGroup.AddProperty(parsePropertyName, parseConfigurationValue); // foreach property, pull it out of Configuration and set derived values. foreach (var property in ConfigurationFactory.GetProperties().Where(configurationSelector)) { var choosePropertiesElement = project.CreateChooseElement(); project.AppendChild(choosePropertiesElement); foreach (var value in ConfigurationFactory.GetValues(property)) { var propertiesCondition = CreateContainsCondition(parsePropertyName, ConfigurationFactory.PropertySeparator + value.Value + ConfigurationFactory.PropertySeparator); var whenPropertiesElement = project.CreateWhenElement(propertiesCondition); choosePropertiesElement.AppendChild(whenPropertiesElement); AddProperties(whenPropertiesElement, value, includeAdditionalProperites, parsedValuePrefix); } if (property.Insignificant) { // for insignificant properties, don't overwrite with default if already set, but derive values from it. foreach (var value in ConfigurationFactory.GetValues(property)) { var propertiesCondition = $"'$({property.Name})' == '{value.Value}'"; var whenPropertiesElement = project.CreateWhenElement(propertiesCondition); choosePropertiesElement.AppendChild(whenPropertiesElement); // only write additionalProperties since actual property is already set,. AddProperties(whenPropertiesElement, value, includeAdditionalProperites, parsedValuePrefix, includePropertyValue: false); } } var otherwisePropertiesElement = project.CreateOtherwiseElement(); choosePropertiesElement.AppendChild(otherwisePropertiesElement); if (property.DefaultValue != null) { AddProperties(otherwisePropertiesElement, property.DefaultValue, includeAdditionalProperites, parsedValuePrefix); } else { var otherwiseErrorPropertyGroup = project.CreatePropertyGroupElement(); otherwisePropertiesElement.AppendChild(otherwiseErrorPropertyGroup); otherwiseErrorPropertyGroup.AddProperty(ErrorMessageProperty, $"$({ErrorMessageProperty})Could not find a value for {property.Name} from {propertyName} '$({propertyName})'."); } } }
public override bool Execute() { LoadConfiguration(); Dictionary <Configuration, Configuration> supportedProjectConfigurations = SupportedConfigurations.Where(c => !string.IsNullOrWhiteSpace(c)) .Distinct(StringComparer.OrdinalIgnoreCase) .Select(c => ConfigurationFactory.ParseConfiguration(c)) .ToDictionary(c => c, Configuration.CompatibleComparer); var bestConfigurations = new List <ITaskItem>(); foreach (var configurationItem in Configurations) { var buildConfiguration = ConfigurationFactory.ParseConfiguration(configurationItem.ItemSpec); var compatibleConfigurations = ConfigurationFactory.GetCompatibleConfigurations(buildConfiguration, DoNotAllowCompatibleValues); Configuration supportedProjectConfiguration = null; var bestConfiguration = compatibleConfigurations.FirstOrDefault(c => supportedProjectConfigurations.TryGetValue(c, out supportedProjectConfiguration)); if (bestConfiguration == null) { Log.LogMessage(MessageImportance.Low, $"Could not find any applicable configuration for '{buildConfiguration}' among projectConfigurations {string.Join(", ", supportedProjectConfigurations.Select(c => c.ToString()))}"); Log.LogMessage(MessageImportance.Low, $"Compatible configurations: {string.Join(", ", compatibleConfigurations.Select(c => c.ToString()))}"); } else { if (supportedProjectConfiguration.IsPlaceHolderConfiguration) { Log.LogMessage($"Skipped configuration: {supportedProjectConfiguration.ToString()} because was marked as a placeholder configuration"); continue; } Log.LogMessage(MessageImportance.Low, $"Chose configuration {bestConfiguration}"); var bestConfigurationItem = new TaskItem(bestConfiguration.ToString(), (IDictionary)bestConfiguration.GetProperties()); // preserve metadata on the configuration that selected this configurationItem.CopyMetadataTo(bestConfigurationItem); // preserve the configuration that selected this bestConfigurationItem.SetMetadata("BuildConfiguration", configurationItem.ItemSpec); foreach (var additionalProperty in buildConfiguration.GetProperties()) { bestConfigurationItem.SetMetadata("BuildConfiguration_" + additionalProperty.Key, additionalProperty.Value); } bestConfigurations.Add(bestConfigurationItem); } } BestConfigurations = bestConfigurations.ToArray(); return(!Log.HasLoggedErrors); }
public override bool Execute() { LoadConfiguration(); var supportedProjectConfigurations = new HashSet <Configuration>( BuildConfigurations.Where(c => !string.IsNullOrWhiteSpace(c)).Select(c => ConfigurationFactory.ParseConfiguration(c)), Configuration.CompatibleComparer); var buildConfiguration = ConfigurationFactory.ParseConfiguration(BuildConfiguration); var compatibleConfigurations = ConfigurationFactory.GetCompatibleConfigurations(buildConfiguration, DoNotAllowCompatibleValues); var bestConfiguration = compatibleConfigurations.FirstOrDefault(c => supportedProjectConfigurations.Contains(c)); if (bestConfiguration == null) { Log.LogMessage($"Could not find any applicable configuration for '{buildConfiguration}' among projectConfigurations {string.Join(", ", supportedProjectConfigurations.Select(c => c.ToString()))}"); Log.LogMessage(MessageImportance.Low, $"Compatible configurations: {string.Join(", ", compatibleConfigurations.Select(c => c.ToString()))}"); } else { Log.LogMessage(MessageImportance.Low, $"Chose configuration {bestConfiguration}"); BestConfiguration = new TaskItem(bestConfiguration.ToString(), (IDictionary)bestConfiguration.GetProperties()); } return(!Log.HasLoggedErrors); }