prototmpl.go 893 B

123456789101112131415161718192021222324252627282930313233343536
  1. package generator
  2. import (
  3. _ "embed"
  4. "path/filepath"
  5. "strings"
  6. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util"
  7. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  8. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/stringx"
  9. )
  10. //go:embed rpc.tpl
  11. var rpcTemplateText string
  12. // ProtoTmpl returns a sample of a proto file
  13. func ProtoTmpl(out string) error {
  14. protoFilename := filepath.Base(out)
  15. serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
  16. text, err := pathx.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
  17. if err != nil {
  18. return err
  19. }
  20. dir := filepath.Dir(out)
  21. err = pathx.MkdirIfNotExist(dir)
  22. if err != nil {
  23. return err
  24. }
  25. err = util.With("t").Parse(text).SaveTo(map[string]string{
  26. "package": serviceName.Untitle(),
  27. "serviceName": serviceName.Title(),
  28. }, out, false)
  29. return err
  30. }