diff --git a/GFramework.Core.Abstractions/Directory.Build.props b/GFramework.Core.Abstractions/Directory.Build.props new file mode 100644 index 0000000..efa07a7 --- /dev/null +++ b/GFramework.Core.Abstractions/Directory.Build.props @@ -0,0 +1,24 @@ + + + + netstandard2.0;net8.0 + true + true + + preview + + + + all + runtime; build; native; contentfiles; analyzers + + + all + runtime; build; native; contentfiles; analyzers + + + diff --git a/GFramework.Core.Abstractions/GFramework.Core.Abstractions.csproj b/GFramework.Core.Abstractions/GFramework.Core.Abstractions.csproj new file mode 100644 index 0000000..9af95d4 --- /dev/null +++ b/GFramework.Core.Abstractions/GFramework.Core.Abstractions.csproj @@ -0,0 +1,19 @@ + + + + + false + true + T:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute + enable + + + + + + + + diff --git a/GFramework.Core/architecture/ArchitectureOptions.cs b/GFramework.Core.Abstractions/architecture/ArchitectureOptions.cs similarity index 71% rename from GFramework.Core/architecture/ArchitectureOptions.cs rename to GFramework.Core.Abstractions/architecture/ArchitectureOptions.cs index 8d5c916..769645b 100644 --- a/GFramework.Core/architecture/ArchitectureOptions.cs +++ b/GFramework.Core.Abstractions/architecture/ArchitectureOptions.cs @@ -1,4 +1,4 @@ -namespace GFramework.Core.architecture; +namespace GFramework.Core.Abstractions.architecture; /// /// 架构选项配置类,用于定义架构行为的相关配置选项 @@ -11,10 +11,10 @@ public sealed class ArchitectureOptions( /// /// 允许延迟注册开关,当设置为true时允许在初始化完成后进行组件注册 /// - public bool AllowLateRegistration = allowLateRegistration; + public bool AllowLateRegistration { get; } = allowLateRegistration; /// /// 严格阶段验证开关,当设置为true时启用严格的阶段验证机制 /// - public bool StrictPhaseValidation = strictPhaseValidation; + public bool StrictPhaseValidation { get; } = strictPhaseValidation; } \ No newline at end of file diff --git a/GFramework.Core/architecture/IArchitecture.cs b/GFramework.Core.Abstractions/architecture/IArchitecture.cs similarity index 92% rename from GFramework.Core/architecture/IArchitecture.cs rename to GFramework.Core.Abstractions/architecture/IArchitecture.cs index a501457..cc43382 100644 --- a/GFramework.Core/architecture/IArchitecture.cs +++ b/GFramework.Core.Abstractions/architecture/IArchitecture.cs @@ -1,8 +1,8 @@ -using GFramework.Core.model; -using GFramework.Core.system; -using GFramework.Core.utility; +using GFramework.Core.Abstractions.model; +using GFramework.Core.Abstractions.system; +using GFramework.Core.Abstractions.utility; -namespace GFramework.Core.architecture; +namespace GFramework.Core.Abstractions.architecture; /// /// 架构接口,专注于生命周期管理,包括系统、模型、工具的注册和获取 diff --git a/GFramework.Core/architecture/IArchitectureConfiguration.cs b/GFramework.Core.Abstractions/architecture/IArchitectureConfiguration.cs similarity index 83% rename from GFramework.Core/architecture/IArchitectureConfiguration.cs rename to GFramework.Core.Abstractions/architecture/IArchitectureConfiguration.cs index cf4bd3d..51f3b1b 100644 --- a/GFramework.Core/architecture/IArchitectureConfiguration.cs +++ b/GFramework.Core.Abstractions/architecture/IArchitectureConfiguration.cs @@ -1,6 +1,6 @@ -using GFramework.Core.logging; +using GFramework.Core.Abstractions.logging; -namespace GFramework.Core.architecture; +namespace GFramework.Core.Abstractions.architecture; /// /// 定义架构配置的接口,提供日志工厂、日志级别和架构选项的配置功能 diff --git a/GFramework.Core/architecture/IArchitectureContext.cs b/GFramework.Core.Abstractions/architecture/IArchitectureContext.cs similarity index 89% rename from GFramework.Core/architecture/IArchitectureContext.cs rename to GFramework.Core.Abstractions/architecture/IArchitectureContext.cs index ae589d6..57f50b6 100644 --- a/GFramework.Core/architecture/IArchitectureContext.cs +++ b/GFramework.Core.Abstractions/architecture/IArchitectureContext.cs @@ -1,12 +1,13 @@ -using GFramework.Core.command; -using GFramework.Core.events; -using GFramework.Core.logging; -using GFramework.Core.model; -using GFramework.Core.query; -using GFramework.Core.system; -using GFramework.Core.utility; +using System; +using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.events; +using GFramework.Core.Abstractions.logging; +using GFramework.Core.Abstractions.model; +using GFramework.Core.Abstractions.query; +using GFramework.Core.Abstractions.system; +using GFramework.Core.Abstractions.utility; -namespace GFramework.Core.architecture; +namespace GFramework.Core.Abstractions.architecture; /// /// 架构上下文接口,提供对系统、模型、工具类的访问以及命令、查询、事件的发送和注册功能 diff --git a/GFramework.Core/architecture/IArchitectureLifecycle.cs b/GFramework.Core.Abstractions/architecture/IArchitectureLifecycle.cs similarity index 88% rename from GFramework.Core/architecture/IArchitectureLifecycle.cs rename to GFramework.Core.Abstractions/architecture/IArchitectureLifecycle.cs index 97cc324..c2f5103 100644 --- a/GFramework.Core/architecture/IArchitectureLifecycle.cs +++ b/GFramework.Core.Abstractions/architecture/IArchitectureLifecycle.cs @@ -1,4 +1,4 @@ -namespace GFramework.Core.architecture; +namespace GFramework.Core.Abstractions.architecture; /// /// 架构生命周期接口,定义了架构在不同阶段的回调方法 diff --git a/GFramework.Core/architecture/IArchitectureModule.cs b/GFramework.Core.Abstractions/architecture/IArchitectureModule.cs similarity index 89% rename from GFramework.Core/architecture/IArchitectureModule.cs rename to GFramework.Core.Abstractions/architecture/IArchitectureModule.cs index 273cc14..2dc62cd 100644 --- a/GFramework.Core/architecture/IArchitectureModule.cs +++ b/GFramework.Core.Abstractions/architecture/IArchitectureModule.cs @@ -1,4 +1,4 @@ -namespace GFramework.Core.architecture; +namespace GFramework.Core.Abstractions.architecture; /// /// 架构模块接口,继承自架构生命周期接口。 diff --git a/GFramework.Core/architecture/IArchitecturePhaseAware.cs b/GFramework.Core.Abstractions/architecture/IArchitecturePhaseAware.cs similarity index 88% rename from GFramework.Core/architecture/IArchitecturePhaseAware.cs rename to GFramework.Core.Abstractions/architecture/IArchitecturePhaseAware.cs index 557fd79..b7000ce 100644 --- a/GFramework.Core/architecture/IArchitecturePhaseAware.cs +++ b/GFramework.Core.Abstractions/architecture/IArchitecturePhaseAware.cs @@ -1,4 +1,4 @@ -namespace GFramework.Core.architecture; +namespace GFramework.Core.Abstractions.architecture; /// /// 定义架构阶段感知接口,用于在架构的不同阶段执行相应的逻辑 diff --git a/GFramework.Core/architecture/IArchitectureRuntime.cs b/GFramework.Core.Abstractions/architecture/IArchitectureRuntime.cs similarity index 91% rename from GFramework.Core/architecture/IArchitectureRuntime.cs rename to GFramework.Core.Abstractions/architecture/IArchitectureRuntime.cs index 134da12..7589072 100644 --- a/GFramework.Core/architecture/IArchitectureRuntime.cs +++ b/GFramework.Core.Abstractions/architecture/IArchitectureRuntime.cs @@ -1,8 +1,9 @@ -using GFramework.Core.command; -using GFramework.Core.events; -using GFramework.Core.query; +using System; +using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.events; +using GFramework.Core.Abstractions.query; -namespace GFramework.Core.architecture; +namespace GFramework.Core.Abstractions.architecture; /// /// 架构运行时接口,提供统一的命令、查询、事件操作入口 diff --git a/GFramework.Core/architecture/IArchitectureServices.cs b/GFramework.Core.Abstractions/architecture/IArchitectureServices.cs similarity index 79% rename from GFramework.Core/architecture/IArchitectureServices.cs rename to GFramework.Core.Abstractions/architecture/IArchitectureServices.cs index fe72e56..34c2433 100644 --- a/GFramework.Core/architecture/IArchitectureServices.cs +++ b/GFramework.Core.Abstractions/architecture/IArchitectureServices.cs @@ -1,7 +1,7 @@ -using GFramework.Core.events; -using GFramework.Core.ioc; +using GFramework.Core.Abstractions.events; +using GFramework.Core.Abstractions.ioc; -namespace GFramework.Core.architecture; +namespace GFramework.Core.Abstractions.architecture; /// /// 架构服务接口,定义了框架核心架构所需的服务组件 diff --git a/GFramework.Core/architecture/IAsyncInitializable.cs b/GFramework.Core.Abstractions/architecture/IAsyncInitializable.cs similarity index 80% rename from GFramework.Core/architecture/IAsyncInitializable.cs rename to GFramework.Core.Abstractions/architecture/IAsyncInitializable.cs index 03db994..77b0420 100644 --- a/GFramework.Core/architecture/IAsyncInitializable.cs +++ b/GFramework.Core.Abstractions/architecture/IAsyncInitializable.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.architecture; +using System.Threading.Tasks; + +namespace GFramework.Core.Abstractions.architecture; /// /// 定义异步初始化接口,用于需要异步初始化的组件或服务 diff --git a/GFramework.Core/command/ICommand.cs b/GFramework.Core.Abstractions/command/ICommand.cs similarity index 92% rename from GFramework.Core/command/ICommand.cs rename to GFramework.Core.Abstractions/command/ICommand.cs index b4c2214..16fd392 100644 --- a/GFramework.Core/command/ICommand.cs +++ b/GFramework.Core.Abstractions/command/ICommand.cs @@ -1,6 +1,6 @@ -using GFramework.Core.rule; +using GFramework.Core.Abstractions.rule; -namespace GFramework.Core.command; +namespace GFramework.Core.Abstractions.command; /// /// 命令接口,定义了无返回值命令的基本契约 diff --git a/GFramework.Core/controller/IController.cs b/GFramework.Core.Abstractions/controller/IController.cs similarity index 77% rename from GFramework.Core/controller/IController.cs rename to GFramework.Core.Abstractions/controller/IController.cs index cca5063..c69c455 100644 --- a/GFramework.Core/controller/IController.cs +++ b/GFramework.Core.Abstractions/controller/IController.cs @@ -1,6 +1,6 @@ -using GFramework.Core.rule; +using GFramework.Core.Abstractions.rule; -namespace GFramework.Core.controller; +namespace GFramework.GFramework.Core.Abstractions.controller; /// /// 控制器接口,定义了控制器需要实现的所有功能契约 diff --git a/GFramework.Core/controller/README.md b/GFramework.Core.Abstractions/controller/README.md similarity index 100% rename from GFramework.Core/controller/README.md rename to GFramework.Core.Abstractions/controller/README.md diff --git a/GFramework.Core/architecture/ArchitecturePhase.cs b/GFramework.Core.Abstractions/enums/ArchitecturePhase.cs similarity index 95% rename from GFramework.Core/architecture/ArchitecturePhase.cs rename to GFramework.Core.Abstractions/enums/ArchitecturePhase.cs index ef93dfa..88cfd51 100644 --- a/GFramework.Core/architecture/ArchitecturePhase.cs +++ b/GFramework.Core.Abstractions/enums/ArchitecturePhase.cs @@ -1,4 +1,4 @@ -namespace GFramework.Core.architecture; +namespace GFramework.Core.Abstractions.architecture; /// /// 架构阶段枚举,定义了系统架构初始化和运行过程中的各个关键阶段 diff --git a/GFramework.Core/events/IEasyEvent.cs b/GFramework.Core.Abstractions/events/IEasyEvent.cs similarity index 85% rename from GFramework.Core/events/IEasyEvent.cs rename to GFramework.Core.Abstractions/events/IEasyEvent.cs index ce4dd3d..c83a7e0 100644 --- a/GFramework.Core/events/IEasyEvent.cs +++ b/GFramework.Core.Abstractions/events/IEasyEvent.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.events; +using System; + +namespace GFramework.Core.Abstractions.events; /// /// 事件接口,定义了事件注册的基本功能 diff --git a/GFramework.Core/events/ITypeEventSystem.cs b/GFramework.Core.Abstractions/events/ITypeEventSystem.cs similarity index 94% rename from GFramework.Core/events/ITypeEventSystem.cs rename to GFramework.Core.Abstractions/events/ITypeEventSystem.cs index e645daf..014b6ef 100644 --- a/GFramework.Core/events/ITypeEventSystem.cs +++ b/GFramework.Core.Abstractions/events/ITypeEventSystem.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.events; +using System; + +namespace GFramework.Core.Abstractions.events; /// /// 类型事件系统接口,定义基于类型的事件发送、注册和注销功能 diff --git a/GFramework.Core/events/IUnRegister.cs b/GFramework.Core.Abstractions/events/IUnRegister.cs similarity index 79% rename from GFramework.Core/events/IUnRegister.cs rename to GFramework.Core.Abstractions/events/IUnRegister.cs index a086e8b..a41b8c7 100644 --- a/GFramework.Core/events/IUnRegister.cs +++ b/GFramework.Core.Abstractions/events/IUnRegister.cs @@ -1,4 +1,4 @@ -namespace GFramework.Core.events; +namespace GFramework.Core.Abstractions.events; /// /// 提供注销功能的接口 diff --git a/GFramework.Core/events/IUnRegisterList.cs b/GFramework.Core.Abstractions/events/IUnRegisterList.cs similarity index 63% rename from GFramework.Core/events/IUnRegisterList.cs rename to GFramework.Core.Abstractions/events/IUnRegisterList.cs index 3edbc88..d95d4de 100644 --- a/GFramework.Core/events/IUnRegisterList.cs +++ b/GFramework.Core.Abstractions/events/IUnRegisterList.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.events; +using System.Collections.Generic; + +namespace GFramework.Core.Abstractions.events; /// /// 提供统一注销功能的接口,用于管理需要注销的对象列表 @@ -8,5 +10,5 @@ public interface IUnRegisterList /// /// 获取需要注销的对象列表 /// - List UnregisterList { get; } + IList UnregisterList { get; } } \ No newline at end of file diff --git a/GFramework.Core/ioc/IIocContainer.cs b/GFramework.Core.Abstractions/ioc/IIocContainer.cs similarity index 95% rename from GFramework.Core/ioc/IIocContainer.cs rename to GFramework.Core.Abstractions/ioc/IIocContainer.cs index dda1924..1994820 100644 --- a/GFramework.Core/ioc/IIocContainer.cs +++ b/GFramework.Core.Abstractions/ioc/IIocContainer.cs @@ -1,7 +1,9 @@ -using GFramework.Core.rule; -using GFramework.Core.system; +using System; +using System.Collections.Generic; +using GFramework.Core.Abstractions.rule; +using GFramework.Core.Abstractions.system; -namespace GFramework.Core.ioc; +namespace GFramework.Core.Abstractions.ioc; /// /// 依赖注入容器接口,定义了服务注册、解析和管理的基本操作 diff --git a/GFramework.Core/logging/ILogger.cs b/GFramework.Core.Abstractions/logging/ILogger.cs similarity index 99% rename from GFramework.Core/logging/ILogger.cs rename to GFramework.Core.Abstractions/logging/ILogger.cs index d0d91fe..44d1929 100644 --- a/GFramework.Core/logging/ILogger.cs +++ b/GFramework.Core.Abstractions/logging/ILogger.cs @@ -1,4 +1,7 @@ -namespace GFramework.Core.logging; +using System; +using GFramework.Core.logging; + +namespace GFramework.Core.Abstractions.logging; /// /// 定义日志记录接口,提供日志记录和级别检查功能 diff --git a/GFramework.Core/logging/ILoggerFactory.cs b/GFramework.Core.Abstractions/logging/ILoggerFactory.cs similarity index 88% rename from GFramework.Core/logging/ILoggerFactory.cs rename to GFramework.Core.Abstractions/logging/ILoggerFactory.cs index b4f2179..af7c876 100644 --- a/GFramework.Core/logging/ILoggerFactory.cs +++ b/GFramework.Core.Abstractions/logging/ILoggerFactory.cs @@ -1,4 +1,4 @@ -namespace GFramework.Core.logging; +namespace GFramework.Core.Abstractions.logging; /// /// 定义日志工厂接口,用于创建日志记录器实例 diff --git a/GFramework.Core/logging/LogLevel.cs b/GFramework.Core.Abstractions/logging/LogLevel.cs similarity index 100% rename from GFramework.Core/logging/LogLevel.cs rename to GFramework.Core.Abstractions/logging/LogLevel.cs diff --git a/GFramework.Core/model/IModel.cs b/GFramework.Core.Abstractions/model/IModel.cs similarity index 80% rename from GFramework.Core/model/IModel.cs rename to GFramework.Core.Abstractions/model/IModel.cs index a83fd2a..3ae34d8 100644 --- a/GFramework.Core/model/IModel.cs +++ b/GFramework.Core.Abstractions/model/IModel.cs @@ -1,4 +1,4 @@ -namespace GFramework.Core.model; +namespace GFramework.Core.Abstractions.model; /// /// 模型接口,定义了模型的基本行为和功能 diff --git a/GFramework.Core/property/IBindableProperty.cs b/GFramework.Core.Abstractions/property/IBindableProperty.cs similarity index 91% rename from GFramework.Core/property/IBindableProperty.cs rename to GFramework.Core.Abstractions/property/IBindableProperty.cs index 68a659f..4b27ac0 100644 --- a/GFramework.Core/property/IBindableProperty.cs +++ b/GFramework.Core.Abstractions/property/IBindableProperty.cs @@ -1,4 +1,4 @@ -namespace GFramework.Core.property; +namespace GFramework.Core.Abstractions.property; /// /// 可绑定属性接口,继承自只读可绑定属性接口,提供可读写的属性绑定功能 diff --git a/GFramework.Core/property/IReadonlyBindableProperty.cs b/GFramework.Core.Abstractions/property/IReadonlyBindableProperty.cs similarity index 92% rename from GFramework.Core/property/IReadonlyBindableProperty.cs rename to GFramework.Core.Abstractions/property/IReadonlyBindableProperty.cs index a9fc3ad..ee3e9d6 100644 --- a/GFramework.Core/property/IReadonlyBindableProperty.cs +++ b/GFramework.Core.Abstractions/property/IReadonlyBindableProperty.cs @@ -1,6 +1,6 @@ -using GFramework.Core.events; +using GFramework.Core.Abstractions.events; -namespace GFramework.Core.property; +namespace GFramework.Core.Abstractions.property; /// /// 只读可绑定属性接口,提供属性值的读取和变更监听功能 diff --git a/GFramework.Core/query/IQuery.cs b/GFramework.Core.Abstractions/query/IQuery.cs similarity index 81% rename from GFramework.Core/query/IQuery.cs rename to GFramework.Core.Abstractions/query/IQuery.cs index 6439077..8e3ffee 100644 --- a/GFramework.Core/query/IQuery.cs +++ b/GFramework.Core.Abstractions/query/IQuery.cs @@ -1,6 +1,6 @@ -using GFramework.Core.rule; +using GFramework.Core.Abstractions.rule; -namespace GFramework.Core.query; +namespace GFramework.Core.Abstractions.query; /// /// 查询接口,定义了执行查询操作的契约 diff --git a/GFramework.Core/rule/IContextAware.cs b/GFramework.Core.Abstractions/rule/IContextAware.cs similarity index 80% rename from GFramework.Core/rule/IContextAware.cs rename to GFramework.Core.Abstractions/rule/IContextAware.cs index d11140a..01cac5e 100644 --- a/GFramework.Core/rule/IContextAware.cs +++ b/GFramework.Core.Abstractions/rule/IContextAware.cs @@ -1,6 +1,6 @@ -using GFramework.Core.architecture; +using GFramework.Core.Abstractions.architecture; -namespace GFramework.Core.rule; +namespace GFramework.Core.Abstractions.rule; /// /// 上下文感知接口,用于为实现该接口的类提供设置架构上下文的能力 diff --git a/GFramework.Core/rule/ILogAware.cs b/GFramework.Core.Abstractions/rule/ILogAware.cs similarity index 78% rename from GFramework.Core/rule/ILogAware.cs rename to GFramework.Core.Abstractions/rule/ILogAware.cs index d52b6e6..12b4daa 100644 --- a/GFramework.Core/rule/ILogAware.cs +++ b/GFramework.Core.Abstractions/rule/ILogAware.cs @@ -1,6 +1,6 @@ -using GFramework.Core.logging; +using GFramework.Core.Abstractions.logging; -namespace GFramework.Core.rule; +namespace GFramework.Core.Abstractions.rule; /// /// 定义一个支持日志记录的接口,允许实现类设置和使用日志记录器 diff --git a/GFramework.Core/system/ISystem.cs b/GFramework.Core.Abstractions/system/ISystem.cs similarity index 86% rename from GFramework.Core/system/ISystem.cs rename to GFramework.Core.Abstractions/system/ISystem.cs index c9261b0..1dbb3aa 100644 --- a/GFramework.Core/system/ISystem.cs +++ b/GFramework.Core.Abstractions/system/ISystem.cs @@ -1,6 +1,6 @@ -using GFramework.Core.rule; +using GFramework.Core.Abstractions.rule; -namespace GFramework.Core.system; +namespace GFramework.Core.Abstractions.system; /// /// 系统接口,定义了系统的基本行为和功能 diff --git a/GFramework.Core/utility/IContextUtility.cs b/GFramework.Core.Abstractions/utility/IContextUtility.cs similarity index 77% rename from GFramework.Core/utility/IContextUtility.cs rename to GFramework.Core.Abstractions/utility/IContextUtility.cs index 34fdedb..245aba7 100644 --- a/GFramework.Core/utility/IContextUtility.cs +++ b/GFramework.Core.Abstractions/utility/IContextUtility.cs @@ -1,6 +1,6 @@ -using GFramework.Core.rule; +using GFramework.Core.Abstractions.rule; -namespace GFramework.Core.utility; +namespace GFramework.Core.Abstractions.utility; /// /// 上下文工具接口,继承自IUtility和IContextAware接口 diff --git a/GFramework.Core/utility/IUtility.cs b/GFramework.Core.Abstractions/utility/IUtility.cs similarity index 80% rename from GFramework.Core/utility/IUtility.cs rename to GFramework.Core.Abstractions/utility/IUtility.cs index f097af3..db2c418 100644 --- a/GFramework.Core/utility/IUtility.cs +++ b/GFramework.Core.Abstractions/utility/IUtility.cs @@ -1,4 +1,4 @@ -namespace GFramework.Core.utility; +namespace GFramework.Core.Abstractions.utility; /// /// IUtility接口定义了通用工具类的基本契约 diff --git a/GFramework.Core/GFramework.Core.csproj b/GFramework.Core/GFramework.Core.csproj index 8d83dd1..8eed18b 100644 --- a/GFramework.Core/GFramework.Core.csproj +++ b/GFramework.Core/GFramework.Core.csproj @@ -6,5 +6,7 @@ enable enable - + + + diff --git a/GFramework.Core/architecture/Architecture.cs b/GFramework.Core/architecture/Architecture.cs index 5a55424..731b7fa 100644 --- a/GFramework.Core/architecture/Architecture.cs +++ b/GFramework.Core/architecture/Architecture.cs @@ -1,9 +1,11 @@ +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.events; +using GFramework.Core.Abstractions.ioc; +using GFramework.Core.Abstractions.logging; +using GFramework.Core.Abstractions.model; +using GFramework.Core.Abstractions.system; +using GFramework.Core.Abstractions.utility; using GFramework.Core.events; -using GFramework.Core.ioc; -using GFramework.Core.logging; -using GFramework.Core.model; -using GFramework.Core.system; -using GFramework.Core.utility; namespace GFramework.Core.architecture; diff --git a/GFramework.Core/architecture/ArchitectureConfiguration.cs b/GFramework.Core/architecture/ArchitectureConfiguration.cs index 89f7f89..2cfb976 100644 --- a/GFramework.Core/architecture/ArchitectureConfiguration.cs +++ b/GFramework.Core/architecture/ArchitectureConfiguration.cs @@ -1,4 +1,6 @@ -using GFramework.Core.logging; +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.logging; +using GFramework.Core.logging; namespace GFramework.Core.architecture; diff --git a/GFramework.Core/architecture/ArchitectureConstants.cs b/GFramework.Core/architecture/ArchitectureConstants.cs index 19e71bc..7012b69 100644 --- a/GFramework.Core/architecture/ArchitectureConstants.cs +++ b/GFramework.Core/architecture/ArchitectureConstants.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using GFramework.Core.Abstractions.architecture; namespace GFramework.Core.architecture; diff --git a/GFramework.Core/architecture/ArchitectureContext.cs b/GFramework.Core/architecture/ArchitectureContext.cs index cb5265b..3b6472d 100644 --- a/GFramework.Core/architecture/ArchitectureContext.cs +++ b/GFramework.Core/architecture/ArchitectureContext.cs @@ -1,11 +1,13 @@ -using GFramework.Core.command; -using GFramework.Core.events; -using GFramework.Core.ioc; +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.events; +using GFramework.Core.Abstractions.ioc; +using GFramework.Core.Abstractions.logging; +using GFramework.Core.Abstractions.model; +using GFramework.Core.Abstractions.query; +using GFramework.Core.Abstractions.system; +using GFramework.Core.Abstractions.utility; using GFramework.Core.logging; -using GFramework.Core.model; -using GFramework.Core.query; -using GFramework.Core.system; -using GFramework.Core.utility; namespace GFramework.Core.architecture; diff --git a/GFramework.Core/architecture/ArchitectureRuntime.cs b/GFramework.Core/architecture/ArchitectureRuntime.cs index b5ac076..5d368d8 100644 --- a/GFramework.Core/architecture/ArchitectureRuntime.cs +++ b/GFramework.Core/architecture/ArchitectureRuntime.cs @@ -1,6 +1,7 @@ -using GFramework.Core.command; -using GFramework.Core.events; -using GFramework.Core.query; +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.events; +using GFramework.Core.Abstractions.query; namespace GFramework.Core.architecture; diff --git a/GFramework.Core/architecture/ArchitectureServices.cs b/GFramework.Core/architecture/ArchitectureServices.cs index 804b657..d25ec5f 100644 --- a/GFramework.Core/architecture/ArchitectureServices.cs +++ b/GFramework.Core/architecture/ArchitectureServices.cs @@ -1,4 +1,7 @@ -using GFramework.Core.events; +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.events; +using GFramework.Core.Abstractions.ioc; +using GFramework.Core.events; using GFramework.Core.ioc; namespace GFramework.Core.architecture; diff --git a/GFramework.Core/command/AbstractCommand.cs b/GFramework.Core/command/AbstractCommand.cs index 751777c..aef9baf 100644 --- a/GFramework.Core/command/AbstractCommand.cs +++ b/GFramework.Core/command/AbstractCommand.cs @@ -1,4 +1,5 @@ -using GFramework.Core.rule; +using GFramework.Core.Abstractions.command; +using GFramework.Core.rule; namespace GFramework.Core.command; diff --git a/GFramework.Core/events/DefaultUnRegister.cs b/GFramework.Core/events/DefaultUnRegister.cs index 78cb95c..ddc624b 100644 --- a/GFramework.Core/events/DefaultUnRegister.cs +++ b/GFramework.Core/events/DefaultUnRegister.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.events; +using GFramework.Core.Abstractions.events; + +namespace GFramework.Core.events; /// /// 默认注销器类,用于执行注销操作 diff --git a/GFramework.Core/events/EasyEvent.cs b/GFramework.Core/events/EasyEvent.cs index cfc9aca..5e4d3cf 100644 --- a/GFramework.Core/events/EasyEvent.cs +++ b/GFramework.Core/events/EasyEvent.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.events; +using GFramework.Core.Abstractions.events; + +namespace GFramework.Core.events; /// /// 简单事件类,用于注册、注销和触发无参事件回调 diff --git a/GFramework.Core/events/EasyEventGeneric.cs b/GFramework.Core/events/EasyEventGeneric.cs index 129d0f5..1cce0d6 100644 --- a/GFramework.Core/events/EasyEventGeneric.cs +++ b/GFramework.Core/events/EasyEventGeneric.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.events; +using GFramework.Core.Abstractions.events; + +namespace GFramework.Core.events; /// /// 泛型事件类,支持一个泛型参数 T 的事件注册、注销与触发。 diff --git a/GFramework.Core/events/EasyEvents.cs b/GFramework.Core/events/EasyEvents.cs index 32d979c..d8b84ce 100644 --- a/GFramework.Core/events/EasyEvents.cs +++ b/GFramework.Core/events/EasyEvents.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.events; +using GFramework.Core.Abstractions.events; + +namespace GFramework.Core.events; /// /// EasyEvents事件管理器类,用于全局事件的注册、获取和管理 diff --git a/GFramework.Core/events/OrEvent.cs b/GFramework.Core/events/OrEvent.cs index 2744693..8c5f34e 100644 --- a/GFramework.Core/events/OrEvent.cs +++ b/GFramework.Core/events/OrEvent.cs @@ -1,3 +1,4 @@ +using GFramework.Core.Abstractions.events; using GFramework.Core.extensions; namespace GFramework.Core.events; @@ -12,7 +13,7 @@ public class OrEvent : IUnRegisterList /// /// 获取取消注册列表 /// - public List UnregisterList { get; } = new(); + public IList UnregisterList { get; } /// /// 将指定的事件与当前OrEvent进行或逻辑组合 diff --git a/GFramework.Core/events/TypeEventSystem.cs b/GFramework.Core/events/TypeEventSystem.cs index fd54abb..5893056 100644 --- a/GFramework.Core/events/TypeEventSystem.cs +++ b/GFramework.Core/events/TypeEventSystem.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.events; +using GFramework.Core.Abstractions.events; + +namespace GFramework.Core.events; /// /// 类型事件系统,提供基于类型的事件发送、注册和注销功能 diff --git a/GFramework.Core/events/UnRegisterList.cs b/GFramework.Core/events/UnRegisterList.cs index c1bf0b0..3f413c3 100644 --- a/GFramework.Core/events/UnRegisterList.cs +++ b/GFramework.Core/events/UnRegisterList.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.events; +using GFramework.Core.Abstractions.events; + +namespace GFramework.Core.events; /// /// 取消注册列表类,用于管理多个需要取消注册的对象 @@ -10,7 +12,7 @@ public class UnRegisterList : IUnRegisterList /// /// 获取取消注册列表的只读属性 /// - public List UnregisterList { get; } + public IList UnregisterList { get; } /// /// 向取消注册列表中添加一个新的可取消注册对象 diff --git a/GFramework.Core/extensions/OrEventExtensions.cs b/GFramework.Core/extensions/OrEventExtensions.cs index 49a880f..96ddb04 100644 --- a/GFramework.Core/extensions/OrEventExtensions.cs +++ b/GFramework.Core/extensions/OrEventExtensions.cs @@ -1,4 +1,5 @@ -using GFramework.Core.events; +using GFramework.Core.Abstractions.events; +using GFramework.Core.events; namespace GFramework.Core.extensions; diff --git a/GFramework.Core/extensions/UnRegisterListExtension.cs b/GFramework.Core/extensions/UnRegisterListExtension.cs index 6a0a56d..adde12e 100644 --- a/GFramework.Core/extensions/UnRegisterListExtension.cs +++ b/GFramework.Core/extensions/UnRegisterListExtension.cs @@ -1,4 +1,4 @@ -using GFramework.Core.events; +using GFramework.Core.Abstractions.events; namespace GFramework.Core.extensions; diff --git a/GFramework.Core/ioc/IocContainer.cs b/GFramework.Core/ioc/IocContainer.cs index cbb289a..739c6ba 100644 --- a/GFramework.Core/ioc/IocContainer.cs +++ b/GFramework.Core/ioc/IocContainer.cs @@ -1,6 +1,7 @@ -using GFramework.Core.logging; +using GFramework.Core.Abstractions.ioc; +using GFramework.Core.Abstractions.logging; +using GFramework.Core.Abstractions.system; using GFramework.Core.rule; -using GFramework.Core.system; namespace GFramework.Core.ioc; diff --git a/GFramework.Core/logging/AbstractLogger.cs b/GFramework.Core/logging/AbstractLogger.cs index 622b749..68d9470 100644 --- a/GFramework.Core/logging/AbstractLogger.cs +++ b/GFramework.Core/logging/AbstractLogger.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.logging; +using GFramework.Core.Abstractions.logging; + +namespace GFramework.Core.logging; /// /// 日志抽象基类,封装日志级别判断、格式化与异常处理逻辑。 diff --git a/GFramework.Core/logging/ConsoleLogger.cs b/GFramework.Core/logging/ConsoleLogger.cs index b457f23..08afb58 100644 --- a/GFramework.Core/logging/ConsoleLogger.cs +++ b/GFramework.Core/logging/ConsoleLogger.cs @@ -1,3 +1,5 @@ +using GFramework.Core.Abstractions.logging; + namespace GFramework.Core.logging; /// diff --git a/GFramework.Core/logging/ConsoleLoggerFactory.cs b/GFramework.Core/logging/ConsoleLoggerFactory.cs index 770461c..711a02b 100644 --- a/GFramework.Core/logging/ConsoleLoggerFactory.cs +++ b/GFramework.Core/logging/ConsoleLoggerFactory.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.logging; +using GFramework.Core.Abstractions.logging; + +namespace GFramework.Core.logging; /// /// 控制台日志提供程序,用于创建控制台日志记录器实例 diff --git a/GFramework.Core/logging/NoopLogger.cs b/GFramework.Core/logging/NoopLogger.cs index 2767fe6..b77b3b5 100644 --- a/GFramework.Core/logging/NoopLogger.cs +++ b/GFramework.Core/logging/NoopLogger.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.logging; +using GFramework.Core.Abstractions.logging; + +namespace GFramework.Core.logging; /// /// 空操作日志记录器实现,不执行任何实际的日志记录操作 diff --git a/GFramework.Core/logging/NoopLoggerFactory.cs b/GFramework.Core/logging/NoopLoggerFactory.cs index 3b505b6..14d8628 100644 --- a/GFramework.Core/logging/NoopLoggerFactory.cs +++ b/GFramework.Core/logging/NoopLoggerFactory.cs @@ -1,4 +1,6 @@ -namespace GFramework.Core.logging; +using GFramework.Core.Abstractions.logging; + +namespace GFramework.Core.logging; /// /// 无操作日志工厂实现,用于提供空的日志记录功能 diff --git a/GFramework.Core/model/AbstractModel.cs b/GFramework.Core/model/AbstractModel.cs index 9e9d9f0..13d2973 100644 --- a/GFramework.Core/model/AbstractModel.cs +++ b/GFramework.Core/model/AbstractModel.cs @@ -1,4 +1,5 @@ -using GFramework.Core.architecture; +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.model; namespace GFramework.Core.model; diff --git a/GFramework.Core/property/BindableProperty.cs b/GFramework.Core/property/BindableProperty.cs index 6cac10b..4154176 100644 --- a/GFramework.Core/property/BindableProperty.cs +++ b/GFramework.Core/property/BindableProperty.cs @@ -1,4 +1,5 @@ -using GFramework.Core.events; +using GFramework.Core.Abstractions.events; +using GFramework.Core.Abstractions.property; namespace GFramework.Core.property; @@ -48,6 +49,21 @@ public class BindableProperty(T defaultValue = default!) : IBindableProperty< MValue = newValue; } + /// + /// 实现IEasyEvent接口的注册方法,将无参事件转换为有参事件处理 + /// + /// 无参事件回调 + /// 可用于取消注册的接口 + IUnRegister IEasyEvent.Register(Action onEvent) + { + return Register(Action); + + void Action(T _) + { + onEvent(); + } + } + /// /// 注册属性值变化事件回调 /// @@ -79,21 +95,6 @@ public class BindableProperty(T defaultValue = default!) : IBindableProperty< _mOnValueChanged -= onValueChanged; } - /// - /// 实现IEasyEvent接口的注册方法,将无参事件转换为有参事件处理 - /// - /// 无参事件回调 - /// 可用于取消注册的接口 - IUnRegister IEasyEvent.Register(Action onEvent) - { - return Register(Action); - - void Action(T _) - { - onEvent(); - } - } - /// /// 设置自定义比较器 /// diff --git a/GFramework.Core/property/BindablePropertyUnRegister.cs b/GFramework.Core/property/BindablePropertyUnRegister.cs index b3526ad..28a1fa0 100644 --- a/GFramework.Core/property/BindablePropertyUnRegister.cs +++ b/GFramework.Core/property/BindablePropertyUnRegister.cs @@ -1,4 +1,4 @@ -using GFramework.Core.events; +using GFramework.Core.Abstractions.events; namespace GFramework.Core.property; diff --git a/GFramework.Core/query/AbstractQuery.cs b/GFramework.Core/query/AbstractQuery.cs index efb6381..fbbc1fe 100644 --- a/GFramework.Core/query/AbstractQuery.cs +++ b/GFramework.Core/query/AbstractQuery.cs @@ -1,3 +1,4 @@ +using GFramework.Core.Abstractions.query; using GFramework.Core.rule; namespace GFramework.Core.query; diff --git a/GFramework.Core/rule/ContextAwareBase.cs b/GFramework.Core/rule/ContextAwareBase.cs index 696ddbf..09a8de2 100644 --- a/GFramework.Core/rule/ContextAwareBase.cs +++ b/GFramework.Core/rule/ContextAwareBase.cs @@ -1,4 +1,5 @@ -using GFramework.Core.architecture; +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.rule; namespace GFramework.Core.rule; diff --git a/GFramework.Core/system/AbstractSystem.cs b/GFramework.Core/system/AbstractSystem.cs index d066820..aab9b5a 100644 --- a/GFramework.Core/system/AbstractSystem.cs +++ b/GFramework.Core/system/AbstractSystem.cs @@ -1,4 +1,5 @@ -using GFramework.Core.logging; +using GFramework.Core.Abstractions.logging; +using GFramework.Core.Abstractions.system; using GFramework.Core.rule; namespace GFramework.Core.system; diff --git a/GFramework.Core/utility/AbstractContextUtility.cs b/GFramework.Core/utility/AbstractContextUtility.cs index 86fa4a3..a068eb8 100644 --- a/GFramework.Core/utility/AbstractContextUtility.cs +++ b/GFramework.Core/utility/AbstractContextUtility.cs @@ -1,4 +1,5 @@ -using GFramework.Core.logging; +using GFramework.Core.Abstractions.logging; +using GFramework.Core.Abstractions.utility; using GFramework.Core.rule; namespace GFramework.Core.utility; diff --git a/GFramework.Godot.SourceGenerators/GFramework.Godot.SourceGenerators.csproj b/GFramework.Godot.SourceGenerators/GFramework.Godot.SourceGenerators.csproj index 41b198c..d0c2e31 100644 --- a/GFramework.Godot.SourceGenerators/GFramework.Godot.SourceGenerators.csproj +++ b/GFramework.Godot.SourceGenerators/GFramework.Godot.SourceGenerators.csproj @@ -26,7 +26,7 @@ - + diff --git a/GFramework.SourceGenerators.Common/GFramework.SourceGenerators.Common.csproj b/GFramework.SourceGenerators.Common/GFramework.SourceGenerators.Common.csproj index 2bcf12c..eb20b60 100644 --- a/GFramework.SourceGenerators.Common/GFramework.SourceGenerators.Common.csproj +++ b/GFramework.SourceGenerators.Common/GFramework.SourceGenerators.Common.csproj @@ -8,6 +8,7 @@ false true T:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute + enable @@ -16,6 +17,11 @@ + + all + runtime; build; native; contentfiles; analyzers + + diff --git a/GFramework.SourceGenerators.Common/diagnostics/CommonDiagnostics.cs b/GFramework.SourceGenerators.Common/diagnostics/CommonDiagnostics.cs index 78cd1b8..2bea910 100644 --- a/GFramework.SourceGenerators.Common/diagnostics/CommonDiagnostics.cs +++ b/GFramework.SourceGenerators.Common/diagnostics/CommonDiagnostics.cs @@ -24,6 +24,6 @@ public static class CommonDiagnostics "Class '{0}' must be declared partial for code generation", "GFramework.Common", DiagnosticSeverity.Error, - true + isEnabledByDefault: true ); } \ No newline at end of file diff --git a/GFramework.SourceGenerators.Common/generator/AttributeClassGeneratorBase.cs b/GFramework.SourceGenerators.Common/generator/AttributeClassGeneratorBase.cs new file mode 100644 index 0000000..366a885 --- /dev/null +++ b/GFramework.SourceGenerators.Common/generator/AttributeClassGeneratorBase.cs @@ -0,0 +1,173 @@ +using System; +using System.Linq; +using GFramework.SourceGenerators.Common.diagnostics; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace GFramework.SourceGenerators.Common.generator; + +/// +/// 属性类生成器基类,用于处理带有特定属性的类并生成相应的源代码 +/// +public abstract class AttributeClassGeneratorBase : IIncrementalGenerator +{ + /// + /// 获取属性的元数据名称 + /// + protected abstract Type AttributeType { get; } + + /// + /// Attribute 的短名称(不含 Attribute 后缀) + /// 仅用于 Syntax 层宽松匹配 + /// + protected abstract string AttributeShortNameWithoutSuffix { get; } + + /// + /// 初始化增量生成器 + /// + /// 增量生成器初始化上下文 + public void Initialize(IncrementalGeneratorInitializationContext context) + { + var targets = context.SyntaxProvider.CreateSyntaxProvider( + predicate: (node, _) => + node is ClassDeclarationSyntax cls && + cls.AttributeLists + .SelectMany(a => a.Attributes) + .Any(a => a.Name.ToString() + .Contains(AttributeShortNameWithoutSuffix)), + transform: static (ctx, t) => + { + var cls = (ClassDeclarationSyntax)ctx.Node; + var symbol = ctx.SemanticModel.GetDeclaredSymbol(cls, cancellationToken: t); + return (ClassDecl: cls, Symbol: symbol); + } + ) + .Where(x => x.Symbol is not null); + + context.RegisterSourceOutput(targets, (spc, pair) => + { + try + { + Execute(spc, pair.ClassDecl, pair.Symbol!); + } + catch (Exception ex) + { + EmitError(spc, pair.Symbol, ex); + } + }); + } + + /// + /// 执行源代码生成的主要逻辑 + /// + /// 源生产上下文 + /// 类声明语法节点 + /// 命名类型符号 + private void Execute( + SourceProductionContext context, + ClassDeclarationSyntax classDecl, + INamedTypeSymbol symbol) + { + var attr = GetAttribute(symbol); + if (attr == null) return; + + // partial 校验 + if (!classDecl.Modifiers.Any(SyntaxKind.PartialKeyword)) + { + ReportClassMustBePartial(context, classDecl, symbol); + return; + } + + // 子类校验 + if (!ValidateSymbol(context, classDecl, symbol, attr)) + return; + + var source = Generate(symbol, attr); + context.AddSource(GetHintName(symbol), source); + } + + #region 可覆写点 + + /// + /// 验证符号的有效性 + /// + /// 源生产上下文 + /// 类声明语法节点 + /// 命名类型符号 + /// 属性数据 + /// 验证是否通过 + protected virtual bool ValidateSymbol( + SourceProductionContext context, + ClassDeclarationSyntax syntax, + INamedTypeSymbol symbol, + AttributeData attr) + => true; + + /// + /// 生成源代码 + /// + /// 命名类型符号 + /// 属性数据 + /// 生成的源代码字符串 + protected abstract string Generate( + INamedTypeSymbol symbol, + AttributeData attr); + + /// + /// 获取生成文件的提示名称 + /// + /// 命名类型符号 + /// 生成文件的提示名称 + protected virtual string GetHintName(INamedTypeSymbol symbol) + => $"{symbol.Name}.g.cs"; + + #endregion + + #region Attribute / Diagnostic + + /// + /// 获取指定符号的属性数据 + /// + /// 命名类型符号 + /// 属性数据,如果未找到则返回null + protected virtual AttributeData? GetAttribute(INamedTypeSymbol symbol) + => symbol.GetAttributes().FirstOrDefault(a => + string.Equals(a.AttributeClass?.ToDisplayString(), AttributeType.FullName, StringComparison.Ordinal)); + + /// + /// 报告类必须是partial的诊断信息 + /// + /// 源生产上下文 + /// 类声明语法节点 + /// 命名类型符号 + protected virtual void ReportClassMustBePartial( + SourceProductionContext context, + ClassDeclarationSyntax syntax, + INamedTypeSymbol symbol) + { + context.ReportDiagnostic(Diagnostic.Create( + CommonDiagnostics.ClassMustBePartial, + syntax.Identifier.GetLocation(), + symbol.Name)); + } + + /// + /// 发出错误信息 + /// + /// 源生产上下文 + /// 命名类型符号 + /// 异常对象 + protected virtual void EmitError( + SourceProductionContext context, + INamedTypeSymbol? symbol, + Exception ex) + { + var name = symbol?.Name ?? "Unknown"; + var text = + $"// source generator error: {ex.Message}\n// {ex.StackTrace}"; + context.AddSource($"{name}.Error.g.cs", text); + } + + #endregion +} \ No newline at end of file diff --git a/GFramework.SourceGenerators/GFramework.SourceGenerators.csproj b/GFramework.SourceGenerators/GFramework.SourceGenerators.csproj index cec2481..e43c807 100644 --- a/GFramework.SourceGenerators/GFramework.SourceGenerators.csproj +++ b/GFramework.SourceGenerators/GFramework.SourceGenerators.csproj @@ -26,10 +26,11 @@ - + + diff --git a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs index d6a8ffb..90a885e 100644 --- a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs +++ b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs @@ -1,102 +1,60 @@ -using System.Linq; +using System; +using System.Linq; using System.Text; -using GFramework.SourceGenerators.Common.diagnostics; -using GFramework.SourceGenerators.constants; +using GFramework.SourceGenerators.Abstractions.rule; +using GFramework.SourceGenerators.Common.generator; using GFramework.SourceGenerators.diagnostics; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace GFramework.SourceGenerators.rule; [Generator] -public sealed class ContextAwareGenerator : IIncrementalGenerator +public sealed class ContextAwareGenerator : AttributeClassGeneratorBase { - private const string AttributeMetadataName = - $"{PathContests.RootAbstractionsPath}.rule.ContextAwareAttribute"; + /// + /// 使用强类型 Attribute,替代字符串 + /// + protected override Type AttributeType => typeof(ContextAwareAttribute); - public void Initialize(IncrementalGeneratorInitializationContext context) + /// + /// 仅用于 Syntax 粗筛选 + /// + protected override string AttributeShortNameWithoutSuffix => "ContextAware"; + + /// + /// 额外语义校验:必须实现 IContextAware + /// + protected override bool ValidateSymbol( + SourceProductionContext context, + ClassDeclarationSyntax syntax, + INamedTypeSymbol symbol, + AttributeData attr) { - // 1️⃣ 查找候选类 - var candidates = context.SyntaxProvider - .CreateSyntaxProvider( - predicate: static (node, _) => node is ClassDeclarationSyntax, - transform: static (ctx, _) => GetCandidate(ctx) - ) - .Where(static s => s is not null); - - // 2️⃣ 注册生成输出 - context.RegisterSourceOutput(candidates, static (spc, symbol) => - { - if (symbol != null) - GenerateOutput(spc, symbol); - }); - } - - #region 候选类查找 - - private static INamedTypeSymbol? GetCandidate(GeneratorSyntaxContext context) - { - if (context.SemanticModel.GetDeclaredSymbol(context.Node) is not INamedTypeSymbol symbol) - return null; - - // 仅筛选带有 ContextAwareAttribute 的类 - var hasAttr = symbol.GetAttributes() - .Any(attr => attr.AttributeClass?.ToDisplayString() == AttributeMetadataName); - - return hasAttr ? symbol : null; - } - - #endregion - - #region 输出生成 + 诊断 - - private static void GenerateOutput(SourceProductionContext context, INamedTypeSymbol symbol) - { - var syntax = symbol.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() as ClassDeclarationSyntax; - if (syntax == null) - return; - - // 1️⃣ 必须是 partial - if (!syntax.Modifiers.Any(SyntaxKind.PartialKeyword)) - { - context.ReportDiagnostic(Diagnostic.Create( - CommonDiagnostics.ClassMustBePartial, - syntax.Identifier.GetLocation(), - symbol.Name - )); - return; - } - - // 2️⃣ 必须实现 IContextAware(直接或间接) - if (!symbol.AllInterfaces.Any(i => i.ToDisplayString() == "GFramework.Core.rule.IContextAware")) + if (!symbol.AllInterfaces.Any(i => + i.ToDisplayString() == typeof(IContextAware).FullName)) { context.ReportDiagnostic(Diagnostic.Create( ContextAwareDiagnostic.ClassMustImplementIContextAware, syntax.Identifier.GetLocation(), - symbol.Name - )); - return; + symbol.Name)); + return false; } - // 3️⃣ 生成源码 + return true; + } + + /// + /// 生成源码 + /// + protected override string Generate( + INamedTypeSymbol symbol, + AttributeData attr) + { var ns = symbol.ContainingNamespace.IsGlobalNamespace ? null : symbol.ContainingNamespace.ToDisplayString(); - var source = GenerateSource(ns, symbol); - context - .AddSource( - $"{symbol.Name}.ContextAware.g.cs", - source); - } - - #endregion - - #region 源码生成 - - private static string GenerateSource(string? ns, INamedTypeSymbol symbol) - { var sb = new StringBuilder(); sb.AppendLine("// "); sb.AppendLine("#nullable enable"); @@ -121,5 +79,9 @@ public sealed class ContextAwareGenerator : IIncrementalGenerator return sb.ToString().TrimEnd(); } - #endregion + /// + /// 自定义生成文件名 + /// + protected override string GetHintName(INamedTypeSymbol symbol) + => $"{symbol.Name}.ContextAware.g.cs"; } \ No newline at end of file diff --git a/GFramework.csproj b/GFramework.csproj index bbdeb31..f8b9dd3 100644 --- a/GFramework.csproj +++ b/GFramework.csproj @@ -43,6 +43,7 @@ + @@ -69,6 +70,7 @@ + @@ -81,6 +83,7 @@ + diff --git a/GFramework.sln b/GFramework.sln index 76f7d5f..25d6dac 100644 --- a/GFramework.sln +++ b/GFramework.sln @@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.SourceGenerators EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.SourceGenerators.Common", "GFramework.SourceGenerators.Common\GFramework.SourceGenerators.Common.csproj", "{B6511C9A-40E1-4E51-8D1F-18EAFB3C5BFC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.Core.Abstractions", "GFramework.Core.Abstractions\GFramework.Core.Abstractions.csproj", "{31BA9F62-153A-4943-A8A0-7571FC7D5FEE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -66,5 +68,9 @@ Global {B6511C9A-40E1-4E51-8D1F-18EAFB3C5BFC}.Debug|Any CPU.Build.0 = Debug|Any CPU {B6511C9A-40E1-4E51-8D1F-18EAFB3C5BFC}.Release|Any CPU.ActiveCfg = Release|Any CPU {B6511C9A-40E1-4E51-8D1F-18EAFB3C5BFC}.Release|Any CPU.Build.0 = Release|Any CPU + {31BA9F62-153A-4943-A8A0-7571FC7D5FEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31BA9F62-153A-4943-A8A0-7571FC7D5FEE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31BA9F62-153A-4943-A8A0-7571FC7D5FEE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31BA9F62-153A-4943-A8A0-7571FC7D5FEE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal