XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.AutoConfig

【DLL】自动实现配置文件的存储

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.AutoConfig

将项目更改为增量生成器,初步完成AOT支持

d1a989f
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

9 个文件 +91 -54
Modified AutoConfig.Analyzer.Test/AutoConfig.Analyzer.Test.csproj +3 -2
@@ -5,11 +5,12 @@
5 5 <TargetFramework>net8.0</TargetFramework>
6 6 <ImplicitUsings>enable</ImplicitUsings>
7 7 <Nullable>enable</Nullable>
8 <PublishAot>true</PublishAot>
9 <JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
8 10 </PropertyGroup>
9 11
10 12 <ItemGroup>
11 <PackageReference Include="XFEExtension.NetCore" Version="3.0.0" />
12 <PackageReference Include="XFEExtension.NetCore.XUnit" Version="2.0.3" />
13 <PackageReference Include="XFEExtension.NetCore" Version="3.2.5" />
13 14 </ItemGroup>
14 15
15 16 <ItemGroup>
Modified AutoConfig.Analyzer.Test/Program.cs +21 -6
@@ -1,12 +1,27 @@
1 namespace AutoConfig.Analyzer.Test;
1 using PDDShopManagementSystem.ServerConsole;
2 using XFEExtension.NetCore.StringExtension.Json;
3
4 namespace AutoConfig.Analyzer.Test;
2 5
3 6 public class Program
4 7 {
5 [SMTest]
6 public static void MainTest()
8 public static void Main(string[] args)
7 9 {
8 SystemProfile.TestNum = 3;
9 Console.WriteLine(SystemProfile.TestNum);
10 Console.WriteLine(SystemProfile.ProfilePath);
10 Console.WriteLine($"上一次读取的值是:{UserProfile.UserInfoList.ToJson()}");
11 Console.WriteLine("添加值:");
12 Console.WriteLine("请输入店铺名称:");
13 var shopName = Console.ReadLine();
14 Console.WriteLine("请输入店铺ID:");
15 var shopID = Console.ReadLine() ?? "暂无ID";
16 UserProfile.UserInfoList.Add(new()
17 {
18 SessionID = Guid.NewGuid().ToString(),
19 ShopID = shopID,
20 RecentShopName = shopName,
21 CurrentIpAddress = "198.131.114.41",
22 Banned = false,
23 EndDateTime = DateTime.Now
24 });
25 Console.ReadLine();
11 26 }
12 27 }
Modified AutoConfig.Analyzer.Test/SystemProfile.cs +1 -2
@@ -5,6 +5,5 @@ namespace AutoConfig.Analyzer.Test;
5 5 public partial class SystemProfile
6 6 {
7 7 [ProfileProperty]
8 private int testNum;
9 public SystemProfile() => ProfilePath = @"C:\Users\XFEstudio\Downloads\SystemProfile.xfe";
8 private string myText = "";
10 9 }
Added AutoConfig.Analyzer.Test/UserInfo.cs +32 -0
@@ -0,0 +1,32 @@
1 namespace PDDShopManagementSystem.ServerConsole;
2
3 /// <summary>
4 /// 用户信息
5 /// </summary>
6 public class UserInfo
7 {
8 /// <summary>
9 /// 店铺最近登录使用的店铺名称
10 /// </summary>
11 public string? RecentShopName { get; set; }
12 /// <summary>
13 /// 店铺ID
14 /// </summary>
15 public required string ShopID { get; set; }
16 /// <summary>
17 /// 当前活动期ID
18 /// </summary>
19 public required string SessionID { get; set; }
20 /// <summary>
21 /// 最近一次登录的IP地址
22 /// </summary>
23 public required string CurrentIpAddress { get; set; }
24 /// <summary>
25 /// 剩余天数
26 /// </summary>
27 public DateTime EndDateTime { get; set; } = DateTime.Now;
28 /// <summary>
29 /// 该商家店铺是否被封禁
30 /// </summary>
31 public bool Banned { get; set; }
32 }
Added AutoConfig.Analyzer.Test/UserProfile.cs +15 -0
@@ -0,0 +1,15 @@
1 using XFEExtension.NetCore.AutoConfig;
2
3 namespace PDDShopManagementSystem.ServerConsole;
4
5 /// <summary>
6 /// 用户配置文件
7 /// </summary>
8 internal partial class UserProfile
9 {
10 /// <summary>
11 /// 用户信息列表
12 /// </summary>
13 [ProfileProperty]
14 private ProfileList<UserProfile, UserInfo> userInfoList = [];
15 }
Modified XFEExtension.NetCore.AutoConfig.Analyzer/Generator/ProfilePropertyAutoGenerator.cs +15 -17
@@ -7,16 +7,17 @@ using System.Linq;
7 7 namespace XFEExtension.NetCore.AutoConfig.Generator;
8 8
9 9 [Generator]
10 public class ProfilePropertyAutoGenerator : ISourceGenerator
10 public class ProfilePropertyAutoGenerator : IIncrementalGenerator
11 11 {
12 public void Initialize(GeneratorInitializationContext context)
12
13 public void Initialize(IncrementalGeneratorInitializationContext context)
13 14 {
14 context.RegisterForSyntaxNotifications(() => new ProfilePropertySyntaxReceiver());
15 context.RegisterSourceOutput(context.CompilationProvider, Generate);
15 16 }
16 17
17 public void Execute(GeneratorExecutionContext context)
18 public void Generate(SourceProductionContext context, Compilation compilation)
18 19 {
19 var syntaxTrees = context.Compilation.SyntaxTrees;
20 var syntaxTrees = compilation.SyntaxTrees;
20 21 foreach (var syntaxTree in syntaxTrees)
21 22 {
22 23 var root = syntaxTree.GetRoot();
@@ -104,7 +105,7 @@ public class ProfilePropertyAutoGenerator : ISourceGenerator
104 105 #endregion
105 106 var setExpressionStatements = new List<StatementSyntax>()
106 107 {
107 SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"{setMethodName}(value)")).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
108 SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"{setMethodName}(ref value)")).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
108 109 };
109 110 if (fieldDeclarationSyntax.AttributeLists.Any(IsProfilePropertyAddSetAttribute))
110 111 {
@@ -151,13 +152,13 @@ public class ProfilePropertyAutoGenerator : ISourceGenerator
151 152 .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
152 153 .AddAttributeLists(attributeSyntax)
153 154 .WithAccessorList(SyntaxFactory.AccessorList(
154 SyntaxFactory.List(new[]
155 {
155 SyntaxFactory.List(
156 [
156 157 SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
157 158 .WithBody(SyntaxFactory.Block(getExpressionStatements)),
158 159 SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
159 160 .WithBody(SyntaxFactory.Block(setExpressionStatements))
160 })))
161 ])))
161 162 .WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia(triviaText));
162 163 var getMethod = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName("void"), getMethodName)
163 164 .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.PartialKeyword)))
@@ -175,7 +176,6 @@ public class ProfilePropertyAutoGenerator : ISourceGenerator
175 176 }
176 177 }
177 178 }
178
179 179 public static bool IsProfilePropertyAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfileProperty");
180 180
181 181 public static List<AttributeSyntax> GetProfilePropertyAttributeList(FieldDeclarationSyntax fieldDeclaration) => fieldDeclaration.AttributeLists.Where(IsProfilePropertyAttribute).SelectMany(attributeList => attributeList.Attributes).ToList();
@@ -206,7 +206,7 @@ public class ProfilePropertyAutoGenerator : ISourceGenerator
206 206
207 207 public static IEnumerable<ClassDeclarationSyntax> GetClassDeclarations(SyntaxNode rootNode) => rootNode.DescendantNodes()
208 208 .OfType<ClassDeclarationSyntax>()
209 .Where(classDeclaration => classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword) && !classDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword));
209 .Where(classDeclaration => !classDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword));
210 210
211 211 private static SyntaxTree GenerateProfileClassSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes, List<PropertyDeclarationSyntax> propertyDeclarationSyntaxes, List<MethodDeclarationSyntax> methodDeclarationSyntaxes, FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax)
212 212 {
@@ -221,13 +221,12 @@ public class ProfilePropertyAutoGenerator : ISourceGenerator
221 221 SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName("string"), "ProfilePath")
222 222 .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
223 223 .WithAccessorList(SyntaxFactory.AccessorList(SyntaxFactory.List(
224 new[]
225 {
224 [
226 225 SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
227 226 .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
228 227 SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
229 228 .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
230 })))
229 ])))
231 230 .WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression(@"""""")))
232 231 .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
233 232 .WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia($@"/// <summary>
@@ -240,13 +239,12 @@ public class ProfilePropertyAutoGenerator : ISourceGenerator
240 239 .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
241 240 .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.Attribute(SyntaxFactory.ParseName("global::XFEExtension.NetCore.AutoConfig.ProfileInstanceAttribute")))))
242 241 .WithAccessorList(SyntaxFactory.AccessorList(SyntaxFactory.List(
243 new[]
244 {
242 [
245 243 SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
246 244 .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
247 245 SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
248 246 .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
249 })))
247 ])))
250 248 .WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression($"new {className}()")))
251 249 .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
252 250 .WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia($@"/// <summary>
Deleted XFEExtension.NetCore.AutoConfig.Analyzer/Generator/ProfilePropertySyntaxReceiver.cs +0 -23
@@ -1,23 +0,0 @@
1 using Microsoft.CodeAnalysis;
2 using Microsoft.CodeAnalysis.CSharp.Syntax;
3 using System.Collections.Generic;
4 using System.Linq;
5
6 namespace XFEExtension.NetCore.AutoConfig.Generator
7 {
8 public class ProfilePropertySyntaxReceiver : ISyntaxReceiver
9 {
10 public List<FieldDeclarationSyntax> CandidateFields { get; } = new List<FieldDeclarationSyntax>();
11
12 public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
13 {
14 if (syntaxNode is FieldDeclarationSyntax fieldDeclarationSyntax)
15 {
16 if (fieldDeclarationSyntax.AttributeLists.Any(ProfilePropertyAutoGenerator.IsProfilePropertyAttribute))
17 {
18 CandidateFields.Add(fieldDeclarationSyntax);
19 }
20 }
21 }
22 }
23 }
Modified XFEExtension.NetCore.AutoConfig.Analyzer/XFEExtension.NetCore.AutoConfig.Analyzer.csproj +3 -3
@@ -13,9 +13,9 @@
13 13 </PropertyGroup>
14 14
15 15 <ItemGroup>
16 <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
17 <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" />
18 <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.9.2" />
16 <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
17 <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all" />
18 <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.12.0" />
19 19 </ItemGroup>
20 20
21 21 </Project>
Modified XFEExtension.NetCore.AutoConfig/XFEExtension.NetCore.AutoConfig.csproj +1 -1
@@ -48,7 +48,7 @@
48 48 </ItemGroup>
49 49
50 50 <ItemGroup>
51 <PackageReference Include="XFEExtension.NetCore" Version="3.0.*" />
51 <PackageReference Include="XFEExtension.NetCore" Version="3.2.5" />
52 52 </ItemGroup>
53 53
54 54 </Project>