添加各个木块代码生成
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Template.Const;
|
||||
|
||||
namespace Yi.Framework.Template.Abstract
|
||||
{
|
||||
public abstract class ModelTemplateProvider : ProgramTemplateProvider
|
||||
{
|
||||
|
||||
public ModelTemplateProvider(string modelName, string entityName) : base(modelName, entityName)
|
||||
{
|
||||
AddIgnoreEntityField("Id", "TenantId");
|
||||
}
|
||||
|
||||
private string entityPath;
|
||||
|
||||
/// <summary>
|
||||
/// 实体路径,该类生成需要实体与模板两个同时构建成
|
||||
/// </summary>
|
||||
public string EntityPath
|
||||
{
|
||||
get => this.entityPath;
|
||||
set
|
||||
{
|
||||
value = value!.Replace(TemplateConst.EntityName, EntityName);
|
||||
value = value.Replace(TemplateConst.ModelName, ModelName);
|
||||
this.entityPath = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 生成模板忽略实体字段
|
||||
/// </summary>
|
||||
private List<string> IgnoreEntityFields { get; set; } = new();
|
||||
|
||||
public override void Build()
|
||||
{
|
||||
if (BuildPath is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(BuildPath));
|
||||
}
|
||||
//模板信息
|
||||
var templateData = GetTemplateData();
|
||||
|
||||
//实体信息
|
||||
var enetityDatas = GetEntityData().ToList();
|
||||
|
||||
//获取全部属性字段
|
||||
for (var i = enetityDatas.Count() - 1; i >= 0; i--)
|
||||
{
|
||||
//不是字段属性直接删除跳过
|
||||
if (!enetityDatas[i].Contains("{ get; set; }"))
|
||||
{
|
||||
enetityDatas.RemoveAt(i);
|
||||
continue;
|
||||
}
|
||||
//是字段属性,同时还包含忽略字段
|
||||
foreach (var IgnoreEntityField in IgnoreEntityFields)
|
||||
{
|
||||
if (enetityDatas[i].Contains(IgnoreEntityField))
|
||||
{
|
||||
enetityDatas.RemoveAt(i);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//拼接实体字段
|
||||
var entityFieldsbuild = string.Join("\r\n", enetityDatas);
|
||||
|
||||
|
||||
//模板替换属性字段
|
||||
templateData = templateData.Replace(TemplateConst.EntityField, entityFieldsbuild);
|
||||
|
||||
templateData = base.ReplaceTemplateDic(templateData);
|
||||
|
||||
if (!Directory.Exists(Path.GetDirectoryName(BuildPath)))
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(BuildPath)!);
|
||||
}
|
||||
File.WriteAllText(BuildPath, templateData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取实体信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public virtual string[] GetEntityData()
|
||||
{
|
||||
if (TemplatePath is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(entityPath));
|
||||
}
|
||||
if (!File.Exists(entityPath))
|
||||
{
|
||||
throw new FileNotFoundException($"请检查路径:{entityPath}\r\n未包含实体:{EntityName}");
|
||||
}
|
||||
|
||||
return File.ReadAllLines(entityPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加忽略实体字段
|
||||
/// </summary>
|
||||
/// <param name="field"></param>
|
||||
public void AddIgnoreEntityField(params string[] field)
|
||||
{
|
||||
IgnoreEntityFields.AddRange(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ namespace Yi.Framework.Template.Abstract
|
||||
EntityName = entityName;
|
||||
base.AddTemplateDic(TemplateConst.EntityName, EntityName);
|
||||
base.AddTemplateDic(TemplateConst.ModelName, ModelName);
|
||||
base.AddTemplateDic(TemplateConst.LowerEntityName, EntityName.Substring(0, 1).ToLower() + EntityName.Substring(1));
|
||||
base.AddTemplateDic(TemplateConst.LowerModelName, ModelName.ToLower());
|
||||
}
|
||||
/// <summary>
|
||||
/// 实体名称
|
||||
@@ -34,12 +36,22 @@ namespace Yi.Framework.Template.Abstract
|
||||
get => base.BuildPath;
|
||||
set
|
||||
{
|
||||
value = value!.Replace(TemplateConst.EntityName, EntityName);
|
||||
value = value.Replace(TemplateConst.ModelName, ModelName);
|
||||
value = ReplaceTemplateDic(value!);
|
||||
|
||||
base.BuildPath = value;
|
||||
}
|
||||
}
|
||||
|
||||
public string ReplaceTemplateDic(string str)
|
||||
{
|
||||
foreach (var ky in TemplateDic)
|
||||
{
|
||||
str = str.Replace(ky.Key, ky.Value);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
public override void Build()
|
||||
{
|
||||
if (BuildPath is null)
|
||||
@@ -47,12 +59,9 @@ namespace Yi.Framework.Template.Abstract
|
||||
throw new ArgumentNullException(nameof(BuildPath));
|
||||
}
|
||||
var templateData = GetTemplateData();
|
||||
foreach (var ky in TemplateDic)
|
||||
{
|
||||
templateData = templateData.Replace(ky.Key, ky.Value);
|
||||
}
|
||||
templateData = ReplaceTemplateDic(templateData);
|
||||
if (!Directory.Exists(Path.GetDirectoryName(BuildPath)))
|
||||
{
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(BuildPath)!);
|
||||
}
|
||||
File.WriteAllText(BuildPath, templateData);
|
||||
|
||||
Reference in New Issue
Block a user