Skip to content

Commit 24163b9

Browse files
committed
NEW: Base classes/helpers to create project specific source generators
1 parent 44a16d3 commit 24163b9

59 files changed

Lines changed: 314 additions & 186 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<LangVersion>preview</LangVersion>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<PreserveCompilationContext>true</PreserveCompilationContext>
6+
</PropertyGroup>
7+
<PropertyGroup>
8+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
9+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<PackageReference Include="GodotSharp.SourceGenerators" Version="1.1.0" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
namespace CustomGeneratorTests
4+
{
5+
[AttributeUsage(AttributeTargets.Class)]
6+
public class MyClassAttribute : Attribute { }
7+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using GodotSharp.SourceGenerators;
3+
using Microsoft.CodeAnalysis;
4+
5+
namespace CustomGeneratorTests
6+
{
7+
[Generator]
8+
internal class MyClassAttributeGenerator : SourceGeneratorForDeclaredTypesWithAttribute<MyClassAttribute>
9+
{
10+
protected override (string GeneratedCode, DiagnosticDetail Error) GenerateCode(Compilation compilation, INamedTypeSymbol symbol, AttributeData attribute)
11+
{
12+
var content = symbol.GeneratePartialClass(Content(), Usings());
13+
return (content, null);
14+
15+
static IEnumerable<string> Content()
16+
{
17+
yield return "private void MyClassAttributeGeneratedThisMethod()";
18+
yield return "{";
19+
yield return "}";
20+
}
21+
22+
static IEnumerable<string> Usings()
23+
{
24+
yield return "using System;";
25+
}
26+
}
27+
}
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
namespace CustomGeneratorTests
4+
{
5+
[AttributeUsage(AttributeTargets.Method)]
6+
public class MyMethodAttribute : Attribute { }
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
using GodotSharp.SourceGenerators;
3+
using Microsoft.CodeAnalysis;
4+
5+
namespace CustomGeneratorTests
6+
{
7+
[Generator]
8+
internal class MyMethodAttributeGenerator : SourceGeneratorForDeclaredMethodsWithAttribute<MyMethodAttribute>
9+
{
10+
protected override (string GeneratedCode, DiagnosticDetail Error) GenerateCode(Compilation compilation, IMethodSymbol symbol, AttributeData attribute)
11+
{
12+
var content = symbol.ContainingType.GeneratePartialClass(Content());
13+
return (content, null);
14+
15+
static IEnumerable<string> Content()
16+
{
17+
yield return "private void MyMethodAttributeGeneratedThisMethod()";
18+
yield return "{";
19+
yield return "}";
20+
}
21+
}
22+
}
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
using CustomGeneratorTests;
3+
4+
namespace GodotTests.CustomGeneratorTests
5+
{
6+
[MyClass]
7+
public partial class MyTestClass<T1>
8+
{
9+
[MyMethod]
10+
public IEnumerable<(decimal? D, T2 T)?> MyTestMethod<T2>(ref float? _, out IEnumerable<int?> __)
11+
{
12+
// If these compile, test passes
13+
MyClassAttributeGeneratedThisMethod();
14+
MyMethodAttributeGeneratedThisMethod();
15+
16+
__ = null;
17+
return null;
18+
}
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
using CustomGeneratorTests;
3+
4+
[MyClass]
5+
public partial class MyTestClass<T1>
6+
{
7+
[MyMethod]
8+
public IEnumerable<(decimal? D, T2 T)?> MyTestMethod<T2>(ref float? _, out IEnumerable<int?> __)
9+
{
10+
// If these compile, test passes
11+
MyClassAttributeGeneratedThisMethod();
12+
MyMethodAttributeGeneratedThisMethod();
13+
14+
__ = null;
15+
return null;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<Project Sdk="Godot.NET.Sdk/4.0.0-dev3">
22
<PropertyGroup>
33
<LangVersion>preview</LangVersion>
4-
<RootNamespace>GodotTests</RootNamespace>
54
<TargetFramework>netstandard2.1</TargetFramework>
65
</PropertyGroup>
76
<PropertyGroup>
@@ -13,8 +12,11 @@
1312
<None Include="**\*.tscn" />
1413
</ItemGroup>
1514
<ItemGroup>
16-
<PackageReference Include="FluentAssertions" Version="5.10.3" />
15+
<PackageReference Include="FluentAssertions" Version="6.0.0-alpha0002" />
1716
<PackageReference Include="FluentAssertions.Analyzers" Version="0.11.4" />
18-
<PackageReference Include="GodotSharp.SourceGenerators" Version="1.0.0" />
17+
<PackageReference Include="GodotSharp.SourceGenerators" Version="1.1.0" />
1918
</ItemGroup>
20-
</Project>
19+
<ItemGroup>
20+
<ProjectReference Include="..\CustomGeneratorTests\CustomGeneratorTests.csproj" OutputItemType="analyzer" />
21+
</ItemGroup>
22+
</Project>
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.30907.101
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Godot.Tests", "Godot.Tests.csproj", "{8D6FD73B-6457-4D5B-AA02-DC26FBAB1999}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GodotTests", "GodotTests.csproj", "{8D6FD73B-6457-4D5B-AA02-DC26FBAB1999}"
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5BD6ADE4-7B2D-4E76-AC42-AB68BB1E1931}"
99
ProjectSection(SolutionItems) = preProject
@@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1313
project.godot = project.godot
1414
EndProjectSection
1515
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomGeneratorTests", "..\CustomGeneratorTests\CustomGeneratorTests.csproj", "{16EEA1DE-64CA-4778-9DA8-8499F5FE5464}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -29,6 +31,14 @@ Global
2931
{8D6FD73B-6457-4D5B-AA02-DC26FBAB1999}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU
3032
{8D6FD73B-6457-4D5B-AA02-DC26FBAB1999}.Release|Any CPU.ActiveCfg = ExportRelease|Any CPU
3133
{8D6FD73B-6457-4D5B-AA02-DC26FBAB1999}.Release|Any CPU.Build.0 = ExportRelease|Any CPU
34+
{16EEA1DE-64CA-4778-9DA8-8499F5FE5464}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{16EEA1DE-64CA-4778-9DA8-8499F5FE5464}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{16EEA1DE-64CA-4778-9DA8-8499F5FE5464}.ExportDebug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{16EEA1DE-64CA-4778-9DA8-8499F5FE5464}.ExportDebug|Any CPU.Build.0 = Debug|Any CPU
38+
{16EEA1DE-64CA-4778-9DA8-8499F5FE5464}.ExportRelease|Any CPU.ActiveCfg = Release|Any CPU
39+
{16EEA1DE-64CA-4778-9DA8-8499F5FE5464}.ExportRelease|Any CPU.Build.0 = Release|Any CPU
40+
{16EEA1DE-64CA-4778-9DA8-8499F5FE5464}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{16EEA1DE-64CA-4778-9DA8-8499F5FE5464}.Release|Any CPU.Build.0 = Release|Any CPU
3242
EndGlobalSection
3343
GlobalSection(SolutionProperties) = preSolution
3444
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)