gen.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  12. func NewDefaultRpcGenerator() *RpcGenerator {
  13. return NewRpcGenerator(NewDefaultGenerator())
  14. }
  15. func NewRpcGenerator(g Generator) *RpcGenerator {
  16. return &RpcGenerator{
  17. g: g,
  18. }
  19. }
  20. func (g *RpcGenerator) Generate(src, target string, protoImportPath []string) error {
  21. abs, err := filepath.Abs(target)
  22. if err != nil {
  23. return err
  24. }
  25. err = util.MkdirIfNotExist(abs)
  26. if err != nil {
  27. return err
  28. }
  29. err = g.g.Prepare()
  30. if err != nil {
  31. return err
  32. }
  33. projectCtx, err := ctx.Prepare(abs)
  34. if err != nil {
  35. return err
  36. }
  37. p := parser.NewDefaultProtoParser()
  38. proto, err := p.Parse(src)
  39. if err != nil {
  40. return err
  41. }
  42. dirCtx, err := mkdir(projectCtx, proto)
  43. if err != nil {
  44. return err
  45. }
  46. err = g.g.GenEtc(dirCtx, proto)
  47. if err != nil {
  48. return err
  49. }
  50. err = g.g.GenPb(dirCtx, protoImportPath, proto)
  51. if err != nil {
  52. return err
  53. }
  54. err = g.g.GenConfig(dirCtx, proto)
  55. if err != nil {
  56. return err
  57. }
  58. err = g.g.GenSvc(dirCtx, proto)
  59. if err != nil {
  60. return err
  61. }
  62. err = g.g.GenLogic(dirCtx, proto)
  63. if err != nil {
  64. return err
  65. }
  66. err = g.g.GenServer(dirCtx, proto)
  67. if err != nil {
  68. return err
  69. }
  70. err = g.g.GenMain(dirCtx, proto)
  71. if err != nil {
  72. return err
  73. }
  74. err = g.g.GenCall(dirCtx, proto)
  75. console.NewColorConsole().MarkDone()
  76. return err
  77. }