gen.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package generator
  2. import (
  3. "path/filepath"
  4. "github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
  5. "github.com/zeromicro/go-zero/tools/goctl/util/console"
  6. "github.com/zeromicro/go-zero/tools/goctl/util/ctx"
  7. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  8. )
  9. type ZRpcContext struct {
  10. Src string
  11. ProtocCmd string
  12. ProtoGenGrpcDir string
  13. ProtoGenGoDir string
  14. IsGooglePlugin bool
  15. GoOutput string
  16. GrpcOutput string
  17. Output string
  18. }
  19. // Generate generates an rpc service, through the proto file,
  20. // code storage directory, and proto import parameters to control
  21. // the source file and target location of the rpc service that needs to be generated
  22. func (g *Generator) Generate(zctx *ZRpcContext) error {
  23. abs, err := filepath.Abs(zctx.Output)
  24. if err != nil {
  25. return err
  26. }
  27. err = pathx.MkdirIfNotExist(abs)
  28. if err != nil {
  29. return err
  30. }
  31. err = g.Prepare()
  32. if err != nil {
  33. return err
  34. }
  35. projectCtx, err := ctx.Prepare(abs)
  36. if err != nil {
  37. return err
  38. }
  39. p := parser.NewDefaultProtoParser()
  40. proto, err := p.Parse(zctx.Src)
  41. if err != nil {
  42. return err
  43. }
  44. dirCtx, err := mkdir(projectCtx, proto, g.cfg, zctx)
  45. if err != nil {
  46. return err
  47. }
  48. err = g.GenEtc(dirCtx, proto, g.cfg)
  49. if err != nil {
  50. return err
  51. }
  52. err = g.GenPb(dirCtx, zctx)
  53. if err != nil {
  54. return err
  55. }
  56. err = g.GenConfig(dirCtx, proto, g.cfg)
  57. if err != nil {
  58. return err
  59. }
  60. err = g.GenSvc(dirCtx, proto, g.cfg)
  61. if err != nil {
  62. return err
  63. }
  64. err = g.GenLogic(dirCtx, proto, g.cfg)
  65. if err != nil {
  66. return err
  67. }
  68. err = g.GenServer(dirCtx, proto, g.cfg)
  69. if err != nil {
  70. return err
  71. }
  72. err = g.GenMain(dirCtx, proto, g.cfg)
  73. if err != nil {
  74. return err
  75. }
  76. err = g.GenCall(dirCtx, proto, g.cfg)
  77. console.NewColorConsole().MarkDone()
  78. return err
  79. }