gen.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package generator
  2. import (
  3. "path/filepath"
  4. conf "github.com/tal-tech/go-zero/tools/goctl/config"
  5. "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
  6. "github.com/tal-tech/go-zero/tools/goctl/util/console"
  7. "github.com/tal-tech/go-zero/tools/goctl/util/ctx"
  8. "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
  9. )
  10. // RPCGenerator defines a generator and configure
  11. type RPCGenerator struct {
  12. g Generator
  13. cfg *conf.Config
  14. ctx *ZRpcContext
  15. }
  16. type RPCGeneratorOption func(g *RPCGenerator)
  17. type ZRpcContext struct {
  18. Src string
  19. ProtocCmd string
  20. ProtoGenGrpcDir string
  21. ProtoGenGoDir string
  22. Output string
  23. }
  24. // NewDefaultRPCGenerator wraps Generator with configure
  25. func NewDefaultRPCGenerator(style string, options ...RPCGeneratorOption) (*RPCGenerator, error) {
  26. cfg, err := conf.NewConfig(style)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return NewRPCGenerator(NewDefaultGenerator(), cfg, options...), nil
  31. }
  32. // NewRPCGenerator creates an instance for RPCGenerator
  33. func NewRPCGenerator(g Generator, cfg *conf.Config, options ...RPCGeneratorOption) *RPCGenerator {
  34. out := &RPCGenerator{
  35. g: g,
  36. cfg: cfg,
  37. }
  38. for _, opt := range options {
  39. opt(out)
  40. }
  41. return out
  42. }
  43. func WithZRpcContext(c *ZRpcContext) RPCGeneratorOption {
  44. return func(g *RPCGenerator) {
  45. g.ctx = c
  46. }
  47. }
  48. // Generate generates an rpc service, through the proto file,
  49. // code storage directory, and proto import parameters to control
  50. // the source file and target location of the rpc service that needs to be generated
  51. func (g *RPCGenerator) Generate(src, target string, protoImportPath []string, goOptions ...string) error {
  52. abs, err := filepath.Abs(target)
  53. if err != nil {
  54. return err
  55. }
  56. err = pathx.MkdirIfNotExist(abs)
  57. if err != nil {
  58. return err
  59. }
  60. err = g.g.Prepare()
  61. if err != nil {
  62. return err
  63. }
  64. projectCtx, err := ctx.Prepare(abs)
  65. if err != nil {
  66. return err
  67. }
  68. p := parser.NewDefaultProtoParser()
  69. proto, err := p.Parse(src)
  70. if err != nil {
  71. return err
  72. }
  73. dirCtx, err := mkdir(projectCtx, proto, g.cfg, g.ctx)
  74. if err != nil {
  75. return err
  76. }
  77. err = g.g.GenEtc(dirCtx, proto, g.cfg)
  78. if err != nil {
  79. return err
  80. }
  81. err = g.g.GenPb(dirCtx, protoImportPath, proto, g.cfg, g.ctx, goOptions...)
  82. if err != nil {
  83. return err
  84. }
  85. err = g.g.GenConfig(dirCtx, proto, g.cfg)
  86. if err != nil {
  87. return err
  88. }
  89. err = g.g.GenSvc(dirCtx, proto, g.cfg)
  90. if err != nil {
  91. return err
  92. }
  93. err = g.g.GenLogic(dirCtx, proto, g.cfg)
  94. if err != nil {
  95. return err
  96. }
  97. err = g.g.GenServer(dirCtx, proto, g.cfg)
  98. if err != nil {
  99. return err
  100. }
  101. err = g.g.GenMain(dirCtx, proto, g.cfg)
  102. if err != nil {
  103. return err
  104. }
  105. err = g.g.GenCall(dirCtx, proto, g.cfg)
  106. console.NewColorConsole().MarkDone()
  107. return err
  108. }