gen.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package generator
  2. import (
  3. "path/filepath"
  4. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/rpc/parser"
  5. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/console"
  6. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/ctx"
  7. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/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. // Whether to generate rpc client
  29. IsGenClient bool
  30. }
  31. // Generate generates a rpc service, through the proto file,
  32. // code storage directory, and proto import parameters to control
  33. // the source file and target location of the rpc service that needs to be generated
  34. func (g *Generator) Generate(zctx *ZRpcContext) error {
  35. abs, err := filepath.Abs(zctx.Output)
  36. if err != nil {
  37. return err
  38. }
  39. err = pathx.MkdirIfNotExist(abs)
  40. if err != nil {
  41. return err
  42. }
  43. err = g.Prepare()
  44. if err != nil {
  45. return err
  46. }
  47. projectCtx, err := ctx.Prepare(abs)
  48. if err != nil {
  49. return err
  50. }
  51. p := parser.NewDefaultProtoParser()
  52. proto, err := p.Parse(zctx.Src, zctx.Multiple)
  53. if err != nil {
  54. return err
  55. }
  56. dirCtx, err := mkdir(projectCtx, proto, g.cfg, zctx)
  57. if err != nil {
  58. return err
  59. }
  60. err = g.GenEtc(dirCtx, proto, g.cfg)
  61. if err != nil {
  62. return err
  63. }
  64. err = g.GenPb(dirCtx, zctx)
  65. if err != nil {
  66. return err
  67. }
  68. err = g.GenConfig(dirCtx, proto, g.cfg)
  69. if err != nil {
  70. return err
  71. }
  72. err = g.GenSvc(dirCtx, proto, g.cfg)
  73. if err != nil {
  74. return err
  75. }
  76. err = g.GenLogic(dirCtx, proto, g.cfg, zctx)
  77. if err != nil {
  78. return err
  79. }
  80. err = g.GenServer(dirCtx, proto, g.cfg, zctx)
  81. if err != nil {
  82. return err
  83. }
  84. err = g.GenMain(dirCtx, proto, g.cfg, zctx)
  85. if err != nil {
  86. return err
  87. }
  88. if zctx.IsGenClient {
  89. err = g.GenCall(dirCtx, proto, g.cfg, zctx)
  90. }
  91. console.NewColorConsole().MarkDone()
  92. return err
  93. }