From 30a8942080dce03020a5350e796d8f39bf0fac74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sun, 24 Nov 2024 23:46:12 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=E6=96=B0=E5=A2=9E=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=8F=92=E4=BB=B6ICodePlugin=EF=BC=8C?= =?UTF-8?q?=E5=90=84=E4=B8=AA=E5=9B=A2=E9=98=9F=E5=8F=AF=E4=BB=A5=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E8=87=AA=E5=B7=B1=E9=9C=80=E8=A6=81=E7=BC=96=E5=86=99?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=EF=BC=8C=E4=BB=8E=E8=80=8C=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XCode.PostgreSQL/XCode.PostgreSQL.csproj | 2 +- XCode/Code/ICodePlugin.cs | 39 ++++++++++++++++++++++++ XCode/XCode.csproj | 2 +- XCodeTool/Program.cs | 31 +++++++++++++++++-- XUnitTest.XCode/XUnitTest.XCode.csproj | 4 +-- 5 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 XCode/Code/ICodePlugin.cs diff --git a/XCode.PostgreSQL/XCode.PostgreSQL.csproj b/XCode.PostgreSQL/XCode.PostgreSQL.csproj index df8c222e5..21705da41 100644 --- a/XCode.PostgreSQL/XCode.PostgreSQL.csproj +++ b/XCode.PostgreSQL/XCode.PostgreSQL.csproj @@ -36,7 +36,7 @@ - + diff --git a/XCode/Code/ICodePlugin.cs b/XCode/Code/ICodePlugin.cs new file mode 100644 index 000000000..d0c2cccce --- /dev/null +++ b/XCode/Code/ICodePlugin.cs @@ -0,0 +1,39 @@ +using NewLife; +using NewLife.Model; +using XCode.DataAccessLayer; + +namespace XCode.Code; + +/// 代码生成插件 +public interface ICodePlugin : IPlugin +{ + /// 修正数据表 + /// + void FixTables(IList tables); +} + +/// 代码生成插件基类 +[Plugin("CodeBuild")] +public abstract class CodePlugin : DisposeBase, ICodePlugin +{ + /// 服务提供者 + public IServiceProvider? Provider { get; set; } + + /// 初始化插件 + /// + /// + /// + /// + public virtual Boolean Init(String? identity, IServiceProvider provider) + { + if (identity != "CodeBuild") return false; + + Provider = provider; + + return true; + } + + /// 修正数据表 + /// + public virtual void FixTables(IList tables) { } +} \ No newline at end of file diff --git a/XCode/XCode.csproj b/XCode/XCode.csproj index 8a96718fe..e10f9c661 100644 --- a/XCode/XCode.csproj +++ b/XCode/XCode.csproj @@ -44,7 +44,7 @@ - + diff --git a/XCodeTool/Program.cs b/XCodeTool/Program.cs index 344d1791c..e1edf9e44 100644 --- a/XCodeTool/Program.cs +++ b/XCodeTool/Program.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using NewLife; using NewLife.Log; +using NewLife.Model; using XCode; using XCode.Code; @@ -41,6 +42,15 @@ static void Main(string[] args) // 检查工具 var task = Task.Run(CheckTool); + // 加载插件 + var manager = new PluginManager + { + Identity = "CodeBuild", + Log = XTrace.Log, + }; + manager.Load(); + manager.Init(); + // 在当前目录查找模型文件 var file = ""; if (args.Length > 0) file = args.LastOrDefault(); @@ -57,7 +67,7 @@ static void Main(string[] args) return; } - Build(file, log); + Build(file, log, manager); } else { @@ -77,7 +87,7 @@ static void Main(string[] args) // 循环处理 foreach (var item in files) { - Build(item, log); + Build(item, log, manager); } } else @@ -96,7 +106,7 @@ static void Main(string[] args) /// 生成实体类。用户可以调整该方法可以改变生成实体类代码的逻辑,从而得到自己的代码生成器 /// - static void Build(String modelFile, ILog log) + static void Build(String modelFile, ILog log, PluginManager manager) { XTrace.WriteLine("正在处理:{0}", modelFile); @@ -115,6 +125,21 @@ static void Build(String modelFile, ILog log) try { XTrace.WriteLine("修正模型:{0}", modelFile); + foreach (var item in manager.Plugins) + { + if (item is ICodePlugin plugin) + { + try + { + plugin.FixTables(tables); + } + catch (Exception ex) + { + XTrace.WriteException(ex); + } + } + } + EntityBuilder.FixModelFile(modelFile, option, atts, tables, log); } catch (Exception ex) diff --git a/XUnitTest.XCode/XUnitTest.XCode.csproj b/XUnitTest.XCode/XUnitTest.XCode.csproj index 23601067e..48445eebf 100644 --- a/XUnitTest.XCode/XUnitTest.XCode.csproj +++ b/XUnitTest.XCode/XUnitTest.XCode.csproj @@ -80,8 +80,8 @@ - - + +