SourceGen started

This commit is contained in:
ThomasCharester committed 2026-01-20 16:59:57 +03:00
1 parent a3cd3e86a1
commit a409054750
274 files changed
+14862 -3

No files matched your search

-3
View File
@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 07cf07a937934e0094469237b5865b60
timeCreated: 1766350980
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3d2c689e65afcd2498be16afac55fa0f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/contentModel.xml
/modules.xml
/projectSettingsUpdater.xml
/.idea.InfraGens.iml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
@@ -0,0 +1 @@
InfraGens
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
</component>
</project>
@@ -0,0 +1,5 @@
Customer
Product
Stock
Shop
Employee
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using Entities;
namespace InfraGens.Sample;
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();
}
}
@@ -0,0 +1,12 @@
using Generators;
namespace InfraGens.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";
}
@@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v8.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v8.0": {
"InfraGens.Sample/1.0.0": {
"runtime": {
"InfraGens.Sample.dll": {}
}
}
}
},
"libraries": {
"InfraGens.Sample/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}
@@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]
@@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("InfraGens.Sample")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+a3cd3e86a161e3ee450256b1b37c6a4583c70840")]
[assembly: System.Reflection.AssemblyProductAttribute("InfraGens.Sample")]
[assembly: System.Reflection.AssemblyTitleAttribute("InfraGens.Sample")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.
@@ -0,0 +1 @@
ff49a1d065da3858c1224dae22c001fae5c9d17e1737f7b26cef44c567a20d60
@@ -0,0 +1,13 @@
is_global = true
build_property.TargetFramework = net8.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = InfraGens.Sample
build_property.ProjectDir = A:\Projects\Infra\Assets\SourceGeneration\InfraGens~\InfraGens\InfraGens.Sample\
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =
@@ -0,0 +1 @@
5537530838be492c04f86b64a3b48c9b7673bfd8a4cff99fd1e4630f277aae64
@@ -0,0 +1,11 @@
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\bin\Debug\net8.0\InfraGens.Sample.deps.json
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\bin\Debug\net8.0\InfraGens.Sample.dll
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\bin\Debug\net8.0\InfraGens.Sample.pdb
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\obj\Debug\net8.0\InfraGens.Sample.GeneratedMSBuildEditorConfig.editorconfig
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\obj\Debug\net8.0\InfraGens.Sample.AssemblyInfoInputs.cache
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\obj\Debug\net8.0\InfraGens.Sample.AssemblyInfo.cs
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\obj\Debug\net8.0\InfraGens.Sample.csproj.CoreCompileInputs.cache
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\obj\Debug\net8.0\InfraGens.Sample.dll
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\obj\Debug\net8.0\refint\InfraGens.Sample.dll
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\obj\Debug\net8.0\InfraGens.Sample.pdb
A:\Projects\Infra\Assets\SourceGeneration\InfraGens\InfraGens\InfraGens.Sample\obj\Debug\net8.0\ref\InfraGens.Sample.dll
@@ -0,0 +1,66 @@
{
"format": 1,
"restore": {
"A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\InfraGens.Sample.csproj": {}
},
"projects": {
"A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\InfraGens.Sample.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\InfraGens.Sample.csproj",
"projectName": "InfraGens.Sample",
"projectPath": "A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\InfraGens.Sample.csproj",
"packagesPath": "C:\\Users\\Administrator\\.nuget\\packages\\",
"outputPath": "A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\obj\\",
"projectStyle": "PackageReference",
"configFilePaths": [
"C:\\Users\\Administrator\\AppData\\Roaming\\NuGet\\NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
},
"restoreAuditProperties": {
"enableAudit": "true",
"auditLevel": "low",
"auditMode": "direct"
}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.417/PortableRuntimeIdentifierGraph.json"
}
}
}
}
}
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Administrator\.nuget\packages\</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.10.1</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\Administrator\.nuget\packages\" />
</ItemGroup>
</Project>
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
@@ -0,0 +1,71 @@
{
"version": 3,
"targets": {
"net8.0": {}
},
"libraries": {},
"projectFileDependencyGroups": {
"net8.0": []
},
"packageFolders": {
"C:\\Users\\Administrator\\.nuget\\packages\\": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\InfraGens.Sample.csproj",
"projectName": "InfraGens.Sample",
"projectPath": "A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\InfraGens.Sample.csproj",
"packagesPath": "C:\\Users\\Administrator\\.nuget\\packages\\",
"outputPath": "A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\obj\\",
"projectStyle": "PackageReference",
"configFilePaths": [
"C:\\Users\\Administrator\\AppData\\Roaming\\NuGet\\NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
},
"restoreAuditProperties": {
"enableAudit": "true",
"auditLevel": "low",
"auditMode": "direct"
}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.417/PortableRuntimeIdentifierGraph.json"
}
}
}
}
@@ -0,0 +1,8 @@
{
"version": 2,
"dgSpecHash": "EVFRe6jvxS4=",
"success": true,
"projectFilePath": "A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\InfraGens.Sample.csproj",
"expectedPackageFiles": [],
"logs": []
}
@@ -0,0 +1 @@
"restore":{"projectUniqueName":"A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\InfraGens.Sample.csproj","projectName":"InfraGens.Sample","projectPath":"A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\InfraGens.Sample.csproj","outputPath":"A:\\Projects\\Infra\\Assets\\SourceGeneration\\InfraGens~\\InfraGens\\InfraGens.Sample\\obj\\","projectStyle":"PackageReference","originalTargetFrameworks":["net8.0"],"sources":{"https://api.nuget.org/v3/index.json":{}},"frameworks":{"net8.0":{"targetAlias":"net8.0","projectReferences":{}}},"warningProperties":{"warnAsError":["NU1605"]},"restoreAuditProperties":{"enableAudit":"true","auditLevel":"low","auditMode":"direct"}}"frameworks":{"net8.0":{"targetAlias":"net8.0","imports":["net461","net462","net47","net471","net472","net48","net481"],"assetTargetFallback":true,"warn":true,"frameworkReferences":{"Microsoft.NETCore.App":{"privateAssets":"all"}},"runtimeIdentifierGraphPath":"C:\\Program Files\\dotnet\\sdk\\8.0.417/PortableRuntimeIdentifierGraph.json"}}
@@ -0,0 +1,67 @@
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
namespace InfraGens.Tests;
public class SampleIncrementalSourceGeneratorTests
{
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 SampleIncrementalSourceGenerator();
// 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(SampleSourceGeneratorTests),
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);
}
}
@@ -0,0 +1,48 @@
using System.IO;
using System.Linq;
using InfraGens.Tests.Utils;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
namespace InfraGens.Tests;
public class SampleSourceGeneratorTests
{
private const string DddRegistryText = @"User
Document
Customer";
[Fact]
public void GenerateClassesBasedOnDDDRegistry()
{
// Create an instance of the source generator.
var generator = new SampleSourceGenerator();
// Source generators should be tested using 'GeneratorDriver'.
var driver = CSharpGeneratorDriver.Create(new[] { generator },
new[]
{
// Add the additional file separately from the compilation.
new TestAdditionalFile("./DDD.UbiquitousLanguageRegistry.txt", DddRegistryText)
});
// To run generators, we can use an empty compilation.
var compilation = CSharpCompilation.Create(nameof(SampleSourceGeneratorTests));
// 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);
}
}
@@ -0,0 +1,20 @@
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace InfraGens.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; }
}
@@ -0,0 +1,12 @@
{
"runtimeOptions": {
"tfm": "net8.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "8.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
Loaded 100 of 274 files, more files were not shown because too many files have changed in this diff. Show more