template.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package gogen
  2. import (
  3. "fmt"
  4. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  5. )
  6. const (
  7. category = "api"
  8. configTemplateFile = "config.tpl"
  9. contextTemplateFile = "context.tpl"
  10. etcTemplateFile = "etc.tpl"
  11. handlerTemplateFile = "handler.tpl"
  12. logicTemplateFile = "logic.tpl"
  13. mainTemplateFile = "main.tpl"
  14. middlewareImplementCodeFile = "middleware.tpl"
  15. routesTemplateFile = "routes.tpl"
  16. routesAdditionTemplateFile = "route-addition.tpl"
  17. typesTemplateFile = "types.tpl"
  18. )
  19. var templates = map[string]string{
  20. configTemplateFile: configTemplate,
  21. contextTemplateFile: contextTemplate,
  22. etcTemplateFile: etcTemplate,
  23. handlerTemplateFile: handlerTemplate,
  24. logicTemplateFile: logicTemplate,
  25. mainTemplateFile: mainTemplate,
  26. middlewareImplementCodeFile: middlewareImplementCode,
  27. routesTemplateFile: routesTemplate,
  28. routesAdditionTemplateFile: routesAdditionTemplate,
  29. typesTemplateFile: typesTemplate,
  30. }
  31. // Category returns the category of the api files.
  32. func Category() string {
  33. return category
  34. }
  35. // Clean cleans the generated deployment files.
  36. func Clean() error {
  37. return pathx.Clean(category)
  38. }
  39. // GenTemplates generates api template files.
  40. func GenTemplates() error {
  41. return pathx.InitTemplates(category, templates)
  42. }
  43. // RevertTemplate reverts the given template file to the default value.
  44. func RevertTemplate(name string) error {
  45. content, ok := templates[name]
  46. if !ok {
  47. return fmt.Errorf("%s: no such file name", name)
  48. }
  49. return pathx.CreateTemplate(category, name, content)
  50. }
  51. // Update updates the template files to the templates built in current goctl.
  52. func Update() error {
  53. err := Clean()
  54. if err != nil {
  55. return err
  56. }
  57. return pathx.InitTemplates(category, templates)
  58. }