-
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.
Merge pull request #1 from kandreyc/task/filter-system
Filter System generator
- Loading branch information
Showing
48 changed files
with
1,056 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
global using System.Linq; | ||
global using System.Threading; | ||
|
||
global using Microsoft.CodeAnalysis; | ||
global using Microsoft.CodeAnalysis.CSharp; | ||
global using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
global using Scellecs.Morpeh.SourceGenerator.Core; | ||
global using Scellecs.Morpeh.SourceGenerator.Core.Extensions; | ||
|
||
global using Scellecs.Morpeh.SourceGenerator.AspectGenerator.Model; | ||
global using Scellecs.Morpeh.SourceGenerator.AspectGenerator.Templates; |
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
15 changes: 7 additions & 8 deletions
15
AspectGenerator/Templates/AspectFilterExtensionsTemplate.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects> | ||
<HasSharedItems>true</HasSharedItems> | ||
<SharedGUID>E85D9420-616C-4636-A5C8-1181428E4C57</SharedGUID> | ||
</PropertyGroup> | ||
<PropertyGroup Label="Configuration"> | ||
<Import_RootNamespace>Core</Import_RootNamespace> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\AttributeDataExtensions.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\INamespaceSymbolExtensions.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ITypeSymbolExtensions.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\StringExtensions.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)GeneratedFile.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)IncrementalGenerator.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)ISourceFileGenerator.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Logger\Logger.cs" /> | ||
</ItemGroup> | ||
</Project> |
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,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>{B2A951FB-4BAD-4AA7-8986-A291B103D443}</ProjectGuid> | ||
<AssemblyName>Scellecs.Morpeh.SourceGenerator.Core</AssemblyName> | ||
<RootNamespace>Scellecs.Morpeh.SourceGenerator.Core</RootNamespace> | ||
</PropertyGroup> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" /> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" /> | ||
<Import Project="Core.projitems" Label="Shared" /> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" /> | ||
</Project> |
4 changes: 1 addition & 3 deletions
4
...ore/Extensions/AttributeDataExtensions.cs → Core/Extensions/AttributeDataExtensions.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
4 changes: 1 addition & 3 deletions
4
.../Extensions/INamespaceSymbolExtensions.cs → .../Extensions/INamespaceSymbolExtensions.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
4 changes: 1 addition & 3 deletions
4
.../Core/Extensions/ITypeSymbolExtensions.cs → Core/Extensions/ITypeSymbolExtensions.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
2 changes: 1 addition & 1 deletion
2
...rator/Core/Extensions/StringExtensions.cs → Core/Extensions/StringExtensions.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
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
2 changes: 1 addition & 1 deletion
2
AspectGenerator/Core/ISourceFileGenerator.cs → Core/ISourceFileGenerator.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
namespace Scellecs.Morpeh.SourceGenerator.FilterSystemGenerator; | ||
|
||
[Generator] | ||
public class FilterSystemGenerator : IncrementalGenerator<SystemInfo> | ||
{ | ||
private const string MethodName = "GetFilter"; | ||
private const string DisposableInterface = "System.IDisposable"; | ||
private const string AspectInterface = "Scellecs.Morpeh.IAspect"; | ||
private const string FilterBuilderType = "Scellecs.Morpeh.FilterBuilder"; | ||
private const string FilterSystemType = "Scellecs.Morpeh.SourceGenerator.FilterSystemGenerator.FilterSystem"; | ||
|
||
private static readonly HashSet<string> WithMethodNames = new() { "With", "Extend", "WithAspect" }; | ||
|
||
protected override void OnPostInitialize() | ||
{ | ||
AddPostInitializeSource(FilterSystemTemplate.GenerateFile()); | ||
} | ||
|
||
protected override bool SyntaxFilter(SyntaxNode node, CancellationToken _) | ||
{ | ||
return node is MethodDeclarationSyntax { Parent: ClassDeclarationSyntax classDeclarationSyntax } | ||
&& classDeclarationSyntax.Modifiers.Any(SyntaxKind.PartialKeyword) | ||
&& !classDeclarationSyntax.Modifiers.Any(SyntaxKind.AbstractKeyword); | ||
} | ||
|
||
protected override bool Filter(ISymbol? symbol) | ||
{ | ||
|
||
return symbol is IMethodSymbol methodSymbol | ||
&& methodSymbol.ContainingType?.BaseType?.ToDisplayString() is FilterSystemType | ||
&& IsGetFilterMethod(methodSymbol); | ||
} | ||
|
||
private static bool IsGetFilterMethod(IMethodSymbol method) | ||
{ | ||
|
||
return method is { Name: MethodName, Parameters.Length: 1 } | ||
&& method.ReturnType.ToDisplayString() is FilterBuilderType | ||
&& method.Parameters[0].Type.ToDisplayString() is FilterBuilderType; | ||
} | ||
|
||
protected override SystemInfo Select(SyntaxNode methodSyntax, ISymbol symbol, SemanticModel model) | ||
{ | ||
var methodSymbol = (IMethodSymbol)symbol; | ||
var @class = methodSymbol.ContainingType; | ||
|
||
return new SystemInfo | ||
{ | ||
Name = @class.Name, | ||
Namespace = @class.GetNamespaceName(), | ||
Filters = GetFilters(methodSyntax, model).ToArray() | ||
}; | ||
} | ||
|
||
private static IEnumerable<FilterInfo> GetFilters(SyntaxNode methodSyntax, SemanticModel model) | ||
{ | ||
foreach (var componentType in GetAllTypeArgumentsWithIdentifier(WithMethodNames, methodSyntax, model)) | ||
{ | ||
yield return new FilterInfo | ||
{ | ||
NamePascalCase = componentType.Name, | ||
NameCamelCase = componentType.Name.FirstLetterLowerCase(), | ||
Namespace = componentType.GetNamespaceName(), | ||
IsAspect = componentType.AllInterfaces.Any(i => i.ToDisplayString() is AspectInterface), | ||
IsDisposable = componentType.AllInterfaces.Any(i => i.ToDisplayString() is DisposableInterface) | ||
}; | ||
} | ||
} | ||
|
||
private static IEnumerable<ITypeSymbol> GetAllTypeArgumentsWithIdentifier(HashSet<string> identifiers, SyntaxNode methodSyntax, SemanticModel model) | ||
{ | ||
foreach (var syntax in methodSyntax.DescendantNodes()) | ||
{ | ||
if (syntax is not GenericNameSyntax genericNameSyntax) continue; | ||
if (!identifiers.Contains(genericNameSyntax.Identifier.Text)) continue; | ||
if (genericNameSyntax.TypeArgumentList.Arguments.Count is not 1) continue; | ||
|
||
var typeArgument = genericNameSyntax.TypeArgumentList.Arguments[0]; | ||
var type = model.GetTypeInfo(typeArgument).Type; | ||
|
||
if (type is not null) | ||
{ | ||
yield return type; | ||
} | ||
} | ||
} | ||
|
||
protected override void Generate(SystemInfo systemInfo) | ||
{ | ||
AddSource(PartialImplTemplate.GenerateFile(systemInfo)); | ||
} | ||
} |
Oops, something went wrong.