Skip to content

Commit 48f3134

Browse files
simplify on platform
1 parent 97a39e4 commit 48f3134

File tree

8 files changed

+124
-31
lines changed

8 files changed

+124
-31
lines changed

src/Controls/src/SourceGen/InitializeComponentCodeWriter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ PrePost newblock() =>
9191
FilePath = xamlItem.ProjectItem.RelativePath,
9292
EnableLineInfo = xamlItem.ProjectItem.EnableLineInfo,
9393
EnableDiagnostics = xamlItem.ProjectItem.EnableDiagnostics,
94-
TargetPlatform = xamlItem.ProjectItem.TargetFramework ?? "",
94+
TargetFramework = xamlItem.ProjectItem.TargetFramework ?? "",
9595
};
9696
using (newblock())
9797
{
@@ -124,8 +124,8 @@ static void Visit(RootNode rootnode, SourceGenContext visitorContext, bool useDe
124124
if (useDesignProperties)
125125
rootnode.Accept(new RemoveDuplicateDesignNodes(), null);
126126
rootnode.Accept(new SimplifyTypeExtensionVisitor(), null);
127-
if (!string.IsNullOrEmpty(visitorContext.TargetPlatform))
128-
rootnode.Accept(new SimplifyOnPlatformVisitor(visitorContext.TargetPlatform), null);
127+
if (!string.IsNullOrEmpty(visitorContext.TargetFramework))
128+
rootnode.Accept(new SimplifyOnPlatformVisitor(visitorContext.TargetFramework), null);
129129
rootnode.Accept(new CreateValuesVisitor(visitorContext), null);
130130
rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null); //set namescopes for {x:Reference} and FindByName
131131
rootnode.Accept(new SetFieldsForXNamesVisitor(visitorContext), null);

src/Controls/src/SourceGen/SourceGenContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SourceGenContext(IndentedTextWriter writer, Compilation compilation, Sourc
3232
public IList<string> LocalMethods { get; } = new List<string>();
3333
public bool EnableLineInfo { get; set; }
3434
public bool EnableDiagnostics { get; internal set; }
35-
public string TargetPlatform { get; internal set; } = "";
35+
public string TargetFramework { get; internal set; } = "";
3636

3737
public void AddLocalMethod(string code)
3838
{

src/Controls/src/Xaml/SimplifyOnPlatformVisitor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public void Visit(ElementNode node, INode parentNode)
4949
if (node.XmlType.IsOfAnyType("OnPlatformExtension"))
5050
{
5151
if ( node.Properties.TryGetValue(new XmlName("", Target), out INode targetNode)
52-
|| node.Properties.TryGetValue(new XmlName("", "Default"), out targetNode))
52+
|| node.Properties.TryGetValue(new XmlName(null, Target), out targetNode)
53+
|| node.Properties.TryGetValue(new XmlName("", "Default"), out targetNode)
54+
|| node.Properties.TryGetValue(new XmlName(null, "Default"), out targetNode))
5355
{
5456
if (!node.TryGetPropertyName(parentNode, out XmlName name))
5557
return;

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

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,116 @@ public TestPage()
4545

4646
var expected =
4747
"""
48-
using System;
49-
using Microsoft.Maui.Controls;
50-
using Microsoft.Maui.Controls.Xaml;
48+
49+
//------------------------------------------------------------------------------
50+
// <auto-generated>
51+
// This code was generated by a .NET MAUI source generator.
52+
//
53+
// Changes to this file may cause incorrect behavior and will be lost if
54+
// the code is regenerated.
55+
// </auto-generated>
56+
//------------------------------------------------------------------------------
57+
#nullable enable
5158
5259
namespace Test;
5360
54-
public partial class TestPage : ContentPage
61+
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Maui.Controls.SourceGen, Version=10.0.0.0, Culture=neutral, PublicKeyToken=null", "10.0.0.0")]
62+
public partial class TestPage
5563
{
56-
public TestPage()
64+
private partial void InitializeComponent()
5765
{
58-
InitializeComponent();
66+
var setter = new global::Microsoft.Maui.Controls.Setter();
67+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(setter!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 8, 14);
68+
var setter1 = new global::Microsoft.Maui.Controls.Setter();
69+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(setter1!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 9, 14);
70+
var xamlServiceProvider = new global::Microsoft.Maui.Controls.Xaml.Internals.XamlServiceProvider(this);
71+
var xmlNamespaceResolver = new global::Microsoft.Maui.Controls.Xaml.Internals.XmlNamespaceResolver();
72+
xmlNamespaceResolver.Add("__f__", "http://schemas.microsoft.com/dotnet/2021/maui");
73+
xmlNamespaceResolver.Add("__g__", "http://schemas.microsoft.com/dotnet/maui/global");
74+
xmlNamespaceResolver.Add("", "http://schemas.microsoft.com/dotnet/2021/maui");
75+
xmlNamespaceResolver.Add("x", "http://schemas.microsoft.com/winfx/2009/xaml");
76+
xamlServiceProvider.Add(typeof(global::Microsoft.Maui.Controls.Xaml.IXamlTypeResolver), new global::Microsoft.Maui.Controls.Xaml.Internals.XamlTypeResolver(xmlNamespaceResolver, typeof(global::Test.TestPage).Assembly));
77+
var style1 = new global::Microsoft.Maui.Controls.Style(((global::Microsoft.Maui.Controls.IExtendedTypeConverter)new global::Microsoft.Maui.Controls.TypeTypeConverter()).ConvertFromInvariantString("Label", xamlServiceProvider) as global::System.Type);
78+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(style1!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 7, 10);
79+
var __root = this;
80+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(__root!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 2, 2);
81+
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
82+
global::Microsoft.Maui.Controls.Internals.INameScope iNameScope = global::Microsoft.Maui.Controls.Internals.NameScope.GetNameScope(__root) ?? new global::Microsoft.Maui.Controls.Internals.NameScope();
83+
#endif
84+
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
85+
global::Microsoft.Maui.Controls.Internals.NameScope.SetNameScope(__root, iNameScope);
86+
#endif
87+
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
88+
global::Microsoft.Maui.Controls.Internals.INameScope iNameScope1 = new global::Microsoft.Maui.Controls.Internals.NameScope();
89+
#endif
90+
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
91+
global::Microsoft.Maui.Controls.Internals.INameScope iNameScope2 = new global::Microsoft.Maui.Controls.Internals.NameScope();
92+
#endif
93+
#line 8 "Test.xaml"
94+
var xamlServiceProvider1 = new global::Microsoft.Maui.Controls.Xaml.Internals.XamlServiceProvider(this);
95+
var iProvideValueTarget = new global::Microsoft.Maui.Controls.Xaml.Internals.SimpleValueTargetProvider(
96+
new object[] {setter, style1, __root},
97+
typeof(global::Microsoft.Maui.Controls.Setter).GetProperty("Property"),
98+
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
99+
new [] { iNameScope1 },
100+
#else
101+
null,
102+
#endif
103+
false);
104+
xamlServiceProvider1.Add(typeof(global::Microsoft.Maui.Controls.Xaml.IReferenceProvider), iProvideValueTarget);
105+
xamlServiceProvider1.Add(typeof(global::Microsoft.Maui.Controls.Xaml.IProvideValueTarget), iProvideValueTarget);
106+
var xmlNamespaceResolver1 = new global::Microsoft.Maui.Controls.Xaml.Internals.XmlNamespaceResolver();
107+
xmlNamespaceResolver1.Add("__f__", "http://schemas.microsoft.com/dotnet/2021/maui");
108+
xmlNamespaceResolver1.Add("__g__", "http://schemas.microsoft.com/dotnet/maui/global");
109+
xmlNamespaceResolver1.Add("", "http://schemas.microsoft.com/dotnet/2021/maui");
110+
xmlNamespaceResolver1.Add("x", "http://schemas.microsoft.com/winfx/2009/xaml");
111+
xamlServiceProvider1.Add(typeof(global::Microsoft.Maui.Controls.Xaml.IXamlTypeResolver), new global::Microsoft.Maui.Controls.Xaml.Internals.XamlTypeResolver(xmlNamespaceResolver1, typeof(global::Test.TestPage).Assembly));
112+
setter.Property = ((global::Microsoft.Maui.Controls.IExtendedTypeConverter)new global::Microsoft.Maui.Controls.BindablePropertyConverter()).ConvertFromInvariantString("TextColor", xamlServiceProvider1) as global::Microsoft.Maui.Controls.BindableProperty;
113+
#line default
114+
#line 8 "Test.xaml"
115+
setter.Value = "Pink";
116+
#line default
117+
var setter2 = new global::Microsoft.Maui.Controls.Setter {Property = global::Microsoft.Maui.Controls.Label.TextColorProperty, Value = global::Microsoft.Maui.Graphics.Color.Parse("Pink")};
118+
if (global::Microsoft.Maui.VisualDiagnostics.GetSourceInfo(setter2!) == null)
119+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(setter2!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 8, 14);
120+
#line 8 "Test.xaml"
121+
((global::System.Collections.Generic.ICollection<global::Microsoft.Maui.Controls.Setter>)style1.Setters).Add((global::Microsoft.Maui.Controls.Setter)setter2);
122+
#line default
123+
#line 9 "Test.xaml"
124+
var xamlServiceProvider2 = new global::Microsoft.Maui.Controls.Xaml.Internals.XamlServiceProvider(this);
125+
var iProvideValueTarget1 = new global::Microsoft.Maui.Controls.Xaml.Internals.SimpleValueTargetProvider(
126+
new object[] {setter1, style1, __root},
127+
typeof(global::Microsoft.Maui.Controls.Setter).GetProperty("Property"),
128+
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
129+
new [] { iNameScope2 },
130+
#else
131+
null,
132+
#endif
133+
false);
134+
xamlServiceProvider2.Add(typeof(global::Microsoft.Maui.Controls.Xaml.IReferenceProvider), iProvideValueTarget1);
135+
xamlServiceProvider2.Add(typeof(global::Microsoft.Maui.Controls.Xaml.IProvideValueTarget), iProvideValueTarget1);
136+
var xmlNamespaceResolver2 = new global::Microsoft.Maui.Controls.Xaml.Internals.XmlNamespaceResolver();
137+
xmlNamespaceResolver2.Add("__f__", "http://schemas.microsoft.com/dotnet/2021/maui");
138+
xmlNamespaceResolver2.Add("__g__", "http://schemas.microsoft.com/dotnet/maui/global");
139+
xmlNamespaceResolver2.Add("", "http://schemas.microsoft.com/dotnet/2021/maui");
140+
xmlNamespaceResolver2.Add("x", "http://schemas.microsoft.com/winfx/2009/xaml");
141+
xamlServiceProvider2.Add(typeof(global::Microsoft.Maui.Controls.Xaml.IXamlTypeResolver), new global::Microsoft.Maui.Controls.Xaml.Internals.XamlTypeResolver(xmlNamespaceResolver2, typeof(global::Test.TestPage).Assembly));
142+
setter1.Property = ((global::Microsoft.Maui.Controls.IExtendedTypeConverter)new global::Microsoft.Maui.Controls.BindablePropertyConverter()).ConvertFromInvariantString("IsVisible", xamlServiceProvider2) as global::Microsoft.Maui.Controls.BindableProperty;
143+
#line default
144+
#line 1 "Test.xaml"
145+
setter1.Value = "True";
146+
#line default
147+
var setter3 = new global::Microsoft.Maui.Controls.Setter {Property = global::Microsoft.Maui.Controls.VisualElement.IsVisibleProperty, Value = (bool)new global::Microsoft.Maui.Controls.VisualElement.VisibilityConverter().ConvertFromInvariantString("True")!};
148+
if (global::Microsoft.Maui.VisualDiagnostics.GetSourceInfo(setter3!) == null)
149+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(setter3!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 9, 14);
150+
#line 9 "Test.xaml"
151+
((global::System.Collections.Generic.ICollection<global::Microsoft.Maui.Controls.Setter>)style1.Setters).Add((global::Microsoft.Maui.Controls.Setter)setter3);
152+
#line default
153+
var resourceDictionary = __root.Resources;
154+
resourceDictionary.Add("style", style1);
59155
}
60156
}
157+
61158
""";
62159

63160
var (result, generated) = RunGenerator(xaml, code, targetFramework: "net10.0-android");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ namespace Microsoft.Maui.Controls.SourceGen.UnitTests;
99

1010
public class SourceGenXamlInitializeComponentTestBase : SourceGenTestsBase
1111
{
12-
protected record AdditionalXamlFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null)
13-
: AdditionalFile(Text: ToAdditionalText(Path, Content), Kind: "Xaml", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName, TargetFramework: TargetFramework);
12+
protected record AdditionalXamlFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null, string? NoWarn = null)
13+
: AdditionalFile(Text: ToAdditionalText(Path, Content), Kind: "Xaml", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName, TargetFramework: TargetFramework, NoWarn: NoWarn);
1414

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

2222
return (result, generated);

src/Controls/tests/SourceGen.UnitTests/SourceGenCssTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.Maui.Controls.Xaml.UnitTests.SourceGen;
1212
public class SourceGenCssTests : SourceGenTestsBase
1313
{
1414
private record AdditionalCssFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null)
15-
: AdditionalFile(Text: SourceGeneratorDriver.ToAdditionalText(Path, Content), Kind: "Css", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName ?? Path, TargetFramework: TargetFramework);
15+
: AdditionalFile(Text: SourceGeneratorDriver.ToAdditionalText(Path, Content), Kind: "Css", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName ?? Path, TargetFramework: TargetFramework, NoWarn: "");
1616

1717
[Test]
1818
public void TestCodeBehindGenerator_BasicCss()

src/Controls/tests/SourceGen.UnitTests/SourceGenXamlCodeBehindTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ namespace Microsoft.Maui.Controls.Xaml.UnitTests.SourceGen;
1212

1313
public class SourceGenXamlCodeBehindTests : SourceGenTestsBase
1414
{
15-
private record AdditionalXamlFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null)
16-
: AdditionalFile(Text: SourceGeneratorDriver.ToAdditionalText(Path, Content), Kind: "Xaml", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName, TargetFramework: TargetFramework);
15+
private record AdditionalXamlFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null, string? NoWarn = null)
16+
: AdditionalFile(Text: SourceGeneratorDriver.ToAdditionalText(Path, Content), Kind: "Xaml", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName, TargetFramework: TargetFramework, NoWarn: NoWarn);
1717

1818
[Test]
1919
public void TestCodeBehindGenerator_BasicXaml()

src/Controls/tests/SourceGen.UnitTests/SourceGeneratorDriver.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ public static class SourceGeneratorDriver
1818
{
1919
private static MetadataReference[]? MauiReferences;
2020

21-
public record AdditionalFile(AdditionalText Text, string Kind, string RelativePath, string? TargetPath, string? ManifestResourceName, string? TargetFramework);
21+
public record AdditionalFile(AdditionalText Text, string Kind, string RelativePath, string? TargetPath, string? ManifestResourceName, string? TargetFramework, string? NoWarn);
2222

2323
public static GeneratorDriverRunResult RunGenerator<T>(Compilation compilation, params AdditionalFile[] additionalFiles)
24-
where T : IIncrementalGenerator, new() => RunGenerator<T>(compilation, "", additionalFiles);
25-
public static GeneratorDriverRunResult RunGenerator<T>(Compilation compilation, string noWarn, params AdditionalFile[] additionalFiles)
2624
where T : IIncrementalGenerator, new()
2725
{
2826
ISourceGenerator generator = new T().AsSourceGenerator();
@@ -33,8 +31,8 @@ public static GeneratorDriverRunResult RunGenerator<T>(Compilation compilation,
3331
trackIncrementalGeneratorSteps: true);
3432

3533
GeneratorDriver driver = CSharpGeneratorDriver.Create([generator], driverOptions: options)
36-
.AddAdditionalTexts(additionalFiles.Select(f => f.Text).ToImmutableArray())
37-
.WithUpdatedAnalyzerConfigOptions(new CustomAnalyzerConfigOptionsProvider(additionalFiles, noWarn));
34+
.AddAdditionalTexts([.. additionalFiles.Select(f => f.Text)])
35+
.WithUpdatedAnalyzerConfigOptions(new CustomAnalyzerConfigOptionsProvider(additionalFiles));
3836

3937
driver = driver.RunGenerators(compilation);
4038

@@ -108,12 +106,10 @@ private static MetadataReference[] GetMauiReferences()
108106
private class CustomAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider
109107
{
110108
private readonly IImmutableDictionary<string, AdditionalFile> _additionalFiles;
111-
private readonly string _noWarn;
112109

113-
public CustomAnalyzerConfigOptionsProvider(AdditionalFile[] additionalFiles, string noWarn = "")
110+
public CustomAnalyzerConfigOptionsProvider(AdditionalFile[] additionalFiles)
114111
{
115112
_additionalFiles = additionalFiles.ToImmutableDictionary(f => f.Text.Path, f => f);
116-
_noWarn = noWarn;
117113
}
118114

119115
public override AnalyzerConfigOptions GlobalOptions => throw new System.NotImplementedException();
@@ -126,22 +122,20 @@ public override AnalyzerConfigOptions GetOptions(SyntaxTree tree)
126122
public override AnalyzerConfigOptions GetOptions(AdditionalText textFile)
127123
{
128124
return _additionalFiles.TryGetValue(textFile.Path, out var additionalFile)
129-
? (AnalyzerConfigOptions)new CustomAnalyzerConfigOptions(additionalFile, _noWarn)
125+
? (AnalyzerConfigOptions)new CustomAnalyzerConfigOptions(additionalFile)
130126
: CustomAnalyzerConfigOptions.Empty;
131127
}
132128

133129
private class CustomAnalyzerConfigOptions : AnalyzerConfigOptions
134130
{
135131
readonly AdditionalFile? _additionalFile;
136-
readonly string _noWarn;
137132

138-
public CustomAnalyzerConfigOptions(AdditionalFile? additionalFile, string noWarn)
133+
public CustomAnalyzerConfigOptions(AdditionalFile? additionalFile)
139134
{
140135
_additionalFile = additionalFile;
141-
_noWarn = noWarn;
142136
}
143137

144-
public static AnalyzerConfigOptions Empty { get; } = new CustomAnalyzerConfigOptions(null, "");
138+
public static AnalyzerConfigOptions Empty { get; } = new CustomAnalyzerConfigOptions(null);
145139

146140
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value)
147141
{
@@ -161,7 +155,7 @@ public override bool TryGetValue(string key, [NotNullWhen(true)] out string? val
161155
"build_property.targetframework" => _additionalFile.TargetFramework,
162156
"build_property.EnableMauiXamlDiagnostics" => "true",
163157
"build_property.MauiXamlLineInfo" => "enable",
164-
"build_property.MauiXamlNoWarn" => _noWarn,
158+
"build_property.MauiXamlNoWarn" => _additionalFile.NoWarn,
165159

166160
_ => null
167161
};

0 commit comments

Comments
 (0)