2024-02-27 18:39:15 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.apinb.com/bsm-sdk/engine/utils"
|
2024-02-27 21:02:40 +08:00
|
|
|
"github.com/jhump/protoreflect/desc"
|
|
|
|
"github.com/jhump/protoreflect/desc/protoparse"
|
2024-02-27 18:39:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
protoPath string = "./proto"
|
|
|
|
jsPath string = "./axios/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-27 23:01:27 +08:00
|
|
|
fmt.Println("CONV: poroto file to js file.")
|
2024-02-27 18:39:15 +08:00
|
|
|
|
|
|
|
srvPaths := getSubDirs(protoPath)
|
2024-02-27 23:01:27 +08:00
|
|
|
fmt.Println("CONV:", protoPath, srvPaths, "JS Out:", jsPath)
|
2024-02-27 18:39:15 +08:00
|
|
|
|
2024-02-27 22:32:45 +08:00
|
|
|
for _, v := range srvPaths {
|
2024-02-27 23:01:27 +08:00
|
|
|
fmt.Println("CONV:", protoPath, "=>", v)
|
2024-02-27 22:32:45 +08:00
|
|
|
parseProtos(protoPath, v)
|
|
|
|
}
|
2024-02-27 18:39:15 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSubDirs(root string) []string {
|
|
|
|
var paths []string
|
|
|
|
|
|
|
|
files, _ := os.ReadDir(root)
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
paths = append(paths, file.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseProtos(root, dir string) {
|
2024-02-27 21:02:40 +08:00
|
|
|
//var msg []pbparser.MessageElement
|
|
|
|
var mess []*desc.MessageDescriptor
|
2024-02-27 18:39:15 +08:00
|
|
|
|
|
|
|
fPath := path.Join(root, dir)
|
|
|
|
tsPath := path.Join(jsPath, dir)
|
|
|
|
|
|
|
|
files, _ := os.ReadDir(fPath)
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
pfPath := filepath.Join(fPath, file.Name())
|
2024-02-27 21:02:40 +08:00
|
|
|
|
|
|
|
Parser := protoparse.Parser{
|
|
|
|
IncludeSourceCodeInfo: true,
|
|
|
|
}
|
|
|
|
descs, err := Parser.ParseFiles(pfPath)
|
2024-02-27 18:39:15 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-02-27 22:26:03 +08:00
|
|
|
if len(descs) > 0 {
|
|
|
|
mess = append(mess, descs[0].GetMessageTypes()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(descs[0].GetServices()) > 0 {
|
|
|
|
for _, srvItem := range descs[0].GetServices() {
|
2024-02-27 22:32:45 +08:00
|
|
|
writeSrvs(dir, tsPath, srvItem.GetName(), srvItem.GetSourceInfo().GetLeadingComments(), srvItem.GetMethods())
|
2024-02-27 22:26:03 +08:00
|
|
|
}
|
|
|
|
}
|
2024-02-27 18:39:15 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
writeMessageTypes(tsPath, mess)
|
2024-02-27 18:39:15 +08:00
|
|
|
}
|
|
|
|
|
2024-02-27 22:26:03 +08:00
|
|
|
func writeSrvs(PkgName, tsPath, srvName, commit string, methods []*desc.MethodDescriptor) {
|
|
|
|
var bodys []string
|
|
|
|
var types []string
|
|
|
|
var funcs []string
|
|
|
|
|
|
|
|
headerTpl := `
|
|
|
|
import request from "../../request";
|
|
|
|
import { AxiosPromise } from "axios";
|
|
|
|
import { {{Types}} } from "./types";
|
|
|
|
|
|
|
|
`
|
|
|
|
methodsTpl := `
|
|
|
|
{{Commit}}
|
|
|
|
export function {{MethodName}}(data: {{In}}): AxiosPromise<{{Reply}}> {
|
|
|
|
return request({
|
|
|
|
url: "/{{PkgName}}.{{SrvName}}.{{MethodName}}",
|
|
|
|
method: "post",
|
|
|
|
data: data,
|
2024-02-27 23:01:27 +08:00
|
|
|
});
|
2024-02-27 22:26:03 +08:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
for _, v := range methods {
|
|
|
|
tmp := methodsTpl
|
|
|
|
tmp = strings.ReplaceAll(tmp, "{{PkgName}}", PkgName)
|
|
|
|
tmp = strings.ReplaceAll(tmp, "{{SrvName}}", srvName)
|
|
|
|
tmp = strings.ReplaceAll(tmp, "{{MethodName}}", v.GetName())
|
|
|
|
|
|
|
|
var commit string = ""
|
|
|
|
if v.GetSourceInfo().GetLeadingComments() != "" {
|
|
|
|
commit = commit + v.GetSourceInfo().GetLeadingComments() + " "
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.GetSourceInfo().GetTrailingComments() != "" {
|
|
|
|
commit = commit + v.GetSourceInfo().GetTrailingComments() + " "
|
|
|
|
}
|
|
|
|
commit = strings.ReplaceAll(commit, "\r\n", "")
|
|
|
|
|
|
|
|
if commit == "" {
|
|
|
|
tmp = strings.ReplaceAll(tmp, "{{Commit}}", "")
|
|
|
|
} else {
|
|
|
|
tmp = strings.ReplaceAll(tmp, "{{Commit}}", " // "+commit)
|
|
|
|
}
|
|
|
|
|
|
|
|
in := v.GetInputType().GetName()
|
|
|
|
reply := v.GetOutputType().GetName()
|
|
|
|
|
|
|
|
tmp = strings.ReplaceAll(tmp, "{{In}}", in)
|
|
|
|
tmp = strings.ReplaceAll(tmp, "{{Reply}}", reply)
|
|
|
|
|
|
|
|
if !utils.In(in, types) {
|
|
|
|
types = append(types, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !utils.In(reply, types) {
|
|
|
|
types = append(types, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
funcs = append(funcs, tmp)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
header := strings.ReplaceAll(headerTpl, "{{Types}}", strings.Join(types, ","))
|
|
|
|
|
|
|
|
bodys = append(bodys, header)
|
|
|
|
bodys = append(bodys, funcs...)
|
2024-02-27 18:39:15 +08:00
|
|
|
|
2024-02-27 23:01:27 +08:00
|
|
|
if !utils.PathExists(tsPath) {
|
|
|
|
os.Mkdir(tsPath, 0755)
|
|
|
|
}
|
2024-02-27 22:26:03 +08:00
|
|
|
tsFileName := strings.ToLower(srvName) + ".ts"
|
|
|
|
destPath := filepath.Join(tsPath, tsFileName)
|
|
|
|
utils.StringToFile(destPath, strings.Join(bodys, "\r\n"))
|
2024-02-27 18:39:15 +08:00
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
func writeMessageTypes(tsPath string, msg []*desc.MessageDescriptor) {
|
2024-02-27 18:39:15 +08:00
|
|
|
tpl := `
|
|
|
|
{{Commit}}
|
|
|
|
export interface {{Name}} {
|
|
|
|
{{Data}}
|
|
|
|
}`
|
|
|
|
|
|
|
|
keyMap := make(map[string]int)
|
|
|
|
|
|
|
|
var bodys []string
|
|
|
|
for _, msgItem := range msg {
|
|
|
|
var data []string
|
|
|
|
var msgBody = tpl
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
for _, fd := range msgItem.GetFields() {
|
2024-02-27 18:39:15 +08:00
|
|
|
data = append(data, dispFields(fd))
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
keyFname := strings.ToLower(msgItem.GetName())
|
2024-02-27 18:39:15 +08:00
|
|
|
if _, ok := keyMap[keyFname]; !ok {
|
2024-02-27 21:02:40 +08:00
|
|
|
var commit string = ""
|
|
|
|
if msgItem.GetSourceInfo().GetLeadingComments() != "" {
|
|
|
|
commit = msgItem.GetSourceInfo().GetLeadingComments()
|
|
|
|
}
|
2024-02-28 10:24:21 +08:00
|
|
|
commit = strings.ReplaceAll(commit, "\r\n", " ")
|
|
|
|
commit = strings.ReplaceAll(commit, "\n", " ")
|
|
|
|
|
|
|
|
if commit == "" {
|
|
|
|
msgBody = strings.ReplaceAll(msgBody, "{{Commit}}", "")
|
|
|
|
} else {
|
|
|
|
msgBody = strings.ReplaceAll(msgBody, "{{Commit}}", " // "+commit)
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
msgBody = strings.ReplaceAll(msgBody, "{{Name}}", msgItem.GetName())
|
2024-02-27 18:39:15 +08:00
|
|
|
msgBody = strings.ReplaceAll(msgBody, "{{Data}}", strings.Join(data, "\r\n"))
|
|
|
|
|
|
|
|
bodys = append(bodys, msgBody)
|
|
|
|
keyMap[keyFname] = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typesPath := filepath.Join(tsPath, "types.ts")
|
2024-02-27 23:01:27 +08:00
|
|
|
if !utils.PathExists(tsPath) {
|
|
|
|
os.Mkdir(tsPath, 0755)
|
|
|
|
}
|
2024-02-27 18:39:15 +08:00
|
|
|
utils.StringToFile(typesPath, strings.Join(bodys, "\r\n"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
func dispFields(fd *desc.FieldDescriptor) string {
|
2024-02-27 18:39:15 +08:00
|
|
|
tpl := " {{Name}}?: {{Type}}; {{Commit}}"
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
name := strings.Trim(fd.GetName(), "")
|
2024-02-27 18:39:15 +08:00
|
|
|
name = strings.ReplaceAll(name, "\t", "")
|
|
|
|
tpl = strings.ReplaceAll(tpl, "{{Name}}", name)
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
var commit string = ""
|
|
|
|
if fd.GetSourceInfo().GetLeadingComments() != "" {
|
|
|
|
commit = commit + fd.GetSourceInfo().GetLeadingComments() + " "
|
|
|
|
}
|
|
|
|
|
|
|
|
if fd.GetSourceInfo().GetTrailingComments() != "" {
|
|
|
|
commit = commit + fd.GetSourceInfo().GetTrailingComments() + " "
|
|
|
|
}
|
2024-02-27 18:39:15 +08:00
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
if commit == "" {
|
2024-02-27 18:39:15 +08:00
|
|
|
tpl = strings.ReplaceAll(tpl, "{{Commit}}", "")
|
|
|
|
} else {
|
2024-02-27 21:02:40 +08:00
|
|
|
tpl = strings.ReplaceAll(tpl, "{{Commit}}", " // "+commit)
|
2024-02-27 18:39:15 +08:00
|
|
|
}
|
|
|
|
|
2024-02-28 10:24:21 +08:00
|
|
|
tpl = strings.ReplaceAll(tpl, "{{Type}}", getFdType(fd))
|
|
|
|
|
|
|
|
tpl = strings.ReplaceAll(tpl, "\r\n", "")
|
|
|
|
|
|
|
|
return tpl
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFdType(fd *desc.FieldDescriptor) string {
|
|
|
|
|
|
|
|
if fd.IsMap() {
|
|
|
|
return "{ [key in string]: any}"
|
|
|
|
}
|
|
|
|
|
|
|
|
if fd.IsRepeated() {
|
|
|
|
switch fd.GetType().String() {
|
|
|
|
case "TYPE_STRING":
|
|
|
|
return "string[]"
|
|
|
|
case "TYPE_INT64", "TYPE_INT32", "TYPE_FLOAT32", "TYPE_FLOAT64":
|
|
|
|
return "number[]"
|
|
|
|
case "TYPE_MESSAGE":
|
|
|
|
return fd.GetMessageType().GetName() + "[]"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-27 21:02:40 +08:00
|
|
|
switch fd.GetType().String() {
|
|
|
|
case "TYPE_STRING":
|
2024-02-28 10:24:21 +08:00
|
|
|
return "string"
|
2024-02-28 10:28:34 +08:00
|
|
|
case "TYPE_INT64", "TYPE_INT32", "TYPE_FLOAT32", "TYPE_FLOAT64", "TYPE_DOUBLE":
|
2024-02-28 10:24:21 +08:00
|
|
|
return "number"
|
|
|
|
case "TYPE_BOOL":
|
2024-02-28 10:28:34 +08:00
|
|
|
return "boolean"
|
2024-02-28 10:24:21 +08:00
|
|
|
case "TYPE_MESSAGE":
|
|
|
|
return fd.GetMessageType().GetName()
|
2024-02-27 18:39:15 +08:00
|
|
|
default:
|
2024-02-28 10:24:21 +08:00
|
|
|
return fd.GetType().String()
|
2024-02-27 18:39:15 +08:00
|
|
|
}
|
|
|
|
}
|