private void MergeAndGenerateXaml(MergedDictionary mergedDictionary, List <string> files, string targetOSVersion, int apiVersion)
        {
            Log.LogMessage("Merge and generate xaml Files for target os" + targetOSVersion);

            foreach (string file in files)
            {
                try
                {
                    mergedDictionary.MergeContent(File.ReadAllText(file));
                }
                catch (Exception)
                {
                    Log.LogError("Exception found when merge file " + file);
                    throw;
                }
            }

            string content = mergedDictionary.ToString();

            string name     = targetOSVersion + "_" + PostfixForGeneratedFile + ".xaml";
            string fullPath = Path.Combine(OutputDirectory, name);

            string prefixedName     = targetOSVersion + "_" + postfixForPrefixedGeneratedFile + ".xaml";
            string prefixedFullPath = Path.Combine(OutputDirectory, prefixedName);

            string strippedContent = StripNamespaces.StripNamespaceForAPIVersion(content, apiVersion);

            filesWritten.Add(Utils.RewriteFileIfNecessary(prefixedFullPath, content));
            filesWritten.Add(Utils.RewriteFileIfNecessary(fullPath, strippedContent));

            nextBaseFile = prefixedFullPath;
        }
        private void ExecuteForTaskItems(ITaskItem[] items, string targetOSVersion)
        {
            MergedDictionary mergedDictionary = MergedDictionary.CreateMergedDicionary();
            List <string>    files            = new List <string>();

            if (nextBaseFile != null)
            {
                files.Add(nextBaseFile);
            }
            if (items != null)
            {
                foreach (ITaskItem item in items)
                {
                    string file = item.ItemSpec;
                    if (File.Exists(file))
                    {
                        files.Add(file);
                    }
                    else
                    {
                        Log.LogError("Can't find page file " + file);
                    }
                }
            }

            int apiVersion = StripNamespaces.universalApiContractVersionMapping[targetOSVersion];

            MergeAndGenerateXaml(mergedDictionary, files, targetOSVersion.ToLower(), apiVersion);
        }
Exemple #3
0
        private void ExecuteForTaskItems(ITaskItem[] items, string targetOSVersion)
        {
            MergedDictionary mergedDictionary = MergedDictionary.CreateMergedDicionary();
            List <string>    files            = new List <string>();

            if (nextBaseFile != null)
            {
                files.Add(nextBaseFile);
            }
            if (items != null)
            {
                foreach (ITaskItem item in items)
                {
                    if (!string.IsNullOrEmpty(PagesFilteredBy) && !item.GetMetadata(PagesFilteredBy).Equals("true", StringComparison.OrdinalIgnoreCase))
                    {
                        this.LogMessage(MessageImportance.Low, "Filtered item " + item.ItemSpec);
                        continue;
                    }

                    string file = item.ItemSpec;
                    if (File.Exists(file))
                    {
                        files.Add(file);
                    }
                    else
                    {
                        this.LogError("Can't find page file " + file);
                    }
                }
            }

            int apiVersion = StripNamespaces.universalApiContractVersionMapping[targetOSVersion];

            MergeAndGenerateXaml(mergedDictionary, files, targetOSVersion.ToLower(), apiVersion);
        }
Exemple #4
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;
        }
        public override bool Execute()
        {
            if (string.IsNullOrEmpty(ForOSVersion) || !StripNamespaces.universalApiContractVersionMapping.ContainsKey(ForOSVersion))
            {
                Log.LogError("Unknown OS version, and valid version is ", string.Join(",", StripNamespaces.universalApiContractVersionMapping.Keys));
            }

            MergedDictionary mergedDictionary = MergedDictionary.CreateMergedDicionary();

            foreach (ITaskItem item in Sources)
            {
                string file = item.ItemSpec;
                if (File.Exists(file))
                {
                    mergedDictionary.MergeContent(File.ReadAllText(file));
                }
                else
                {
                    Log.LogError("Error to look for file " + item.ItemSpec);
                }
            }

            int    apiVersion = StripNamespaces.universalApiContractVersionMapping[ForOSVersion];
            string content    = mergedDictionary.ToString();

            string prefixedName    = XamlFileGenerated.Substring(0, XamlFileGenerated.Length - 5) + ".prefixed.xaml";
            string strippedContent = StripNamespaces.StripNamespaceForAPIVersion(content, apiVersion);

            string[] filesWritten = new string[2];
            filesWritten[0] = Utils.RewriteFileIfNecessary(prefixedName, content);
            filesWritten[1] = Utils.RewriteFileIfNecessary(XamlFileGenerated, strippedContent);

            FilesWritten = filesWritten;

            return(!Log.HasLoggedErrors);
        }
Exemple #6
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);
                }
            }
        }