/// <summary> /// Generates a set of props files which can statically determine the best $(Configuration) for /// a given $(BuildConfiguration) from a set of configurations $(BuildConfigurations). /// Props files also set properties based on $(Configuration) like TargetGroup, OSGroup, /// NuGetTargetMoniker, etc. /// </summary> /// <returns></returns> public override bool Execute() { LoadConfiguration(); var project = ProjectRootElement.Create(); var buildConfigurationPropsFilePath = $"{ConfigurationPropsPrefix}{PropsFileExtension}"; var buildConfigurationImport = project.AddImport($"{CurrentDirectoryIdentifier}{buildConfigurationPropsFilePath}"); CreateBuildConfigurationPropsFile(buildConfigurationPropsFilePath); // Parse the properties that are part of Configuration ParseProperties(ConfigurationProperty, project, true, p => !p.Independent); // Parse the independent properties that aren't part of Configuration ParseProperties(BuildConfigurationProperty, project, true, p => p.Independent); CreateRuntimeIdentifier(project); var projectPath = Path.Combine(PropsFolder, $"{PropsFileName}{PropsFileExtension}"); project.TreatAsLocalProperty = string.Join <string>(";", ConfigurationFactory.GetProperties().Select(pi => pi.Name)); project.Save(projectPath); 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 guarantees 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})'."); } } }