feat: 添加代码生成器模块

This commit is contained in:
橙子
2024-02-16 15:40:13 +08:00
parent da1d24bb5c
commit f7ef896f48
48 changed files with 986 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
common.props = common.props
end.sh = end.sh
start.sh = start.sh
usings.props = usings.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.SqlSugarCore.Abstractions", "framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj", "{FD6D6860-3753-4747-8A26-977E4A3001F9}"
@@ -104,6 +105,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.TenantManageme
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.TenantManagement.Application.Contracts", "module\tenant-management\Yi.Framework.TenantManagement.Application.Contracts\Yi.Framework.TenantManagement.Application.Contracts.csproj", "{FA735055-CBDD-4EFD-B84B-85810DA1425E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "code-gun", "code-gun", "{4FFE7212-21F2-476D-B628-3C65E6C5075E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.CodeGun.Application", "module\code-gun\Yi.Framework.CodeGun.Application\Yi.Framework.CodeGun.Application.csproj", "{97EC40D7-DBFA-467A-98CB-221AF27B14F2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.CodeGun.Application.Contracts", "module\code-gun\Yi.Framework.CodeGun.Application.Contracts\Yi.Framework.CodeGun.Application.Contracts.csproj", "{882BC563-2F75-4B95-AC96-F4BF23F5E69D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.CodeGun.Domain", "module\code-gun\Yi.Framework.CodeGun.Domain\Yi.Framework.CodeGun.Domain.csproj", "{85CB8517-2B80-42D8-B954-081079AC9BA0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.CodeGun.Domain.Shared", "module\code-gun\Yi.Framework.CodeGun.Domain.Shared\Yi.Framework.CodeGun.Domain.Shared.csproj", "{EEFF0F05-2709-4151-A8CE-667935CEAE0B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -262,6 +273,22 @@ Global
{FA735055-CBDD-4EFD-B84B-85810DA1425E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA735055-CBDD-4EFD-B84B-85810DA1425E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA735055-CBDD-4EFD-B84B-85810DA1425E}.Release|Any CPU.Build.0 = Release|Any CPU
{97EC40D7-DBFA-467A-98CB-221AF27B14F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97EC40D7-DBFA-467A-98CB-221AF27B14F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97EC40D7-DBFA-467A-98CB-221AF27B14F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97EC40D7-DBFA-467A-98CB-221AF27B14F2}.Release|Any CPU.Build.0 = Release|Any CPU
{882BC563-2F75-4B95-AC96-F4BF23F5E69D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{882BC563-2F75-4B95-AC96-F4BF23F5E69D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{882BC563-2F75-4B95-AC96-F4BF23F5E69D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{882BC563-2F75-4B95-AC96-F4BF23F5E69D}.Release|Any CPU.Build.0 = Release|Any CPU
{85CB8517-2B80-42D8-B954-081079AC9BA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85CB8517-2B80-42D8-B954-081079AC9BA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85CB8517-2B80-42D8-B954-081079AC9BA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85CB8517-2B80-42D8-B954-081079AC9BA0}.Release|Any CPU.Build.0 = Release|Any CPU
{EEFF0F05-2709-4151-A8CE-667935CEAE0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEFF0F05-2709-4151-A8CE-667935CEAE0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEFF0F05-2709-4151-A8CE-667935CEAE0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEFF0F05-2709-4151-A8CE-667935CEAE0B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -309,6 +336,11 @@ Global
{9C8C3C53-3DCE-4516-867E-228858E61B26} = {73CCF2C4-B9FD-44AB-8D4B-0A421805B094}
{17816837-E53B-486B-B796-53C601FE6CD9} = {499A8C71-7892-42D0-A77E-48756E1EFF16}
{FA735055-CBDD-4EFD-B84B-85810DA1425E} = {499A8C71-7892-42D0-A77E-48756E1EFF16}
{4FFE7212-21F2-476D-B628-3C65E6C5075E} = {2317227D-7796-4E7B-BEDB-7CD1CAE7B853}
{97EC40D7-DBFA-467A-98CB-221AF27B14F2} = {4FFE7212-21F2-476D-B628-3C65E6C5075E}
{882BC563-2F75-4B95-AC96-F4BF23F5E69D} = {4FFE7212-21F2-476D-B628-3C65E6C5075E}
{85CB8517-2B80-42D8-B954-081079AC9BA0} = {4FFE7212-21F2-476D-B628-3C65E6C5075E}
{EEFF0F05-2709-4151-A8CE-667935CEAE0B} = {4FFE7212-21F2-476D-B628-3C65E6C5075E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {23D6FBC9-C970-4641-BC1E-2AEA59F51C18}

View File

@@ -1,4 +1,6 @@
<Project>
<Import Project="usings.props" />
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>

View File

@@ -0,0 +1,42 @@
using Volo.Abp.Application.Dtos;
using Yi.Framework.CodeGun.Domain.Shared.Enums;
namespace Yi.Framework.CodeGun.Application.Contracts.Dtos.Field
{
public class FieldDto : EntityDto<Guid>
{
/// <summary>
/// 字段名称
/// </summary>
public string Name { get; set; }
public string? Description { get; set; }
public int OrderNum { get; set; }
public int Length { get; set; }
public FieldTypeEnum FieldType { get; set; }
public Guid TableId { get; set; }
/// <summary>
/// 是否必填
/// </summary>
public bool IsRequired { get; set; }
/// <summary>
/// 是否主键
/// </summary>
public bool IsKey { get; set; }
/// <summary>
/// 是否自增
/// </summary>
public bool IsAutoAdd { get; set; }
/// <summary>
/// 是否公共
/// </summary>
public bool IsPublic { get; set; }
public Guid Id { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using Volo.Abp.Application.Dtos;
namespace Yi.Framework.CodeGun.Application.Contracts.Dtos.Field
{
public class FieldGetListInput : PagedAndSortedResultRequestDto
{
/// <summary>
/// 字段名称
/// </summary>
public string? Name { get; set; }
public long? TableId { get; set; }
}
}

View File

@@ -0,0 +1,24 @@
using Volo.Abp.Application.Dtos;
using Yi.Framework.CodeGun.Application.Contracts.Dtos.Field;
namespace Yi.Framework.CodeGun.Application.Contracts.Dtos.Table
{
public class TableDto : EntityDto<Guid>
{
public Guid Id { get; set; }
/// <summary>
/// 表名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 备注
/// </summary>
public string? Description { get; set; }
/// <summary>
/// 一表多字段
/// </summary>
public List<FieldDto> Fields { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
using Volo.Abp.Application.Dtos;
namespace Yi.Framework.CodeGun.Application.Contracts.Dtos.Table
{
public class TableGetListInput : PagedAndSortedResultRequestDto
{
}
}

View File

@@ -0,0 +1,30 @@
using Volo.Abp.Application.Dtos;
namespace Yi.Framework.CodeGun.Application.Contracts.Dtos.Template
{
public class TemplateDto : EntityDto<Guid>
{
public Guid Id { get; set; }
/// <summary>
/// 模板字符串
/// </summary>
public string TemplateStr { get; set; } = string.Empty;
/// <summary>
/// 生成路径
/// </summary>
public string BuildPath { get; set; }
/// <summary>
/// 模板名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 备注
/// </summary>
public string? Remarks { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using Volo.Abp.Application.Dtos;
namespace Yi.Framework.CodeGun.Application.Contracts.Dtos.Template
{
public class TemplateGetListInput : PagedAndSortedResultRequestDto
{
/// <summary>
/// 模板名称
/// </summary>
public string? Name { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
using Volo.Abp.Application.Services;
namespace Yi.Framework.CodeGun.Application.Contracts.IServices
{
public interface ICodeGunService : IApplicationService
{
}
}

View File

@@ -0,0 +1,9 @@
using Yi.Framework.CodeGun.Application.Contracts.Dtos.Field;
using Yi.Framework.Ddd.Application.Contracts;
namespace Yi.Framework.CodeGun.Application.Contracts.IServices
{
public interface IFieldService : IYiCrudAppService<FieldDto, Guid, FieldGetListInput>
{
}
}

View File

@@ -0,0 +1,9 @@
using Yi.Framework.CodeGun.Application.Contracts.Dtos.Table;
using Yi.Framework.Ddd.Application.Contracts;
namespace Yi.Framework.CodeGun.Application.Contracts.IServices
{
public interface ITableService : IYiCrudAppService<TableDto, Guid, TableGetListInput>
{
}
}

View File

@@ -0,0 +1,9 @@
using Yi.Framework.CodeGun.Application.Contracts.Dtos.Template;
using Yi.Framework.Ddd.Application.Contracts;
namespace Yi.Framework.CodeGun.Application.Contracts.IServices
{
public interface ITemplateService : IYiCrudAppService<TemplateDto, Guid, TemplateGetListInput>
{
}
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\common.props" />
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.Ddd.Application.Contracts\Yi.Framework.Ddd.Application.Contracts.csproj" />
<ProjectReference Include="..\Yi.Framework.CodeGun.Domain.Shared\Yi.Framework.CodeGun.Domain.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,12 @@
using Yi.Framework.CodeGun.Domain.Shared;
using Yi.Framework.Ddd.Application.Contracts;
namespace Yi.Framework.CodeGun.Application.Contracts
{
[DependsOn(typeof(YiFrameworkCodeGunDomainSharedModule),
typeof(YiFrameworkDddApplicationContractsModule))]
public class YiFrameworkCodeGunApplicationContractsModule:AbpModule
{
}
}

View File

@@ -0,0 +1,97 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using Volo.Abp.Application.Services;
using Volo.Abp.Uow;
using Yi.Framework.CodeGun.Application.Contracts.IServices;
using Yi.Framework.CodeGun.Domain.Entities;
using Yi.Framework.CodeGun.Domain.Managers;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.CodeGun.Application.Services
{
/// <summary>
/// CodeGun
/// </summary>
public class CodeGunService : ApplicationService, ICodeGunService
{
private ISqlSugarRepository<TableAggregateRoot, Guid> _tableRepository;
private CodeFileManager _codeFileManager;
private WebTemplateManager _webTemplateManager;
public CodeGunService(ISqlSugarRepository<TableAggregateRoot, Guid> tableRepository, CodeFileManager codeFileManager, WebTemplateManager webTemplateManager)
{
_tableRepository = tableRepository;
_codeFileManager = codeFileManager;
_webTemplateManager = webTemplateManager;
}
/// <summary>
/// Web To Code
/// </summary>
/// <returns></returns>
public async Task PostWebBuildCodeAsync(List<Guid> ids)
{
//获取全部表
var tables = await _tableRepository._DbQueryable.Where(x => ids.Contains(x.Id)).Includes(x => x.Fields).ToListAsync();
foreach (var table in tables)
{
await _codeFileManager.BuildWebToCodeAsync(table);
}
}
/// <summary>
/// Web To Db
/// </summary>
/// <returns></returns>
public async Task PostWebBuildDbAsync()
{
throw new NotImplementedException();
}
/// <summary>
/// Code To Web
/// </summary>
/// <returns></returns>
[UnitOfWork]
public async Task PostCodeBuildWebAsync()
{
var tableAggregateRoots = await _webTemplateManager.BuildCodeToWebAsync();
//覆盖数据库,将聚合根保存到数据库
_tableRepository._Db.DbMaintenance.TruncateTable<TableAggregateRoot>();
_tableRepository._Db.DbMaintenance.TruncateTable<FieldEntity>();
//导航插入即可
await _tableRepository._Db.InsertNav(tableAggregateRoots).Include(x => x.Fields).ExecuteCommandAsync();
}
/// <summary>
/// Code To Db
/// </summary>
/// <returns></returns>
public async Task PostCodeBuildDbAsync()
{
throw new NotImplementedException();
}
/// <summary>
/// 打开目录
/// </summary>
/// <returns></returns>
public async Task PostDir(string path)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
path = Uri.UnescapeDataString(path);
//去除包含@的目录
path = string.Join("\\", path.Split("\\").Where(x => !x.Contains("@")).ToList());
Process.Start("explorer.exe", path);
}
else
{
throw new UserFriendlyException("当前操作系统不支持打开目录");
}
}
}
}

View File

@@ -0,0 +1,54 @@
using System;
using System.Reflection;
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SqlSugar;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Yi.Framework.CodeGun.Application.Contracts.Dtos.Field;
using Yi.Framework.CodeGun.Application.Contracts.IServices;
using Yi.Framework.CodeGun.Domain.Entities;
using Yi.Framework.CodeGun.Domain.Shared.Enums;
using Yi.Framework.Ddd.Application;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.CodeGun.Application.Services
{
/// <summary>
/// 字段管理
/// </summary>
public class FieldService : YiCrudAppService<FieldEntity, FieldDto, Guid, FieldGetListInput>, IFieldService
{
private ISqlSugarRepository<FieldEntity, Guid> _repository;
public FieldService(ISqlSugarRepository<FieldEntity, Guid> repository) : base(repository)
{
_repository = repository;
}
public async override Task<PagedResultDto<FieldDto>> GetListAsync([FromQuery] FieldGetListInput input)
{
RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(input.TableId is not null, x => x.TableId.Equals(input.TableId!))
.WhereIF(input.Name is not null, x => x.Name!.Contains(input.Name!))
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<FieldDto>
{
TotalCount = total,
Items = await MapToGetListOutputDtosAsync(entities)
};
}
/// <summary>
/// 获取类型枚举
/// </summary>
/// <returns></returns>
[Route("type")]
public object GetFieldTypeEnum()
{
return typeof(FieldTypeEnum).GetFields(BindingFlags.Static | BindingFlags.Public).Select(x => new { lable = x.Name, value = (int)Enum.Parse(typeof(FieldTypeEnum), x.Name) }).ToList();
}
}
}

View File

@@ -0,0 +1,15 @@
using Volo.Abp.Domain.Repositories;
using Yi.Framework.CodeGun.Application.Contracts.Dtos.Table;
using Yi.Framework.CodeGun.Application.Contracts.IServices;
using Yi.Framework.CodeGun.Domain.Entities;
using Yi.Framework.Ddd.Application;
namespace Yi.Framework.CodeGun.Application.Services
{
public class TableService : YiCrudAppService<TableAggregateRoot, TableDto, Guid, TableGetListInput>, ITableService
{
public TableService(IRepository<TableAggregateRoot, Guid> repository) : base(repository)
{
}
}
}

View File

@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Volo.Abp.Application.Dtos;
using Yi.Framework.CodeGun.Application.Contracts.Dtos.Template;
using Yi.Framework.CodeGun.Application.Contracts.IServices;
using Yi.Framework.CodeGun.Domain.Entities;
using Yi.Framework.Ddd.Application;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.CodeGun.Application.Services;
public class TemplateService : YiCrudAppService<TemplateEntity, TemplateDto, Guid, TemplateGetListInput>, ITemplateService
{
private ISqlSugarRepository<TemplateEntity, Guid> _repository;
public TemplateService(ISqlSugarRepository<TemplateEntity, Guid> repository) : base(repository)
{
_repository = repository;
}
public async override Task<PagedResultDto<TemplateDto>> GetListAsync([FromQuery] TemplateGetListInput input)
{
RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(input.Name is not null, x => x.Name.Equals(input.Name!))
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<TemplateDto>
{
TotalCount = total,
Items = await MapToGetListOutputDtosAsync(entities)
};
}
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\common.props" />
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.Ddd.Application\Yi.Framework.Ddd.Application.csproj" />
<ProjectReference Include="..\Yi.Framework.CodeGun.Application.Contracts\Yi.Framework.CodeGun.Application.Contracts.csproj" />
<ProjectReference Include="..\Yi.Framework.CodeGun.Domain\Yi.Framework.CodeGun.Domain.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,14 @@
using Yi.Framework.CodeGun.Application.Contracts;
using Yi.Framework.CodeGun.Domain;
using Yi.Framework.Ddd.Application;
namespace Yi.Framework.CodeGun.Application
{
[DependsOn(typeof(YiFrameworkCodeGunApplicationContractsModule),
typeof(YiFrameworkCodeGunDomainModule),
typeof(YiFrameworkDddApplicationModule))]
public class YiFrameworkCodeGunApplicationModule : AbpModule
{
}
}

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.CodeGun.Domain.Shared.Enums
{
public enum FieldTypeEnum
{
[Display(Name = "string", Description = "String")]
String,
[Display(Name = "int", Description = "Int32")]
Int,
[Display(Name = "long", Description = "Int64")]
Long,
[Display(Name = "bool", Description = "Boolean")]
Bool,
[Display(Name = "decimal", Description = "Decimal")]
Decimal,
[Display(Name = "DateTime", Description = "DateTime")]
DateTime,
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\common.props" />
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.Ddd.Domain.Shared" Version="8.0.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,11 @@
using Volo.Abp.Domain;
using Volo.Abp.Modularity;
namespace Yi.Framework.CodeGun.Domain.Shared
{
[DependsOn(typeof(AbpDddDomainSharedModule))]
public class YiFrameworkCodeGunDomainSharedModule:AbpModule
{
}
}

View File

@@ -0,0 +1,45 @@
using SqlSugar;
using Volo.Abp.Domain.Entities;
using Yi.Framework.CodeGun.Domain.Shared.Enums;
namespace Yi.Framework.CodeGun.Domain.Entities
{
[SugarTable("YiField")]
public class FieldEntity : Entity<Guid>
{
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
public override Guid Id { get; protected set; }
/// <summary>
/// 字段名称
/// </summary>
public string Name { get; set; }
public string? Description { get; set; }
public int OrderNum { get; set; }
public int Length { get; set; }
public FieldTypeEnum FieldType { get; set; }
public Guid TableId { get; set; }
/// <summary>
/// 是否必填
/// </summary>
public bool IsRequired { get; set; }
/// <summary>
/// 是否主键
/// </summary>
public bool IsKey { get; set; }
/// <summary>
/// 是否自增
/// </summary>
public bool IsAutoAdd { get; set; }
/// <summary>
/// 是否公共
/// </summary>
public bool IsPublic { get; set; }
}
}

View File

@@ -0,0 +1,28 @@
using SqlSugar;
using Volo.Abp.Domain.Entities;
namespace Yi.Framework.CodeGun.Domain.Entities
{
[SugarTable("YiTable")]
public class TableAggregateRoot : AggregateRoot<Guid>
{
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
public override Guid Id { get; protected set; }
/// <summary>
/// 表名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 备注
/// </summary>
public string? Description { get; set; }
/// <summary>
/// 一表多字段
/// </summary>
[Navigate(NavigateType.OneToMany, nameof(FieldEntity.TableId))]
public List<FieldEntity> Fields { get; set; }
}
}

View File

@@ -0,0 +1,34 @@
using SqlSugar;
using Volo.Abp.Domain.Entities;
namespace Yi.Framework.CodeGun.Domain.Entities
{
[SugarTable("YiTemplate")]
public class TemplateEntity : Entity<Guid>
{
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
public override Guid Id { get; protected set; }
/// <summary>
/// 模板字符串
/// </summary>
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
public string TemplateStr { get; set; } = string.Empty;
/// <summary>
/// 生成路径
/// </summary>
public string BuildPath { get; set; }
/// <summary>
/// 模板名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 备注
/// </summary>
public string? Remarks { get; set; }
}
}

View File

@@ -0,0 +1,70 @@
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Text;
using Yi.Framework.CodeGun.Domain.Shared.Enums;
namespace Yi.Framework.CodeGun.Domain.Handlers
{
public class FieldTemplateHandler : TemplateHandlerBase, ITemplateHandler
{
public HandledTemplate Invoker(string str, string path)
{
var output = new HandledTemplate();
output.TemplateStr = str.Replace("@field", BuildFields());
output.BuildPath = path;
return output;
}
/// <summary>
/// 生成Fields
/// </summary>
/// <returns></returns>
public string BuildFields()
{
StringBuilder fieldStrs = new StringBuilder();
foreach (var field in Table.Fields)
{
var typeStr = typeof(FieldTypeEnum).GetFields().Where(x => x.Name == field.FieldType.ToString())?.FirstOrDefault().GetCustomAttribute<DisplayAttribute>().Name;
if (typeStr is null)
{
continue;
}
var nameStr = field.Name;
//添加备注
if (!string.IsNullOrEmpty(field.Description))
{
var desStr = "/// <summary>\n" +
$"///{field.Description}\n" +
"/// </summary>\n";
fieldStrs.AppendLine(desStr);
}
//添加长度
if (field.Length != 0)
{
var lengthStr = $"[SugarColumn(Length ={field.Length})]";
fieldStrs.AppendLine(lengthStr);
}
//添加可空类型
string nullStr = "";
if (field.IsRequired == false)
{
nullStr = "?";
}
//添加字段
var fieldStr = $"public {typeStr}{nullStr} {nameStr} {{ get; set; }}";
fieldStrs.AppendLine(fieldStr);
}
return fieldStrs.ToString();
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.CodeGun.Domain.Handlers
{
public class HandledTemplate
{
public string TemplateStr { get; set; }
public string BuildPath { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using Volo.Abp.DependencyInjection;
using Yi.Framework.CodeGun.Domain.Entities;
namespace Yi.Framework.CodeGun.Domain.Handlers
{
public interface ITemplateHandler: ISingletonDependency
{
void SetTable(TableAggregateRoot table);
HandledTemplate Invoker(string str, string path);
}
}

View File

@@ -0,0 +1,13 @@
namespace Yi.Framework.CodeGun.Domain.Handlers
{
public class ModelTemplateHandler : TemplateHandlerBase, ITemplateHandler
{
public HandledTemplate Invoker(string str, string path)
{
var output = new HandledTemplate();
output.TemplateStr = str.Replace("@model", char.ToLowerInvariant(Table.Name[0]) + Table.Name.Substring(1)).Replace("@Model", char.ToUpperInvariant(Table.Name[0]) + Table.Name.Substring(1));
output.BuildPath = path.Replace("@model", char.ToLowerInvariant(Table.Name[0]) + Table.Name.Substring(1)).Replace("@Model", char.ToUpperInvariant(Table.Name[0]) + Table.Name.Substring(1));
return output;
}
}
}

View File

@@ -0,0 +1,13 @@
namespace Yi.Framework.CodeGun.Domain.Handlers
{
public class NameSpaceTemplateHandler : TemplateHandlerBase, ITemplateHandler
{
public HandledTemplate Invoker(string str, string path)
{
var output = new HandledTemplate();
output.TemplateStr = str.Replace("@namespace", "");
output.BuildPath = path;
return output;
}
}
}

View File

@@ -0,0 +1,14 @@
using Yi.Framework.CodeGun.Domain.Entities;
namespace Yi.Framework.CodeGun.Domain.Handlers
{
public class TemplateHandlerBase
{
protected TableAggregateRoot Table { get; set; }
public void SetTable(TableAggregateRoot table)
{
Table = table;
}
}
}

View File

@@ -0,0 +1,53 @@
using Volo.Abp.Domain.Services;
using Yi.Framework.CodeGun.Domain.Entities;
using Yi.Framework.CodeGun.Domain.Handlers;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.CodeGun.Domain.Managers
{
/// <summary>
/// 代码文件领域服务,与代码文件生成相关web to code
/// </summary>
public class CodeFileManager : DomainService
{
private IEnumerable<ITemplateHandler> _templateHandlers;
private ISqlSugarRepository<TemplateEntity> _repository;
private ISqlSugarRepository<FieldEntity> _fieldRepository;
public CodeFileManager(IEnumerable<ITemplateHandler> templateHandlers, ISqlSugarRepository<FieldEntity> fieldRepository, ISqlSugarRepository<TemplateEntity> repository)
{
_templateHandlers = templateHandlers;
_repository = repository;
_fieldRepository = fieldRepository;
}
public async Task BuildWebToCodeAsync(TableAggregateRoot tableEntity)
{
var templates = await _repository.GetListAsync();
foreach (var template in templates)
{
var handledTempalte = new HandledTemplate();
handledTempalte.TemplateStr = template.TemplateStr;
handledTempalte.BuildPath = template.BuildPath;
foreach (var templateHandler in _templateHandlers)
{
templateHandler.SetTable(tableEntity);
handledTempalte = templateHandler.Invoker(handledTempalte.TemplateStr, handledTempalte.BuildPath);
}
await BuildToFileAsync(handledTempalte);
}
}
private async Task BuildToFileAsync(HandledTemplate handledTemplate)
{
if (!Directory.Exists(Path.GetDirectoryName(handledTemplate.BuildPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(handledTemplate.BuildPath));
}
await File.WriteAllTextAsync(handledTemplate.BuildPath, handledTemplate.TemplateStr);
}
}
}

View File

@@ -0,0 +1,11 @@
using Volo.Abp.Domain.Services;
namespace Yi.Framework.CodeGun.Domain.Managers
{
/// <summary>
/// 数据库领域服务与数据库相关同步到数据库web to db code to db
/// </summary>
public class DataBaseManger : DomainService
{
}
}

View File

@@ -0,0 +1,123 @@
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using SqlSugar;
using Volo.Abp.Domain.Services;
using Yi.Framework.CodeGun.Domain.Entities;
using Yi.Framework.CodeGun.Domain.Shared.Enums;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.CodeGun.Domain.Managers
{
/// <summary>
/// 与webfrist相关同步到webcode to web
/// </summary>
public class WebTemplateManager : DomainService
{
private ISqlSugarRepository<TableAggregateRoot> _repository;
private IModuleContainer _moduleContainer;
public WebTemplateManager(ISqlSugarRepository<TableAggregateRoot> repository, IModuleContainer moduleContainer)
{
_repository = repository;
_moduleContainer= moduleContainer;
}
/// <summary>
/// 通过当前的实体代码获取表存储
/// </summary>
/// <returns></returns>
public Task<List<TableAggregateRoot>> BuildCodeToWebAsync()
{
List<Type> entityTypes = new List<Type>();
foreach (var module in _moduleContainer.Modules)
{
entityTypes.AddRange(module.Assembly.GetTypes()
.Where(x => x.GetCustomAttribute<IgnoreCodeFirstAttribute>() == null)
.Where(x => x.GetCustomAttribute<SugarTable>() != null)
.Where(x => x.GetCustomAttribute<SplitTableAttribute>() is null));
}
var tableAggregateRoots = new List<TableAggregateRoot>();
foreach (var entityType in entityTypes)
{
tableAggregateRoots.Add(EntityTypeMapperToTable(entityType));
}
return Task.FromResult(tableAggregateRoots);
}
private TableAggregateRoot EntityTypeMapperToTable(Type entityType)
{
var tableAggregateRoot = new TableAggregateRoot();
tableAggregateRoot.Fields = new List<FieldEntity>();
var table = entityType.GetCustomAttribute<SugarTable>();
tableAggregateRoot.Name = table.TableName;
foreach (var p in entityType.GetProperties())
{
tableAggregateRoot.Fields.Add(PropertyMapperToFiled(p));
}
tableAggregateRoot.Fields.ForEach(x => x.TableId = tableAggregateRoot.Id);
return tableAggregateRoot;
}
private FieldEntity PropertyMapperToFiled(PropertyInfo propertyInfo)
{
var fieldEntity = new FieldEntity();
fieldEntity.Name = propertyInfo.Name;
//获取数据类型,包括可空类型
Type? fieldType = null;
// 如果字段类型是 Nullable<T> 泛型类型
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type nullableType = Nullable.GetUnderlyingType(propertyInfo.PropertyType)!;
fieldType = nullableType;
}
else
{
fieldType = propertyInfo.PropertyType;
}
//判断类型
var enumName = typeof(FieldTypeEnum).GetFields(BindingFlags.Static | BindingFlags.Public).Where(x => x.GetCustomAttribute<DisplayAttribute>()?.Description == fieldType.Name).FirstOrDefault()?.Name;
if (enumName is null)
{
fieldEntity.FieldType = FieldTypeEnum.String;
// App.GetRequiredService<ILogger<WebTemplateManager>>().LogError($"字段类型:{propertyInfo.PropertyType.Name},未定义");
}
else
{
fieldEntity.FieldType = (FieldTypeEnum)Enum.Parse(typeof(FieldTypeEnum), enumName);
}
//判断是否可空
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
fieldEntity.IsRequired = false;
}
else
{
fieldEntity.IsRequired = true;
}
//判断是否主键
if (propertyInfo.GetCustomAttribute<SugarColumn>()?.IsPrimaryKey == true)
{
fieldEntity.IsKey = true;
}
//判断长度
var colum = propertyInfo.GetCustomAttribute<SugarColumn>();
if (colum is not null && colum.Length != 0)
{
fieldEntity.Length = colum.Length;
}
return fieldEntity;
}
}
}

View File

@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\common.props" />
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" />
<ProjectReference Include="..\Yi.Framework.CodeGun.Domain.Shared\Yi.Framework.CodeGun.Domain.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,15 @@
using Volo.Abp.Domain;
using Volo.Abp.Modularity;
using Yi.Framework.CodeGun.Domain.Shared;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.CodeGun.Domain
{
[DependsOn(typeof(YiFrameworkCodeGunDomainSharedModule),
typeof(AbpDddDomainModule),
typeof(YiFrameworkSqlSugarCoreAbstractionsModule))]
public class YiFrameworkCodeGunDomainModule : AbpModule
{
}
}

View File

@@ -8,6 +8,7 @@
<ItemGroup>
<ProjectReference Include="..\..\framework\Yi.Framework.Ddd.Application\Yi.Framework.Ddd.Application.csproj" />
<ProjectReference Include="..\..\module\bbs\Yi.Framework.Bbs.Application\Yi.Framework.Bbs.Application.csproj" />
<ProjectReference Include="..\..\module\code-gun\Yi.Framework.CodeGun.Application\Yi.Framework.CodeGun.Application.csproj" />
<ProjectReference Include="..\..\module\rbac\Yi.Framework.Rbac.Application\Yi.Framework.Rbac.Application.csproj" />
<ProjectReference Include="..\..\module\tenant-management\Yi.Framework.TenantManagement.Application\Yi.Framework.TenantManagement.Application.csproj" />
<ProjectReference Include="..\Yi.Abp.Application.Contracts\Yi.Abp.Application.Contracts.csproj" />

View File

@@ -25,6 +25,7 @@ using Yi.Framework.AspNetCore.Authentication.OAuth.QQ;
using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder;
using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection;
using Yi.Framework.Bbs.Application;
using Yi.Framework.CodeGun.Application;
using Yi.Framework.Rbac.Application;
using Yi.Framework.Rbac.Domain.Authorization;
using Yi.Framework.Rbac.Domain.Shared.Consts;
@@ -74,6 +75,7 @@ namespace Yi.Abp.Web
options.ConventionalControllers.Create(typeof(YiFrameworkRbacApplicationModule).Assembly, options => options.RemoteServiceName = "rbac");
options.ConventionalControllers.Create(typeof(YiFrameworkBbsApplicationModule).Assembly, options => options.RemoteServiceName = "bbs");
options.ConventionalControllers.Create(typeof(YiFrameworkTenantManagementApplicationModule).Assembly, options => options.RemoteServiceName = "tenant-management");
options.ConventionalControllers.Create(typeof(YiFrameworkCodeGunApplicationModule).Assembly, options => options.RemoteServiceName = "code-gun");
});
//设置api格式

6
Yi.Abp.Net8/usings.props Normal file
View File

@@ -0,0 +1,6 @@
<Project>
<ItemGroup>
<Using Include="Volo.Abp" />
<Using Include="Volo.Abp.Modularity" />
</ItemGroup>
</Project>