-
Notifications
You must be signed in to change notification settings - Fork 447
/
Copy pathHttpApiSourceGenerator.cs
48 lines (45 loc) · 1.53 KB
/
HttpApiSourceGenerator.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
using Microsoft.CodeAnalysis;
using System.Linq;
using WebApiClientCore.Analyzers.SourceGenerator;
namespace WebApiClientCore.Analyzers
{
/// <summary>
/// HttpApi代码生成器
/// </summary>
[Generator]
public class HttpApiSourceGenerator : ISourceGenerator
{
/// <summary>
/// 初始化
/// </summary>
/// <param name="context"></param>
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new HttpApiSyntaxReceiver());
}
/// <summary>
/// 执行
/// </summary>
/// <param name="context"></param>
public void Execute(GeneratorExecutionContext context)
{
if (context.SyntaxReceiver is HttpApiSyntaxReceiver receiver)
{
// System.Diagnostics.Debugger.Launch();
var proxyClasses = receiver
.GetHttpApiTypes(context.Compilation)
.Select(i => new HttpApiProxyClass(i))
.Distinct()
.ToArray();
if (proxyClasses.Length > 0)
{
context.AddSource(HttpApiProxyClassInitializer.FileName, HttpApiProxyClassInitializer.ToSourceText());
foreach (var proxyClass in proxyClasses)
{
context.AddSource(proxyClass.FileName, proxyClass.ToSourceText());
}
}
}
}
}
}