/// <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);
        }
Example #2
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})'.");
                }
            }
        }