From 0dfd176a581dfa3b7dbfd5f11c841fb6a0677220 Mon Sep 17 00:00:00 2001
From: Elie Bariche <33458222+ebariche@users.noreply.github.com>
Date: Tue, 19 Nov 2024 12:48:03 -0500
Subject: [PATCH] feat: Add XAML resources UPRI trimming
---
.../Uno.UI.Tasks/Content/Uno.UI.Tasks.targets | 1 +
.../UpriSubstitutionsGeneratorTask.cs | 50 +++++++++++++++++++
src/Uno.UI/Uno.UI.Skia.csproj | 1 +
src/Uno.UI/Uno.UI.Wasm.csproj | 2 +
4 files changed, 54 insertions(+)
diff --git a/src/SourceGenerators/Uno.UI.Tasks/Content/Uno.UI.Tasks.targets b/src/SourceGenerators/Uno.UI.Tasks/Content/Uno.UI.Tasks.targets
index c69a350e0e5f..6cd48f3ce2b6 100644
--- a/src/SourceGenerators/Uno.UI.Tasks/Content/Uno.UI.Tasks.targets
+++ b/src/SourceGenerators/Uno.UI.Tasks/Content/Uno.UI.Tasks.targets
@@ -211,6 +211,7 @@
diff --git a/src/SourceGenerators/Uno.UI.Tasks/ResourcesGenerator/UpriSubstitutionsGeneratorTask.cs b/src/SourceGenerators/Uno.UI.Tasks/ResourcesGenerator/UpriSubstitutionsGeneratorTask.cs
index c0acf4f974ab..3ff01b071e49 100644
--- a/src/SourceGenerators/Uno.UI.Tasks/ResourcesGenerator/UpriSubstitutionsGeneratorTask.cs
+++ b/src/SourceGenerators/Uno.UI.Tasks/ResourcesGenerator/UpriSubstitutionsGeneratorTask.cs
@@ -1,12 +1,15 @@
#nullable enable
+using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
+using System.Text.RegularExpressions;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
+using Uno.UI.SourceGenerators.BindableTypeProviders;
namespace Uno.UI.Tasks.ResourcesGenerator
{
@@ -22,9 +25,13 @@ public class UpriSubstitutionsGeneratorTask_v0 : Task
[Required]
public ITaskItem[]? Resources { get; set; }
+ public bool EnableXamlTrimmingIntegration { get; set; }
+
[Required]
public string? OutputFile { get; set; }
+ private static Regex _suffixRegex = new(@"\.Strings\..+\.upri", RegexOptions.Compiled | RegexOptions.IgnoreCase);
+
public override bool Execute()
{
// Debugger.Launch();
@@ -37,6 +44,35 @@ public override bool Execute()
xml.AppendChild(linkerNode);
+ if (EnableXamlTrimmingIntegration)
+ {
+ var groups =
+ Resources
+ .Where(r => !r.ItemSpec.StartsWith("Resources", StringComparison.Ordinal) &&
+ !r.ItemSpec.StartsWith("Microsoft." + /* UWP don't rename */ "UI.Xaml.Controls.WinUIResources", StringComparison.Ordinal))
+ .Select(r => (Key: RewriteKey(r.ItemSpec), Value: r.ItemSpec))
+ .ToLookup(kv => kv.Key, kv => kv.Value);
+
+ foreach (var group in groups)
+ {
+ var assemblyNode = xml.CreateElement(string.Empty, "assembly", string.Empty);
+ assemblyNode.SetAttribute("fullname", AssemblyName);
+ assemblyNode.SetAttribute("feature", group.Key);
+ assemblyNode.SetAttribute("featurevalue", "false");
+
+ linkerNode.AppendChild(assemblyNode);
+
+ foreach (var resource in group)
+ {
+ var resourceNode = xml.CreateElement(string.Empty, "resource", string.Empty);
+ resourceNode.SetAttribute("name", resource);
+ resourceNode.SetAttribute("action", "remove");
+
+ assemblyNode.AppendChild(resourceNode);
+ }
+ }
+ }
+
foreach (var resourceGroup in Resources.GroupBy(r => r.GetMetadata("Language").Replace('-', '_').ToLowerInvariant()))
{
var assemblyNode = xml.CreateElement(string.Empty, "assembly", string.Empty);
@@ -65,5 +101,19 @@ public override bool Execute()
return true;
}
+
+ private string RewriteKey(string key)
+ {
+ if (key.StartsWith("UI.Xaml.DragDrop.", StringComparison.Ordinal))
+ {
+ key = key.Replace("UI.Xaml.DragDrop", "Microsoft.UI.Xaml.DragView");
+ }
+ else if (key.StartsWith("UI.Xaml.", StringComparison.Ordinal))
+ {
+ key = key.Replace("UI.Xaml", "Microsoft.UI.Xaml");
+ }
+
+ return LinkerHintsHelpers.GetPropertyAvailableName(_suffixRegex.Replace(key, string.Empty));
+ }
}
}
diff --git a/src/Uno.UI/Uno.UI.Skia.csproj b/src/Uno.UI/Uno.UI.Skia.csproj
index 58ad289f8b69..6ddb47f29626 100644
--- a/src/Uno.UI/Uno.UI.Skia.csproj
+++ b/src/Uno.UI/Uno.UI.Skia.csproj
@@ -20,6 +20,7 @@
.\
true
+ true
Uno.WinUI
diff --git a/src/Uno.UI/Uno.UI.Wasm.csproj b/src/Uno.UI/Uno.UI.Wasm.csproj
index 4b96422857a0..ddf31dfad096 100644
--- a/src/Uno.UI/Uno.UI.Wasm.csproj
+++ b/src/Uno.UI/Uno.UI.Wasm.csproj
@@ -22,6 +22,8 @@
Uno.WinUI
true
+ true
+
WebAssembly
.\