genpb.go 804 B

12345678910111213141516171819202122232425262728293031
  1. package generator
  2. import (
  3. "bytes"
  4. "path/filepath"
  5. "strings"
  6. "github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
  7. "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
  8. )
  9. func (g *defaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto) error {
  10. dir := ctx.GetPb()
  11. cw := new(bytes.Buffer)
  12. base := filepath.Dir(proto.Src)
  13. cw.WriteString("protoc ")
  14. for _, ip := range protoImportPath {
  15. cw.WriteString(" -I=" + ip)
  16. }
  17. cw.WriteString(" -I=" + base)
  18. cw.WriteString(" " + proto.Name)
  19. if strings.Contains(proto.GoPackage, "/") {
  20. cw.WriteString(" --go_out=plugins=grpc:" + ctx.GetInternal().Filename)
  21. } else {
  22. cw.WriteString(" --go_out=plugins=grpc:" + dir.Filename)
  23. }
  24. command := cw.String()
  25. g.log.Debug(command)
  26. _, err := execx.Run(command, "")
  27. return err
  28. }