新的生成模板引擎引入
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
2ffd1a2520
commit
bfe5379206
|
@ -6,15 +6,23 @@
|
|||
<IsPackable>false</IsPackable>
|
||||
|
||||
<LangVersion>default</LangVersion>
|
||||
|
||||
<OutputType>Exe</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<OutputPath>..\bin</OutputPath>
|
||||
<OutputPath>bin</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Template\EntityService.liquid">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Template\IEntityService.liquid">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="TT_Template\IRaCIS .Core.ServiceAsync.tt">
|
||||
<LastGenOutput>IRaCIS .Core.ServiceAsync.cs</LastGenOutput>
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
|
@ -65,9 +73,11 @@
|
|||
<Folder Include="TT_Template\IServices_New\" />
|
||||
<Folder Include="TT_Template\Models_New\" />
|
||||
<Folder Include="TT_Template\Services_New\" />
|
||||
<Folder Include="TemplateOutPut\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Fluid.Core" Version="2.11.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -0,0 +1,124 @@
|
|||
|
||||
using Fluid;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
partial class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
//发布的目录和项目的目录有区别的
|
||||
var rootPath = AppContext.BaseDirectory.Replace(@"\bin\net8.0\", "").Replace(@"\bin\Release\net8.0\", "");
|
||||
|
||||
var templateFolderPath = Path.Combine(rootPath, "Template");
|
||||
var outPutTemplateFolderPath = Path.Combine(rootPath, "TemplateOutPut");
|
||||
|
||||
#region 生成文件目录准备
|
||||
if (!Directory.Exists(outPutTemplateFolderPath))
|
||||
{
|
||||
Directory.CreateDirectory(outPutTemplateFolderPath);
|
||||
}
|
||||
var servicePath = Path.Combine(outPutTemplateFolderPath, "Service");
|
||||
var iServicePath = Path.Combine(outPutTemplateFolderPath, "IService");
|
||||
var entityPath = Path.Combine(outPutTemplateFolderPath, "Entity");
|
||||
var dtoPath = Path.Combine(outPutTemplateFolderPath, "DTO");
|
||||
|
||||
if (!Directory.Exists(servicePath))
|
||||
{
|
||||
Directory.CreateDirectory(servicePath);
|
||||
}
|
||||
if (!Directory.Exists(iServicePath))
|
||||
{
|
||||
Directory.CreateDirectory(iServicePath);
|
||||
}
|
||||
if (!Directory.Exists(entityPath))
|
||||
{
|
||||
Directory.CreateDirectory(entityPath);
|
||||
}
|
||||
if (!Directory.Exists(dtoPath))
|
||||
{
|
||||
Directory.CreateDirectory(dtoPath);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
// 要生成的表名数组
|
||||
var tableNames = new List<string> { "User", "Product", "Order" };
|
||||
|
||||
try
|
||||
{
|
||||
//便利所有模板文件进行生成操作
|
||||
foreach (var templateFilePath in Directory.GetFiles(templateFolderPath))
|
||||
{
|
||||
|
||||
var fileName=Path.GetFileNameWithoutExtension(templateFilePath);
|
||||
|
||||
foreach (var tableName in tableNames)
|
||||
{
|
||||
var model = new TemplateModel
|
||||
{
|
||||
TableName = tableName,
|
||||
|
||||
IsPaged = true
|
||||
};
|
||||
|
||||
|
||||
var parser = new FluidParser();
|
||||
|
||||
var source = File.ReadAllText(templateFilePath);
|
||||
|
||||
if (parser.TryParse(source, out var template, out var error))
|
||||
{
|
||||
var context = new TemplateContext(model);
|
||||
|
||||
//Console.WriteLine(template.Render(context));
|
||||
|
||||
string outputFilePath = Path.Combine(outPutTemplateFolderPath, $"{fileName.Replace("Entity", tableName)}.cs");
|
||||
|
||||
File.WriteAllText(outputFilePath, template.Render(context));
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Error: {error}");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Console.WriteLine($"Error: {ex.Message}");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Model class to pass data to the template
|
||||
public class TemplateModel
|
||||
{
|
||||
public string TableName { get; set; }
|
||||
|
||||
//生成的列表是是分页 还是不分页
|
||||
public bool IsPaged { get; set; }
|
||||
|
||||
// 列表视图模型名称
|
||||
public string TableNameView => TableName + "View";
|
||||
//列表查询模型名称
|
||||
public string TableNameQuery => TableName + "Query";
|
||||
//添加和编辑模型名称
|
||||
public string TableNameAddOrEdit => TableName + "AddOrEdit";
|
||||
//删除主键属性名
|
||||
public string LowercaseTableNameId => char.ToLower(TableName[0]) + TableName.Substring(1) + "Id";
|
||||
|
||||
public string LowercaseRepositoryName => $"_{char.ToLower(TableName[0]) + TableName.Substring(1)}Repository" ;
|
||||
|
||||
public string LowercaseQueryableName => $"{char.ToLower(TableName[0]) + TableName.Substring(1)}Queryable";
|
||||
|
||||
public DateTime DateTimeNow = DateTime.Now;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
|
||||
//--------------------------------------------------------------------
|
||||
// 此代码由模板自动生成 byzhouhang 20210918
|
||||
// 生成时间 {{DateTimeNow}}
|
||||
// 对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。
|
||||
//--------------------------------------------------------------------
|
||||
using IRaCIS.Core.Domain.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using IRaCIS.Core.Application.Interfaces;
|
||||
using IRaCIS.Core.Application.ViewModel;
|
||||
namespace IRaCIS.Core.Application.Service
|
||||
{
|
||||
|
||||
[ ApiExplorerSettings(GroupName = "Test")]
|
||||
public class {{TableName}}Service(IRepository<{{TableName}}> {{LowercaseRepositoryName}}): BaseService, I{{TableName}}Service
|
||||
{
|
||||
|
||||
{% if IsPaged %}
|
||||
[HttpPost]
|
||||
public async Task<PageOutput<{{TableNameView}}>> Get{{TableName}}List({{TableNameQuery}} inQuery)
|
||||
{
|
||||
|
||||
var {{LowercaseQueryableName}} ={{LowercaseRepositoryName}}
|
||||
.ProjectTo<{{TableNameView}}>(_mapper.ConfigurationProvider);
|
||||
|
||||
var pageList= await {{LowercaseQueryableName}}.ToPagedListAsync(inQuery);
|
||||
|
||||
return pageList;
|
||||
}
|
||||
{% else %}
|
||||
[HttpPost]
|
||||
public async Task<List<{{TableNameView}}>> Get{{TableName}}List({{TableNameQuery}} inQuery)
|
||||
{
|
||||
|
||||
|
||||
var {{LowercaseQueryableName}} ={{LowercaseRepositoryName}}
|
||||
.ProjectTo<{{TableNameView}}>(_mapper.ConfigurationProvider);
|
||||
|
||||
return await {{LowercaseQueryableName}}.ToListAsync();
|
||||
}
|
||||
{% endif %}
|
||||
|
||||
|
||||
public async Task<IResponseOutput> AddOrUpdate{{TableName}}({{TableName}}AddOrEdit addOrEdit{{TableName}})
|
||||
{
|
||||
// 在此处拷贝automapper 映射
|
||||
|
||||
CreateMap<{{TableName}}, {{TableNameView}}>();
|
||||
CreateMap<{{TableName}},{{TableNameAddOrEdit}}>().ReverseMap();
|
||||
|
||||
|
||||
var entity = await {{LowercaseRepositoryName}}.InsertOrUpdateAsync(addOrEdit{{TableName}}, true);
|
||||
|
||||
return ResponseOutput.Ok(entity.Id.ToString());
|
||||
|
||||
}
|
||||
|
||||
|
||||
[HttpDelete("{{ '{' }}{{LowercaseTableNameId}}:guid{{ '}' }}")]
|
||||
public async Task<IResponseOutput> Delete{{TableName}}(Guid {{LowercaseTableNameId}})
|
||||
{
|
||||
var success = await _<#=char.ToLower(tableName[0]) + tableName.Substring(1)#>Repository.DeleteFromQueryAsync(t => t.Id == {{LowercaseTableNameId}},true);
|
||||
return ResponseOutput.Ok();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
//--------------------------------------------------------------------
|
||||
// 此代码由模板自动生成 byzhouhang 20210918
|
||||
// 生成时间 {{DateTimeNow}}
|
||||
// 对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。
|
||||
//--------------------------------------------------------------------
|
||||
using IRaCIS.Core.Application.ViewModel;
|
||||
namespace IRaCIS.Core.Application.Interfaces
|
||||
{
|
||||
|
||||
public interface I{{TableName}}Service
|
||||
{
|
||||
|
||||
{% if IsPaged %}
|
||||
Task<PageOutput<<{{TableNameView}}>> Get{{TableName}}List({{TableNameQuery}} inQuery);
|
||||
{% else %}
|
||||
Task<List<{{TableNameView}}>> Get{{TableName}}List({{TableNameQuery}} inQuery);
|
||||
{% endif %}
|
||||
|
||||
Task<IResponseOutput> AddOrUpdate{{TableName}}({{TableNameAddOrEdit}} addOrEdit{{TableName}});
|
||||
|
||||
Task<IResponseOutput> Delete{{TableNameView}}(Guid {{LowercaseTableNameId}});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue