Skip to content

Commit

Permalink
Merge pull request #18851 from unoplatform/dev/eb/xaml-trimming
Browse files Browse the repository at this point in the history
feat: Add XAML resources UPRI trimming
  • Loading branch information
ebariche authored Nov 20, 2024
2 parents f021b99 + 0dfd176 commit 2f48f5f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
<UpriSubstitutionsGeneratorTask_v0
AssemblyName="$(AssemblyName)"
Resources="@(_UnoUpriResource)"
EnableXamlTrimmingIntegration="$(UnoXamlResourcesUpriTrimming)"
OutputFile="$(_UnoUpriSubstitutionsFile)" />

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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));
}
}
}
1 change: 1 addition & 0 deletions src/Uno.UI/Uno.UI.Skia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PlatformItemsBasePath>.\</PlatformItemsBasePath>

<UnoXamlResourcesTrimming>true</UnoXamlResourcesTrimming>
<UnoXamlResourcesUpriTrimming>true</UnoXamlResourcesUpriTrimming>

<PackageId Condition="'$(UNO_UWP_BUILD)'!='true'">Uno.WinUI</PackageId>
</PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/Uno.UI.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<PackageId Condition="'$(UNO_UWP_BUILD)'!='true'">Uno.WinUI</PackageId>

<UnoXamlResourcesTrimming>true</UnoXamlResourcesTrimming>
<UnoXamlResourcesUpriTrimming>true</UnoXamlResourcesUpriTrimming>

<UnoRuntimeIdentifier>WebAssembly</UnoRuntimeIdentifier>
<PlatformItemsBasePath>.\</PlatformItemsBasePath>

Expand Down

0 comments on commit 2f48f5f

Please sign in to comment.