【重构】重构proto JobController代码,添加新的代码生成不再需要修改JobController代码。
parent
675f9954cf
commit
9083915b62
|
|
@ -2,6 +2,7 @@ using Luban.Common.Protos;
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
|
@ -122,8 +123,7 @@ namespace {ctx.TopModule}
|
|||
public override string RenderService(string name, string module, List<DefTable> tables)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("config/cpp_bin/tables");
|
||||
var result = template.Render(new
|
||||
{
|
||||
var result = template.Render(new {
|
||||
Name = name,
|
||||
Tables = tables,
|
||||
});
|
||||
|
|
@ -133,8 +133,7 @@ namespace {ctx.TopModule}
|
|||
public string RenderStub(string topModule, List<DefTypeBase> types)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("config/cpp_bin/stub");
|
||||
return template.RenderCode(new
|
||||
{
|
||||
return template.RenderCode(new {
|
||||
TopModule = topModule,
|
||||
Types = types,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Luban.Job.Cfg.Generate
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ using Luban.Common.Protos;
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using Scriban;
|
||||
using System;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ using Luban.Common.Protos;
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using Scriban;
|
||||
using System;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
@ -24,8 +25,7 @@ namespace Luban.Job.Cfg.Generate
|
|||
public override string RenderService(string name, string module, List<DefTable> tables)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("config/cs_bin/tables");
|
||||
var result = template.RenderCode(new
|
||||
{
|
||||
var result = template.RenderCode(new {
|
||||
Name = name,
|
||||
Namespace = module,
|
||||
Tables = tables,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
|
|||
public override string RenderService(string name, string module, List<DefTable> tables)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("config/cs_json/tables");
|
||||
var result = template.RenderCode(new
|
||||
{
|
||||
var result = template.RenderCode(new {
|
||||
Name = name,
|
||||
Namespace = module,
|
||||
Tables = tables,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
|
|||
public override string RenderService(string name, string module, List<DefTable> tables)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("config/cs_unity_json/tables");
|
||||
var result = template.RenderCode(new
|
||||
{
|
||||
var result = template.RenderCode(new {
|
||||
Name = name,
|
||||
Namespace = module,
|
||||
Tables = tables,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using Luban.Common.Protos;
|
||||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
|
@ -44,8 +45,7 @@ namespace Luban.Job.Cfg.Generate
|
|||
public override string RenderService(string name, string module, List<DefTable> tables)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("config/cs_editor_json/tables");
|
||||
var result = template.Render(new
|
||||
{
|
||||
var result = template.Render(new {
|
||||
Name = name,
|
||||
Namespace = module,
|
||||
Tables = tables,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using Luban.Common.Protos;
|
||||
using Luban.Job.Cfg.Cache;
|
||||
using Luban.Job.Cfg.Utils;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using Luban.Job.Cfg.Cache;
|
|||
using Luban.Job.Cfg.DataConverts;
|
||||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Cfg.Utils;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Types;
|
||||
using Luban.Job.Common.TypeVisitors;
|
||||
using Luban.Job.Common.Utils;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
@ -32,8 +33,7 @@ namespace Luban.Job.Cfg.Generate
|
|||
string package = "cfg";
|
||||
|
||||
var template = StringTemplateUtil.GetTemplate("config/go_json/tables");
|
||||
var result = template.Render(new
|
||||
{
|
||||
var result = template.Render(new {
|
||||
Name = name,
|
||||
Namespace = module,
|
||||
Tables = tables,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
|
|||
public override string RenderService(string name, string module, List<DefTable> tables)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("config/java_bin/tables");
|
||||
var result = template.Render(new
|
||||
{
|
||||
var result = template.Render(new {
|
||||
Name = name,
|
||||
Package = module,
|
||||
Tables = tables,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
|
|||
public override string RenderService(string name, string module, List<DefTable> tables)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("config/java_json/tables");
|
||||
var result = template.Render(new
|
||||
{
|
||||
var result = template.Render(new {
|
||||
Name = name,
|
||||
Package = module,
|
||||
Tables = tables,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using Luban.Common.Protos;
|
||||
using Luban.Job.Cfg.Utils;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using Scriban;
|
||||
using System.Collections.Generic;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using Scriban;
|
||||
using System;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
|
||||
namespace Luban.Job.Cfg.Generate
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using Luban.Job.Common.Generate;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using Luban.Common.Protos;
|
||||
using Luban.Job.Cfg.RawDefs;
|
||||
using Luban.Job.Cfg.Utils;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using Luban.Common.Protos;
|
||||
using Luban.Job.Cfg.Cache;
|
||||
using Luban.Job.Cfg.Utils;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using Luban.Job.Cfg.Cache;
|
||||
using Luban.Job.Cfg.DataVisitors;
|
||||
using Luban.Job.Cfg.Utils;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
|
|||
public override string RenderService(string name, string module, List<DefTable> tables)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("config/typescript_bin/tables");
|
||||
var result = template.RenderCode(new
|
||||
{
|
||||
var result = template.RenderCode(new {
|
||||
Name = name,
|
||||
Namespace = module,
|
||||
Tables = tables,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
|
|||
public override string RenderService(string name, string module, List<DefTable> tables)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("config/typescript_json/tables");
|
||||
var result = template.RenderCode(new
|
||||
{
|
||||
var result = template.RenderCode(new {
|
||||
Name = name,
|
||||
Namespace = module,
|
||||
Tables = tables,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
using System;
|
||||
|
||||
namespace Luban.Job.Cfg.Generate
|
||||
namespace Luban.Job.Common.Generate
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
|
||||
class RenderAttribute : System.Attribute
|
||||
public class RenderAttribute : System.Attribute
|
||||
{
|
||||
public string Name { get; }
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using CommandLine;
|
||||
using Luban.Job.Common;
|
||||
|
||||
namespace Luban.Job.Proto
|
||||
{
|
||||
public class GenArgs : GenArgsBase
|
||||
{
|
||||
[Option('g', "gen_type", Required = true, HelpText = "cs,lua,java,cpp,typescript")]
|
||||
public string GenType { get; set; }
|
||||
|
||||
[Option('s', "service", Required = true, HelpText = "service")]
|
||||
public string Service { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
using Luban.Job.Common;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Proto.Defs;
|
||||
using Luban.Job.Proto.Generate;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using FileInfo = Luban.Common.Protos.FileInfo;
|
||||
|
||||
namespace Luban.Job.Proto
|
||||
{
|
||||
class GenContext
|
||||
{
|
||||
public GenArgs GenArgs { get; init; }
|
||||
public DefAssembly Assembly { get; init; }
|
||||
public string GenType { get; set; }
|
||||
public IRender Render { get; set; }
|
||||
public ELanguage Lan { get; set; }
|
||||
|
||||
public List<DefTypeBase> ExportTypes { get; init; }
|
||||
|
||||
public ConcurrentBag<FileInfo> GenCodeFilesInOutputCodeDir { get; init; }
|
||||
public ConcurrentBag<FileInfo> GenScatteredFiles { get; init; }
|
||||
public List<Task> Tasks { get; init; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using Luban.Job.Proto.Defs;
|
||||
using System;
|
||||
|
|
@ -6,27 +7,15 @@ using System.Collections.Generic;
|
|||
|
||||
namespace Luban.Job.Proto.Generate
|
||||
{
|
||||
class CsRender
|
||||
[Render("cs")]
|
||||
class CsRender : RenderBase
|
||||
{
|
||||
public string RenderAny(object o)
|
||||
{
|
||||
switch (o)
|
||||
{
|
||||
case DefEnum e: return Render(e);
|
||||
case DefBean b: return Render(b);
|
||||
case DefProto p: return Render(p);
|
||||
case DefRpc r: return Render(r);
|
||||
|
||||
default: throw new Exception($"unknown render type:'{o}'");
|
||||
}
|
||||
}
|
||||
|
||||
private string Render(DefEnum e)
|
||||
protected override string Render(DefEnum e)
|
||||
{
|
||||
return RenderUtil.RenderCsEnumClass(e);
|
||||
}
|
||||
|
||||
private string Render(DefBean b)
|
||||
protected override string Render(DefBean b)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("proto/cs/bean");
|
||||
var result = template.RenderCode(b);
|
||||
|
|
@ -34,7 +23,7 @@ namespace Luban.Job.Proto.Generate
|
|||
return result;
|
||||
}
|
||||
|
||||
private string Render(DefProto p)
|
||||
protected override string Render(DefProto p)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("proto/cs/proto");
|
||||
var result = template.RenderCode(p);
|
||||
|
|
@ -42,7 +31,7 @@ namespace Luban.Job.Proto.Generate
|
|||
return result;
|
||||
}
|
||||
|
||||
private string Render(DefRpc r)
|
||||
protected override string Render(DefRpc r)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("proto/cs/rpc");
|
||||
var result = template.RenderCode(r);
|
||||
|
|
@ -50,7 +39,7 @@ namespace Luban.Job.Proto.Generate
|
|||
return result;
|
||||
}
|
||||
|
||||
public string RenderStubs(string name, string module, List<DefTypeBase> protos, List<DefTypeBase> rpcs)
|
||||
public override string RenderStubs(string name, string module, List<DefProto> protos, List<DefRpc> rpcs)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("proto/cs/stub");
|
||||
var result = template.Render(new
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
using Luban.Job.Proto.Defs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Luban.Job.Proto.Generate
|
||||
{
|
||||
interface IRender
|
||||
{
|
||||
void Render(GenContext ctx);
|
||||
|
||||
string RenderAny(object o);
|
||||
|
||||
string RenderStubs(string name, string module, List<DefProto> protos, List<DefRpc> rpcs);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,29 @@
|
|||
using Luban.Common.Protos;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using Luban.Job.Proto.Defs;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Luban.Job.Proto.Generate
|
||||
{
|
||||
class LuaRender
|
||||
[Render("lua")]
|
||||
class LuaRender : RenderBase
|
||||
{
|
||||
public override void Render(GenContext ctx)
|
||||
{
|
||||
DefAssembly ass = ctx.Assembly;
|
||||
ctx.Tasks.Add(Task.Run(() =>
|
||||
{
|
||||
var content = FileHeaderUtil.ConcatAutoGenerationHeader(RenderTypes(ctx.ExportTypes), Common.ELanguage.LUA);
|
||||
var file = "Types.lua";
|
||||
var md5 = CacheFileUtil.GenMd5AndAddCache(file, content);
|
||||
ctx.GenCodeFilesInOutputCodeDir.Add(new FileInfo() { FilePath = file, MD5 = md5 });
|
||||
}));
|
||||
}
|
||||
|
||||
public string RenderTypes(List<DefTypeBase> types)
|
||||
{
|
||||
var enums = types.Where(t => t is DefEnum).ToList();
|
||||
|
|
@ -17,5 +33,30 @@ namespace Luban.Job.Proto.Generate
|
|||
var template = StringTemplateUtil.GetTemplate("proto/lua/all");
|
||||
return template.RenderCode(new { Enums = enums, Beans = beans, Protos = protos, Rpcs = rpcs });
|
||||
}
|
||||
|
||||
protected override string Render(DefEnum e)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
protected override string Render(DefBean b)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
protected override string Render(DefProto p)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
protected override string Render(DefRpc r)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public override string RenderStubs(string name, string module, List<DefProto> protos, List<DefRpc> rpcs)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,73 @@
|
|||
using Luban.Common.Protos;
|
||||
using Luban.Job.Common;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Utils;
|
||||
using Luban.Job.Proto.Defs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Luban.Job.Proto.Generate
|
||||
{
|
||||
abstract class RenderBase : IRender
|
||||
{
|
||||
public virtual void Render(GenContext ctx)
|
||||
{
|
||||
GenerateCodeScatter(ctx);
|
||||
}
|
||||
|
||||
public string RenderAny(object o)
|
||||
{
|
||||
switch (o)
|
||||
{
|
||||
case DefEnum e: return Render(e);
|
||||
case DefBean b: return Render(b);
|
||||
case DefProto p: return Render(p);
|
||||
case DefRpc r: return Render(r);
|
||||
default: throw new Exception($"unknown render type:'{o}'");
|
||||
}
|
||||
}
|
||||
|
||||
protected void GenerateCodeScatter(GenContext ctx)
|
||||
{
|
||||
ELanguage lan = ctx.Lan;
|
||||
var render = ctx.Render;
|
||||
DefAssembly ass = ctx.Assembly;
|
||||
foreach (var c in ctx.ExportTypes)
|
||||
{
|
||||
ctx.Tasks.Add(Task.Run(() =>
|
||||
{
|
||||
var content = FileHeaderUtil.ConcatAutoGenerationHeader(render.RenderAny(c), lan);
|
||||
var file = RenderFileUtil.GetDefTypePath(c.FullName, lan);
|
||||
var md5 = CacheFileUtil.GenMd5AndAddCache(file, content);
|
||||
ctx.GenCodeFilesInOutputCodeDir.Add(new FileInfo() { FilePath = file, MD5 = md5 });
|
||||
}));
|
||||
}
|
||||
ctx.Tasks.Add(Task.Run(() =>
|
||||
{
|
||||
var module = ass.TopModule;
|
||||
var name = "ProtocolStub";
|
||||
var content = FileHeaderUtil.ConcatAutoGenerationHeader(
|
||||
render.RenderStubs(name, module,
|
||||
ctx.ExportTypes.Where(t => t is DefProto).Cast<DefProto>().ToList(),
|
||||
ctx.ExportTypes.Where(t => t is DefRpc).Cast<DefRpc>().ToList()),
|
||||
lan);
|
||||
var file = RenderFileUtil.GetDefTypePath(name, lan);
|
||||
var md5 = CacheFileUtil.GenMd5AndAddCache(file, content);
|
||||
ctx.GenCodeFilesInOutputCodeDir.Add(new FileInfo() { FilePath = file, MD5 = md5 });
|
||||
}));
|
||||
}
|
||||
|
||||
protected abstract string Render(DefEnum e);
|
||||
|
||||
protected abstract string Render(DefBean b);
|
||||
|
||||
protected abstract string Render(DefProto p);
|
||||
|
||||
protected abstract string Render(DefRpc r);
|
||||
|
||||
public abstract string RenderStubs(string name, string module, List<DefProto> protos, List<DefRpc> rpcs);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
using Luban.Job.Common.Generate;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Luban.Job.Proto.Generate
|
||||
{
|
||||
static class RenderFactory
|
||||
{
|
||||
static RenderFactory()
|
||||
{
|
||||
Dictionary<string, IRender> renders = new();
|
||||
foreach (var type in typeof(JobController).Assembly.DefinedTypes.Where(t => t.AsType().GetCustomAttributes(typeof(RenderAttribute)).Any()))
|
||||
{
|
||||
foreach (var attr in type.GetCustomAttributes<RenderAttribute>())
|
||||
{
|
||||
renders.Add(attr.Name, (IRender)System.Activator.CreateInstance(type));
|
||||
}
|
||||
}
|
||||
|
||||
s_renders = renders;
|
||||
}
|
||||
|
||||
private static readonly Dictionary<string, IRender> s_renders;
|
||||
|
||||
public static IRender CreateRender(string genType)
|
||||
{
|
||||
return s_renders.TryGetValue(genType, out var render) ? render : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,32 +1,76 @@
|
|||
using Luban.Job.Common.Defs;
|
||||
using Luban.Common.Protos;
|
||||
using Luban.Job.Common;
|
||||
using Luban.Job.Common.Defs;
|
||||
using Luban.Job.Common.Generate;
|
||||
using Luban.Job.Common.Utils;
|
||||
using Luban.Job.Proto.Defs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Luban.Job.Proto.Generate
|
||||
{
|
||||
class TypescriptRender
|
||||
[Render("typescript")]
|
||||
class TypescriptRender : RenderBase
|
||||
{
|
||||
public string RenderAny(object o)
|
||||
public override void Render(GenContext ctx)
|
||||
{
|
||||
switch (o)
|
||||
ctx.Tasks.Add(Task.Run(() =>
|
||||
{
|
||||
case DefEnum e: return Render(e);
|
||||
case DefBean b: return Render(b);
|
||||
case DefProto p: return Render(p);
|
||||
case DefRpc r: return Render(r);
|
||||
|
||||
default: throw new Exception($"unknown render type:{o}");
|
||||
GenArgs args = ctx.GenArgs;
|
||||
var brightRequirePath = args.TypescriptBrightRequirePath;
|
||||
var brightPackageName = args.TypescriptBrightPackageName;
|
||||
var render = ctx.Render;
|
||||
var ass = ctx.Assembly;
|
||||
var fileContent = new List<string>();
|
||||
if (args.UsePuertsByteBuf)
|
||||
{
|
||||
fileContent.Add(TypescriptStringTemplate.PuertsByteBufImports);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileContent.Add(TypescriptStringTemplate.GetByteBufImports(brightRequirePath, brightPackageName));
|
||||
}
|
||||
if (args.EmbedBrightTypes)
|
||||
{
|
||||
fileContent.Add(StringTemplateUtil.GetTemplateString("config/typescript_bin/vectors"));
|
||||
fileContent.Add(TypescriptStringTemplate.SerializeTypes);
|
||||
fileContent.Add(TypescriptStringTemplate.ProtoTypes);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileContent.Add(TypescriptStringTemplate.GetSerializeImports(brightRequirePath, brightPackageName));
|
||||
fileContent.Add(TypescriptStringTemplate.GetProtocolImports(brightRequirePath, brightPackageName));
|
||||
fileContent.Add(TypescriptStringTemplate.GetVectorImports(brightRequirePath, brightPackageName));
|
||||
}
|
||||
|
||||
private string Render(DefEnum e)
|
||||
fileContent.Add(@$"export namespace {ass.TopModule} {{");
|
||||
|
||||
foreach (var type in ctx.ExportTypes)
|
||||
{
|
||||
fileContent.Add(render.RenderAny(type));
|
||||
}
|
||||
|
||||
fileContent.Add(render.RenderStubs("ProtocolStub", ass.TopModule,
|
||||
ctx.ExportTypes.Where(t => t is DefProto).Cast<DefProto>().ToList(),
|
||||
ctx.ExportTypes.Where(t => t is DefRpc).Cast<DefRpc>().ToList()));
|
||||
|
||||
fileContent.Add("}"); // end of topmodule
|
||||
|
||||
var content = FileHeaderUtil.ConcatAutoGenerationHeader(string.Join('\n', fileContent), ELanguage.TYPESCRIPT);
|
||||
var file = "Types.ts";
|
||||
var md5 = CacheFileUtil.GenMd5AndAddCache(file, content);
|
||||
ctx.GenCodeFilesInOutputCodeDir.Add(new FileInfo() { FilePath = file, MD5 = md5 });
|
||||
}));
|
||||
}
|
||||
|
||||
protected override string Render(DefEnum e)
|
||||
{
|
||||
return RenderUtil.RenderTypescriptEnumClass(e);
|
||||
}
|
||||
|
||||
private string Render(DefBean b)
|
||||
protected override string Render(DefBean b)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("proto/typescript/bean");
|
||||
var result = template.RenderCode(b);
|
||||
|
|
@ -34,7 +78,7 @@ namespace Luban.Job.Proto.Generate
|
|||
return result;
|
||||
}
|
||||
|
||||
private string Render(DefProto p)
|
||||
protected override string Render(DefProto p)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("proto/typescript/proto");
|
||||
var result = template.RenderCode(p);
|
||||
|
|
@ -42,7 +86,7 @@ namespace Luban.Job.Proto.Generate
|
|||
return result;
|
||||
}
|
||||
|
||||
private string Render(DefRpc r)
|
||||
protected override string Render(DefRpc r)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("proto/typescript/rpc");
|
||||
var result = template.RenderCode(r);
|
||||
|
|
@ -50,7 +94,7 @@ namespace Luban.Job.Proto.Generate
|
|||
return result;
|
||||
}
|
||||
|
||||
public string RenderStubs(string name, string module, List<DefTypeBase> protos, List<DefTypeBase> rpcs)
|
||||
public override string RenderStubs(string name, string module, List<DefProto> protos, List<DefRpc> rpcs)
|
||||
{
|
||||
var template = StringTemplateUtil.GetTemplate("proto/typescript/stub");
|
||||
var result = template.Render(new
|
||||
|
|
|
|||
|
|
@ -17,21 +17,12 @@ using FileInfo = Luban.Common.Protos.FileInfo;
|
|||
|
||||
namespace Luban.Job.Proto
|
||||
{
|
||||
|
||||
[Controller("proto")]
|
||||
public class JobController : IJobController
|
||||
{
|
||||
private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
class GenArgs : GenArgsBase
|
||||
{
|
||||
[Option('g', "gen_type", Required = true, HelpText = "cs,lua,java,cpp,typescript")]
|
||||
public string GenType { get; set; }
|
||||
|
||||
[Option('s', "service", Required = true, HelpText = "service")]
|
||||
public string Service { get; set; }
|
||||
}
|
||||
|
||||
|
||||
private bool TryParseArg(List<string> args, out GenArgs options, out string errMsg)
|
||||
{
|
||||
var helpWriter = new StringWriter();
|
||||
|
|
@ -117,114 +108,34 @@ namespace Luban.Job.Proto
|
|||
|
||||
var tasks = new List<Task>();
|
||||
var genCodeFiles = new ConcurrentBag<FileInfo>();
|
||||
var genScatteredFiles = new ConcurrentBag<FileInfo>();
|
||||
|
||||
|
||||
var genType = args.GenType;
|
||||
switch (genType)
|
||||
{
|
||||
case "cs":
|
||||
{
|
||||
ass.CurrentLanguage = ELanguage.CS;
|
||||
var render = new CsRender();
|
||||
foreach (var c in ass.Types.Values)
|
||||
{
|
||||
tasks.Add(Task.Run(() =>
|
||||
{
|
||||
var content = FileHeaderUtil.ConcatAutoGenerationHeader(render.RenderAny(c), Common.ELanguage.CS);
|
||||
var file = RenderFileUtil.GetDefTypePath(c.FullName, Common.ELanguage.CS);
|
||||
var md5 = CacheFileUtil.GenMd5AndAddCache(file, content);
|
||||
genCodeFiles.Add(new FileInfo() { FilePath = file, MD5 = md5 });
|
||||
}));
|
||||
}
|
||||
tasks.Add(Task.Run(() =>
|
||||
{
|
||||
var module = ass.TopModule;
|
||||
var name = "ProtocolStub";
|
||||
var content = FileHeaderUtil.ConcatAutoGenerationHeader(
|
||||
render.RenderStubs(name, module,
|
||||
ass.Types.Values.Where(t => t is DefProto).ToList(),
|
||||
ass.Types.Values.Where(t => t is DefRpc).ToList()),
|
||||
Common.ELanguage.CS);
|
||||
var file = RenderFileUtil.GetDefTypePath(name, Common.ELanguage.CS);
|
||||
var md5 = CacheFileUtil.GenMd5AndAddCache(file, content);
|
||||
genCodeFiles.Add(new FileInfo() { FilePath = file, MD5 = md5 });
|
||||
}));
|
||||
break;
|
||||
}
|
||||
case "lua":
|
||||
{
|
||||
ass.CurrentLanguage = ELanguage.LUA;
|
||||
tasks.Add(Task.Run(() =>
|
||||
{
|
||||
var render = new LuaRender();
|
||||
var content = FileHeaderUtil.ConcatAutoGenerationHeader(render.RenderTypes(ass.Types.Values.ToList()), Common.ELanguage.LUA);
|
||||
var file = "Types.lua";
|
||||
var md5 = CacheFileUtil.GenMd5AndAddCache(file, content);
|
||||
genCodeFiles.Add(new FileInfo() { FilePath = file, MD5 = md5 });
|
||||
}));
|
||||
break;
|
||||
}
|
||||
case "typescript":
|
||||
{
|
||||
ass.CurrentLanguage = ELanguage.TYPESCRIPT;
|
||||
var render = new TypescriptRender();
|
||||
var brightRequirePath = args.TypescriptBrightRequirePath;
|
||||
var brightPackageName = args.TypescriptBrightPackageName;
|
||||
|
||||
tasks.Add(Task.Run(() =>
|
||||
{
|
||||
var fileContent = new List<string>();
|
||||
if (args.UsePuertsByteBuf)
|
||||
{
|
||||
fileContent.Add(TypescriptStringTemplate.PuertsByteBufImports);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileContent.Add(TypescriptStringTemplate.GetByteBufImports(brightRequirePath, brightPackageName));
|
||||
}
|
||||
if (args.EmbedBrightTypes)
|
||||
{
|
||||
fileContent.Add(StringTemplateUtil.GetTemplateString("config/typescript_bin/vectors"));
|
||||
fileContent.Add(TypescriptStringTemplate.SerializeTypes);
|
||||
fileContent.Add(TypescriptStringTemplate.ProtoTypes);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileContent.Add(TypescriptStringTemplate.GetSerializeImports(brightRequirePath, brightPackageName));
|
||||
fileContent.Add(TypescriptStringTemplate.GetProtocolImports(brightRequirePath, brightPackageName));
|
||||
fileContent.Add(TypescriptStringTemplate.GetVectorImports(brightRequirePath, brightPackageName));
|
||||
}
|
||||
|
||||
fileContent.Add(@$"export namespace {ass.TopModule} {{");
|
||||
|
||||
foreach (var type in exportTypes)
|
||||
{
|
||||
fileContent.Add(render.RenderAny(type));
|
||||
}
|
||||
|
||||
fileContent.Add(render.RenderStubs("ProtocolStub", ass.TopModule, ass.Types.Values.Where(t => t is DefProto).ToList(),
|
||||
ass.Types.Values.Where(t => t is DefRpc).ToList()));
|
||||
|
||||
fileContent.Add("}"); // end of topmodule
|
||||
|
||||
var content = FileHeaderUtil.ConcatAutoGenerationHeader(string.Join('\n', fileContent), ELanguage.TYPESCRIPT);
|
||||
var file = "Types.ts";
|
||||
var md5 = CacheFileUtil.GenMd5AndAddCache(file, content);
|
||||
genCodeFiles.Add(new FileInfo() { FilePath = file, MD5 = md5 });
|
||||
}));
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
var render = RenderFactory.CreateRender(genType);
|
||||
if (render == null)
|
||||
{
|
||||
throw new NotSupportedException($"not support gen type:{genType}");
|
||||
}
|
||||
ass.CurrentLanguage = RenderFileUtil.GetLanguage(genType);
|
||||
render.Render(new GenContext()
|
||||
{
|
||||
GenArgs = args,
|
||||
Assembly = ass,
|
||||
Lan = ass.CurrentLanguage,
|
||||
GenType = genType,
|
||||
Render = render,
|
||||
Tasks = tasks,
|
||||
ExportTypes = exportTypes,
|
||||
GenCodeFilesInOutputCodeDir = genCodeFiles,
|
||||
GenScatteredFiles = genScatteredFiles,
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks.ToArray());
|
||||
|
||||
res.FileGroups.Add(new FileGroup() { Dir = outputCodeDir, Files = genCodeFiles.ToList() });
|
||||
res.ScatteredFiles.AddRange(genScatteredFiles);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue