genpb.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. package generator
  2. import (
  3. "bytes"
  4. "path/filepath"
  5. "strings"
  6. conf "git.i2edu.net/i2/go-zero/tools/goctl/config"
  7. "git.i2edu.net/i2/go-zero/tools/goctl/rpc/execx"
  8. "git.i2edu.net/i2/go-zero/tools/goctl/rpc/parser"
  9. )
  10. // GenPb generates the pb.go file, which is a layer of packaging for protoc to generate gprc,
  11. // but the commands and flags in protoc are not completely joined in goctl. At present, proto_path(-I) is introduced
  12. func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto, _ *conf.Config) error {
  13. dir := ctx.GetPb()
  14. cw := new(bytes.Buffer)
  15. base := filepath.Dir(proto.Src)
  16. cw.WriteString("protoc ")
  17. for _, ip := range protoImportPath {
  18. cw.WriteString(" -I=" + ip)
  19. }
  20. cw.WriteString(" -I=" + base)
  21. cw.WriteString(" " + proto.Name)
  22. if strings.Contains(proto.GoPackage, "/") {
  23. cw.WriteString(" --go_out=plugins=grpc:" + ctx.GetMain().Filename)
  24. } else {
  25. cw.WriteString(" --go_out=plugins=grpc:" + dir.Filename)
  26. }
  27. command := cw.String()
  28. g.log.Debug(command)
  29. _, err := execx.Run(command, "")
  30. return err
  31. }