Пример #1
0
        /// <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.PropertySeperator}$({propertyName}){ConfigurationFactory.PropertySeperator}";

            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.PropertySeperator + value.Value + ConfigurationFactory.PropertySeperator);
                    var whenPropertiesElement = project.CreateWhenElement(propertiesCondition);
                    choosePropertiesElement.AppendChild(whenPropertiesElement);

                    AddProperties(whenPropertiesElement, value, includeAdditionalProperites, parsedValuePrefix);
                }

                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})'.");
                }
            }
        }