Exemplo n.º 1
0
        public override bool Execute()
        {
            MergedDictionary mergedDictionary = MergedDictionary.CreateMergedDicionary();
            List <string>    pages            = new List <string>();

            if (Pages != null)
            {
                foreach (ITaskItem pageItem in Pages)
                {
                    string page = pageItem.ItemSpec;
                    if (File.Exists(page))
                    {
                        pages.Add(page);
                    }
                    else
                    {
                        LogError($"Can't find page {page}!");
                    }
                }
            }

            if (HasLoggedErrors)
            {
                return(false);
            }

            LogMessage($"Merging XAML files into {MergedXamlFile}...");

            foreach (string page in pages)
            {
                try
                {
                    mergedDictionary.MergeContent(File.ReadAllText(page));
                }
                catch (Exception)
                {
                    LogError($"Exception found when merging page {page}!");
                    throw;
                }
            }

            mergedDictionary.FinalizeXaml();
            filesWritten.Add(Utils.RewriteFileIfNecessary(MergedXamlFile, mergedDictionary.ToString()));

            File.WriteAllLines(TlogReadFilesOutputPath, Pages.Select(page => page.ItemSpec));
            File.WriteAllLines(TlogWriteFilesOutputPath, FilesWritten);

            return(!HasLoggedErrors);
        }
Exemplo n.º 2
0
        private MergedDictionary(XmlDocument document, MergedDictionary parentDictionary)
        {
            owningDocument = document;
            xmlElement     = owningDocument.CreateElement("ResourceDictionary", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");

            if (parentDictionary == null)
            {
                xmlElement = owningDocument.AppendChild(xmlElement) as XmlElement;
                xmlElement.SetAttribute("xmlns:x", "http://schemas.microsoft.com/winfx/2006/xaml");
            }

            nodeList = new List <XmlNode>();
            nodeListNodesToIgnore                = new List <int>();
            nodeKeyToNodeListIndexDictionary     = new Dictionary <string, int>();
            mergedThemeDictionaryByKeyDictionary = new Dictionary <string, MergedDictionary>();
            namespaceList         = new List <string>();
            this.parentDictionary = parentDictionary;
        }
Exemplo n.º 3
0
        private MergedDictionary(XmlDocument document, MergedDictionary parentDictionary)
        {
            owningDocument = document;
            xmlElement     = owningDocument.CreateElement("ResourceDictionary", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");

            if (parentDictionary == null)
            {
                xmlElement = owningDocument.AppendChild(xmlElement) as XmlElement;
                xmlElement.SetAttribute("xmlns:x", "http://schemas.microsoft.com/winfx/2006/xaml");
            }

            nodeList = new List <XmlNode>();
            nodeListNodesToIgnore                = new List <int>();
            nodeKeyToNodeListIndexDictionary     = new Dictionary <string, int>();
            mergedThemeDictionaryByKeyDictionary = new Dictionary <string, MergedDictionary>();
            namespaceList         = new List <string>();
            this.parentDictionary = parentDictionary;

            AddNamespace("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            xmlElement.SetAttribute("Ignorable", "http://schemas.openxmlformats.org/markup-compatibility/2006", "ios android wasm skia");
        }
Exemplo n.º 4
0
        private void AddNode(XmlNode node, Dictionary <string, string> xmlnsReplacementDictionary)
        {
            // Remove Comment
            if (node is XmlComment)
            {
                return;
            }

            node = owningDocument.ImportNode(node, true);
            ReplaceNamespacePrefix(node, xmlnsReplacementDictionary);

            if (node.Name == "ResourceDictionary.ThemeDictionaries")
            {
                // This will be a list of either ResourceDictionaries or comments.
                // We'll figure out what the ResourceDictionaries' keys are,
                // then either add their contents to the existing theme dictionaries we have,
                // or create new ones if we've found new keys.
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    string nodeKey = GetKey(childNode);

                    if (nodeKey == null || nodeKey.Length == 0)
                    {
                        continue;
                    }
                    else if (nodeKey == "Dark")
                    {
                        // If we don't specify the dictionary "Dark", then "Default" will be used instead.
                        // Having both of those, however, will result in "Default" never being used, even
                        // if it contains something that "Dark" does not.
                        // Since we're merging everything into a single dictionary, we need to standardize
                        // to only one of the two. Since most dictionaries use "Default", we'll go with that.
                        nodeKey = "Default";
                    }

                    MergedDictionary mergedThemeDictionary = null;

                    if (mergedThemeDictionaryByKeyDictionary.TryGetValue(nodeKey, out mergedThemeDictionary) == false)
                    {
                        mergedThemeDictionary = new MergedDictionary(owningDocument, this);
                        mergedThemeDictionaryByKeyDictionary.Add(nodeKey, mergedThemeDictionary);
                    }

                    foreach (XmlNode resourceDictionaryChild in childNode.ChildNodes)
                    {
                        mergedThemeDictionary.AddNode(resourceDictionaryChild, xmlnsReplacementDictionary);
                    }
                }
            }
            else
            {
                // First, we need to check if this is a node with a key.  If it is, then we'll replace
                // the previous node we saw with this key, in order to have only one entry per key.
                // if it's not, or if we haven't seen a previous node with this key, then we'll just
                // add it to our list.
                string nodeKey = GetKey(node);

                if (nodeKey.Length == 0 || !nodeKeyToNodeListIndexDictionary.ContainsKey(nodeKey))
                {
                    if (nodeKey.Length != 0)
                    {
                        nodeKeyToNodeListIndexDictionary.Add(nodeKey, nodeList.Count);
                    }

                    nodeList.Add(node);
                }
                else
                {
                    int previousNodeIndex = nodeKeyToNodeListIndexDictionary[nodeKey];
                    nodeList[previousNodeIndex] = node;
                }

                if (nodeKey.Length > 0 && parentDictionary != null)
                {
                    parentDictionary.RemoveAncestorNodesWithKey(nodeKey);
                }

                if (nodeKey.Length > 0 && parentDictionary != null)
                {
                    parentDictionary.RemoveAncestorNodesWithKey(nodeKey);
                }
            }
        }