【重构】重构proto JobController代码,添加新的代码生成不再需要修改JobController代码。

main
walon 2021-11-26 19:03:03 +08:00
parent 675f9954cf
commit 9083915b62
35 changed files with 331 additions and 169 deletions

View File

@ -2,6 +2,7 @@ using Luban.Common.Protos;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common; using Luban.Job.Common;
using Luban.Job.Common.Defs; using Luban.Job.Common.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -122,8 +123,7 @@ namespace {ctx.TopModule}
public override string RenderService(string name, string module, List<DefTable> tables) public override string RenderService(string name, string module, List<DefTable> tables)
{ {
var template = StringTemplateUtil.GetTemplate("config/cpp_bin/tables"); var template = StringTemplateUtil.GetTemplate("config/cpp_bin/tables");
var result = template.Render(new var result = template.Render(new {
{
Name = name, Name = name,
Tables = tables, Tables = tables,
}); });
@ -133,8 +133,7 @@ namespace {ctx.TopModule}
public string RenderStub(string topModule, List<DefTypeBase> types) public string RenderStub(string topModule, List<DefTypeBase> types)
{ {
var template = StringTemplateUtil.GetTemplate("config/cpp_bin/stub"); var template = StringTemplateUtil.GetTemplate("config/cpp_bin/stub");
return template.RenderCode(new return template.RenderCode(new {
{
TopModule = topModule, TopModule = topModule,
Types = types, Types = types,
}); });

View File

@ -1,5 +1,6 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Defs; using Luban.Job.Common.Defs;
using Luban.Job.Common.Generate;
using System.Collections.Generic; using System.Collections.Generic;
namespace Luban.Job.Cfg.Generate namespace Luban.Job.Cfg.Generate

View File

@ -2,6 +2,7 @@ using Luban.Common.Protos;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common; using Luban.Job.Common;
using Luban.Job.Common.Defs; using Luban.Job.Common.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using Scriban; using Scriban;
using System; using System;

View File

@ -2,6 +2,7 @@ using Luban.Common.Protos;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common; using Luban.Job.Common;
using Luban.Job.Common.Defs; using Luban.Job.Common.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using Scriban; using Scriban;
using System; using System;

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
@ -24,8 +25,7 @@ namespace Luban.Job.Cfg.Generate
public override string RenderService(string name, string module, List<DefTable> tables) public override string RenderService(string name, string module, List<DefTable> tables)
{ {
var template = StringTemplateUtil.GetTemplate("config/cs_bin/tables"); var template = StringTemplateUtil.GetTemplate("config/cs_bin/tables");
var result = template.RenderCode(new var result = template.RenderCode(new {
{
Name = name, Name = name,
Namespace = module, Namespace = module,
Tables = tables, Tables = tables,

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
public override string RenderService(string name, string module, List<DefTable> tables) public override string RenderService(string name, string module, List<DefTable> tables)
{ {
var template = StringTemplateUtil.GetTemplate("config/cs_json/tables"); var template = StringTemplateUtil.GetTemplate("config/cs_json/tables");
var result = template.RenderCode(new var result = template.RenderCode(new {
{
Name = name, Name = name,
Namespace = module, Namespace = module,
Tables = tables, Tables = tables,

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
public override string RenderService(string name, string module, List<DefTable> tables) public override string RenderService(string name, string module, List<DefTable> tables)
{ {
var template = StringTemplateUtil.GetTemplate("config/cs_unity_json/tables"); var template = StringTemplateUtil.GetTemplate("config/cs_unity_json/tables");
var result = template.RenderCode(new var result = template.RenderCode(new {
{
Name = name, Name = name,
Namespace = module, Namespace = module,
Tables = tables, Tables = tables,

View File

@ -1,6 +1,7 @@
using Luban.Common.Protos; using Luban.Common.Protos;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common; using Luban.Job.Common;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -44,8 +45,7 @@ namespace Luban.Job.Cfg.Generate
public override string RenderService(string name, string module, List<DefTable> tables) public override string RenderService(string name, string module, List<DefTable> tables)
{ {
var template = StringTemplateUtil.GetTemplate("config/cs_editor_json/tables"); var template = StringTemplateUtil.GetTemplate("config/cs_editor_json/tables");
var result = template.Render(new var result = template.Render(new {
{
Name = name, Name = name,
Namespace = module, Namespace = module,
Tables = tables, Tables = tables,

View File

@ -1,6 +1,7 @@
using Luban.Common.Protos; using Luban.Common.Protos;
using Luban.Job.Cfg.Cache; using Luban.Job.Cfg.Cache;
using Luban.Job.Cfg.Utils; using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -3,6 +3,7 @@ using Luban.Job.Cfg.Cache;
using Luban.Job.Cfg.DataConverts; using Luban.Job.Cfg.DataConverts;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils; using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Types; using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors; using Luban.Job.Common.TypeVisitors;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
@ -32,8 +33,7 @@ namespace Luban.Job.Cfg.Generate
string package = "cfg"; string package = "cfg";
var template = StringTemplateUtil.GetTemplate("config/go_json/tables"); var template = StringTemplateUtil.GetTemplate("config/go_json/tables");
var result = template.Render(new var result = template.Render(new {
{
Name = name, Name = name,
Namespace = module, Namespace = module,
Tables = tables, Tables = tables,

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
public override string RenderService(string name, string module, List<DefTable> tables) public override string RenderService(string name, string module, List<DefTable> tables)
{ {
var template = StringTemplateUtil.GetTemplate("config/java_bin/tables"); var template = StringTemplateUtil.GetTemplate("config/java_bin/tables");
var result = template.Render(new var result = template.Render(new {
{
Name = name, Name = name,
Package = module, Package = module,
Tables = tables, Tables = tables,

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
public override string RenderService(string name, string module, List<DefTable> tables) public override string RenderService(string name, string module, List<DefTable> tables)
{ {
var template = StringTemplateUtil.GetTemplate("config/java_json/tables"); var template = StringTemplateUtil.GetTemplate("config/java_json/tables");
var result = template.Render(new var result = template.Render(new {
{
Name = name, Name = name,
Package = module, Package = module,
Tables = tables, Tables = tables,

View File

@ -1,5 +1,6 @@
using Luban.Common.Protos; using Luban.Common.Protos;
using Luban.Job.Cfg.Utils; using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -1,5 +1,6 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Defs; using Luban.Job.Common.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using Scriban; using Scriban;
using System.Collections.Generic; using System.Collections.Generic;

View File

@ -1,5 +1,6 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Defs; using Luban.Job.Common.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using Scriban; using Scriban;
using System; using System;

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
namespace Luban.Job.Cfg.Generate namespace Luban.Job.Cfg.Generate

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using Luban.Job.Common.Generate;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;

View File

@ -1,6 +1,7 @@
using Luban.Common.Protos; using Luban.Common.Protos;
using Luban.Job.Cfg.RawDefs; using Luban.Job.Cfg.RawDefs;
using Luban.Job.Cfg.Utils; using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -1,5 +1,6 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Defs; using Luban.Job.Common.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

View File

@ -1,6 +1,7 @@
using Luban.Common.Protos; using Luban.Common.Protos;
using Luban.Job.Cfg.Cache; using Luban.Job.Cfg.Cache;
using Luban.Job.Cfg.Utils; using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -2,6 +2,7 @@
using Luban.Job.Cfg.Cache; using Luban.Job.Cfg.Cache;
using Luban.Job.Cfg.DataVisitors; using Luban.Job.Cfg.DataVisitors;
using Luban.Job.Cfg.Utils; using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
public override string RenderService(string name, string module, List<DefTable> tables) public override string RenderService(string name, string module, List<DefTable> tables)
{ {
var template = StringTemplateUtil.GetTemplate("config/typescript_bin/tables"); var template = StringTemplateUtil.GetTemplate("config/typescript_bin/tables");
var result = template.RenderCode(new var result = template.RenderCode(new {
{
Name = name, Name = name,
Namespace = module, Namespace = module,
Tables = tables, Tables = tables,

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
@ -26,8 +27,7 @@ namespace Luban.Job.Cfg.Generate
public override string RenderService(string name, string module, List<DefTable> tables) public override string RenderService(string name, string module, List<DefTable> tables)
{ {
var template = StringTemplateUtil.GetTemplate("config/typescript_json/tables"); var template = StringTemplateUtil.GetTemplate("config/typescript_json/tables");
var result = template.RenderCode(new var result = template.RenderCode(new {
{
Name = name, Name = name,
Namespace = module, Namespace = module,
Tables = tables, Tables = tables,

View File

@ -1,9 +1,9 @@
using System; using System;
namespace Luban.Job.Cfg.Generate namespace Luban.Job.Common.Generate
{ {
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class RenderAttribute : System.Attribute public class RenderAttribute : System.Attribute
{ {
public string Name { get; } public string Name { get; }

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -1,4 +1,5 @@
using Luban.Job.Common.Defs; using Luban.Job.Common.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using Luban.Job.Proto.Defs; using Luban.Job.Proto.Defs;
using System; using System;
@ -6,27 +7,15 @@ using System.Collections.Generic;
namespace Luban.Job.Proto.Generate namespace Luban.Job.Proto.Generate
{ {
class CsRender [Render("cs")]
class CsRender : RenderBase
{ {
public string RenderAny(object o) protected override string Render(DefEnum e)
{
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)
{ {
return RenderUtil.RenderCsEnumClass(e); return RenderUtil.RenderCsEnumClass(e);
} }
private string Render(DefBean b) protected override string Render(DefBean b)
{ {
var template = StringTemplateUtil.GetTemplate("proto/cs/bean"); var template = StringTemplateUtil.GetTemplate("proto/cs/bean");
var result = template.RenderCode(b); var result = template.RenderCode(b);
@ -34,7 +23,7 @@ namespace Luban.Job.Proto.Generate
return result; return result;
} }
private string Render(DefProto p) protected override string Render(DefProto p)
{ {
var template = StringTemplateUtil.GetTemplate("proto/cs/proto"); var template = StringTemplateUtil.GetTemplate("proto/cs/proto");
var result = template.RenderCode(p); var result = template.RenderCode(p);
@ -42,7 +31,7 @@ namespace Luban.Job.Proto.Generate
return result; return result;
} }
private string Render(DefRpc r) protected override string Render(DefRpc r)
{ {
var template = StringTemplateUtil.GetTemplate("proto/cs/rpc"); var template = StringTemplateUtil.GetTemplate("proto/cs/rpc");
var result = template.RenderCode(r); var result = template.RenderCode(r);
@ -50,7 +39,7 @@ namespace Luban.Job.Proto.Generate
return result; 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 template = StringTemplateUtil.GetTemplate("proto/cs/stub");
var result = template.Render(new var result = template.Render(new

View File

@ -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);
}
}

View File

@ -1,13 +1,29 @@
using Luban.Common.Protos;
using Luban.Job.Common.Defs; using Luban.Job.Common.Defs;
using Luban.Job.Common.Generate;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using Luban.Job.Proto.Defs; using Luban.Job.Proto.Defs;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace Luban.Job.Proto.Generate 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) public string RenderTypes(List<DefTypeBase> types)
{ {
var enums = types.Where(t => t is DefEnum).ToList(); 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"); var template = StringTemplateUtil.GetTemplate("proto/lua/all");
return template.RenderCode(new { Enums = enums, Beans = beans, Protos = protos, Rpcs = rpcs }); 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();
}
} }
} }

View File

@ -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);
}
}

View File

@ -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;
}
}
}

View File

@ -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.Common.Utils;
using Luban.Job.Proto.Defs; using Luban.Job.Proto.Defs;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Luban.Job.Proto.Generate 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); GenArgs args = ctx.GenArgs;
case DefBean b: return Render(b); var brightRequirePath = args.TypescriptBrightRequirePath;
case DefProto p: return Render(p); var brightPackageName = args.TypescriptBrightPackageName;
case DefRpc r: return Render(r); 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));
}
default: throw new Exception($"unknown render type:{o}"); 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 });
}));
} }
private string Render(DefEnum e) protected override string Render(DefEnum e)
{ {
return RenderUtil.RenderTypescriptEnumClass(e); return RenderUtil.RenderTypescriptEnumClass(e);
} }
private string Render(DefBean b) protected override string Render(DefBean b)
{ {
var template = StringTemplateUtil.GetTemplate("proto/typescript/bean"); var template = StringTemplateUtil.GetTemplate("proto/typescript/bean");
var result = template.RenderCode(b); var result = template.RenderCode(b);
@ -34,7 +78,7 @@ namespace Luban.Job.Proto.Generate
return result; return result;
} }
private string Render(DefProto p) protected override string Render(DefProto p)
{ {
var template = StringTemplateUtil.GetTemplate("proto/typescript/proto"); var template = StringTemplateUtil.GetTemplate("proto/typescript/proto");
var result = template.RenderCode(p); var result = template.RenderCode(p);
@ -42,7 +86,7 @@ namespace Luban.Job.Proto.Generate
return result; return result;
} }
private string Render(DefRpc r) protected override string Render(DefRpc r)
{ {
var template = StringTemplateUtil.GetTemplate("proto/typescript/rpc"); var template = StringTemplateUtil.GetTemplate("proto/typescript/rpc");
var result = template.RenderCode(r); var result = template.RenderCode(r);
@ -50,7 +94,7 @@ namespace Luban.Job.Proto.Generate
return result; 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 template = StringTemplateUtil.GetTemplate("proto/typescript/stub");
var result = template.Render(new var result = template.Render(new

View File

@ -17,21 +17,12 @@ using FileInfo = Luban.Common.Protos.FileInfo;
namespace Luban.Job.Proto namespace Luban.Job.Proto
{ {
[Controller("proto")] [Controller("proto")]
public class JobController : IJobController public class JobController : IJobController
{ {
private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); 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) private bool TryParseArg(List<string> args, out GenArgs options, out string errMsg)
{ {
var helpWriter = new StringWriter(); var helpWriter = new StringWriter();
@ -117,114 +108,34 @@ namespace Luban.Job.Proto
var tasks = new List<Task>(); var tasks = new List<Task>();
var genCodeFiles = new ConcurrentBag<FileInfo>(); var genCodeFiles = new ConcurrentBag<FileInfo>();
var genScatteredFiles = new ConcurrentBag<FileInfo>();
var genType = args.GenType; var genType = args.GenType;
switch (genType) var render = RenderFactory.CreateRender(genType);
if (render == null)
{ {
case "cs": throw new NotSupportedException($"not support gen type:{genType}");
{
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:
{
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()); await Task.WhenAll(tasks.ToArray());
res.FileGroups.Add(new FileGroup() { Dir = outputCodeDir, Files = genCodeFiles.ToList() }); res.FileGroups.Add(new FileGroup() { Dir = outputCodeDir, Files = genCodeFiles.ToList() });
res.ScatteredFiles.AddRange(genScatteredFiles);
} }
catch (Exception e) catch (Exception e)
{ {