genpb.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. isSamePackage := true
  23. for _, ip := range protoImportPath {
  24. pip := " --proto_path=" + ip
  25. if protoImportPathSet.Contains(pip) {
  26. continue
  27. }
  28. abs, err := filepath.Abs(ip)
  29. if err != nil {
  30. return err
  31. }
  32. if abs == directory {
  33. isSamePackage = true
  34. } else {
  35. isSamePackage = false
  36. }
  37. protoImportPathSet.AddStr(pip)
  38. cw.WriteString(pip)
  39. }
  40. currentPath := " --proto_path=" + directory
  41. if !protoImportPathSet.Contains(currentPath) {
  42. cw.WriteString(currentPath)
  43. }
  44. cw.WriteString(" " + proto.Name)
  45. if strings.Contains(proto.GoPackage, "/") {
  46. cw.WriteString(" --go_out=plugins=grpc:" + ctx.GetMain().Filename)
  47. } else {
  48. cw.WriteString(" --go_out=plugins=grpc:" + dir.Filename)
  49. }
  50. // Compatible with version 1.4.0,github.com/golang/protobuf/protoc-gen-go@v1.4.0
  51. // --go_opt usage please see https://developers.google.com/protocol-buffers/docs/reference/go-generated#package
  52. optSet := collection.NewSet()
  53. for _, op := range goOptions {
  54. opt := " --go_opt=" + op
  55. if optSet.Contains(opt) {
  56. continue
  57. }
  58. optSet.AddStr(op)
  59. cw.WriteString(" --go_opt=" + op)
  60. }
  61. var currentFileOpt string
  62. if !isSamePackage || (len(proto.GoPackage) > 0 && proto.GoPackage != proto.Package.Name) {
  63. if filepath.IsAbs(proto.GoPackage) {
  64. currentFileOpt = " --go_opt=M" + base + "=" + proto.GoPackage
  65. } else if strings.Contains(proto.GoPackage, string(filepath.Separator)) {
  66. currentFileOpt = " --go_opt=M" + base + "=./" + proto.GoPackage
  67. } else {
  68. currentFileOpt = " --go_opt=M" + base + "=../" + proto.GoPackage
  69. }
  70. } else {
  71. currentFileOpt = " --go_opt=M" + base + "=."
  72. }
  73. if !optSet.Contains(currentFileOpt) {
  74. cw.WriteString(currentFileOpt)
  75. }
  76. command := cw.String()
  77. g.log.Debug(command)
  78. _, err := execx.Run(command, "")
  79. if err != nil {
  80. if strings.Contains(err.Error(), googleProtocGenGoErr) {
  81. return errors.New(`Unsupported plugin protoc-gen-go which installed from the following source:
  82. google.golang.org/protobuf/cmd/protoc-gen-go,
  83. github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go;
  84. Please replace it by the following command, we recommend to use version before v1.3.5:
  85. go get -u github.com/golang/protobuf/protoc-gen-go`)
  86. }
  87. return err
  88. }
  89. return nil
  90. }