genpb.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "github.com/tal-tech/go-zero/tools/goctl/util/env"
  12. )
  13. const googleProtocGenGoErr = `--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC`
  14. // GenPb generates the pb.go file, which is a layer of packaging for protoc to generate gprc,
  15. // but the commands and flags in protoc are not completely joined in goctl. At present, proto_path(-I) is introduced
  16. func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto, _ *conf.Config, goOptions ...string) error {
  17. var useGoctl bool
  18. _, err := env.LookUpProtocGenGoctl()
  19. if err == nil {
  20. useGoctl = true
  21. }
  22. dir := ctx.GetPb()
  23. cw := new(bytes.Buffer)
  24. directory, base := filepath.Split(proto.Src)
  25. directory = filepath.Clean(directory)
  26. cw.WriteString("protoc ")
  27. protoImportPathSet := collection.NewSet()
  28. isSamePackage := true
  29. for _, ip := range protoImportPath {
  30. pip := " --proto_path=" + ip
  31. if protoImportPathSet.Contains(pip) {
  32. continue
  33. }
  34. abs, err := filepath.Abs(ip)
  35. if err != nil {
  36. return err
  37. }
  38. if abs == directory {
  39. isSamePackage = true
  40. } else {
  41. isSamePackage = false
  42. }
  43. protoImportPathSet.AddStr(pip)
  44. cw.WriteString(pip)
  45. }
  46. currentPath := " --proto_path=" + directory
  47. if !protoImportPathSet.Contains(currentPath) {
  48. cw.WriteString(currentPath)
  49. }
  50. cw.WriteString(" " + proto.Name)
  51. outFlag := " --go_out"
  52. if useGoctl {
  53. outFlag = " --goctl_out"
  54. }
  55. if strings.Contains(proto.GoPackage, "/") {
  56. cw.WriteString(outFlag + "=plugins=grpc:" + ctx.GetMain().Filename)
  57. } else {
  58. cw.WriteString(outFlag + "=plugins=grpc:" + dir.Filename)
  59. }
  60. // Compatible with version 1.4.0,github.com/golang/protobuf/protoc-gen-go@v1.4.0
  61. // --go_opt usage please see https://developers.google.com/protocol-buffers/docs/reference/go-generated#package
  62. optSet := collection.NewSet()
  63. for _, op := range goOptions {
  64. opt := " --go_opt=" + op
  65. if optSet.Contains(opt) {
  66. continue
  67. }
  68. optSet.AddStr(op)
  69. }
  70. var currentFileOpt string
  71. if !isSamePackage || (len(proto.GoPackage) > 0 && proto.GoPackage != proto.Package.Name) {
  72. if filepath.IsAbs(proto.GoPackage) {
  73. currentFileOpt = " --go_opt=M" + base + "=" + proto.GoPackage
  74. } else if strings.Contains(proto.GoPackage, string(filepath.Separator)) {
  75. currentFileOpt = " --go_opt=M" + base + "=./" + proto.GoPackage
  76. } else {
  77. currentFileOpt = " --go_opt=M" + base + "=../" + proto.GoPackage
  78. }
  79. } else {
  80. currentFileOpt = " --go_opt=M" + base + "=."
  81. }
  82. if !optSet.Contains(currentFileOpt) && !useGoctl {
  83. cw.WriteString(currentFileOpt)
  84. }
  85. command := cw.String()
  86. g.log.Debug(command)
  87. _, err = execx.Run(command, "")
  88. if err != nil {
  89. if strings.Contains(err.Error(), googleProtocGenGoErr) {
  90. return errors.New(`unsupported plugin protoc-gen-go which installed from the following source:
  91. google.golang.org/protobuf/cmd/protoc-gen-go,
  92. github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go;
  93. Please replace it by the following command, we recommend to use version before v1.3.5:
  94. go get -u github.com/golang/protobuf/protoc-gen-go`)
  95. }
  96. return err
  97. }
  98. return nil
  99. }