Skip to content

Commit dd3d179

Browse files
committed
fix(reg): Adjust UnoHasLocalizationResources metadata
The change ensure that backward compatibility code path is not used when UnoHasLocalizationResources has been added to the assembly. This reverts the behavior where all assemblies containing a `GlobalStaticResources` type were considered candidates for resources lookups.
1 parent 6cf8e3b commit dd3d179

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlCodeGeneration.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,13 @@ public List<KeyValuePair<string, string>> Generate(GenerationRunInfo generationR
382382

383383
private void TryGenerateUnoResourcesKeyAttribute(ImmutableHashSet<string> resourceKeys)
384384
{
385-
if (!resourceKeys.IsEmpty)
386-
{
387-
_generatorContext.AddSource("LocalizationResources", "[assembly: global::System.Reflection.AssemblyMetadata(\"UnoHasLocalizationResources\", \"True\")]");
388-
}
385+
var hasResources = !resourceKeys.IsEmpty;
386+
387+
_generatorContext.AddSource(
388+
"LocalizationResources",
389+
$"[assembly: global::System.Reflection.AssemblyMetadata(" +
390+
$"\"UnoHasLocalizationResources\", " +
391+
$"\"{hasResources.ToString(CultureInfo.InvariantCulture)}\")]");
389392
}
390393

391394
#if !NETFRAMEWORK

src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -753,18 +753,62 @@ private void GenerateResourceLoader(IndentedStringBuilder writer)
753753

754754
private void BuildResourceLoaderFromAssembly(IndentedStringBuilder writer, IAssemblySymbol assembly)
755755
{
756-
var hasUnoHasLocalizationResources = assembly.GetAttributes().Any(a =>
756+
var unoHasLocalizationResourcesAttribute = assembly.GetAttributes().FirstOrDefault(a =>
757757
SymbolEqualityComparer.Default.Equals(a.AttributeClass, _assemblyMetadataSymbol)
758758
&& a.ConstructorArguments.Length == 2
759-
&& a.ConstructorArguments[0].Value is "UnoHasLocalizationResources"
760-
&& (a.ConstructorArguments[1].Value?.ToString().Equals("True", StringComparison.OrdinalIgnoreCase) ?? false));
761-
759+
&& a.ConstructorArguments[0].Value is "UnoHasLocalizationResources");
760+
var unoHasLocalizationResourcesAttributeDefined = unoHasLocalizationResourcesAttribute is not null;
761+
762+
var hasUnoHasLocalizationResourcesAttributeEnabled = unoHasLocalizationResourcesAttribute
763+
?.ConstructorArguments[1]
764+
.Value
765+
?.ToString()
766+
.Equals("True", StringComparison.OrdinalIgnoreCase) ?? false;
767+
762768
// Legacy behavior relying on the fact that GlobalStaticResources is generated using the default namespace.
763-
var hasGlobalStaticResources = assembly.GetTypeByMetadataName(assembly.Name + ".GlobalStaticResources") is not null;
769+
var globalStaticResourcesSymbol = assembly.GetTypeByMetadataName(assembly.Name + ".GlobalStaticResources");
770+
771+
if (
772+
// The assembly contains resources to be used
773+
hasUnoHasLocalizationResourcesAttributeEnabled
764774

765-
if (hasUnoHasLocalizationResources || hasGlobalStaticResources)
775+
// The assembly does not have the UnoHasLocalizationResources attribute defined, but
776+
// may still contain resources as it may have been built with a previous version of Uno.
777+
|| (!unoHasLocalizationResourcesAttributeDefined && globalStaticResourcesSymbol is not null)
778+
)
766779
{
767-
writer.AppendLineIndented($"global::Windows.ApplicationModel.Resources.ResourceLoader.AddLookupAssembly(global::System.Reflection.Assembly.Load(\"{assembly.Name}\"));");
780+
if (_isWasm)
781+
{
782+
var anchorType = globalStaticResourcesSymbol
783+
?? assembly
784+
.Modules
785+
.First()
786+
.GlobalNamespace
787+
.GetNamespaceTypes()
788+
.FirstOrDefault(s => s.IsLocallyPublic(_metadataHelper.Compilation.Assembly.Modules.First()));
789+
790+
if (anchorType is INamedTypeSymbol namedSymbol)
791+
{
792+
// Use a public type to get the assembly to work around a WASM assembly loading issue
793+
writer.AppendLineIndented(
794+
$"global::Windows.ApplicationModel.Resources.ResourceLoader" +
795+
$".AddLookupAssembly(typeof(global::{namedSymbol.GetFullMetadataName()}).Assembly);"
796+
#if DEBUG
797+
+ $" /* {assembly.Name}, hasUnoHasLocalizationResourcesAttributeEnabled:{hasUnoHasLocalizationResourcesAttributeEnabled}, unoHasLocalizationResourcesAttributeDefined:{unoHasLocalizationResourcesAttributeDefined} */"
798+
#endif
799+
);
800+
}
801+
else
802+
{
803+
#if DEBUG
804+
writer.AppendLineIndented($"/* No anchor type for reference {assembly.Name} */");
805+
#endif
806+
}
807+
}
808+
else
809+
{
810+
writer.AppendLineIndented($"global::Windows.ApplicationModel.Resources.ResourceLoader.AddLookupAssembly(global::System.Reflection.Assembly.Load(\"{assembly.Name}\"));");
811+
}
768812
}
769813
else
770814
{

0 commit comments

Comments
 (0)