-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathAutoInjectSourceGenerator.cs
129 lines (106 loc) · 5.25 KB
/
AutoInjectSourceGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace Bit.SourceGenerators;
[Generator]
public class AutoInjectSourceGenerator : ISourceGenerator
{
private static int counter;
private static readonly DiagnosticDescriptor NonPartialClassError = new DiagnosticDescriptor(id: "BITGEN001",
title: "The class needs to be partial",
messageFormat: "{0} is not partial. The AutoInject attribute needs to be used only in partial classes.",
category: "Bit.SourceGenerators",
DiagnosticSeverity.Error,
isEnabledByDefault: true);
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new AutoInjectSyntaxReceiver());
}
public void Execute(GeneratorExecutionContext context)
{
if (context.SyntaxContextReceiver is not AutoInjectSyntaxReceiver receiver)
return;
INamedTypeSymbol? attributeSymbol = context.Compilation.GetTypeByMetadataName(AutoInjectHelper.AutoInjectAttributeFullName);
foreach (IGrouping<INamedTypeSymbol, ISymbol> group in receiver.EligibleMembers.GroupBy<ISymbol, INamedTypeSymbol>(f => f.ContainingType, SymbolEqualityComparer.Default))
{
if (IsClassIsPartial(context, group.Key) is false)
return;
string? partialClassSource = GenerateSource(attributeSymbol, group.Key, group.ToList());
if (string.IsNullOrEmpty(partialClassSource) is false)
{
context.AddSource($"{group.Key.Name}_{counter++}_autoInject.g.cs", SourceText.From(partialClassSource!, Encoding.UTF8));
}
}
foreach (var @class in receiver.EligibleClassesWithBaseClassUsedAutoInject)
{
if (IsClassIsPartial(context, @class) is false)
return;
if (IsClassIsPartial(context, @class.BaseType!) is false)
return;
string? partialClassSource = GenerateSource(attributeSymbol, @class, new List<ISymbol>());
if (string.IsNullOrEmpty(partialClassSource) is false)
{
context.AddSource($"{@class.Name}_{counter++}_autoInject.g.cs", SourceText.From(partialClassSource!, Encoding.UTF8));
}
}
}
private static bool IsClassIsPartial(GeneratorExecutionContext context, INamedTypeSymbol @class)
{
var syntaxReferences = @class.DeclaringSyntaxReferences;
foreach (var refrence in syntaxReferences)
{
var classDeclarationSyntax = (ClassDeclarationSyntax)refrence.GetSyntax();
var classHasPartial = classDeclarationSyntax.Modifiers.Any(o => o.IsKind(SyntaxKind.PartialKeyword));
if (classHasPartial is false)
{
context.ReportDiagnostic(Diagnostic.Create(NonPartialClassError, classDeclarationSyntax.GetLocation(), @class.Name));
return false;
}
}
return true;
}
private static string? GenerateSource(INamedTypeSymbol? attributeSymbol, INamedTypeSymbol? classSymbol, IReadOnlyCollection<ISymbol> eligibleMembers)
{
AutoInjectClassType env = FigureOutTypeOfEnvironment(classSymbol);
return env switch
{
AutoInjectClassType.NormalClass => AutoInjectNormalClassHandler.Generate(attributeSymbol, classSymbol, eligibleMembers),
AutoInjectClassType.RazorComponent => AutoInjectRazorComponentHandler.Generate(classSymbol, eligibleMembers),
_ => string.Empty
};
}
private static AutoInjectClassType FigureOutTypeOfEnvironment(INamedTypeSymbol? @class)
{
if (@class is null)
throw new ArgumentNullException(nameof(@class));
if (IsClassIsRazorComponent(@class))
return AutoInjectClassType.RazorComponent;
else
return AutoInjectClassType.NormalClass;
}
private static bool IsClassIsRazorComponent(INamedTypeSymbol @class)
{
bool isInheritIComponent = @class.AllInterfaces.Any(o => o.ToDisplayString() == "Microsoft.AspNetCore.Components.IComponent");
if (isInheritIComponent)
return true;
var classFilePaths = @class.Locations
.Where(o => o.SourceTree is not null)
.Select(o => o.SourceTree?.FilePath)
.ToList();
string razorFileName = $"{@class.Name}.razor";
foreach (var path in classFilePaths)
{
string directoryPath = Path.GetDirectoryName(path) ?? string.Empty;
string filePath = Path.Combine(directoryPath, razorFileName);
if (File.Exists(filePath))
return true;
}
return false;
}
}