123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package generator
- import (
- _ "embed"
- "fmt"
- "path/filepath"
- "strings"
- conf "github.com/zeromicro/go-zero/tools/goctl/config"
- "github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
- "github.com/zeromicro/go-zero/tools/goctl/util"
- "github.com/zeromicro/go-zero/tools/goctl/util/format"
- "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
- "github.com/zeromicro/go-zero/tools/goctl/util/stringx"
- )
- //go:embed main.tpl
- var mainTemplate string
- // GenMain generates the main file of the rpc service, which is an rpc service program call entry
- func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
- mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
- if err != nil {
- return err
- }
- fileName := filepath.Join(ctx.GetMain().Filename, fmt.Sprintf("%v.go", mainFilename))
- imports := make([]string, 0)
- pbImport := fmt.Sprintf(`"%v"`, ctx.GetPb().Package)
- svcImport := fmt.Sprintf(`"%v"`, ctx.GetSvc().Package)
- remoteImport := fmt.Sprintf(`"%v"`, ctx.GetServer().Package)
- configImport := fmt.Sprintf(`"%v"`, ctx.GetConfig().Package)
- imports = append(imports, configImport, pbImport, remoteImport, svcImport)
- text, err := pathx.LoadTemplate(category, mainTemplateFile, mainTemplate)
- if err != nil {
- return err
- }
- etcFileName, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
- if err != nil {
- return err
- }
- return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
- "serviceName": etcFileName,
- "imports": strings.Join(imports, pathx.NL),
- "pkg": proto.PbPackage,
- "serviceNew": stringx.From(proto.Service.Name).ToCamel(),
- "service": parser.CamelCase(proto.Service.Name),
- }, fileName, false)
- }
|