【特性】新增convert_template支持

main
walon 2021-12-18 20:11:59 +08:00
parent c2fc86215c
commit 2021e38485
10 changed files with 92 additions and 7 deletions

View File

@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataConverts
{
class LuaConvertor : DataVisitors.ToLuaLiteralVisitor
{
//public static new LuaConvertor Ins { get; } = new();
public static new LuaConvertor Ins { get; } = new();
private string _indentStr = "";

View File

@ -20,7 +20,7 @@ namespace Luban.Job.Cfg
[Option("output_data_dir", Required = false, HelpText = "output data directory")]
public string OutputDataDir { get; set; }
[Option("input:convert_data_dir", Required = false, HelpText = "override input data dir with convert data dir")]
[Option("input:convert:data_dir", Required = false, HelpText = "override input data dir with convert data dir")]
public string InputConvertDataDir { get; set; }
[Option("output:tables", Required = false, HelpText = "override tables in export group with this list")]
@ -53,6 +53,12 @@ namespace Luban.Job.Cfg
[Option("template:code:dir", Required = false, HelpText = "code template dir. use with gen_types=code_template")]
public string TemplateCodeDir { get; set; }
[Option("template:convert:file", Required = false, HelpText = "convert template file name. use with gen_tpes=convert_template")]
public string TemplateConvertFile { get; set; }
[Option("output:convert:file_extension", Required = false, HelpText = "output convert file extension. default guess by convert template name")]
public string OutputConvertFileExtension { get; set; }
[Option("l10n:timezone", Required = false, HelpText = "timezone")]
public string L10nTimeZone { get; set; }

View File

@ -0,0 +1,48 @@
using Luban.Common.Protos;
using Luban.Job.Cfg.Cache;
using Luban.Job.Cfg.DataSources;
using Luban.Job.Cfg.DataVisitors;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Tpl;
using Luban.Job.Common.Utils;
using Scriban;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Luban.Job.Cfg.Generate
{
[Render("convert_template")]
class TemplateConvertRender : DataRenderBase
{
public override void Render(GenContext ctx)
{
string genType = ctx.GenType;
Template template = StringTemplateManager.Ins.GetTemplate($"config/convert/{ctx.GenArgs.TemplateConvertFile}");
foreach (var table in ctx.ExportTables)
{
var records = ctx.Assembly.GetTableAllDataList(table);
int index = 0;
string dirName = table.FullName;
foreach (var record in records)
{
ctx.Tasks.Add(Task.Run(() =>
{
var fileName = table.IsMapTable ?
record.Data.GetField(table.IndexField.Name).Apply(ToStringVisitor2.Ins).Replace("\"", "").Replace("'", "")
: (++index).ToString();
var file = RenderFileUtil.GetOutputFileName(genType, $"{dirName}/{fileName}", ctx.GenArgs.OutputConvertFileExtension);
var content = template.RenderData(table, record.Data);
var md5 = CacheFileUtil.GenStringOrBytesMd5AndAddCache(file, content);
FileRecordCacheManager.Ins.AddCachedRecordOutputData(table, records, genType, md5);
ctx.GenDataFilesInOutputDataDir.Add(new FileInfo() { FilePath = file, MD5 = md5 });
}));
}
}
}
}
}

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace Luban.Job.Cfg.Generate
{
[Render("data_template")]
class TemplateDataScatterRender : DataRenderBase
class TemplateDataRender : DataRenderBase
{
public override void Render(GenContext ctx)
{

View File

@ -25,7 +25,7 @@ namespace Luban.Job.Cfg.Generate
var fileName = table.IsMapTable ?
record.Data.GetField(table.IndexField.Name).Apply(ToStringVisitor2.Ins).Replace("\"", "").Replace("'", "")
: (++index).ToString();
var file = RenderFileUtil.GetOutputFileName(genType, $"{dirName}/{fileName}", ctx.GenArgs.OutputDataFileExtension);
var file = RenderFileUtil.GetOutputFileName(genType, $"{dirName}/{fileName}", ctx.GenArgs.OutputConvertFileExtension);
ctx.Tasks.Add(Task.Run(() =>
{
//if (!FileRecordCacheManager.Ins.TryGetRecordOutputData(table, records, genType, out string md5))

View File

@ -86,6 +86,11 @@ namespace Luban.Job.Cfg
errMsg = "gen_types data_template should use with --template:data:file";
return false;
}
if (genTypes.Contains("convert_template") ^ !string.IsNullOrWhiteSpace(options.TemplateConvertFile))
{
errMsg = "gen_types convert_template should use with --template:convert:file";
return false;
}
if (genTypes.Contains("code_template") ^ !string.IsNullOrWhiteSpace(options.TemplateCodeDir))
{
errMsg = "gen_types code_template should use with --template:code:dir";

View File

@ -27,7 +27,7 @@ namespace Luban.Job.Cfg
return template.Render(ctx);
}
public static string RenderData(this Template template, DefTable table, List<DBean> exportDatas, Dictionary<string, object> extraModels = null)
public static string RenderDatas(this Template template, DefTable table, List<DBean> exportDatas, Dictionary<string, object> extraModels = null)
{
var ctx = new TemplateContext();
@ -47,5 +47,27 @@ namespace Luban.Job.Cfg
ctx.PushGlobal(env);
return template.Render(ctx);
}
public static string RenderData(this Template template, DefTable table, DBean data, Dictionary<string, object> extraModels = null)
{
var ctx = new TemplateContext();
var env = new DTypeTemplateExtends
{
["table"] = table,
["data"] = data,
["assembly"] = DefAssembly.LocalAssebmly,
};
if (extraModels != null)
{
foreach ((var k, var v) in extraModels)
{
env[k] = v;
}
}
ctx.PushGlobal(env);
return template.Render(ctx);
}
}
}

View File

@ -37,7 +37,7 @@ namespace Luban.Job.Cfg.Utils
}
case "convert_lua":
{
return new LuaConvertor().ExportRecord(table, record);
return LuaConvertor.Ins.ExportRecord(table, record);
}
default:
{

View File

@ -25,7 +25,7 @@ namespace Luban.Job.Cfg.Utils
public static string ToTemplateOutputData(DefTable table, List<Record> records, string templateName)
{
Template template = StringTemplateManager.Ins.GetTemplate($"config/data/{templateName}");
return template.RenderData(table, records.Select(r => r.Data).ToList());
return template.RenderDatas(table, records.Select(r => r.Data).ToList());
}
public static object ToOutputData(DefTable table, List<Record> records, string dataType)

View File

@ -26,6 +26,10 @@
"ExternalCodeTemplate": {
"commandName": "Project",
"commandLineArgs": "-t D:\\workspace\\luban_examples\\Projects\\Csharp_CustomTemplate_ExternalCodeTemplate\\CustomTemplate --disable_cache"
},
"convert_template": {
"commandName": "Project",
"commandLineArgs": "-t D:\\workspace\\luban_examples\\Projects\\ConvertTemplates\\CustomTemplates"
}
}
}