gen.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package gen
  2. import (
  3. "github.com/tal-tech/go-zero/tools/goctl/rpc/ctx"
  4. "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
  5. )
  6. const (
  7. dirTarget = "dirTarget"
  8. dirConfig = "config"
  9. dirEtc = "etc"
  10. dirSvc = "svc"
  11. dirServer = "server"
  12. dirLogic = "logic"
  13. dirPb = "pb"
  14. dirInternal = "internal"
  15. fileConfig = "config.go"
  16. fileServiceContext = "servicecontext.go"
  17. )
  18. type defaultRpcGenerator struct {
  19. dirM map[string]string
  20. Ctx *ctx.RpcContext
  21. ast *parser.PbAst
  22. }
  23. func NewDefaultRpcGenerator(ctx *ctx.RpcContext) *defaultRpcGenerator {
  24. return &defaultRpcGenerator{
  25. Ctx: ctx,
  26. }
  27. }
  28. func (g *defaultRpcGenerator) Generate() (err error) {
  29. g.Ctx.Info("generating code...")
  30. defer func() {
  31. if err == nil {
  32. g.Ctx.Success("Done.")
  33. }
  34. }()
  35. err = g.createDir()
  36. if err != nil {
  37. return
  38. }
  39. err = g.initGoMod()
  40. if err != nil {
  41. return
  42. }
  43. err = g.genEtc()
  44. if err != nil {
  45. return
  46. }
  47. err = g.genPb()
  48. if err != nil {
  49. return
  50. }
  51. err = g.genConfig()
  52. if err != nil {
  53. return
  54. }
  55. err = g.genSvc()
  56. if err != nil {
  57. return
  58. }
  59. err = g.genLogic()
  60. if err != nil {
  61. return
  62. }
  63. err = g.genHandler()
  64. if err != nil {
  65. return
  66. }
  67. err = g.genMain()
  68. if err != nil {
  69. return
  70. }
  71. err = g.genCall()
  72. if err != nil {
  73. return
  74. }
  75. return
  76. }