forked from AvaloniaUI/Avalonia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Avalonia.CssSrcGen with source generators and examples
Introduced a new project `Avalonia.CssSrcGen` to demonstrate Roslyn source generators. Added sample generators, tests, and examples for incremental and non-incremental generators. Integrated it with existing projects, along with supporting assets like launch settings and documentation.
- Loading branch information
wdcossey
committed
Dec 29, 2024
1 parent
07f3ad2
commit 9320583
Showing
17 changed files
with
525 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Avalonia.CssSrcGen/Avalonia.CssSrcGen.Sample/Avalonia.CssSrcGen.Sample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<RootNamespace>Avalonia.CssSrcGen.Sample</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Avalonia.CssSrcGen\Avalonia.CssSrcGen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="DDD.UbiquitousLanguageRegistry.txt"/> | ||
<AdditionalFiles Include="DDD.UbiquitousLanguageRegistry.txt"/> | ||
</ItemGroup> | ||
|
||
</Project> |
5 changes: 5 additions & 0 deletions
5
src/Avalonia.CssSrcGen/Avalonia.CssSrcGen.Sample/DDD.UbiquitousLanguageRegistry.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Customer | ||
Product | ||
Stock | ||
Shop | ||
Employee |
21 changes: 21 additions & 0 deletions
21
src/Avalonia.CssSrcGen/Avalonia.CssSrcGen.Sample/Examples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Collections.Generic; | ||
using Entities; | ||
|
||
namespace Avalonia.CssSrcGen.Sample; | ||
|
||
// This code will not compile until you build the project with the Source Generators | ||
|
||
public class Examples | ||
{ | ||
// Create generated entities, based on DDD.UbiquitousLanguageRegistry.txt | ||
public object[] CreateEntities() | ||
{ | ||
return new object[] { new Customer(), new Employee(), new Product(), new Shop(), new Stock() }; | ||
} | ||
|
||
// Execute generated method Report | ||
public IEnumerable<string> CreateEntityReport(SampleEntity entity) | ||
{ | ||
return entity.Report(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Avalonia.CssSrcGen/Avalonia.CssSrcGen.Sample/SampleEntity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Generators; | ||
|
||
namespace Avalonia.CssSrcGen.Sample; | ||
|
||
// This code will not compile until you build the project with the Source Generators | ||
|
||
[Report] | ||
public partial class SampleEntity | ||
{ | ||
public int Id { get; } = 42; | ||
public string? Name { get; } = "Sample"; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Avalonia.CssSrcGen/Avalonia.CssSrcGen.Tests/Avalonia.CssSrcGen.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
|
||
<RootNamespace>Avalonia.CssSrcGen.Tests</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.1"/> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2"/> | ||
<PackageReference Include="xunit" Version="2.4.2"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Avalonia.CssSrcGen\Avalonia.CssSrcGen.csproj"/> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
46 changes: 46 additions & 0 deletions
46
src/Avalonia.CssSrcGen/Avalonia.CssSrcGen.Tests/SourceGeneratorWithAdditionalFilesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Collections.Immutable; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Avalonia.CssSrcGen.Tests.Utils; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Xunit; | ||
|
||
namespace Avalonia.CssSrcGen.Tests; | ||
|
||
public class SourceGeneratorWithAdditionalFilesTests | ||
{ | ||
private const string DddRegistryText = @"User | ||
Document | ||
Customer"; | ||
|
||
[Fact] | ||
public void GenerateClassesBasedOnDDDRegistry() | ||
{ | ||
// Create an instance of the source generator. | ||
var generator = new SourceGeneratorWithAdditionalFiles(); | ||
|
||
// Source generators should be tested using 'GeneratorDriver'. | ||
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); | ||
|
||
// Add the additional file separately from the compilation. | ||
driver = driver.AddAdditionalTexts( | ||
ImmutableArray.Create<AdditionalText>( | ||
new TestAdditionalFile("./DDD.UbiquitousLanguageRegistry.txt", DddRegistryText)) | ||
); | ||
|
||
// To run generators, we can use an empty compilation. | ||
var compilation = CSharpCompilation.Create(nameof(SourceGeneratorWithAdditionalFilesTests)); | ||
|
||
// Run generators. Don't forget to use the new compilation rather than the previous one. | ||
driver.RunGeneratorsAndUpdateCompilation(compilation, out var newCompilation, out _); | ||
|
||
// Retrieve all files in the compilation. | ||
var generatedFiles = newCompilation.SyntaxTrees | ||
.Select(t => Path.GetFileName(t.FilePath)) | ||
.ToArray(); | ||
|
||
// In this case, it is enough to check the file name. | ||
Assert.Equivalent(new[] { "User.g.cs", "Document.g.cs", "Customer.g.cs" }, generatedFiles); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Avalonia.CssSrcGen/Avalonia.CssSrcGen.Tests/SourceGeneratorWithAttributesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Xunit; | ||
|
||
namespace Avalonia.CssSrcGen.Tests; | ||
|
||
public class SourceGeneratorWithAttributesTests | ||
{ | ||
private const string VectorClassText = @" | ||
namespace TestNamespace; | ||
[Generators.Report] | ||
public partial class Vector3 | ||
{ | ||
public float X { get; set; } | ||
public float Y { get; set; } | ||
public float Z { get; set; } | ||
}"; | ||
|
||
private const string ExpectedGeneratedClassText = @"// <auto-generated/> | ||
using System; | ||
using System.Collections.Generic; | ||
namespace TestNamespace; | ||
partial class Vector3 | ||
{ | ||
public IEnumerable<string> Report() | ||
{ | ||
yield return $""X:{this.X}""; | ||
yield return $""Y:{this.Y}""; | ||
yield return $""Z:{this.Z}""; | ||
} | ||
} | ||
"; | ||
|
||
[Fact] | ||
public void GenerateReportMethod() | ||
{ | ||
// Create an instance of the source generator. | ||
var generator = new SourceGeneratorWithAttributes(); | ||
|
||
// Source generators should be tested using 'GeneratorDriver'. | ||
var driver = CSharpGeneratorDriver.Create(generator); | ||
|
||
// We need to create a compilation with the required source code. | ||
var compilation = CSharpCompilation.Create(nameof(SourceGeneratorWithAdditionalFilesTests), | ||
new[] { CSharpSyntaxTree.ParseText(VectorClassText) }, | ||
new[] | ||
{ | ||
// To support 'System.Attribute' inheritance, add reference to 'System.Private.CoreLib'. | ||
MetadataReference.CreateFromFile(typeof(object).Assembly.Location) | ||
}); | ||
|
||
// Run generators and retrieve all results. | ||
var runResult = driver.RunGenerators(compilation).GetRunResult(); | ||
|
||
// All generated files can be found in 'RunResults.GeneratedTrees'. | ||
var generatedFileSyntax = runResult.GeneratedTrees.Single(t => t.FilePath.EndsWith("Vector3.g.cs")); | ||
|
||
// Complex generators should be tested using text comparison. | ||
Assert.Equal(ExpectedGeneratedClassText, generatedFileSyntax.GetText().ToString(), | ||
ignoreLineEndingDifferences: true); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Avalonia.CssSrcGen/Avalonia.CssSrcGen.Tests/Utils/TestAdditionalFile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Avalonia.CssSrcGen.Tests.Utils; | ||
|
||
public class TestAdditionalFile : AdditionalText | ||
{ | ||
private readonly SourceText _text; | ||
|
||
public TestAdditionalFile(string path, string text) | ||
{ | ||
Path = path; | ||
_text = SourceText.From(text); | ||
} | ||
|
||
public override SourceText GetText(CancellationToken cancellationToken = new()) => _text; | ||
|
||
public override string Path { get; } | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Avalonia.CssSrcGen/Avalonia.CssSrcGen/Avalonia.CssSrcGen.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>latest</LangVersion> | ||
|
||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
<IsRoslynComponent>true</IsRoslynComponent> | ||
|
||
<RootNamespace>Avalonia.CssSrcGen</RootNamespace> | ||
<PackageId>Avalonia.CssSrcGen</PackageId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0"/> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.0"/> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
9 changes: 9 additions & 0 deletions
9
src/Avalonia.CssSrcGen/Avalonia.CssSrcGen/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"DebugRoslynSourceGenerator": { | ||
"commandName": "DebugRoslynComponent", | ||
"targetProject": "../Avalonia.CssSrcGen.Sample/Avalonia.CssSrcGen.Sample.csproj" | ||
} | ||
} | ||
} |
Oops, something went wrong.