template.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package generator
  2. import (
  3. "fmt"
  4. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  5. )
  6. const (
  7. category = "rpc"
  8. callTemplateFile = "call.tpl"
  9. callInterfaceFunctionTemplateFile = "call-interface-func.tpl"
  10. callFunctionTemplateFile = "call-func.tpl"
  11. configTemplateFileFile = "config.tpl"
  12. etcTemplateFileFile = "etc.tpl"
  13. logicTemplateFileFile = "logic.tpl"
  14. logicFuncTemplateFileFile = "logic-func.tpl"
  15. mainTemplateFile = "main.tpl"
  16. serverTemplateFile = "server.tpl"
  17. serverFuncTemplateFile = "server-func.tpl"
  18. svcTemplateFile = "svc.tpl"
  19. rpcTemplateFile = "template.tpl"
  20. )
  21. var templates = map[string]string{
  22. callTemplateFile: callTemplateText,
  23. callInterfaceFunctionTemplateFile: callInterfaceFunctionTemplate,
  24. callFunctionTemplateFile: callFunctionTemplate,
  25. configTemplateFileFile: configTemplate,
  26. etcTemplateFileFile: etcTemplate,
  27. logicTemplateFileFile: logicTemplate,
  28. logicFuncTemplateFileFile: logicFunctionTemplate,
  29. mainTemplateFile: mainTemplate,
  30. serverTemplateFile: serverTemplate,
  31. serverFuncTemplateFile: functionTemplate,
  32. svcTemplateFile: svcTemplate,
  33. rpcTemplateFile: rpcTemplateText,
  34. }
  35. // GenTemplates is the entry for command goctl template,
  36. // it will create the specified category
  37. func GenTemplates() error {
  38. return pathx.InitTemplates(category, templates)
  39. }
  40. // RevertTemplate restores the deleted template files
  41. func RevertTemplate(name string) error {
  42. content, ok := templates[name]
  43. if !ok {
  44. return fmt.Errorf("%s: no such file name", name)
  45. }
  46. return pathx.CreateTemplate(category, name, content)
  47. }
  48. // Clean deletes all template files
  49. func Clean() error {
  50. return pathx.Clean(category)
  51. }
  52. // Update is used to update the template files, it will delete the existing old templates at first,
  53. // and then create the latest template files
  54. func Update() error {
  55. err := Clean()
  56. if err != nil {
  57. return err
  58. }
  59. return pathx.InitTemplates(category, templates)
  60. }
  61. // Category returns a const string value for rpc template category
  62. func Category() string {
  63. return category
  64. }