gen.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // Sre is the source file of the proto.
  11. Src string
  12. // ProtoCmd is the command to generate proto files.
  13. ProtocCmd string
  14. // ProtoGenGrpcDir is the directory to store the generated proto files.
  15. ProtoGenGrpcDir string
  16. // ProtoGenGoDir is the directory to store the generated go files.
  17. ProtoGenGoDir string
  18. // IsGooglePlugin is the flag to indicate whether the proto file is generated by google plugin.
  19. IsGooglePlugin bool
  20. // GoOutput is the output directory of the generated go files.
  21. GoOutput string
  22. // GrpcOutput is the output directory of the generated grpc files.
  23. GrpcOutput string
  24. // Output is the output directory of the generated files.
  25. Output string
  26. // Multiple is the flag to indicate whether the proto file is generated in multiple mode.
  27. Multiple bool
  28. }
  29. // Generate generates a rpc service, through the proto file,
  30. // code storage directory, and proto import parameters to control
  31. // the source file and target location of the rpc service that needs to be generated
  32. func (g *Generator) Generate(zctx *ZRpcContext) error {
  33. abs, err := filepath.Abs(zctx.Output)
  34. if err != nil {
  35. return err
  36. }
  37. err = pathx.MkdirIfNotExist(abs)
  38. if err != nil {
  39. return err
  40. }
  41. err = g.Prepare()
  42. if err != nil {
  43. return err
  44. }
  45. projectCtx, err := ctx.Prepare(abs)
  46. if err != nil {
  47. return err
  48. }
  49. p := parser.NewDefaultProtoParser()
  50. proto, err := p.Parse(zctx.Src, zctx.Multiple)
  51. if err != nil {
  52. return err
  53. }
  54. dirCtx, err := mkdir(projectCtx, proto, g.cfg, zctx)
  55. if err != nil {
  56. return err
  57. }
  58. err = g.GenEtc(dirCtx, proto, g.cfg)
  59. if err != nil {
  60. return err
  61. }
  62. err = g.GenPb(dirCtx, zctx)
  63. if err != nil {
  64. return err
  65. }
  66. err = g.GenConfig(dirCtx, proto, g.cfg)
  67. if err != nil {
  68. return err
  69. }
  70. err = g.GenSvc(dirCtx, proto, g.cfg)
  71. if err != nil {
  72. return err
  73. }
  74. err = g.GenLogic(dirCtx, proto, g.cfg, zctx)
  75. if err != nil {
  76. return err
  77. }
  78. err = g.GenServer(dirCtx, proto, g.cfg, zctx)
  79. if err != nil {
  80. return err
  81. }
  82. err = g.GenMain(dirCtx, proto, g.cfg, zctx)
  83. if err != nil {
  84. return err
  85. }
  86. err = g.GenCall(dirCtx, proto, g.cfg, zctx)
  87. console.NewColorConsole().MarkDone()
  88. return err
  89. }