- tweak: Some view change

This commit is contained in:
2025-01-15 19:47:27 +03:00
parent 08e518602b
commit 5d7f443532
10 changed files with 143 additions and 64 deletions

View File

@@ -11,18 +11,15 @@ namespace Nebula.SourceGenerators;
[Generator]
public class DependencyAutoGenerator : IIncrementalGenerator
{
private static readonly string ConstructGeneratorAttributeName = "ConstructGeneratorAttribute";
private static readonly string GeneratePropertyAttributeName = "GeneratePropertyAttribute";
private static readonly string ConstructGeneratorAttributeName = "Nebula.Shared.Attributes.ConstructGeneratorAttribute";
private static readonly string GeneratePropertyAttributeName = "Nebula.Shared.Attributes.GeneratePropertyAttribute";
public void Initialize(IncrementalGeneratorInitializationContext context)
{
GenerateAttribute(ConstructGeneratorAttributeName, "Class", context);
GenerateAttribute(GeneratePropertyAttributeName, "Property", context);
var provider = context.SyntaxProvider
.CreateSyntaxProvider(
(s, _) => s is ClassDeclarationSyntax,
(ctx, _) => GetClassDeclarationForSourceGen(ctx))
(ctx, _) => SourceHelper.GetClassDeclarationForSourceGen(ctx,ConstructGeneratorAttributeName))
.Where(t => t.reportAttributeFound)
.Select((t, _) => t.Item1);
@@ -30,44 +27,6 @@ public class DependencyAutoGenerator : IIncrementalGenerator
(ctx, t) => GenerateCode(ctx, t.Left, t.Right));
}
private static void GenerateAttribute(string attributeName, string usage,
IncrementalGeneratorInitializationContext context)
{
var attributeSourceCode = $@"// <auto-generated/>
namespace SourceGen
{{
[System.AttributeUsage(System.AttributeTargets.{usage})]
public class {attributeName} : System.Attribute
{{
}}
}}";
context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
$"{attributeName}.g.cs",
SourceText.From(attributeSourceCode, Encoding.UTF8)));
}
private static (ClassDeclarationSyntax, bool reportAttributeFound) GetClassDeclarationForSourceGen(
GeneratorSyntaxContext context)
{
var classDeclarationSyntax = (ClassDeclarationSyntax)context.Node;
foreach (var attributeListSyntax in classDeclarationSyntax.AttributeLists)
foreach (var attributeSyntax in attributeListSyntax.Attributes)
{
if (context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol is not IMethodSymbol attributeSymbol)
continue;
var attributeName = attributeSymbol.ContainingType.ToDisplayString();
if (attributeName == $"SourceGen.{ConstructGeneratorAttributeName}")
return (classDeclarationSyntax, true);
}
return (classDeclarationSyntax, false);
}
private void GenerateCode(SourceProductionContext context, Compilation compilation,
ImmutableArray<ClassDeclarationSyntax> classDeclarations)
{
@@ -118,15 +77,6 @@ partial class {className}
private static IEnumerable<IPropertySymbol> GetProperties(INamedTypeSymbol classSymbol)
{
return classSymbol.GetMembers().OfType<IPropertySymbol>().Where(a =>
HasAttribute(a, $"SourceGen.{GeneratePropertyAttributeName}"));
}
private static bool HasAttribute(ISymbol type, string attributeName)
{
foreach (var attribute in type.GetAttributes())
if (attribute.AttributeClass?.ToDisplayString() == attributeName)
return true;
return false;
SourceHelper.HasAttribute(a, GeneratePropertyAttributeName));
}
}

View File

@@ -0,0 +1,56 @@
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace Nebula.SourceGenerators;
public static class SourceHelper
{
public static void GenerateAttribute(string attributeName, string usage,
IncrementalGeneratorInitializationContext context)
{
var attributeSourceCode = $@"// <auto-generated/>
namespace SourceGen
{{
[System.AttributeUsage(System.AttributeTargets.{usage})]
public class {attributeName} : System.Attribute
{{
}}
}}";
context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
$"{attributeName}.g.cs",
SourceText.From(attributeSourceCode, Encoding.UTF8)));
}
public static bool HasAttribute(ISymbol type, string attributeName)
{
foreach (var attribute in type.GetAttributes())
if (attribute.AttributeClass?.ToDisplayString() == attributeName)
return true;
return false;
}
public static (ClassDeclarationSyntax, bool reportAttributeFound) GetClassDeclarationForSourceGen(
GeneratorSyntaxContext context, string AttributeName)
{
var classDeclarationSyntax = (ClassDeclarationSyntax)context.Node;
foreach (var attributeListSyntax in classDeclarationSyntax.AttributeLists)
foreach (var attributeSyntax in attributeListSyntax.Attributes)
{
if (context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol is not IMethodSymbol attributeSymbol)
continue;
var attributeName = attributeSymbol.ContainingType.ToDisplayString();
if (attributeName == AttributeName)
return (classDeclarationSyntax, true);
}
return (classDeclarationSyntax, false);
}
}

View File

@@ -0,0 +1,58 @@
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace Nebula.SourceGenerators;
//[Generator]
public class ViewConstructGenerator : IIncrementalGenerator
{
public static string ViewForModelAttributeName = "ViewForModelAttribute";
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var provider = context.SyntaxProvider
.CreateSyntaxProvider(
(s, _) => s is ClassDeclarationSyntax, (ctx, _) =>
SourceHelper.GetClassDeclarationForSourceGen(ctx,ViewForModelAttributeName)
)
.Where(t => t.reportAttributeFound)
.Select((t,_) => t.Item1);
context.RegisterSourceOutput(context.CompilationProvider.Combine(provider.Collect()), (ctx,t)=> GenerateCode(ctx, t.Left, t.Right));
}
private void GenerateCode(SourceProductionContext context, Compilation compilation, ImmutableArray<ClassDeclarationSyntax> syntaxes)
{
foreach (var classDeclarationSyntax in syntaxes)
{
var semanticModel = compilation.GetSemanticModel(classDeclarationSyntax.SyntaxTree);
if(semanticModel.GetDeclaredSymbol(classDeclarationSyntax) is not INamedTypeSymbol classSymbol)
continue;
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
var className = classDeclarationSyntax.Identifier.Text;
var code = $@"// <auto-generated/>
using System;
using System.Collections.Generic;
namespace {namespaceName};
partial class {className}
{{
public {className}() : base(){{
}}
}}
";
// Add the source code to the compilation.
context.AddSource($"{className}.g.cs", SourceText.From(code, Encoding.UTF8));
}
}
}