gensvc.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package generator
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "path/filepath"
  6. conf "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/config"
  7. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/rpc/parser"
  8. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util"
  9. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/format"
  10. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  11. )
  12. //go:embed svc.tpl
  13. var svcTemplate string
  14. // GenSvc generates the servicecontext.go file, which is the resource dependency of a service,
  15. // such as rpc dependency, model dependency, etc.
  16. func (g *Generator) GenSvc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
  17. dir := ctx.GetSvc()
  18. svcFilename, err := format.FileNamingFormat(cfg.NamingFormat, "service_context")
  19. if err != nil {
  20. return err
  21. }
  22. fileName := filepath.Join(dir.Filename, svcFilename+".go")
  23. text, err := pathx.LoadTemplate(category, svcTemplateFile, svcTemplate)
  24. if err != nil {
  25. return err
  26. }
  27. return util.With("svc").GoFmt(true).Parse(text).SaveTo(map[string]any{
  28. "imports": fmt.Sprintf(`"%v"`, ctx.GetConfig().Package),
  29. }, fileName, false)
  30. }