forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsGenerator.cs
More file actions
108 lines (89 loc) · 4.09 KB
/
Copy pathSettingsGenerator.cs
File metadata and controls
108 lines (89 loc) · 4.09 KB
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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using System.Text;
namespace BD.WTTS.Generators;
[Generator]
public class SettingsGenerator : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
// retreive the populated receiver
if (!(context.SyntaxReceiver is SettingsGenerationReceiver receiver))
return;
var compilation = context.Compilation;
// loop over the candidate fields, and keep the ones that are actually annotated
var symbols = new List<ITypeSymbol>();
foreach (var decl in receiver.Candidates)
{
var model = compilation.GetSemanticModel(decl.SyntaxTree);
if (model.GetDeclaredSymbol(decl, context.CancellationToken) is ITypeSymbol symbol)
{
symbols.Add(symbol);
}
}
foreach (var symbol in symbols)
{
var code = new StringBuilder();
// Build up the source code
//string source = $@"// <auto-generated/>
//";
// 遍历 settings 类型的所有属性。
foreach (var field in symbol.GetMembers().OfType<IFieldSymbol>())
{
// 获取属性名和属性类型。
var propertyName = field.Name;
var propertyType = field.Type.ToDisplayString();
// 生成代码,打印属性值。
code.AppendLine($"// \"{propertyName}: {0}\", type:{propertyType}");
}
context.AddSource($"{symbol.Name}.g.cs", SourceText.From(code.ToString(), Encoding.UTF8));
}
//foreach (var syntaxTree in context.Compilation.SyntaxTrees)
//{
// var model = context.Compilation.GetSemanticModel(syntaxTree);
// var classDeclarations = syntaxTree.GetRoot()
// .DescendantNodes().OfType<ClassDeclarationSyntax>();
// foreach (var classDeclaration in classDeclarations)
// {
// var classSymbol = model.GetDeclaredSymbol(classDeclaration);
// if (classSymbol == null) continue;
// var attributeData = classSymbol.GetAttributes().FirstOrDefault(ad => ad.AttributeClass?.ToDisplayString() == SettingsGenerationReceiver.AttributeName);
// if (attributeData == null) continue;
// context.Compilation.GetTypeByMetadataName("YourNamespace.Settings");
// // 遍历 settings 类型的所有属性。
// foreach (var property in classSymbol.ContainingType.GetMembers().OfType<IPropertySymbol>())
// {
// // 获取属性名和属性类型。
// var propertyName = property.Name;
// var propertyType = property.Type.ToDisplayString();
// // 生成代码,打印属性值。
// var code = $"Console.WriteLine(\"{propertyName}: {{0}}\", settings.{propertyName});";
// context.AddSource($"{propertyName}.g.cs", code);
// }
// }
//}
//// Find the main method
//var mainMethod = context.Compilation.GetEntryPoint(context.CancellationToken);
//// Build up the source code
//string source = $@"// <auto-generated/>
//using System;
//namespace {mainMethod.ContainingNamespace.ToDisplayString()}
//{{
// public static partial class {mainMethod.ContainingType.Name}
// {{
// static partial void HelloFrom(string name) =>
// Console.WriteLine($""Generator says: Hi from '{{name}}'"");
// }}
//}}
//";
//var typeName = mainMethod.ContainingType.Name;
//// Add the source code to the compilation
//context.AddSource($"{typeName}.g.cs", source);
}
public void Initialize(GeneratorInitializationContext context)
{
// No initialization required for this
context.RegisterForSyntaxNotifications(() => new SettingsGenerationReceiver());
}
}