Skip to content

Commit

Permalink
[feat]新增代码生成插件ICodePlugin,各个团队可以根据自己需要编写插件,从而控制代码生成效果
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Nov 24, 2024
1 parent 198ee23 commit 30a8942
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion XCode.PostgreSQL/XCode.PostgreSQL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Npgsql" Version="8.0.5" />
<PackageReference Include="Npgsql" Version="8.0.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\XCode\XCode.csproj" />
Expand Down
39 changes: 39 additions & 0 deletions XCode/Code/ICodePlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using NewLife;
using NewLife.Model;
using XCode.DataAccessLayer;

namespace XCode.Code;

/// <summary>代码生成插件</summary>
public interface ICodePlugin : IPlugin
{
/// <summary>修正数据表</summary>
/// <param name="tables"></param>
void FixTables(IList<IDataTable> tables);
}

/// <summary>代码生成插件基类</summary>
[Plugin("CodeBuild")]
public abstract class CodePlugin : DisposeBase, ICodePlugin
{
/// <summary>服务提供者</summary>
public IServiceProvider? Provider { get; set; }

/// <summary>初始化插件</summary>
/// <param name="identity"></param>
/// <param name="provider"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public virtual Boolean Init(String? identity, IServiceProvider provider)
{
if (identity != "CodeBuild") return false;

Provider = provider;

return true;
}

/// <summary>修正数据表</summary>
/// <param name="tables"></param>
public virtual void FixTables(IList<IDataTable> tables) { }
}
2 changes: 1 addition & 1 deletion XCode/XCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="NewLife.Core" Version="11.0.2024.1115" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1124-beta1518" />
</ItemGroup>
<ItemGroup>
<Compile Remove="build\**" />
Expand Down
31 changes: 28 additions & 3 deletions XCodeTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using NewLife;
using NewLife.Log;
using NewLife.Model;
using XCode;
using XCode.Code;

Expand Down Expand Up @@ -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();
Expand All @@ -57,7 +67,7 @@ static void Main(string[] args)
return;
}

Build(file, log);
Build(file, log, manager);
}
else
{
Expand All @@ -77,7 +87,7 @@ static void Main(string[] args)
// 循环处理
foreach (var item in files)
{
Build(item, log);
Build(item, log, manager);
}
}
else
Expand All @@ -96,7 +106,7 @@ static void Main(string[] args)

/// <summary>生成实体类。用户可以调整该方法可以改变生成实体类代码的逻辑,从而得到自己的代码生成器</summary>
/// <param name="modelFile"></param>
static void Build(String modelFile, ILog log)
static void Build(String modelFile, ILog log, PluginManager manager)
{
XTrace.WriteLine("正在处理:{0}", modelFile);

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions XUnitTest.XCode/XUnitTest.XCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1115" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1124-beta1518" />
<PackageReference Include="NewLife.IP" Version="2.2.2024.1102" />
<PackageReference Include="NewLife.UnitTest" Version="1.0.2024.1006" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="9.0.0" />
Expand Down

0 comments on commit 30a8942

Please sign in to comment.