Esempio n. 1
0
        //--------------------------------------------------------------------------------------------------

        public override bool Execute()
        {
            if (Output.ItemSpec.Length == 0)
            {
                Log.LogError($"Output for merged resource dictionary not specified.");
                return(false);
            }

            if (Input.ItemSpec.Length == 0)
            {
                Log.LogError($"{Output.ItemSpec}: No input specified.");
                return(false);
            }

            var outputDoc = XamlResMerger.MergeResources(ProjectDir, Input.ItemSpec, DependencyOutput.ItemSpec);

            if (outputDoc == null)
            {
                Log.LogError($"{Output.ItemSpec}: Merging resource dictionaries failed.");
                return(false);
            }

            // Post processing
            if (!XamlProcessor.RunPostProcessing(outputDoc.Root, s => Log.LogError(Output.ItemSpec + s)))
            {
                Log.LogError($"{Output.ItemSpec}: Post processing failed.");
            }

            // save file
            outputDoc.Save(Path.Combine(ProjectDir, Output.ItemSpec));
            return(true);
        }
        //--------------------------------------------------------------------------------------------------

        public override bool Execute()
        {
            bool result = true;

            if (Output.ItemSpec.Length == 0)
            {
                Log.LogError($"Output for icon merge not specified.");
                return(false);
            }

            if (Inputs.Length == 0)
            {
                Log.LogError($"No input items specified for merged icon file {Output.ItemSpec}.");
                return(false);
            }

            //Log.LogWarning(Output.ItemSpec);
            //foreach (var input in Inputs)
            //{
            //    Log.LogWarning(input.ItemSpec);
            //}

            var outputDirectory = Path.GetDirectoryName(Output.ItemSpec);

            // iterate through files
            var fileList = new Dictionary <string, string>();

            foreach (var input in Inputs)
            {
                var key = KeyPrefix + "_" + Path.GetFileNameWithoutExtension(input.ItemSpec);

                if (fileList.ContainsKey(key))
                {
                    Log.LogError($"Key has already a file assignment: {key} -> {input.ItemSpec}");
                    result = false;
                }

                fileList[key] = input.ItemSpec;
            }

            // Process aliases
            var aliasesFilename = Path.Combine(outputDirectory, "Aliases.txt");

            if (File.Exists(aliasesFilename))
            {
                var lines     = File.ReadLines(aliasesFilename);
                var linecount = 0;
                foreach (var line in lines)
                {
                    linecount++;
                    if (string.IsNullOrWhiteSpace(line) || line.StartsWith("//"))
                    {
                        continue;
                    }

                    var splitted = line.Split('=');
                    if (splitted.Length != 2)
                    {
                        Log.LogError($"Aliases.txt Line {linecount}: More than two segments.");
                        result = false;
                        continue;
                    }

                    var key = KeyPrefix + "_" + splitted[0];
                    if (fileList.ContainsKey(key))
                    {
                        Log.LogWarning($"Aliases.txt Line {linecount}: Key has already a file assignment: {key}");
                        continue;
                    }

                    var filePath = Path.Combine(TempPath, splitted[1]);
                    if (!File.Exists(filePath))
                    {
                        Log.LogError($"Aliases.txt Line {linecount}: File does not exist for key: {splitted[0]}");
                        result = false;
                        continue;
                    }

                    fileList[key] = filePath;
                }
            }

            // Prepare main XAML
            int iconCount = 0;
            var outputDoc = XamlProcessor.CreateEmptyResourceDictionary();

            // Process files
            foreach (var kvp in fileList)
            {
                var node = _ProcessFile(kvp.Value, kvp.Key);
                if (node != null)
                {
                    outputDoc.Root.Add(new XComment("#region " + kvp.Key));
                    outputDoc.Root.Add(node);
                    outputDoc.Root.Add(new XComment("#endregion"));
                    iconCount++;
                }
            }

            outputDoc.Save(Output.ItemSpec);
            Log.LogMessage($"{Path.GetFileName(Output.ItemSpec)} generated with {iconCount} icons.");

            return(result);
        }
Esempio n. 3
0
        /***************************************************************/

        /// <summary>
        /// save all resources into one big resource dictionary respecting the dependencies to increase performance
        /// </summary>
        /// <param name="projectPath">project path (C:/..)</param>
        /// <param name="relativeSourceFilePath">relative source file path (/LookAndFeel.xaml)</param>
        /// <param name="dependencyFilePath"></param>
        public static XDocument MergeResources(string projectPath, string relativeSourceFilePath, string dependencyFilePath)
        {
            var sourceFilePath = Path.Combine(projectPath, relativeSourceFilePath);

            // load source doc
            var sourceDoc = XDocument.Load(sourceFilePath);

            // get default namespace for doc creation and filtering
            var defaultNameSpace = sourceDoc.Root.GetDefaultNamespace();

            // get res dict string
            var resDictString = sourceDoc.Root.Name.LocalName;

            // create output doc
            var outputDoc = XamlProcessor.CreateEmptyResourceDictionary();

            // create documents
            var documents = new Dictionary <string, Data>();

            // add elements
            if (!_PrepareDocuments(ref documents, projectPath, relativeSourceFilePath))
            {
                return(null);
            }

            // add merged resource dictionaries
            var merged = new XElement(defaultNameSpace + resDictString + ".MergedDictionaries");

            foreach (var source in documents.Where(kvp => kvp.Value.Document == null))
            {
                merged.Add(new XElement(defaultNameSpace + resDictString, new XAttribute("Source", source.Key)));
            }
            outputDoc.Root.Add(merged);

            // add elements (ordered by dependency count)
            var dependencyList = new List <string>();

            foreach (var item in documents.OrderByDescending(item => item.Value.DependencyCount))
            {
                if (item.Value.Document == null)
                {
                    continue;
                }

                string relativeName = item.Key;
                if (relativeName.StartsWith(projectPath))
                {
                    relativeName = relativeName.Substring(projectPath.Length);
                }

                // add attributes
                foreach (var attribute in item.Value.Document.Root.Attributes())
                {
                    outputDoc.Root.SetAttributeValue(attribute.Name, attribute.Value);
                }

                // add elements
                outputDoc.Root.Add(new XComment("region " + relativeName));
                outputDoc.Root.Add(item.Value.Document.Root.Elements().Where(e => !e.Name.LocalName.StartsWith(resDictString)));
                outputDoc.Root.Add(new XComment("endregion"));

                // add to dependency list
                dependencyList.Add(relativeName);
            }

            // save dependency list
            var depsFilePath = Path.Combine(projectPath, dependencyFilePath);

            if (!string.IsNullOrEmpty(depsFilePath))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(depsFilePath));
                File.WriteAllLines(depsFilePath, dependencyList);
            }

            return(outputDoc);
        }