-
Notifications
You must be signed in to change notification settings - Fork 18
/
TurboLinkGenerator.cs
144 lines (127 loc) · 6.64 KB
/
TurboLinkGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Protobuf.Reflection;
namespace protoc_gen_turbolink
{
public struct GeneratedFile
{
public string FileName;
public string Content;
}
public struct GenerateParam
{
public bool GenerateServiceCode;
public bool GenerateJsonCode;
}
public class TurboLinkGenerator
{
public FileDescriptorProto ProtoFile;
GrpcServiceFile ServiceFile;
public List<GeneratedFile> GeneratedFiles = new List<GeneratedFile>();
public TurboLinkGenerator(FileDescriptorProto protoFile, GrpcServiceFile serviceFile)
{
ProtoFile = protoFile;
ServiceFile = serviceFile;
}
public void BuildOutputFiles(bool generateServiceCode, bool generateJsonCode)
{
GeneratedFile file;
string turboLinkBaseName = ServiceFile.TurboLinkBasicFileName;
GenerateParam generateParam;
generateParam.GenerateServiceCode = generateServiceCode;
generateParam.GenerateJsonCode = generateJsonCode;
// xxxMarshaling.h
Template.MarshalingH marshalingHTemplate = new Template.MarshalingH(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Private", turboLinkBaseName + "Marshaling.h");
file.Content = marshalingHTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxMarshaling.cpp
Template.MarshalingCPP marshalingCPPTemplate = new Template.MarshalingCPP(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Private", turboLinkBaseName + "Marshaling.cpp");
file.Content = marshalingCPPTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxMessage.h
Template.MessageH messageHTemplate = new Template.MessageH(ServiceFile, generateParam);
file.FileName = string.Join("/", "Public", turboLinkBaseName + "Message.h");
file.Content = messageHTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxMessage.cpp
Template.MessageCPP messageCPPTemplate = new Template.MessageCPP(ServiceFile, generateParam);
file.FileName = string.Join("/", "Private", turboLinkBaseName + "Message.cpp");
file.Content = messageCPPTemplate.TransformText();
GeneratedFiles.Add(file);
if (ProtoFile.Service.Count > 0 && generateParam.GenerateServiceCode)
{
// xxxService.h
Template.ServiceH serviceHTemplate = new Template.ServiceH(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Public", turboLinkBaseName + "Service.h");
file.Content = serviceHTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxClient.h
Template.ClientH clientHTemplate = new Template.ClientH(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Public", turboLinkBaseName + "Client.h");
file.Content = clientHTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxClient.cpp
Template.ClientCPP clientCPPTemplate = new Template.ClientCPP(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Private", turboLinkBaseName + "Client.cpp");
file.Content = clientCPPTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxServicePrivate.h
Template.ServicePrivateH servicePrivateHTemplate = new Template.ServicePrivateH(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Private", turboLinkBaseName + "Service_Private.h");
file.Content = servicePrivateHTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxServicePrivate.cpp
Template.ServicePrivateCPP servicePrivateCPPTemplate = new Template.ServicePrivateCPP(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Private", turboLinkBaseName + "Service_Private.cpp");
file.Content = servicePrivateCPPTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxContext.h
Template.ContextH contextHTemplate = new Template.ContextH(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Private", turboLinkBaseName + "Context.h");
file.Content = contextHTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxContext.cpp
Template.ContextCPP contextCPPTemplate = new Template.ContextCPP(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Private", turboLinkBaseName + "Context.cpp");
file.Content = contextCPPTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxService.cpp
Template.ServiceCPP serviceCPPTemplate = new Template.ServiceCPP(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Private", turboLinkBaseName + "Service.cpp");
file.Content = serviceCPPTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxNode.h
if (ServiceFile.GetTotalPingPongMethodCounts() > 0)
{
Template.NodeH nodeHTemplate = new Template.NodeH(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Public", turboLinkBaseName + "Node.h");
file.Content = nodeHTemplate.TransformText();
GeneratedFiles.Add(file);
// xxxNode.cpp
Template.NodeCPP nodeCPPTemplate = new Template.NodeCPP(ServiceFile, generateParam);
file = new GeneratedFile();
file.FileName = string.Join("/", "Private", turboLinkBaseName + "Node.cpp");
file.Content = nodeCPPTemplate.TransformText();
GeneratedFiles.Add(file);
}
}
}
}
}