Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add XAML resources UPRI trimming #18851

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
[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 @@

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 @@

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");

Check notice on line 109 in src/SourceGenerators/Uno.UI.Tasks/ResourcesGenerator/UpriSubstitutionsGeneratorTask.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/SourceGenerators/Uno.UI.Tasks/ResourcesGenerator/UpriSubstitutionsGeneratorTask.cs#L109

Change this call to 'key.Replace' to an overload that accepts a 'StringComparison' as a parameter.
}
else if (key.StartsWith("UI.Xaml.", StringComparison.Ordinal))
{
key = key.Replace("UI.Xaml", "Microsoft.UI.Xaml");

Check notice on line 113 in src/SourceGenerators/Uno.UI.Tasks/ResourcesGenerator/UpriSubstitutionsGeneratorTask.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/SourceGenerators/Uno.UI.Tasks/ResourcesGenerator/UpriSubstitutionsGeneratorTask.cs#L113

Change this call to 'key.Replace' to an overload that accepts a 'StringComparison' as a parameter.
}

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>
jeromelaban marked this conversation as resolved.
Show resolved Hide resolved

<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
Loading