Example #1
0
        /// <summary>
        /// Is the project description valid?
        /// </summary>
        /// <returns></returns>
        public bool IsValid()
        {
            bool isValid = !string.IsNullOrEmpty(Identifier) &&
                           ProjectRelativeFolder != null &&
                           (ConfigurationProperties != null || MatchesForProjectType != null) &&
                           (ConfigurationProperties == null || !ConfigurationProperties.Any(c => !c.IsValid())) &&
                           (MatchesForProjectType == null || !MatchesForProjectType.Any(m => !m.IsValid()));

            return(isValid);
        }
Example #2
0
        private static void ProcessFile(
            ProjectAuthenticationSettings projectAuthenticationSettings,
            string?filePath,
            ConfigurationProperties file)
        {
            if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
            {
                string      fileContent = File.ReadAllText(filePath);
                JsonElement jsonContent = default;
                XmlDocument?xmlDocument = null;

                if (filePath.EndsWith(".json"))
                {
                    jsonContent = JsonSerializer.Deserialize <JsonElement>(fileContent,
                                                                           s_serializerOptionsWithComments);
                }
                else if (filePath.EndsWith(".csproj"))
                {
                    xmlDocument = new XmlDocument();
                    xmlDocument.Load(filePath);
                }

                foreach (PropertyMapping propertyMapping in file.Properties)
                {
                    bool   found    = false;
                    string?property = propertyMapping.Property;
                    if (property != null)
                    {
                        string[] path = property.Split(':');

                        if (filePath.EndsWith(".json"))
                        {
                            IEnumerable <KeyValuePair <JsonElement, int> > elements = FindMatchingElements(jsonContent, path, 0);
                            foreach (var pair in elements)
                            {
                                JsonElement element = pair.Key;
                                int         index   = pair.Value;
                                found = true;
                                string replaceFrom = element.ValueKind == JsonValueKind.Number
                                    ? element.GetInt32().ToString(CultureInfo.InvariantCulture)
                                    : element.ToString() !;

                                UpdatePropertyRepresents(
                                    projectAuthenticationSettings,
                                    filePath,
                                    propertyMapping,
                                    index,
                                    replaceFrom);
                            }
                        }

                        else if (xmlDocument != null)
                        {
                            XmlNode?node = FindMatchingElement(xmlDocument, path);
                            if (node != null)
                            {
                                UpdatePropertyRepresents(
                                    projectAuthenticationSettings,
                                    filePath,
                                    propertyMapping,
                                    0,
                                    node.InnerText);
                            }
                        }
                        else
                        {
                            int index = fileContent.IndexOf(property);
                            if (index != -1)
                            {
                                UpdatePropertyRepresents(
                                    projectAuthenticationSettings,
                                    filePath,
                                    propertyMapping,
                                    0,
                                    property);
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(propertyMapping.Sets) && (found ||
                                                                        (propertyMapping.MatchAny != null && propertyMapping.MatchAny.Any(m => fileContent.Contains(m)))))
                    {
                        projectAuthenticationSettings.ApplicationParameters.Sets(propertyMapping.Sets);
                    }
                }
                // TODO: else AddNotFound?
            }
        }