Skip to content

Commit a00af66

Browse files
committed
refactor(codegen): 重构代码生成器的忽略生成属性功能
- 将 IgnoreGeneratorAttribute 常量从多个类中移除并统一到 GeneratedCodeConsts 类中 - 添加 AttributeDataHelper.IgnoreGenerator 方法来统一处理忽略生成的逻辑 - 将所有使用 IsIgnoreGenerator 或 ShouldIgnoreField 的地方替换为使用 AttributeDataHelper.IgnoreGenerator - 移除 TransitiveDtoGenerator 和 CodeInjectGenerator 中重复的忽略生成判断逻辑 - 从 TransitiveVoGenerator 中移除未使用的私有常量和扩展属性生成功能 - 在 ComObjectWrapBaseGenerator 中使用统一的忽略生成判断方法 - 将 ComObjectWrapBaseGenerator 中的 IsNeeDispose 和 IsMethod 方法改为私有 - 移除 ComObjectWrapBaseGenerator 中重复的 ShouldIgnoreMember 方法
1 parent 8eed552 commit a00af66

14 files changed

+543
-696
lines changed

Core/Mud.CodeGenerator/Consts/GeneratedCodeConsts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ namespace Mud.CodeGenerator;
1212
internal sealed class GeneratedCodeConsts
1313
{
1414
public const string CompilerGeneratedAttribute = "[global::System.Runtime.CompilerServices.CompilerGenerated]";
15+
1516
public static string GeneratedCodeAttribute => $"[global::System.CodeDom.Compiler.GeneratedCode(\"Mud.ServiceCodeGenerator\", \"{GetAssemblyVersion()}\")]";
1617

18+
public static string IgnoreGeneratorAttribute = "IgnoreGeneratorAttribute";
1719
/// <summary>
1820
/// 获取当前程序集的版本号
1921
/// </summary>

Core/Mud.CodeGenerator/Helper/AttributeDataHelper.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ namespace Mud.CodeGenerator;
1212
/// </summary>
1313
internal static class AttributeDataHelper
1414
{
15+
/// <summary>
16+
/// 检查是否应该被忽略生成代码。
17+
/// </summary>
18+
/// <param name="field"></param>
19+
/// <returns></returns>
20+
public static bool IgnoreGenerator(MemberDeclarationSyntax field)
21+
{
22+
var ignoreAttributes = AttributeSyntaxHelper.GetAttributeSyntaxes(field, GeneratedCodeConsts.IgnoreGeneratorAttribute);
23+
return ignoreAttributes?.Any() == true;
24+
}
25+
26+
/// <summary>
27+
/// 检查是否应该被忽略生成代码。
28+
/// </summary>
29+
/// <param name="member">符号</param>
30+
/// <returns>如果应该忽略返回true,否则返回false</returns>
31+
public static bool IgnoreGenerator(ISymbol member)
32+
{
33+
if (member == null)
34+
return false;
35+
return member.GetAttributes().Any(attr => attr.AttributeClass?.Name == GeneratedCodeConsts.IgnoreGeneratorAttribute);
36+
}
37+
1538
/// <summary>
1639
/// 从特性数据中获取整型属性值。
1740
/// </summary>

Core/Mud.CodeGenerator/TransitiveCodeGenerator.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ public abstract class TransitiveCodeGenerator : IIncrementalGenerator
2626
/// </summary>
2727
protected virtual string ClassSuffix => "";
2828

29-
/// <summary>
30-
/// 识别是否忽略生成特性。
31-
/// </summary>
32-
protected const string IgnoreGeneratorAttribute = "IgnoreGeneratorAttribute";
3329
private const string LikeQueryAttributeName = "LikeQueryAttribute";
3430

3531
protected const string CompilerGeneratedAttribute = GeneratedCodeConsts.CompilerGeneratedAttribute;

Core/Mud.EntityCodeGenerator/BoCode/TransitiveBoGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected override Func<PropertyDeclarationSyntax, PropertyDeclarationSyntax> Cr
4343
{
4444
return member =>
4545
{
46-
if (IsIgnoreGenerator(member))
46+
if (AttributeDataHelper.IgnoreGenerator(member))
4747
return null;
4848

4949
var isPrimary = IsPrimary(member);
@@ -61,7 +61,7 @@ protected override Func<FieldDeclarationSyntax, PropertyDeclarationSyntax> Creat
6161
{
6262
return member =>
6363
{
64-
if (IsIgnoreGenerator(member))
64+
if (AttributeDataHelper.IgnoreGenerator(member))
6565
return null;
6666

6767
var isPrimary = IsPrimary(member);

Core/Mud.EntityCodeGenerator/BoCode/TransitiveQueryInputGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected override void GenerateCode(SourceProductionContext context, Compilatio
4444
localClass = BuildLocalClassProperty<PropertyDeclarationSyntax>(orgClassDeclaration, localClass, compilation, propertes,
4545
member =>
4646
{
47-
if (IsIgnoreGenerator(member))
47+
if (AttributeDataHelper.IgnoreGenerator(member))
4848
return null;
4949

5050
return BuildProperty(member);
@@ -54,7 +54,7 @@ protected override void GenerateCode(SourceProductionContext context, Compilatio
5454
localClass = BuildLocalClassProperty<FieldDeclarationSyntax>(orgClassDeclaration, localClass, compilation, propertes,
5555
member =>
5656
{
57-
if (IsIgnoreGenerator(member))
57+
if (AttributeDataHelper.IgnoreGenerator(member))
5858
return null;
5959
return BuildProperty(member, false);
6060
}, null);

Core/Mud.EntityCodeGenerator/ExtensionsCode/BuilderGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private void GeneratePropertyMappings<T>(
129129
sb,
130130
compilation,
131131
generateSetMethod,
132-
IsIgnoreGenerator,
132+
AttributeDataHelper.IgnoreGenerator,
133133
IsPrimary,
134134
GetBuilderPropertyNames,
135135
GetPropertyType);

Core/Mud.EntityCodeGenerator/ExtensionsCode/EntityPropertyGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected override void GenerateCode(SourceProductionContext context, Compilatio
112112
if (!SyntaxHelper.IsValidPrivateField(member))
113113
continue;
114114

115-
if (IsIgnoreGenerator(member))
115+
if (AttributeDataHelper.IgnoreGenerator(member))
116116
continue;
117117

118118
// 检查字段对应的属性是否已存在

Core/Mud.EntityCodeGenerator/TransitiveDtoGenerator.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ protected void ProcessMembers<T>(
223223
memberProcessor(member, orgPropertyName, propertyName);
224224
},
225225
primaryKeyOnly,
226-
IsIgnoreGenerator,
226+
AttributeDataHelper.IgnoreGenerator,
227227
IsPrimary,
228228
GetPropertyNames,
229229
GetPropertyType);
@@ -248,7 +248,7 @@ protected void ProcessMembers<T>(
248248
compilation,
249249
memberProcessor,
250250
primaryKeyOnly,
251-
IsIgnoreGenerator,
251+
AttributeDataHelper.IgnoreGenerator,
252252
IsPrimary,
253253
GetPropertyNames,
254254
GetPropertyType);
@@ -511,23 +511,6 @@ private bool IsAttributeNameMatch(AttributeSyntax attribute, string[] attributes
511511
return attributes.Contains(attributeName, StringComparer.OrdinalIgnoreCase);
512512
}
513513

514-
/// <summary>
515-
/// 是否忽略生成属性。
516-
/// </summary>
517-
/// <param name="memberDeclaration"></param>
518-
/// <returns></returns>
519-
protected bool IsIgnoreGenerator<T>(T memberDeclaration)
520-
where T : MemberDeclarationSyntax
521-
{
522-
// 提高容错性,处理空对象情况
523-
if (memberDeclaration == null)
524-
return true; // 空对象默认忽略生成
525-
526-
var attributes = AttributeSyntaxHelper.GetAttributeSyntaxes(memberDeclaration, IgnoreGeneratorAttribute);
527-
return attributes != null && attributes.Any();
528-
}
529-
530-
531514
/// <summary>
532515
/// 判断属性是否为主键属性。
533516
/// </summary>

Core/Mud.EntityCodeGenerator/VoCode/TransitiveVoGenerator.cs

Lines changed: 2 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ protected override string GetConfiguredClassSuffix()
2727
};
2828
}
2929

30-
private const string ViewPropertyAttribute = "PropertyTranslation";
31-
32-
private const string PropertyValueConvertAttributeName = "PropertyValueConvertAttribute";
33-
34-
private readonly string[] ViewPropertyConvertAttributes = ["DictFormat", "Translation", "Sensitive"];
35-
3630
/// <inheritdoc/>
3731
protected override string[] GetPropertyAttributes()
3832
{
@@ -59,7 +53,7 @@ protected override string[] GetPropertyAttributes()
5953
/// <returns></returns>
6054
private PropertyDeclarationSyntax? GenAttributeFunc(PropertyDeclarationSyntax member)
6155
{
62-
if (IsIgnoreGenerator(member))
56+
if (AttributeDataHelper.IgnoreGenerator(member))
6357
return null;
6458
return BuildProperty(member);
6559
}
@@ -71,160 +65,11 @@ protected override string[] GetPropertyAttributes()
7165
/// <returns></returns>
7266
private PropertyDeclarationSyntax? GenAttributeFunc(FieldDeclarationSyntax member)
7367
{
74-
if (IsIgnoreGenerator(member))
68+
if (AttributeDataHelper.IgnoreGenerator(member))
7569
return null;
7670
return BuildProperty(member, false);
7771
}
7872

79-
/// <summary>
80-
/// 生成扩展属性。
81-
/// </summary>
82-
/// <param name="member"></param>
83-
/// <returns></returns>
84-
private PropertyDeclarationSyntax? GenExtAttributeFunc(PropertyDeclarationSyntax member)
85-
{
86-
if (IsIgnoreGenerator(member))
87-
return null;
88-
var viewAttributes = GetAttributes(member, new[] { ViewPropertyAttribute });
89-
if (!viewAttributes.Any())
90-
return null;
91-
92-
var propertyName = GetPropertyName(member);
93-
var viewAttribute = viewAttributes[0];
94-
var extPropertyName = GetGenPropertyName(viewAttribute, propertyName + "Str");
95-
var convertType = GetConvertPropety(viewAttribute);
96-
//加入映射到属性
97-
var linkPropertyName = SyntaxFactory.AttributeArgument(
98-
SyntaxFactory.NameEquals("MapperFrom"),
99-
null,
100-
SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression,
101-
SyntaxFactory.Literal(propertyName)));
102-
var argsList = SyntaxFactory.SeparatedList(new[] { linkPropertyName });
103-
//加入转换类型属性
104-
if (convertType != null)
105-
argsList = argsList.Add(convertType);
106-
107-
//生成新的属性
108-
var nameSyntax = SyntaxFactory.ParseName(PropertyValueConvertAttributeName);
109-
var attributeSyntax = SyntaxFactory.Attribute(nameSyntax);
110-
var argumentList = SyntaxFactory.AttributeArgumentList(argsList);
111-
attributeSyntax = attributeSyntax.WithArgumentList(argumentList);
112-
//新的属性
113-
var propertyAttributes = SyntaxFactory.SeparatedList(new[] { attributeSyntax });
114-
115-
//获取原属性上需要迁移到新属性上特性。
116-
var attributeList = GetAttributes(member, ViewPropertyConvertAttributes);
117-
if (attributeList.Any())
118-
{
119-
var sttributes = SyntaxFactory.SeparatedList(attributeList);
120-
propertyAttributes = propertyAttributes.AddRange(sttributes);
121-
}
122-
var attributeListSyntax = SyntaxFactory.AttributeList(propertyAttributes);
123-
124-
var property = BuildProperty(extPropertyName, "string");
125-
property = property.AddAttributeLists(attributeListSyntax);
126-
// 为扩展属性添加换行注释
127-
var extInheritdoc = SyntaxFactory.ParseLeadingTrivia($"\n\n///<inheritdoc cref=\"{propertyName}\"/>\n");
128-
property = property.WithLeadingTrivia(extInheritdoc);
129-
return property;
130-
}
131-
132-
/// <summary>
133-
/// 生成扩展属性。
134-
/// </summary>
135-
/// <param name="member"></param>
136-
/// <returns></returns>
137-
private PropertyDeclarationSyntax? GenExtAttributeFunc(FieldDeclarationSyntax member)
138-
{
139-
if (IsIgnoreGenerator(member))
140-
return null;
141-
142-
var viewAttributes = GetAttributes(member, new[] { ViewPropertyAttribute });
143-
if (!viewAttributes.Any())
144-
return null;
145-
146-
var propertyName = GetPropertyName(member);
147-
var viewAttribute = viewAttributes[0];
148-
var extPropertyName = GetGenPropertyName(viewAttribute, propertyName + "Str");
149-
var convertType = GetConvertPropety(viewAttribute);
150-
//加入映射到属性
151-
var linkPropertyName = SyntaxFactory.AttributeArgument(
152-
SyntaxFactory.NameEquals("MapperFrom"),
153-
null,
154-
SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression,
155-
SyntaxFactory.Literal(propertyName)));
156-
var argsList = SyntaxFactory.SeparatedList([linkPropertyName]);
157-
//加入转换类型属性
158-
if (convertType != null)
159-
argsList = argsList.Add(convertType);
160-
161-
//生成新的属性
162-
var nameSyntax = SyntaxFactory.ParseName(PropertyValueConvertAttributeName);
163-
var attributeSyntax = SyntaxFactory.Attribute(nameSyntax);
164-
var argumentList = SyntaxFactory.AttributeArgumentList(argsList);
165-
attributeSyntax = attributeSyntax.WithArgumentList(argumentList);
166-
//新的属性
167-
var propertyAttributes = SyntaxFactory.SeparatedList([attributeSyntax]);
168-
169-
//获取原属性上需要迁移到新属性上特性。
170-
var attributeList = GetAttributes(member, ViewPropertyConvertAttributes);
171-
if (attributeList.Any())
172-
{
173-
var sttributes = SyntaxFactory.SeparatedList(attributeList);
174-
propertyAttributes = propertyAttributes.AddRange(sttributes);
175-
}
176-
var attributeListSyntax = SyntaxFactory.AttributeList(propertyAttributes);
177-
178-
var property = BuildProperty(extPropertyName, "string");
179-
property = property.AddAttributeLists(attributeListSyntax);
180-
// 为扩展属性添加换行注释
181-
var extInheritdoc = SyntaxFactory.ParseLeadingTrivia($"\n\n///<inheritdoc cref=\"{propertyName}\"/>\n");
182-
property = property.WithLeadingTrivia(extInheritdoc);
183-
return property;
184-
}
185-
186-
private AttributeArgumentSyntax? GetConvertPropety(AttributeSyntax attributeSyntax)
187-
{
188-
// 提高容错性,处理空对象情况
189-
if (attributeSyntax?.ArgumentList == null)
190-
return null;
191-
192-
if (!attributeSyntax.ArgumentList.Arguments.Any())
193-
return null;
194-
195-
foreach (var item in attributeSyntax.ArgumentList.Arguments)
196-
{
197-
// 提高容错性,检查NameEquals和Identifier
198-
if (item.NameEquals?.Name?.Identifier.Text?.Equals("convertertype", StringComparison.OrdinalIgnoreCase) == true)
199-
{
200-
return item;
201-
}
202-
}
203-
return null;
204-
}
205-
206-
private string GetGenPropertyName(AttributeSyntax attributeSyntax, string defaultName)
207-
{
208-
// 提高容错性,处理空对象情况
209-
if (attributeSyntax?.ArgumentList == null)
210-
return defaultName;
211-
212-
if (!attributeSyntax.ArgumentList.Arguments.Any())
213-
return defaultName;
214-
215-
foreach (var item in attributeSyntax.ArgumentList.Arguments)
216-
{
217-
// 提高容错性,检查NameEquals和Identifier
218-
if (item.NameEquals?.Name?.Identifier.Text?.Equals("propertyname", StringComparison.OrdinalIgnoreCase) == true)
219-
{
220-
var propertyValue = AttributeSyntaxHelper.ExtractValueFromSyntax(item.Expression);
221-
return propertyValue != null ? ApplyNameCaseConvention(propertyValue.ToString()) : defaultName;
222-
}
223-
}
224-
return defaultName;
225-
226-
}
227-
22873
/// <inheritdoc/>
22974
public override string GeneratorName => "VO Generator";
23075

Core/Mud.ServiceCodeGenerator/CodeInject/CodeInjectGenerator.Interface.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,14 @@ public override void Inject(InjectionContext context, InjectionRequirements requ
135135
var fields = SyntaxHelper.GetClassMemberField(context.ClassDeclaration);
136136
foreach (var field in fields)
137137
{
138-
if (ShouldIgnoreField(field))
138+
if (AttributeDataHelper.IgnoreGenerator(field))
139139
continue;
140140

141141
ProcessFieldInjection(context, field);
142142
}
143143
}
144144

145-
private bool ShouldIgnoreField(FieldDeclarationSyntax field)
146-
{
147-
var ignoreAttributes = AttributeSyntaxHelper.GetAttributeSyntaxes(field, IgnoreGeneratorAttribute);
148-
return ignoreAttributes?.Any() == true;
149-
}
145+
150146

151147
private void ProcessFieldInjection(InjectionContext context, FieldDeclarationSyntax field)
152148
{

0 commit comments

Comments
 (0)