Ejemplo n.º 1
0
 /// <summary>
 /// Should this Xaml be included when defining default styles? Outside of Uno this is always true. In Uno, this excludes WinUI Fluent resources (which consumer code accesses via XamlControlsResources)
 /// </summary>
 private bool IsIncludedResource(XamlFileDefinition xamlFileDefinition, XamlGlobalStaticResourcesMap map) => !IsUnoAssembly ||
 _nonSystemResources.None(s => map.GetSourceLink(xamlFileDefinition).EndsWith(s));
Ejemplo n.º 2
0
        private string GenerateGlobalResources(IEnumerable <XamlFileDefinition> files, XamlGlobalStaticResourcesMap map)
        {
            var outputFile = Path.Combine(_targetPath, "GlobalStaticResources.g.cs");

            var writer = new IndentedStringBuilder();

            writer.AppendLineInvariant("// <autogenerated />");
            writer.AppendLineInvariant("#pragma warning disable 618  // Ignore obsolete members warnings");
            writer.AppendLineInvariant("#pragma warning disable 105  // Ignore duplicate namespaces");
            writer.AppendLineInvariant("#pragma warning disable 1591 // Ignore missing XML comment warnings");
            writer.AppendLineInvariant("using System;");
            writer.AppendLineInvariant("using System.Linq;");
            writer.AppendLineInvariant("using System.Collections.Generic;");
            writer.AppendLineInvariant("using Uno.Extensions;");
            writer.AppendLineInvariant("using Uno;");
            writer.AppendLineInvariant("using System.Diagnostics;");

            //TODO Determine the list of namespaces to use
            writer.AppendLineInvariant($"using {XamlConstants.BaseXamlNamespace};");
            writer.AppendLineInvariant($"using {XamlConstants.Namespaces.Controls};");
            writer.AppendLineInvariant($"using {XamlConstants.Namespaces.Data};");
            writer.AppendLineInvariant($"using {XamlConstants.Namespaces.Documents};");
            writer.AppendLineInvariant($"using {XamlConstants.Namespaces.Media};");
            writer.AppendLineInvariant($"using {XamlConstants.Namespaces.MediaAnimation};");
            writer.AppendLineInvariant("using {0};", _defaultNamespace);
            writer.AppendLineInvariant("");

            using (writer.BlockInvariant("namespace {0}", _defaultNamespace))
            {
                writer.AppendLineInvariant("/// <summary>");
                writer.AppendLineInvariant("/// Contains all the static resources defined for the application");
                writer.AppendLineInvariant("/// </summary>");

                AnalyzerSuppressionsGenerator.Generate(writer, _analyzerSuppressions);
                using (writer.BlockInvariant("public sealed partial class GlobalStaticResources"))
                {
                    writer.AppendLineInvariant("static bool _initialized;");
                    writer.AppendLineInvariant("private static bool _stylesRegistered;");
                    writer.AppendLineInvariant("private static bool _dictionariesRegistered;");

                    using (writer.BlockInvariant("internal static {0} {1} {{get; }} = new {0}()", ParseContextPropertyType, ParseContextPropertyName))
                    {
                        writer.AppendLineInvariant("AssemblyName = \"{0}\",", _projectInstance.GetPropertyValue("AssemblyName"));
                    }

                    writer.AppendLineInvariant(";");
                    writer.AppendLine();

                    using (writer.BlockInvariant("static GlobalStaticResources()"))
                    {
                        writer.AppendLineInvariant("Initialize();");
                    }

                    using (writer.BlockInvariant("public static void Initialize()"))
                    {
                        using (writer.BlockInvariant("if (!_initialized)"))
                        {
                            using (IsUnoAssembly ? writer.BlockInvariant("using (ResourceResolver.WriteInitiateGlobalStaticResourcesEventActivity())") : null)
                            {
                                writer.AppendLineInvariant("_initialized = true;");

                                foreach (var ambientResource in _ambientGlobalResources)
                                {
                                    if (ambientResource.GetMethods().Any(m => m.Name == "Initialize"))
                                    {
                                        writer.AppendLineInvariant("global::{0}.Initialize();", ambientResource.GetFullName());
                                    }
                                }

                                foreach (var ambientResource in _ambientGlobalResources)
                                {
                                    // Note: we do *not* call RegisterDefaultStyles for the current assembly, because those styles are treated as implicit styles, not default styles
                                    if (ambientResource.GetMethods().Any(m => m.Name == "RegisterDefaultStyles"))
                                    {
                                        writer.AppendLineInvariant("global::{0}.RegisterDefaultStyles();", ambientResource.GetFullName());
                                    }
                                }

                                foreach (var ambientResource in _ambientGlobalResources)
                                {
                                    if (ambientResource.GetMethods().Any(m => m.Name == "RegisterResourceDictionariesBySource"))
                                    {
                                        writer.AppendLineInvariant("global::{0}.RegisterResourceDictionariesBySource();", ambientResource.GetFullName());
                                    }
                                }

                                if (IsUnoAssembly && _xamlSourceFiles.Any())
                                {
                                    // Build master dictionary
                                    foreach (var dictProperty in map.GetAllDictionaryProperties(_baseResourceDependencies, _nonSystemResources))
                                    {
                                        writer.AppendLineInvariant("MasterDictionary.MergedDictionaries.Add({0});", dictProperty);
                                    }
                                }
                            }
                        }
                    }

                    using (writer.BlockInvariant("public static void RegisterDefaultStyles()"))
                    {
                        using (writer.BlockInvariant("if(!_stylesRegistered)"))
                        {
                            writer.AppendLineInvariant("_stylesRegistered = true;");
                            foreach (var file in files.Where(f => IsIncludedResource(f, map)).Select(f => f.UniqueID).Distinct())
                            {
                                writer.AppendLineInvariant("RegisterDefaultStyles_{0}();", file);
                            }
                        }
                    }

                    writer.AppendLineInvariant("// Register ResourceDictionaries using ms-appx:/// syntax, this is called for external resources");
                    using (writer.BlockInvariant("public static void RegisterResourceDictionariesBySource()"))
                    {
                        using (writer.BlockInvariant("if(!_dictionariesRegistered)"))
                        {
                            writer.AppendLineInvariant("_dictionariesRegistered = true;");

                            if (!IsUnoAssembly && !IsUnoFluentAssembly)
                            {
                                // For third-party libraries, expose all files using standard uri
                                foreach (var file in files.Where(IsResourceDictionary))
                                {
                                    var url = "{0}/{1}".InvariantCultureFormat(_metadataHelper.AssemblyName, map.GetSourceLink(file));
                                    RegisterForFile(file, url);
                                }
                            }
                            else if (files.Any())                             // The NETSTD reference assembly contains no Xaml files
                            {
                                // For Uno assembly, we expose WinUI resources using same uri as on Windows
                                RegisterForFile(files.FirstOrDefault(f => map.GetSourceLink(f).EndsWith(WinUIThemeResourcePathSuffix)), XamlFilePathHelper.WinUIThemeResourceURL);
                                RegisterForFile(files.FirstOrDefault(f => map.GetSourceLink(f).EndsWith(WinUICompactPathSuffix)), XamlFilePathHelper.WinUICompactURL);
                            }

                            void RegisterForFile(XamlFileDefinition file, string url)
                            {
                                if (file != null)
                                {
                                    writer.AppendLineInvariant("global::Uno.UI.ResourceResolver.RegisterResourceDictionaryBySource(uri: \"ms-appx:///{0}\", context: {1}, dictionary: () => {2}_ResourceDictionary);",
                                                               url,
                                                               ParseContextPropertyName,
                                                               file.UniqueID
                                                               );
                                }
                            }
                        }
                    }

                    writer.AppendLineInvariant("// Register ResourceDictionaries using ms-resource:/// syntax, this is called for local resources");
                    using (writer.BlockInvariant("internal static void RegisterResourceDictionariesBySourceLocal()"))
                    {
                        foreach (var file in files.Where(IsResourceDictionary))
                        {
                            // We leave context null because local resources should be found through Application.Resources
                            writer.AppendLineInvariant("global::Uno.UI.ResourceResolver.RegisterResourceDictionaryBySource(uri: \"{0}{1}\", context: null, dictionary: () => {2}_ResourceDictionary);",
                                                       XamlFilePathHelper.LocalResourcePrefix,
                                                       map.GetSourceLink(file),
                                                       file.UniqueID
                                                       );
                            // Local resources can also be found through the ms-appx:/// prefix
                            writer.AppendLineInvariant("global::Uno.UI.ResourceResolver.RegisterResourceDictionaryBySource(uri: \"{0}{1}\", context: null, dictionary: () => {2}_ResourceDictionary);",
                                                       XamlFilePathHelper.AppXIdentifier,
                                                       map.GetSourceLink(file),
                                                       file.UniqueID
                                                       );
                        }
                    }

                    if (IsUnoAssembly)
                    {
                        // Declare master dictionary
                        writer.AppendLine();
                        writer.AppendLineInvariant("internal static ResourceDictionary MasterDictionary {{get; }} = new ResourceDictionary();");
                    }

                    // Generate all the partial methods, even if they don't exist. That avoids
                    // having to sync the generation of the files with this global table.
                    foreach (var file in files.Select(f => f.UniqueID).Distinct())
                    {
                        writer.AppendLineInvariant("static partial void RegisterDefaultStyles_{0}();", file);
                    }

                    writer.AppendLineInvariant("[global::System.Obsolete(\"This method is provided for binary backward compatibility. It will always return null.\")]");
                    writer.AppendLineInvariant("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]");
                    writer.AppendLineInvariant("public static object FindResource(string name) => null;");

                    writer.AppendLineInvariant("");
                }
            }

            return(writer.ToString());
        }