-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,29 +20,41 @@ public class SourceGenerator : IIncrementalGenerator | |
const string AttributeValueMetadataName = "AutoGen"; | ||
|
||
|
||
|
||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
|
||
var nodes = context.SyntaxProvider.ForAttributeWithMetadataName( | ||
AttributeMetadataName, | ||
(context, attributeSyntax) => true, | ||
(syntaxContext, _) => syntaxContext.TargetNode).Collect(); | ||
|
||
|
||
IncrementalValueProvider<(Compilation, ImmutableArray<SyntaxNode>)> compilationAndTypes = | ||
context.CompilationProvider.Combine(nodes); | ||
|
||
context.RegisterSourceOutput(compilationAndTypes, static (spc, source) => HandleAnnotatedNodes(source.Item1, source.Item2, spc)); | ||
|
||
} | ||
|
||
/// <summary> | ||
/// 无法生成类的错误 | ||
/// </summary> | ||
#pragma warning disable RS2008 // 启用分析器发布跟踪 | ||
private static readonly DiagnosticDescriptor InvalidDeclareError = new(id: "GEN001", | ||
#pragma warning restore RS2008 // 启用分析器发布跟踪 | ||
title: "来自Biwen.AutoClassGen的报错信息", | ||
#pragma warning disable RS1032 // 正确定义诊断消息 | ||
messageFormat: "没有实现基础接口因此不能生成类,请删除标注的特性[AutoGen] or 继承相应的接口.", | ||
#pragma warning restore RS1032 // 正确定义诊断消息 | ||
category: typeof(SourceGenerator).Assembly.GetName().Name, | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true); | ||
|
||
private static void HandleAnnotatedNodes(Compilation compilation, ImmutableArray<SyntaxNode> nodes, SourceProductionContext context) | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.AppendLine("// <auto-generated />"); | ||
sb.AppendLine("// Path: Biwen.AutoClassGen.Gen/SourceGenerator.cs"); | ||
sb.AppendLine("// author :[email protected] 万雅虎"); | ||
sb.AppendLine("// 如果你在使用中遇到问题,请第一时间issue,谢谢!"); | ||
sb.AppendLine("// author:[email protected] 万雅虎"); | ||
sb.AppendLine("// issue:https://github.com/vipwan/Biwen.AutoClassGen/issues 如果你在使用中遇到问题,请第一时间issue,谢谢!"); | ||
sb.AppendLine("// This file is generated by Biwen.AutoClassGen.SourceGenerator"); | ||
sb.AppendLine(); | ||
sb.AppendLine("using System;"); | ||
|
@@ -51,27 +63,32 @@ private static void HandleAnnotatedNodes(Compilation compilation, ImmutableArray | |
sb.AppendLine("using System.ComponentModel.DataAnnotations;"); | ||
sb.AppendLine("using System.Text;"); | ||
sb.AppendLine("using System.Threading.Tasks;"); | ||
sb.AppendLine("#pragma warning disable"); | ||
|
||
sb.AppendLine(); | ||
sb.AppendLine("$namespace"); | ||
|
||
sb.AppendLine(); | ||
|
||
sb.AppendLine("#pragma warning disable"); | ||
sb.AppendLine("namespace $ni"); | ||
sb.AppendLine("{"); | ||
sb.AppendLine("\t$classes"); | ||
sb.AppendLine("$classes"); | ||
sb.AppendLine("}"); | ||
sb.AppendLine("#pragma warning restore"); | ||
|
||
string classTemp = $"public partial class $className : $interfaceName {{ $body }}"; | ||
|
||
|
||
foreach (InterfaceDeclarationSyntax node in nodes.AsEnumerable().Cast<InterfaceDeclarationSyntax>()) | ||
{ | ||
if (node.BaseList == null) | ||
{ | ||
continue; | ||
} | ||
if (!node.BaseList.Types.Any()) | ||
|
||
if (node.BaseList == null || !node.BaseList.Types.Any()) | ||
{ | ||
var type = compilation.GetSymbolsWithName(node.Identifier.ValueText).AsEnumerable().FirstOrDefault(); | ||
var path = type.Locations[0].ToString(); | ||
|
||
// issue error | ||
context.ReportDiagnostic(Diagnostic.Create(InvalidDeclareError, type.Locations[0])); | ||
|
||
continue; | ||
} | ||
|
||
|