gen.go 2.8 KB

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