gen.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package generator
  2. import (
  3. "path/filepath"
  4. "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
  5. "github.com/tal-tech/go-zero/tools/goctl/util"
  6. "github.com/tal-tech/go-zero/tools/goctl/util/console"
  7. "github.com/tal-tech/go-zero/tools/goctl/util/ctx"
  8. )
  9. type RpcGenerator struct {
  10. g Generator
  11. style NamingStyle
  12. }
  13. func NewDefaultRpcGenerator(style NamingStyle) *RpcGenerator {
  14. return NewRpcGenerator(NewDefaultGenerator(), style)
  15. }
  16. func NewRpcGenerator(g Generator, style NamingStyle) *RpcGenerator {
  17. return &RpcGenerator{
  18. g: g,
  19. style: style,
  20. }
  21. }
  22. func (g *RpcGenerator) Generate(src, target string, protoImportPath []string) error {
  23. abs, err := filepath.Abs(target)
  24. if err != nil {
  25. return err
  26. }
  27. err = util.MkdirIfNotExist(abs)
  28. if err != nil {
  29. return err
  30. }
  31. err = g.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(src)
  41. if err != nil {
  42. return err
  43. }
  44. dirCtx, err := mkdir(projectCtx, proto)
  45. if err != nil {
  46. return err
  47. }
  48. err = g.g.GenEtc(dirCtx, proto, g.style)
  49. if err != nil {
  50. return err
  51. }
  52. err = g.g.GenPb(dirCtx, protoImportPath, proto, g.style)
  53. if err != nil {
  54. return err
  55. }
  56. err = g.g.GenConfig(dirCtx, proto, g.style)
  57. if err != nil {
  58. return err
  59. }
  60. err = g.g.GenSvc(dirCtx, proto, g.style)
  61. if err != nil {
  62. return err
  63. }
  64. err = g.g.GenLogic(dirCtx, proto, g.style)
  65. if err != nil {
  66. return err
  67. }
  68. err = g.g.GenServer(dirCtx, proto, g.style)
  69. if err != nil {
  70. return err
  71. }
  72. err = g.g.GenMain(dirCtx, proto, g.style)
  73. if err != nil {
  74. return err
  75. }
  76. err = g.g.GenCall(dirCtx, proto, g.style)
  77. console.NewColorConsole().MarkDone()
  78. return err
  79. }