123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package generator
- import (
- "path/filepath"
- "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
- "github.com/tal-tech/go-zero/tools/goctl/util"
- "github.com/tal-tech/go-zero/tools/goctl/util/console"
- "github.com/tal-tech/go-zero/tools/goctl/util/ctx"
- )
- type RpcGenerator struct {
- g Generator
- style NamingStyle
- }
- func NewDefaultRpcGenerator(style NamingStyle) *RpcGenerator {
- return NewRpcGenerator(NewDefaultGenerator(), style)
- }
- func NewRpcGenerator(g Generator, style NamingStyle) *RpcGenerator {
- return &RpcGenerator{
- g: g,
- style: style,
- }
- }
- func (g *RpcGenerator) Generate(src, target string, protoImportPath []string) error {
- abs, err := filepath.Abs(target)
- if err != nil {
- return err
- }
- err = util.MkdirIfNotExist(abs)
- if err != nil {
- return err
- }
- err = g.g.Prepare()
- if err != nil {
- return err
- }
- projectCtx, err := ctx.Prepare(abs)
- if err != nil {
- return err
- }
- p := parser.NewDefaultProtoParser()
- proto, err := p.Parse(src)
- if err != nil {
- return err
- }
- dirCtx, err := mkdir(projectCtx, proto)
- if err != nil {
- return err
- }
- err = g.g.GenEtc(dirCtx, proto, g.style)
- if err != nil {
- return err
- }
- err = g.g.GenPb(dirCtx, protoImportPath, proto, g.style)
- if err != nil {
- return err
- }
- err = g.g.GenConfig(dirCtx, proto, g.style)
- if err != nil {
- return err
- }
- err = g.g.GenSvc(dirCtx, proto, g.style)
- if err != nil {
- return err
- }
- err = g.g.GenLogic(dirCtx, proto, g.style)
- if err != nil {
- return err
- }
- err = g.g.GenServer(dirCtx, proto, g.style)
- if err != nil {
- return err
- }
- err = g.g.GenMain(dirCtx, proto, g.style)
- if err != nil {
- return err
- }
- err = g.g.GenCall(dirCtx, proto, g.style)
- console.NewColorConsole().MarkDone()
- return err
- }
|