genpb.go 871 B

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