genpb.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package generator
  2. import (
  3. "bytes"
  4. "errors"
  5. "path/filepath"
  6. "strings"
  7. "github.com/tal-tech/go-zero/core/collection"
  8. conf "github.com/tal-tech/go-zero/tools/goctl/config"
  9. "github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
  10. "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
  11. )
  12. const googleProtocGenGoErr = `--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC`
  13. // GenPb generates the pb.go file, which is a layer of packaging for protoc to generate gprc,
  14. // but the commands and flags in protoc are not completely joined in goctl. At present, proto_path(-I) is introduced
  15. func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto, _ *conf.Config, goOptions ...string) error {
  16. dir := ctx.GetPb()
  17. cw := new(bytes.Buffer)
  18. directory, base := filepath.Split(proto.Src)
  19. directory = filepath.Clean(directory)
  20. cw.WriteString("protoc ")
  21. protoImportPathSet := collection.NewSet()
  22. for _, ip := range protoImportPath {
  23. pip := " --proto_path=" + ip
  24. if protoImportPathSet.Contains(pip) {
  25. continue
  26. }
  27. protoImportPathSet.AddStr(pip)
  28. cw.WriteString(pip)
  29. }
  30. currentPath := " --proto_path=" + directory
  31. if !protoImportPathSet.Contains(currentPath) {
  32. cw.WriteString(currentPath)
  33. }
  34. cw.WriteString(" " + proto.Name)
  35. if strings.Contains(proto.GoPackage, "/") {
  36. cw.WriteString(" --go_out=plugins=grpc:" + ctx.GetMain().Filename)
  37. } else {
  38. cw.WriteString(" --go_out=plugins=grpc:" + dir.Filename)
  39. }
  40. // Compatible with version 1.4.0,github.com/golang/protobuf/protoc-gen-go@v1.4.0
  41. // --go_opt usage please see https://developers.google.com/protocol-buffers/docs/reference/go-generated#package
  42. optSet := collection.NewSet()
  43. for _, op := range goOptions {
  44. opt := " --go_opt=" + op
  45. if optSet.Contains(opt) {
  46. continue
  47. }
  48. optSet.AddStr(op)
  49. cw.WriteString(" --go_opt=" + op)
  50. }
  51. var currentFileOpt string
  52. if filepath.IsAbs(proto.GoPackage) {
  53. currentFileOpt = " --go_opt=M" + base + "=" + proto.GoPackage
  54. } else if strings.Contains(proto.GoPackage, string(filepath.Separator)) {
  55. currentFileOpt = " --go_opt=M" + base + "=./" + proto.GoPackage
  56. } else {
  57. currentFileOpt = " --go_opt=M" + base + "=../" + proto.GoPackage
  58. }
  59. if !optSet.Contains(currentFileOpt) {
  60. cw.WriteString(currentFileOpt)
  61. }
  62. command := cw.String()
  63. g.log.Debug(command)
  64. _, err := execx.Run(command, "")
  65. if err != nil {
  66. if strings.Contains(err.Error(), googleProtocGenGoErr) {
  67. return errors.New(`Unsupported plugin protoc-gen-go which installed from the following source:
  68. google.golang.org/protobuf/cmd/protoc-gen-go,
  69. github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go;
  70. Please replace it by the following command, we recommend to use version before v1.3.5:
  71. go get -u github.com/golang/protobuf/protoc-gen-go`)
  72. }
  73. return err
  74. }
  75. return nil
  76. }