Skip to content

Commit a4b6092

Browse files
replace directory separator before comparing
1 parent 74aadde commit a4b6092

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

src/Controls/src/SourceGen/CodeBehindGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ static bool ShouldGenerateSourceGenInitializeComponent(XamlProjectItemForIC xaml
232232
ITypeSymbol xamlResIdAttr = compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Xaml.XamlResourceIdAttribute")!;
233233
INamedTypeSymbol? GetTypeForResourcePath(string resourcePath, IAssemblySymbol assembly)
234234
{
235-
var attr = assembly.GetAttributes(xamlResIdAttr).FirstOrDefault(attr => (string)attr.ConstructorArguments[1].Value! == resourcePath);
235+
//XRID use paths with forward slashes. we can't change that it's used by HR
236+
var attr = assembly.GetAttributes(xamlResIdAttr).FirstOrDefault(attr => ((string)attr.ConstructorArguments[1].Value!).Replace('/', Path.DirectorySeparatorChar) == resourcePath);
236237
return attr?.ConstructorArguments[2].Value as INamedTypeSymbol;
237238
}
238239

src/Controls/src/SourceGen/InitializeComponentCodeWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ PrePost newblock() =>
6060
ITypeSymbol xamlResIdAttr = compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Xaml.XamlResourceIdAttribute")!;
6161
INamedTypeSymbol? GetTypeForResourcePath(string resourcePath, IAssemblySymbol assembly)
6262
{
63-
var attr = assembly.GetAttributes(xamlResIdAttr).FirstOrDefault(attr => (string)attr.ConstructorArguments[1].Value! == resourcePath);
63+
//XRID use paths with forward slashes. we can't change that it's used by HR
64+
var attr = assembly.GetAttributes(xamlResIdAttr).FirstOrDefault(attr => ((string)attr.ConstructorArguments[1].Value!).Replace('/', Path.DirectorySeparatorChar) == resourcePath);
6465
return attr?.ConstructorArguments[2].Value as INamedTypeSymbol;
6566
}
6667

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
4+
namespace Microsoft.Maui.Controls.SourceGen.UnitTests;
5+
6+
public class ResourceDictionary : SourceGenXamlInitializeComponentTestBase
7+
{
8+
[Test]
9+
public void ResourceDictionaryWithoutXClass()
10+
{
11+
var xaml =
12+
"""
13+
<?xml version="1.0" encoding="utf-8" ?>
14+
<ResourceDictionary
15+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
16+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
17+
xmlns:local="using:Microsoft.Maui.Controls.Xaml.UnitTests">
18+
<Color x:Key="AccentColor">#FF4B14</Color>
19+
</ResourceDictionary>
20+
""";
21+
var expected =
22+
"""
23+
24+
//------------------------------------------------------------------------------
25+
// <auto-generated>
26+
// This code was generated by a .NET MAUI source generator.
27+
//
28+
// Changes to this file may cause incorrect behavior and will be lost if
29+
// the code is regenerated.
30+
// </auto-generated>
31+
//------------------------------------------------------------------------------
32+
#nullable enable
33+
34+
namespace __XamlGeneratedCode__;
35+
36+
public partial class __TypeDBD64C1C77CDA760
37+
{
38+
private partial void InitializeComponent()
39+
{
40+
var color = global::Microsoft.Maui.Graphics.Color.Parse("#FF4B14");
41+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(color!, new global::System.Uri(@"Styles.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 6, 4);
42+
var __root = this;
43+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(__root!, new global::System.Uri(@"Styles.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 2, 2);
44+
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
45+
global::Microsoft.Maui.Controls.Internals.INameScope iNameScope = new global::Microsoft.Maui.Controls.Internals.NameScope();
46+
#endif
47+
__root.Add("AccentColor", color);
48+
}
49+
}
50+
51+
""";
52+
53+
var (result, generated) = RunGenerator(xaml, "", path: "Styles.xaml");
54+
Assert.IsFalse(result.Diagnostics.Any());
55+
56+
Assert.AreEqual(expected, generated);
57+
58+
}
59+
}

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/SourceGenXamlInitializeComponentTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public class SourceGenXamlInitializeComponentTestBase : SourceGenTestsBase
1212
protected record AdditionalXamlFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null, string? NoWarn = null)
1313
: AdditionalFile(Text: ToAdditionalText(Path, Content), Kind: "Xaml", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName, TargetFramework: TargetFramework, NoWarn: NoWarn);
1414

15-
protected (GeneratorDriverRunResult result, string? text) RunGenerator(string xaml, string code, string noWarn = "", string targetFramework = "")
15+
protected (GeneratorDriverRunResult result, string? text) RunGenerator(string xaml, string code, string noWarn = "", string targetFramework = "", string? path = null)
1616
{
1717
var compilation = CreateMauiCompilation();
1818
compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));
19-
var result = RunGenerator<CodeBehindGenerator>(compilation, new AdditionalXamlFile("Test.xaml", xaml, TargetFramework: targetFramework, NoWarn: noWarn));
19+
var result = RunGenerator<CodeBehindGenerator>(compilation, new AdditionalXamlFile(path ?? "Test.xaml", xaml, TargetFramework: targetFramework, NoWarn: noWarn, ManifestResourceName: $"{compilation.AssemblyName}.Test.xaml"));
2020
var generated = result.Results.SingleOrDefault().GeneratedSources.SingleOrDefault(gs => gs.HintName.EndsWith(".xsg.cs")).SourceText?.ToString();
2121

2222
return (result, generated);

0 commit comments

Comments
 (0)