|
@@ -2,33 +2,86 @@ package generator
|
|
|
|
|
|
import (
|
|
import (
|
|
"bytes"
|
|
"bytes"
|
|
|
|
+ "errors"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
|
|
+ "github.com/tal-tech/go-zero/core/collection"
|
|
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
|
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+const googleProtocGenGoErr = `--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC`
|
|
|
|
+
|
|
// GenPb generates the pb.go file, which is a layer of packaging for protoc to generate gprc,
|
|
// GenPb generates the pb.go file, which is a layer of packaging for protoc to generate gprc,
|
|
// but the commands and flags in protoc are not completely joined in goctl. At present, proto_path(-I) is introduced
|
|
// but the commands and flags in protoc are not completely joined in goctl. At present, proto_path(-I) is introduced
|
|
-func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto, _ *conf.Config) error {
|
|
|
|
|
|
+func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto, _ *conf.Config, goOptions ...string) error {
|
|
dir := ctx.GetPb()
|
|
dir := ctx.GetPb()
|
|
cw := new(bytes.Buffer)
|
|
cw := new(bytes.Buffer)
|
|
- base := filepath.Dir(proto.Src)
|
|
|
|
|
|
+ directory, base := filepath.Split(proto.Src)
|
|
|
|
+ directory = filepath.Clean(directory)
|
|
cw.WriteString("protoc ")
|
|
cw.WriteString("protoc ")
|
|
|
|
+ protoImportPathSet := collection.NewSet()
|
|
for _, ip := range protoImportPath {
|
|
for _, ip := range protoImportPath {
|
|
- cw.WriteString(" -I=" + ip)
|
|
|
|
|
|
+ pip := " --proto_path=" + ip
|
|
|
|
+ if protoImportPathSet.Contains(pip) {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protoImportPathSet.AddStr(pip)
|
|
|
|
+ cw.WriteString(pip)
|
|
|
|
+ }
|
|
|
|
+ currentPath := " --proto_path=" + directory
|
|
|
|
+ if !protoImportPathSet.Contains(currentPath) {
|
|
|
|
+ cw.WriteString(currentPath)
|
|
}
|
|
}
|
|
- cw.WriteString(" -I=" + base)
|
|
|
|
cw.WriteString(" " + proto.Name)
|
|
cw.WriteString(" " + proto.Name)
|
|
if strings.Contains(proto.GoPackage, "/") {
|
|
if strings.Contains(proto.GoPackage, "/") {
|
|
cw.WriteString(" --go_out=plugins=grpc:" + ctx.GetMain().Filename)
|
|
cw.WriteString(" --go_out=plugins=grpc:" + ctx.GetMain().Filename)
|
|
} else {
|
|
} else {
|
|
cw.WriteString(" --go_out=plugins=grpc:" + dir.Filename)
|
|
cw.WriteString(" --go_out=plugins=grpc:" + dir.Filename)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // Compatible with version 1.4.0,github.com/golang/protobuf/protoc-gen-go@v1.4.0
|
|
|
|
+ // --go_opt usage please see https://developers.google.com/protocol-buffers/docs/reference/go-generated#package
|
|
|
|
+ optSet := collection.NewSet()
|
|
|
|
+ for _, op := range goOptions {
|
|
|
|
+ opt := " --go_opt=" + op
|
|
|
|
+ if optSet.Contains(opt) {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ optSet.AddStr(op)
|
|
|
|
+ cw.WriteString(" --go_opt=" + op)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var currentFileOpt string
|
|
|
|
+ if filepath.IsAbs(proto.GoPackage) {
|
|
|
|
+ currentFileOpt = " --go_opt=M" + base + "=" + proto.GoPackage
|
|
|
|
+ } else if strings.Contains(proto.GoPackage, string(filepath.Separator)) {
|
|
|
|
+ currentFileOpt = " --go_opt=M" + base + "=./" + proto.GoPackage
|
|
|
|
+ } else {
|
|
|
|
+ currentFileOpt = " --go_opt=M" + base + "=../" + proto.GoPackage
|
|
|
|
+ }
|
|
|
|
+ if !optSet.Contains(currentFileOpt) {
|
|
|
|
+ cw.WriteString(currentFileOpt)
|
|
|
|
+ }
|
|
|
|
+
|
|
command := cw.String()
|
|
command := cw.String()
|
|
g.log.Debug(command)
|
|
g.log.Debug(command)
|
|
_, err := execx.Run(command, "")
|
|
_, err := execx.Run(command, "")
|
|
- return err
|
|
|
|
|
|
+ if err != nil {
|
|
|
|
+ if strings.Contains(err.Error(), googleProtocGenGoErr) {
|
|
|
|
+ return errors.New(`Unsupported plugin protoc-gen-go which installed from the following source:
|
|
|
|
+google.golang.org/protobuf/cmd/protoc-gen-go,
|
|
|
|
+github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go;
|
|
|
|
+
|
|
|
|
+Please replace it by the following command, we recommend to use version before v1.3.5:
|
|
|
|
+go get -u github.com/golang/protobuf/protoc-gen-go`)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
}
|
|
}
|