Skip to content

Commit

Permalink
feat : 添加代码生成器模板,完善
Browse files Browse the repository at this point in the history
  • Loading branch information
KawhiWei committed Aug 26, 2021
1 parent 4c5dfd7 commit b8c2692
Show file tree
Hide file tree
Showing 23 changed files with 793 additions and 61 deletions.
23 changes: 20 additions & 3 deletions Sukt.Modules/Sukt.Modules.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31229.75
# Visual Studio Version 17
VisualStudioVersion = 17.0.31521.260
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sukt.Module.Core", "src\Sukt.Module.Core\Sukt.Module.Core.csproj", "{DA69384D-2B6A-4BEB-8A77-99C3B05F37CF}"
EndProject
Expand Down Expand Up @@ -29,7 +29,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sukt.Aop", "src\Sukt.Aop\Su
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sukt.Redis", "src\Sukt.Redis\Sukt.Redis.csproj", "{B0D75B6D-2892-48C4-833A-123D8B92702E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sukt.Tests", "test\Sukt.Tests\Sukt.Tests.csproj", "{7C2374E8-D7AD-47D4-896A-A90F4B6B82C7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sukt.Tests", "test\Sukt.Tests\Sukt.Tests.csproj", "{7C2374E8-D7AD-47D4-896A-A90F4B6B82C7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sukt.MQBus", "src\Sukt.MQBus\Sukt.MQBus.csproj", "{174A40AD-BAB1-4BEC-B5E1-57E23780438E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{45EE9008-E96B-433C-8317-7FFAC44B89FF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sukt.WebScoket", "src\Sukt.WebScoket\Sukt.WebScoket.csproj", "{CB6CBE07-98A9-4657-89BA-92D871FCFBD3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -93,10 +99,21 @@ Global
{7C2374E8-D7AD-47D4-896A-A90F4B6B82C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C2374E8-D7AD-47D4-896A-A90F4B6B82C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C2374E8-D7AD-47D4-896A-A90F4B6B82C7}.Release|Any CPU.Build.0 = Release|Any CPU
{174A40AD-BAB1-4BEC-B5E1-57E23780438E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{174A40AD-BAB1-4BEC-B5E1-57E23780438E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{174A40AD-BAB1-4BEC-B5E1-57E23780438E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{174A40AD-BAB1-4BEC-B5E1-57E23780438E}.Release|Any CPU.Build.0 = Release|Any CPU
{CB6CBE07-98A9-4657-89BA-92D871FCFBD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB6CBE07-98A9-4657-89BA-92D871FCFBD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB6CBE07-98A9-4657-89BA-92D871FCFBD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB6CBE07-98A9-4657-89BA-92D871FCFBD3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7C2374E8-D7AD-47D4-896A-A90F4B6B82C7} = {45EE9008-E96B-433C-8317-7FFAC44B89FF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {70FF5DA8-A0A5-4D80-8423-8316BF74DACE}
EndGlobalSection
Expand Down
6 changes: 5 additions & 1 deletion Sukt.Modules/src/Sukt.CodeGenerator/EntityMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class EntityMetadata
/// </summary>
public bool IsGeneratorDto { get; set; }

/// <summary>
/// 是否是聚合根
/// </summary>
public bool IsAggregate { get; set; }

/// <summary>
/// 属性集合
/// </summary>
Expand All @@ -54,7 +59,6 @@ public class EntityMetadata
/// 是否软删除
/// </summary>
public bool IsSoftDelete { get; set; } = true;

/// <summary>
/// 是否全部审核
/// </summary>
Expand Down
29 changes: 20 additions & 9 deletions Sukt.Modules/src/Sukt.CodeGenerator/ICodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface ICodeGenerator
/// <param name="projectMetadata"></param>
/// <param name="filePath"></param>

void GenerateCode(ProjectMetadata projectMetadata, string filePath);
void GenerateCode(ProjectMetadata projectMetadata);

/// <summary>
/// 创建实体代码
Expand All @@ -28,24 +28,35 @@ public interface ICodeGenerator
CodeData GenerateEntityConfigurationCode(ProjectMetadata metadata);

/// <summary>
/// 创建输入Dto码
/// 生成契约
/// </summary>
/// <param name="metadata">元数据</param>
/// <param name="metadata"></param>
/// <returns></returns>
CodeData GenerateInputDtoCode(ProjectMetadata metadata);
CodeData GenerateIApplicationContract(ProjectMetadata metadata);

/// <summary>
/// 创建输出代码
/// 生成契约实现
/// </summary>
/// <param name="metadata"></param>
/// <returns></returns>
CodeData GenerateOutputDtoCode(ProjectMetadata metadata);

CodeData GenerateApplicationContract(ProjectMetadata metadata);
/// <summary>
/// 生成输入Dto
/// </summary>
/// <param name="metadata"></param>
/// <returns></returns>
CodeData GenerateInputDto(ProjectMetadata metadata);
/// <summary>
/// 生成输出Dto
/// </summary>
/// <param name="metadata"></param>
/// <returns></returns>
CodeData GenerateOutputDto(ProjectMetadata metadata);
/// <summary>
/// 创建分页Dto代码
/// 生成控制器
/// </summary>
/// <param name="metadata"></param>
/// <returns></returns>
CodeData GeneratePageDtoCode(ProjectMetadata metadata);
CodeData GenerateController(ProjectMetadata metadata);
}
}
5 changes: 4 additions & 1 deletion Sukt.Modules/src/Sukt.CodeGenerator/ProjectMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public class ProjectMetadata
/// 命名空间
/// </summary>
public string Namespace { get; set; }

/// <summary>
/// 生成路径
/// </summary>
public string SaveFilePath { get; set; }
/// <summary>
/// 实体元数据
/// </summary>
Expand Down
79 changes: 59 additions & 20 deletions Sukt.Modules/src/Sukt.CodeGenerator/RazorCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ public class RazorCodeGenerator : ICodeGenerator
/// 生成代码
/// </summary>
/// <param name="projectMetadata"></param>
/// <param name="filePath"></param>
public void GenerateCode(ProjectMetadata projectMetadata, string filePath)
public void GenerateCode(ProjectMetadata projectMetadata)
{
List<CodeData> codes = new List<CodeData>();

codes.Add(GenerateEntityCode(projectMetadata));
codes.Add(GenerateEntityConfigurationCode(projectMetadata));
//codes.Add(GenerateInputDtoCode(projectMetadata));
//codes.Add(GenerateOutputDtoCode(projectMetadata));
//codes.Add(GeneratePageDtoCode(projectMetadata));
codes.Add(GenerateIApplicationContract(projectMetadata));
codes.Add(GenerateApplicationContract(projectMetadata));
codes.Add(GenerateInputDto(projectMetadata));
codes.Add(GenerateOutputDto(projectMetadata));
codes.Add(GenerateController(projectMetadata));
foreach (var code in codes.OrderBy(o => o.FileName))
{
var saveFilePath = $"{Path.Combine(@"{0}\{1}", filePath, code.FileName)}";
var saveFilePath = $"{Path.Combine(@"{0}\{1}", projectMetadata.SaveFilePath, code.FileName)}";
var path = Path.GetDirectoryName(saveFilePath);
if (!Directory.Exists(path))
{
Expand Down Expand Up @@ -108,51 +109,78 @@ public CodeData GenerateEntityConfigurationCode(ProjectMetadata metadata)
};
return code;
}

/// <summary>
/// 创建输入代码
/// 生成契约
/// </summary>
/// <param name="metadata"></param>
/// <returns></returns>
public CodeData GenerateIApplicationContract(ProjectMetadata metadata)
{
var template = GetTemplateCode(metadata, CodeType.IApplicationContract);
var code = new CodeData()
{
SourceCode = template,
FileName = $"{metadata.EntityMetadata.EntityName}/I{metadata.EntityMetadata.EntityName}Contract.cs"
};
return code;
}
/// <summary>
/// 生成契约实现
/// </summary>
/// <param name="metadata"></param>
/// <returns></returns>
public CodeData GenerateApplicationContract(ProjectMetadata metadata)
{
var template = GetTemplateCode(metadata, CodeType.ApplicationContract);
var code = new CodeData()
{
SourceCode = template,
FileName = $"{metadata.EntityMetadata.EntityName}/{metadata.EntityMetadata.EntityName}Contract.cs"
};
return code;
}
/// <summary>
/// 生成输入Dto
/// </summary>
/// <param name="metadata"></param>
/// <returns></returns>
public CodeData GenerateInputDtoCode(ProjectMetadata metadata)
public CodeData GenerateInputDto(ProjectMetadata metadata)
{
var template = GetTemplateCode(metadata, CodeType.InputDto);
var code = new CodeData()
{
SourceCode = template,
FileName = $"Dtos/{metadata.EntityMetadata.EntityName}InputDto.cs"
FileName = $"{metadata.EntityMetadata.EntityName}Dto/{metadata.EntityMetadata.EntityName}InputDto.cs"
};
return code;
}

/// <summary>
/// 创建输出代码
/// 生成输出Dto
/// </summary>
/// <param name="metadata"></param>
/// <returns></returns>
public CodeData GenerateOutputDtoCode(ProjectMetadata metadata)
public CodeData GenerateOutputDto(ProjectMetadata metadata)
{
var template = GetTemplateCode(metadata, CodeType.OutputDto);
var code = new CodeData()
{
SourceCode = template,
FileName = $"Dtos/{metadata.EntityMetadata.EntityName}OutputDto.cs"
FileName = $"{metadata.EntityMetadata.EntityName}Dto/{metadata.EntityMetadata.EntityName}OutputDto.cs"
};
return code;
}

/// <summary>
/// 创建分页Dto代码
/// 生成控制器
/// </summary>
/// <param name="metadata"></param>
/// <returns></returns>
public CodeData GeneratePageDtoCode(ProjectMetadata metadata)
public CodeData GenerateController(ProjectMetadata metadata)
{
var template = GetTemplateCode(metadata, CodeType.PageListDto);
var template = GetTemplateCode(metadata, CodeType.Controller);
var code = new CodeData()
{
SourceCode = template,
FileName = $"Dtos/{metadata.EntityMetadata.EntityName}PageListDto.cs"
FileName = $"Controller/{metadata.EntityMetadata.EntityName}Controller.cs"
};
return code;
}
Expand Down Expand Up @@ -199,10 +227,21 @@ public enum CodeType
/// 输入Dto
/// </summary>
InputDto,

/// <summary>
/// 控制器
/// </summary>
Controller,
/// <summary>
/// 分页Dto
/// </summary>
PageListDto,
/// <summary>
/// 契约层实现
/// </summary>
ApplicationContract,
/// <summary>
/// 契约层接口
/// </summary>
IApplicationContract
}
}
10 changes: 10 additions & 0 deletions Sukt.Modules/src/Sukt.CodeGenerator/Sukt.CodeGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
</PropertyGroup>

<ItemGroup>
<None Remove="Templates\ApplicationContract.cshtml" />
<None Remove="Templates\Controller.cshtml" />
<None Remove="Templates\Entity.cshtml" />
<None Remove="Templates\EntityConfiguration.cshtml" />
<None Remove="Templates\IApplicationContract.cshtml" />
<None Remove="Templates\InputDto.cshtml" />
<None Remove="Templates\OutputDto.cshtml" />
<None Include="..\..\..\..\logo.jpg">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Templates\Controller.cshtml" />
<EmbeddedResource Include="Templates\OutputDto.cshtml" />
<EmbeddedResource Include="Templates\InputDto.cshtml" />
<EmbeddedResource Include="Templates\IApplicationContract.cshtml" />
<EmbeddedResource Include="Templates\ApplicationContract.cshtml" />
<EmbeddedResource Include="Templates\Entity.cshtml" />
<EmbeddedResource Include="Templates\EntityConfiguration.cshtml" />
</ItemGroup>
Expand Down
Loading

0 comments on commit b8c2692

Please sign in to comment.