-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullPropertyGenerator.cs
168 lines (138 loc) · 5.66 KB
/
FullPropertyGenerator.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using UtilGenerator.Attributes;
namespace UtilGenerator
{
[Generator]
public class FullPropertyGenerator : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
var syntaxTrees = context.Compilation.SyntaxTrees;
#region INotifyPropertyChangedAttribute
foreach (var syntaxTree in syntaxTrees)
{
var notifyClasses = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>()
.Where(x => x.AttributeLists.Any(a => a.ToString().StartsWith($"[INotifyPropertyChanged"))).ToList();
foreach (var notifyClass in notifyClasses)
{
var usings = syntaxTree.GetRoot().DescendantNodes().OfType<UsingDirectiveSyntax>();
string usingsAsText = string.Join(Environment.NewLine, usings);
StringBuilder sourceBuilder = new StringBuilder();
string className = notifyClass.Identifier.ToString();
string source = $@"//<auto-generated/>
{usingsAsText}
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace SourceGeneratorMVVMTest.ViewModels
{{
public partial class {className} : INotifyPropertyChanged
{{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}}
{BuildPropertyChangedMethods(notifyClass)}
{BuildProperties(notifyClass)}
}}
}}";
sourceBuilder.Append(source);
context.AddSource($"{className}.g.cs", sourceBuilder.ToString());
}
}
#endregion
}
private object BuildPropertyChangedMethods(TypeDeclarationSyntax classDeclaration)
{
StringBuilder onPropertyChangingMethodBuilder = new StringBuilder();
var fields = classDeclaration.ChildNodes()
.Where(x => x is FieldDeclarationSyntax field
&& field.AttributeLists.Any(a => a.ToString().StartsWith("[AutoNotify")))
.Select(a => a as FieldDeclarationSyntax);
foreach (FieldDeclarationSyntax field in fields)
{
string fieldName = field.GetFieldName();
string propertyName = FieldToPropertyName(fieldName);
var propertyType = field.GetFieldType();
string methodTemplate = $"partial void On{propertyName}Changing({propertyType} newValue);";
onPropertyChangingMethodBuilder.AppendLine(methodTemplate);
}
return onPropertyChangingMethodBuilder.ToString();
}
private string BuildProperties(TypeDeclarationSyntax classDeclaration)
{
StringBuilder propertyBuilder = new StringBuilder();
var fields = classDeclaration.ChildNodes()
.Where(x => x is FieldDeclarationSyntax field
&& field.AttributeLists.Any(a => a.ToString().StartsWith($"[AutoNotify")))
.Select(a => a as FieldDeclarationSyntax);
foreach (FieldDeclarationSyntax field in fields)
{
string type = field.GetFieldType();
string fieldName = field.GetFieldName();
string propertyName = FieldToPropertyName(fieldName);
string propertyTemplate = $@"
public {type} {propertyName}
{{
get => {fieldName};
set
{{
{fieldName} = value;
{BuildNotifyPropertyChangedCalls(field)}
On{propertyName}Changing(value);
}}
}}
";
propertyBuilder.Append(propertyTemplate);
}
return propertyBuilder.ToString();
}
private object BuildNotifyPropertyChangedCalls(FieldDeclarationSyntax field)
{
StringBuilder result = new StringBuilder();
result.AppendLine("OnPropertyChanged();");
var alsoNotifyAttributes = field.AttributeLists.SelectMany(x => x.Attributes.Select(a => a.ToString()))
.Where(x => x.StartsWith("AlsoNotify")).ToList();
foreach(var attribute in alsoNotifyAttributes)
{
string propertyName = AlsoNotifyAttribute.GetPropertyName(attribute);
result.AppendLine($"OnPropertyChanged(nameof({propertyName}));");
}
return result.ToString();
}
private string FieldToPropertyName(string fieldName)
{
if (string.IsNullOrWhiteSpace(fieldName))
{
throw new ArgumentNullException("Der Name des Fields darf nicht null oder leer sein");
}
if (fieldName.StartsWith("_"))
{
fieldName = fieldName.Substring(1);
}
string result = string.Concat(fieldName[0].ToString().ToUpper(), fieldName.Substring(1));
return result;
}
public void Initialize(GeneratorInitializationContext context)
{
#if DEBUG
if (!Debugger.IsAttached)
{
Debugger.Launch();
}
#endif
}
}
}