Restructured CodeBehindGenerator pipeline (#20524)
* - Restructured CodeBehindGenerator pipeline to maximize SourceGen cachability - Split out CSS SourceGen, which does not depend on Compilation at all - Added TrackingNames to support new SourceGen unit test project Fixes Issue #12978 CodeBehindGenerator has improper pipeline Fixes AB#1947659: `CodeBehindGenerator` has improper pipeline * - Use file-scoped namespaces throughout PR - Use raw string literals for SourceGen tests
This commit is contained in:
Родитель
79faaf5342
Коммит
4fa29bc436
|
@ -241,6 +241,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UITest.Appium", "src\TestUt
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UITest.NUnit", "src\TestUtils\src\UITest.NUnit\UITest.NUnit.csproj", "{A307B624-48D4-494E-A70D-5B3CDF6620CF}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Controls.SourceGen.UnitTests", "src\Controls\tests\SourceGen.UnitTests\Conrtrols.SourceGen.UnitTests\Controls.SourceGen.UnitTests.csproj", "{06747B55-91DB-47F5-B7A2-56526C28A0D3}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGen.UnitTests", "src\Controls\tests\SourceGen.UnitTests\SourceGen.UnitTests.csproj", "{BC7F7C82-694F-4B97-86FC-273FB3FACA25}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -610,6 +614,14 @@ Global
|
|||
{A307B624-48D4-494E-A70D-5B3CDF6620CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A307B624-48D4-494E-A70D-5B3CDF6620CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A307B624-48D4-494E-A70D-5B3CDF6620CF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{06747B55-91DB-47F5-B7A2-56526C28A0D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{06747B55-91DB-47F5-B7A2-56526C28A0D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{06747B55-91DB-47F5-B7A2-56526C28A0D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{06747B55-91DB-47F5-B7A2-56526C28A0D3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{BC7F7C82-694F-4B97-86FC-273FB3FACA25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{BC7F7C82-694F-4B97-86FC-273FB3FACA25}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{BC7F7C82-694F-4B97-86FC-273FB3FACA25}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BC7F7C82-694F-4B97-86FC-273FB3FACA25}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -721,6 +733,8 @@ Global
|
|||
{8F7B825D-24A8-4E09-AC5B-9117926B7BF3} = {7AC28763-9C68-4BF9-A1BA-25CBFFD2D15C}
|
||||
{26379D0E-4D4D-48CA-94B1-A2C1972AB335} = {7AC28763-9C68-4BF9-A1BA-25CBFFD2D15C}
|
||||
{A307B624-48D4-494E-A70D-5B3CDF6620CF} = {7AC28763-9C68-4BF9-A1BA-25CBFFD2D15C}
|
||||
{06747B55-91DB-47F5-B7A2-56526C28A0D3} = {25D0D27A-C5FE-443D-8B65-D6C987F4A80E}
|
||||
{BC7F7C82-694F-4B97-86FC-273FB3FACA25} = {25D0D27A-C5FE-443D-8B65-D6C987F4A80E}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {0B8ABEAD-D2B5-4370-A187-62B5ABE4EE50}
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
"src\\Controls\\tests\\Core.UnitTests\\Controls.Core.UnitTests.csproj",
|
||||
"src\\Controls\\tests\\CustomAttributes\\Controls.CustomAttributes.csproj",
|
||||
"src\\Controls\\tests\\DeviceTests\\Controls.DeviceTests.csproj",
|
||||
"src\\Controls\\tests\\SourceGen.UnitTests\\SourceGen.UnitTests.csproj",
|
||||
"src\\Controls\\tests\\UITests\\Controls.AppiumTests.csproj",
|
||||
"src\\Controls\\tests\\Xaml.UnitTests.ExternalAssembly\\Controls.Xaml.UnitTests.ExternalAssembly.csproj",
|
||||
"src\\Controls\\tests\\Xaml.UnitTests.InternalsHiddenAssembly\\Controls.Xaml.UnitTests.InternalsHiddenAssembly.csproj",
|
||||
|
@ -47,7 +48,6 @@
|
|||
"src\\Graphics\\samples\\GraphicsTester.Android\\GraphicsTester.Android.csproj",
|
||||
"src\\Graphics\\samples\\GraphicsTester.Portable\\GraphicsTester.Portable.csproj",
|
||||
"src\\Graphics\\samples\\GraphicsTester.Skia.Console\\GraphicsTester.Skia.Console.csproj",
|
||||
"src\\Graphics\\samples\\GraphicsTester.Skia.Tizen\\GraphicsTester.Skia.Tizen.csproj",
|
||||
"src\\Graphics\\samples\\GraphicsTester.Skia.Windows\\GraphicsTester.Skia.Windows.csproj",
|
||||
"src\\Graphics\\samples\\GraphicsTester.WinUI.Desktop\\GraphicsTester.WinUI.Desktop.csproj",
|
||||
"src\\Graphics\\samples\\GraphicsTester.iOS\\GraphicsTester.iOS.csproj",
|
||||
|
@ -63,14 +63,14 @@
|
|||
"src\\SingleProject\\Resizetizer\\test\\UnitTests\\Resizetizer.UnitTests.csproj",
|
||||
"src\\Templates\\src\\Microsoft.Maui.Templates.csproj",
|
||||
"src\\TestUtils\\samples\\DeviceTests.Sample\\TestUtils.DeviceTests.Sample.csproj",
|
||||
"src\\TestUtils\\src\\UITest.Core\\UITest.Core.csproj",
|
||||
"src\\TestUtils\\src\\UITest.Appium\\UITest.Appium.csproj",
|
||||
"src\\TestUtils\\src\\UITest.NUnit\\UITest.NUnit.csproj",
|
||||
"src\\TestUtils\\src\\DeviceTests.Runners.SourceGen\\TestUtils.DeviceTests.Runners.SourceGen.csproj",
|
||||
"src\\TestUtils\\src\\DeviceTests.Runners\\TestUtils.DeviceTests.Runners.csproj",
|
||||
"src\\TestUtils\\src\\DeviceTests\\TestUtils.DeviceTests.csproj",
|
||||
"src\\TestUtils\\src\\Microsoft.Maui.IntegrationTests\\Microsoft.Maui.IntegrationTests.csproj",
|
||||
"src\\TestUtils\\src\\TestUtils\\TestUtils.csproj",
|
||||
"src\\TestUtils\\src\\UITest.Appium\\UITest.Appium.csproj",
|
||||
"src\\TestUtils\\src\\UITest.Core\\UITest.Core.csproj",
|
||||
"src\\TestUtils\\src\\UITest.NUnit\\UITest.NUnit.csproj",
|
||||
"src\\Workload\\Microsoft.Maui.Sdk\\Microsoft.Maui.Sdk.csproj",
|
||||
"src\\Workload\\Microsoft.NET.Sdk.Maui.Manifest\\Microsoft.NET.Sdk.Maui.Manifest.csproj"
|
||||
]
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -0,0 +1,15 @@
|
|||
namespace Microsoft.Maui.Controls.SourceGen;
|
||||
|
||||
/// <summary>
|
||||
/// Names for tracking source generator stages
|
||||
/// </summary>
|
||||
public class TrackingNames
|
||||
{
|
||||
public const string CssProjectItemProvider = nameof(CssProjectItemProvider);
|
||||
public const string ProjectItemProvider = nameof(ProjectItemProvider);
|
||||
public const string ReferenceCompilationProvider = nameof(ReferenceCompilationProvider);
|
||||
public const string ReferenceTypeCacheProvider = nameof(ReferenceTypeCacheProvider);
|
||||
public const string XmlnsDefinitionsProvider = nameof(XmlnsDefinitionsProvider);
|
||||
public const string XamlProjectItemProvider = nameof(XamlProjectItemProvider);
|
||||
public const string XamlSourceProvider = nameof(XamlSourceProvider);
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>$(_MauiDotNetTfm)</TargetFramework>
|
||||
<RootNamespace>Microsoft.Maui.Controls.SourceGen.UnitTests</RootNamespace>
|
||||
<AssemblyName>Microsoft.Maui.Controls.SourceGen.UnitTests</AssemblyName>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<NoWarn>$(NoWarn);0672;0219;0414;CS0436;CS0618</NoWarn>
|
||||
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0618;XC0022;XC0023</WarningsNotAsErrors>
|
||||
<IsPackable>false</IsPackable>
|
||||
<Nullable>enable</Nullable>
|
||||
<DisableMSBuildAssemblyCopyCheck>true</DisableMSBuildAssemblyCopyCheck>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<DefineConstants>DEBUG</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<DebugType>full</DebugType>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.5.0" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.5.0" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Core\Controls.Core.csproj" />
|
||||
<ProjectReference Include="..\..\src\SourceGen\Controls.SourceGen.csproj" />
|
||||
<ProjectReference Include="..\..\src\Xaml\Controls.Xaml.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project="$(MauiSrcDirectory)Maui.InTree.props" Condition=" '$(UseMaui)' != 'true' " />
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,84 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.Maui.Controls.SourceGen;
|
||||
using NUnit.Framework;
|
||||
|
||||
using static Microsoft.Maui.Controls.Xaml.UnitTests.SourceGen.SourceGeneratorDriver;
|
||||
|
||||
namespace Microsoft.Maui.Controls.Xaml.UnitTests.SourceGen;
|
||||
|
||||
public class SourceGenCssTests : SourceGenTestsBase
|
||||
{
|
||||
private record AdditionalCssFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null)
|
||||
: AdditionalFile(Text: SourceGeneratorDriver.ToAdditionalText(Path, Content), Kind: "Css", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName ?? Path, TargetFramework: TargetFramework);
|
||||
|
||||
[Test]
|
||||
public void TestCodeBehindGenerator_BasicCss()
|
||||
{
|
||||
var css =
|
||||
"""
|
||||
h1 {color: purple;
|
||||
background-color: lightcyan;
|
||||
font-weight: 800;
|
||||
}
|
||||
""";
|
||||
var compilation = SourceGeneratorDriver.CreateMauiCompilation();
|
||||
var cssFile = new AdditionalCssFile("Test.css", css);
|
||||
var result = SourceGeneratorDriver.RunGenerator<CodeBehindGenerator>(compilation, cssFile);
|
||||
|
||||
Assert.IsFalse(result.Diagnostics.Any());
|
||||
|
||||
var generated = result.Results.Single().GeneratedSources.Single().SourceText.ToString();
|
||||
|
||||
Assert.IsTrue(generated.Contains($"XamlResourceId(\"{cssFile.ManifestResourceName}\", \"{cssFile.Path}\"", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCodeBehindGenerator_ModifiedCss()
|
||||
{
|
||||
var css =
|
||||
"""
|
||||
h1 {color: purple;
|
||||
background-color: lightcyan;
|
||||
font-weight: 800;
|
||||
}
|
||||
""";
|
||||
var newCss =
|
||||
"""
|
||||
h1 {color: red;
|
||||
background-color: lightcyan;
|
||||
font-weight: 800;
|
||||
}
|
||||
""";
|
||||
var cssFile = new AdditionalCssFile("Test.css", css);
|
||||
var compilation = SourceGeneratorDriver.CreateMauiCompilation();
|
||||
var result = SourceGeneratorDriver.RunGeneratorWithChanges<CodeBehindGenerator>(compilation, ApplyChanges, cssFile);
|
||||
|
||||
var result1 = result.result1.Results.Single();
|
||||
var result2 = result.result2.Results.Single();
|
||||
var output1 = result1.GeneratedSources.Single().SourceText.ToString();
|
||||
var output2 = result2.GeneratedSources.Single().SourceText.ToString();
|
||||
|
||||
Assert.IsTrue(result1.TrackedSteps.All(s => s.Value.Single().Outputs.Single().Reason == IncrementalStepRunReason.New));
|
||||
Assert.AreEqual(output1, output2);
|
||||
|
||||
Assert.IsTrue(output1.Contains($"XamlResourceId(\"{cssFile.ManifestResourceName}\", \"{cssFile.Path}\"", StringComparison.Ordinal));
|
||||
|
||||
(GeneratorDriver, Compilation) ApplyChanges(GeneratorDriver driver, Compilation compilation)
|
||||
{
|
||||
var newCssFile = new AdditionalCssFile("Test.css", newCss);
|
||||
driver = driver.ReplaceAdditionalText(cssFile.Text, newCssFile.Text);
|
||||
return (driver, compilation);
|
||||
}
|
||||
|
||||
var expectedReasons = new Dictionary<string, IncrementalStepRunReason>
|
||||
{
|
||||
{ TrackingNames.ProjectItemProvider, IncrementalStepRunReason.Modified },
|
||||
{ TrackingNames.CssProjectItemProvider, IncrementalStepRunReason.Modified }
|
||||
};
|
||||
|
||||
VerifyStepRunReasons(result2, expectedReasons);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.Maui.Controls.Xaml.UnitTests.SourceGen;
|
||||
|
||||
public class SourceGenTestsBase
|
||||
{
|
||||
public static void VerifyStepRunReasons(GeneratorRunResult result2, Dictionary<string, IncrementalStepRunReason> expectedReasons)
|
||||
{
|
||||
foreach (var expected in expectedReasons)
|
||||
{
|
||||
var actualReason = result2.TrackedSteps[expected.Key].Single().Outputs.Single().Reason;
|
||||
Assert.AreEqual(expected.Value, actualReason, message: expected.Key);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,210 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.Maui.Controls.SourceGen;
|
||||
using NUnit.Framework;
|
||||
|
||||
using static Microsoft.Maui.Controls.Xaml.UnitTests.SourceGen.SourceGeneratorDriver;
|
||||
|
||||
namespace Microsoft.Maui.Controls.Xaml.UnitTests.SourceGen;
|
||||
|
||||
public class SourceGenXamlTests : SourceGenTestsBase
|
||||
{
|
||||
private record AdditionalXamlFile(string Path, string Content, string? RelativePath = null, string? TargetPath = null, string? ManifestResourceName = null, string? TargetFramework = null)
|
||||
: AdditionalFile(Text: SourceGeneratorDriver.ToAdditionalText(Path, Content), Kind: "Xaml", RelativePath: RelativePath ?? Path, TargetPath: TargetPath, ManifestResourceName: ManifestResourceName, TargetFramework: TargetFramework);
|
||||
|
||||
[Test]
|
||||
public void TestCodeBehindGenerator_BasicXaml()
|
||||
{
|
||||
var xaml =
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ContentPage
|
||||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
x:Class="Test.TestPage">
|
||||
<Button x:Name="MyButton" Text="Hello MAUI!" />
|
||||
</ContentPage>
|
||||
""";
|
||||
var compilation = SourceGeneratorDriver.CreateMauiCompilation();
|
||||
var result = SourceGeneratorDriver.RunGenerator<CodeBehindGenerator>(compilation, new AdditionalXamlFile("Test.xaml", xaml));
|
||||
|
||||
Assert.IsFalse(result.Diagnostics.Any());
|
||||
|
||||
var generated = result.Results.Single().GeneratedSources.Single().SourceText.ToString();
|
||||
|
||||
Assert.IsTrue(generated.Contains("Microsoft.Maui.Controls.Button MyButton", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCodeBehindGenerator_LocalXaml()
|
||||
{
|
||||
var xaml =
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ContentPage
|
||||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
xmlns:local="clr-namespace:Test"
|
||||
x:Class="Test.TestPage">
|
||||
<local:TestControl x:Name="MyTestControl" />
|
||||
</ContentPage>
|
||||
""";
|
||||
var compilation = SourceGeneratorDriver.CreateMauiCompilation();
|
||||
var result = SourceGeneratorDriver.RunGenerator<CodeBehindGenerator>(compilation, new AdditionalXamlFile("Test.xaml", xaml));
|
||||
|
||||
Assert.IsFalse(result.Diagnostics.Any());
|
||||
|
||||
var generated = result.Results.Single().GeneratedSources.Single().SourceText.ToString();
|
||||
|
||||
Assert.IsTrue(generated.Contains("Test.TestControl MyTestControl", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCodeBehindGenerator_CompilationClone()
|
||||
{
|
||||
var xaml =
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ContentPage
|
||||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
x:Class="Test.TestPage">
|
||||
<Button x:Name="MyButton" Text="Hello MAUI!" />
|
||||
</ContentPage>
|
||||
""";
|
||||
var xamlFile = new AdditionalXamlFile("Test.xaml", xaml);
|
||||
var compilation = SourceGeneratorDriver.CreateMauiCompilation();
|
||||
var result = SourceGeneratorDriver.RunGeneratorWithChanges<CodeBehindGenerator>(compilation, ApplyChanges, xamlFile);
|
||||
|
||||
var result1 = result.result1.Results.Single();
|
||||
var result2 = result.result2.Results.Single();
|
||||
var output1 = result1.GeneratedSources.Single().SourceText.ToString();
|
||||
var output2 = result2.GeneratedSources.Single().SourceText.ToString();
|
||||
|
||||
Assert.IsTrue(result1.TrackedSteps.All(s => s.Value.Single().Outputs.Single().Reason == IncrementalStepRunReason.New));
|
||||
Assert.AreEqual(output1, output2);
|
||||
|
||||
(GeneratorDriver, Compilation) ApplyChanges(GeneratorDriver driver, Compilation compilation)
|
||||
{
|
||||
return (driver, compilation.Clone());
|
||||
}
|
||||
|
||||
var expectedReasons = new Dictionary<string, IncrementalStepRunReason>
|
||||
{
|
||||
{ TrackingNames.ProjectItemProvider, IncrementalStepRunReason.Cached },
|
||||
{ TrackingNames.ReferenceCompilationProvider, IncrementalStepRunReason.Unchanged },
|
||||
{ TrackingNames.ReferenceTypeCacheProvider, IncrementalStepRunReason.Cached },
|
||||
{ TrackingNames.XmlnsDefinitionsProvider, IncrementalStepRunReason.Cached },
|
||||
{ TrackingNames.XamlProjectItemProvider, IncrementalStepRunReason.Cached },
|
||||
{ TrackingNames.XamlSourceProvider, IncrementalStepRunReason.Cached }
|
||||
};
|
||||
|
||||
VerifyStepRunReasons(result2, expectedReasons);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCodeBehindGenerator_ReferenceAdded()
|
||||
{
|
||||
var xaml =
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ContentPage
|
||||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
x:Class="Test.TestPage">
|
||||
<Button x:Name="MyButton" Text="Hello MAUI!" />
|
||||
</ContentPage>
|
||||
""";
|
||||
var xamlFile = new AdditionalXamlFile("Test.xaml", xaml);
|
||||
var compilation = SourceGeneratorDriver.CreateMauiCompilation();
|
||||
var result = SourceGeneratorDriver.RunGeneratorWithChanges<CodeBehindGenerator>(compilation, ApplyChanges, xamlFile);
|
||||
|
||||
var result1 = result.result1.Results.Single();
|
||||
var result2 = result.result2.Results.Single();
|
||||
var output1 = result1.GeneratedSources.Single().SourceText.ToString();
|
||||
var output2 = result2.GeneratedSources.Single().SourceText.ToString();
|
||||
|
||||
Assert.IsTrue(result1.TrackedSteps.All(s => s.Value.Single().Outputs.Single().Reason == IncrementalStepRunReason.New));
|
||||
Assert.AreEqual(output1, output2);
|
||||
|
||||
(GeneratorDriver, Compilation) ApplyChanges(GeneratorDriver driver, Compilation compilation)
|
||||
{
|
||||
return (driver, compilation.AddReferences(MetadataReference.CreateFromFile(typeof(SourceGenXamlTests).Assembly.Location)));
|
||||
}
|
||||
|
||||
var expectedReasons = new Dictionary<string, IncrementalStepRunReason>
|
||||
{
|
||||
{ TrackingNames.ProjectItemProvider, IncrementalStepRunReason.Cached },
|
||||
{ TrackingNames.XamlProjectItemProvider, IncrementalStepRunReason.Cached },
|
||||
{ TrackingNames.ReferenceCompilationProvider, IncrementalStepRunReason.Modified },
|
||||
{ TrackingNames.XmlnsDefinitionsProvider, IncrementalStepRunReason.Modified },
|
||||
{ TrackingNames.ReferenceTypeCacheProvider, IncrementalStepRunReason.Modified },
|
||||
{ TrackingNames.XamlSourceProvider, IncrementalStepRunReason.Modified }
|
||||
};
|
||||
|
||||
VerifyStepRunReasons(result2, expectedReasons);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCodeBehindGenerator_ModifiedXaml()
|
||||
{
|
||||
var xaml =
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ContentPage
|
||||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
x:Class="Test.TestPage">
|
||||
<Button x:Name="MyButton" Text="Hello MAUI!" />
|
||||
</ContentPage>
|
||||
""";
|
||||
var newXaml =
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ContentPage
|
||||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
x:Class="Test.TestPage">
|
||||
<Button x:Name="MyButton" Text="Hello MAUI!" />
|
||||
<Button x:Name="MyButton2" Text="Hello MAUI!" />
|
||||
</ContentPage>
|
||||
""";
|
||||
var xamlFile = new AdditionalXamlFile("Test.xaml", xaml);
|
||||
var compilation = SourceGeneratorDriver.CreateMauiCompilation();
|
||||
var result = SourceGeneratorDriver.RunGeneratorWithChanges<CodeBehindGenerator>(compilation, ApplyChanges, xamlFile);
|
||||
|
||||
var result1 = result.result1.Results.Single();
|
||||
var result2 = result.result2.Results.Single();
|
||||
var output1 = result1.GeneratedSources.Single().SourceText.ToString();
|
||||
var output2 = result2.GeneratedSources.Single().SourceText.ToString();
|
||||
|
||||
Assert.IsTrue(result1.TrackedSteps.All(s => s.Value.Single().Outputs.Single().Reason == IncrementalStepRunReason.New));
|
||||
Assert.AreNotEqual(output1, output2);
|
||||
|
||||
Assert.IsTrue(output1.Contains("MyButton", StringComparison.Ordinal));
|
||||
Assert.IsFalse(output1.Contains("MyButton2", StringComparison.Ordinal));
|
||||
Assert.IsTrue(output2.Contains("MyButton", StringComparison.Ordinal));
|
||||
Assert.IsTrue(output2.Contains("MyButton2", StringComparison.Ordinal));
|
||||
|
||||
(GeneratorDriver, Compilation) ApplyChanges(GeneratorDriver driver, Compilation compilation)
|
||||
{
|
||||
var newXamlFile = new AdditionalXamlFile(xamlFile.Path, newXaml);
|
||||
driver = driver.ReplaceAdditionalText(xamlFile.Text, newXamlFile.Text);
|
||||
return (driver, compilation);
|
||||
}
|
||||
|
||||
var expectedReasons = new Dictionary<string, IncrementalStepRunReason>
|
||||
{
|
||||
{ TrackingNames.ProjectItemProvider, IncrementalStepRunReason.Modified },
|
||||
{ TrackingNames.XamlProjectItemProvider, IncrementalStepRunReason.Modified },
|
||||
{ TrackingNames.ReferenceCompilationProvider, IncrementalStepRunReason.Cached },
|
||||
{ TrackingNames.XmlnsDefinitionsProvider, IncrementalStepRunReason.Cached },
|
||||
{ TrackingNames.ReferenceTypeCacheProvider, IncrementalStepRunReason.Cached },
|
||||
{ TrackingNames.XamlSourceProvider, IncrementalStepRunReason.Modified }
|
||||
};
|
||||
|
||||
VerifyStepRunReasons(result2, expectedReasons);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,174 @@
|
|||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using Microsoft.Maui.Graphics;
|
||||
|
||||
namespace Microsoft.Maui.Controls.Xaml.UnitTests.SourceGen;
|
||||
|
||||
public static class SourceGeneratorDriver
|
||||
{
|
||||
private static MetadataReference[]? MauiReferences;
|
||||
|
||||
public record AdditionalFile(AdditionalText Text, string Kind, string RelativePath, string? TargetPath, string? ManifestResourceName, string? TargetFramework);
|
||||
|
||||
public static GeneratorDriverRunResult RunGenerator<T>(Compilation compilation, params AdditionalFile[] additionalFiles)
|
||||
where T : IIncrementalGenerator, new()
|
||||
{
|
||||
ISourceGenerator generator = new T().AsSourceGenerator();
|
||||
|
||||
// Tell the driver to track all the incremental generator outputs
|
||||
var options = new GeneratorDriverOptions(
|
||||
disabledOutputs: IncrementalGeneratorOutputKind.None,
|
||||
trackIncrementalGeneratorSteps: true);
|
||||
|
||||
GeneratorDriver driver = CSharpGeneratorDriver.Create([generator], driverOptions: options)
|
||||
.AddAdditionalTexts(additionalFiles.Select(f => f.Text).ToImmutableArray())
|
||||
.WithUpdatedAnalyzerConfigOptions(new CustomAnalyzerConfigOptionsProvider(additionalFiles));
|
||||
|
||||
driver = driver.RunGenerators(compilation);
|
||||
|
||||
GeneratorDriverRunResult runResult = driver.GetRunResult();
|
||||
return runResult;
|
||||
}
|
||||
|
||||
public static (GeneratorDriverRunResult result1, GeneratorDriverRunResult result2) RunGeneratorWithChanges<T>(Compilation compilation,
|
||||
Func<GeneratorDriver, Compilation, (GeneratorDriver driver, Compilation compilation)> applyChanges, params AdditionalFile[] additionalFiles)
|
||||
where T : IIncrementalGenerator, new()
|
||||
{
|
||||
ISourceGenerator generator = new T().AsSourceGenerator();
|
||||
|
||||
// Tell the driver to track all the incremental generator outputs
|
||||
var options = new GeneratorDriverOptions(
|
||||
disabledOutputs: IncrementalGeneratorOutputKind.None,
|
||||
trackIncrementalGeneratorSteps: true);
|
||||
|
||||
GeneratorDriver driver = CSharpGeneratorDriver.Create([generator], driverOptions: options)
|
||||
.AddAdditionalTexts(additionalFiles.Select(f => f.Text).ToImmutableArray())
|
||||
.WithUpdatedAnalyzerConfigOptions(new CustomAnalyzerConfigOptionsProvider(additionalFiles));
|
||||
|
||||
driver = driver.RunGenerators(compilation);
|
||||
GeneratorDriverRunResult runResult1 = driver.GetRunResult();
|
||||
|
||||
var change = applyChanges(driver, compilation);
|
||||
var driver2 = change.driver.RunGenerators(change.compilation);
|
||||
GeneratorDriverRunResult runResult2 = driver2.GetRunResult();
|
||||
|
||||
return (runResult1, runResult2);
|
||||
}
|
||||
|
||||
public static Compilation CreateMauiCompilation()
|
||||
{
|
||||
var name = $"{nameof(SourceGeneratorDriver)}.Generated";
|
||||
var references = GetMauiReferences();
|
||||
|
||||
var compilation = CSharpCompilation.Create(name,
|
||||
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
|
||||
references: references);
|
||||
|
||||
return compilation;
|
||||
}
|
||||
|
||||
public static AdditionalText ToAdditionalText(string path, string text) => CustomAdditionalText.From(path, text);
|
||||
|
||||
private static MetadataReference[] GetMauiReferences()
|
||||
{
|
||||
if (MauiReferences == null)
|
||||
{
|
||||
MauiReferences = new[]
|
||||
{
|
||||
MetadataReference.CreateFromFile(typeof(InternalsVisibleToAttribute).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(Color).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(Button).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(BindingExtension).Assembly.Location),
|
||||
};
|
||||
}
|
||||
return MauiReferences;
|
||||
}
|
||||
|
||||
private class CustomAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider
|
||||
{
|
||||
private readonly IImmutableDictionary<string, AdditionalFile> _additionalFiles;
|
||||
|
||||
public CustomAnalyzerConfigOptionsProvider(AdditionalFile[] additionalFiles)
|
||||
{
|
||||
_additionalFiles = additionalFiles.ToImmutableDictionary(f => f.Text.Path, f => f);
|
||||
}
|
||||
|
||||
public override AnalyzerConfigOptions GlobalOptions => throw new System.NotImplementedException();
|
||||
|
||||
public override AnalyzerConfigOptions GetOptions(SyntaxTree tree)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public override AnalyzerConfigOptions GetOptions(AdditionalText textFile)
|
||||
{
|
||||
return _additionalFiles.TryGetValue(textFile.Path, out var additionalFile)
|
||||
? (AnalyzerConfigOptions)new CustomAnalyzerConfigOptions(additionalFile)
|
||||
: CustomAnalyzerConfigOptions.Empty;
|
||||
}
|
||||
|
||||
private class CustomAnalyzerConfigOptions : AnalyzerConfigOptions
|
||||
{
|
||||
readonly AdditionalFile? _additionalFile;
|
||||
|
||||
public CustomAnalyzerConfigOptions(AdditionalFile? additionalFile)
|
||||
{
|
||||
_additionalFile = additionalFile;
|
||||
}
|
||||
|
||||
public static AnalyzerConfigOptions Empty { get; } = new CustomAnalyzerConfigOptions(null);
|
||||
|
||||
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value)
|
||||
{
|
||||
if (_additionalFile == null)
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
value = key switch
|
||||
{
|
||||
"build_metadata.additionalfiles.GenKind" => _additionalFile.Kind,
|
||||
"build_metadata.additionalfiles.TargetPath" => _additionalFile.TargetPath,
|
||||
"build_metadata.additionalfiles.ManifestResourceName" => _additionalFile.ManifestResourceName,
|
||||
"build_metadata.additionalfiles.RelativePath" => _additionalFile.RelativePath,
|
||||
"build_property.targetframework" => _additionalFile.TargetFramework,
|
||||
_ => null
|
||||
};
|
||||
|
||||
return value is not null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class CustomAdditionalText : AdditionalText
|
||||
{
|
||||
private readonly SourceText _sourceText;
|
||||
|
||||
public static AdditionalText From(string path, string content)
|
||||
{
|
||||
return new CustomAdditionalText(path, content);
|
||||
}
|
||||
|
||||
private CustomAdditionalText(string path, string content)
|
||||
{
|
||||
Path = path;
|
||||
_sourceText = SourceText.From(content);
|
||||
}
|
||||
|
||||
public override string Path { get; }
|
||||
|
||||
public override SourceText GetText(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _sourceText;
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче