genctrl_generate_ctrl.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package genctrl
  7. import (
  8. "fmt"
  9. "github.com/gogf/gf/cmd/gf/v2/internal/consts"
  10. "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
  11. "github.com/gogf/gf/v2/container/gset"
  12. "github.com/gogf/gf/v2/frame/g"
  13. "github.com/gogf/gf/v2/os/gfile"
  14. "github.com/gogf/gf/v2/text/gstr"
  15. )
  16. type controllerGenerator struct{}
  17. func newControllerGenerator() *controllerGenerator {
  18. return &controllerGenerator{}
  19. }
  20. func (c *controllerGenerator) Generate(dstModuleFolderPath string, apiModuleApiItems []apiItem) (err error) {
  21. var (
  22. doneApiItemSet = gset.NewStrSet()
  23. )
  24. for _, item := range apiModuleApiItems {
  25. if doneApiItemSet.Contains(item.String()) {
  26. continue
  27. }
  28. // retrieve all api items of the same module.
  29. subItems := c.getSubItemsByModuleAndVersion(apiModuleApiItems, item.Module, item.Version)
  30. if err = c.doGenerateCtrlNewByModuleAndVersion(
  31. dstModuleFolderPath, item.Module, item.Version, gfile.Dir(item.Import),
  32. ); err != nil {
  33. return
  34. }
  35. for _, subItem := range subItems {
  36. if err = c.doGenerateCtrlItem(dstModuleFolderPath, subItem); err != nil {
  37. return
  38. }
  39. doneApiItemSet.Add(subItem.String())
  40. }
  41. }
  42. return
  43. }
  44. func (c *controllerGenerator) getSubItemsByModuleAndVersion(items []apiItem, module, version string) (subItems []apiItem) {
  45. for _, item := range items {
  46. if item.Module == module && item.Version == version {
  47. subItems = append(subItems, item)
  48. }
  49. }
  50. return
  51. }
  52. func (c *controllerGenerator) doGenerateCtrlNewByModuleAndVersion(
  53. dstModuleFolderPath, module, version, importPath string,
  54. ) (err error) {
  55. var (
  56. moduleFilePath = gfile.Join(dstModuleFolderPath, module+".go")
  57. moduleFilePathNew = gfile.Join(dstModuleFolderPath, module+"_new.go")
  58. ctrlName = fmt.Sprintf(`Controller%s`, gstr.UcFirst(version))
  59. interfaceName = fmt.Sprintf(`%s.I%s%s`, module, gstr.CaseCamel(module), gstr.UcFirst(version))
  60. newFuncName = fmt.Sprintf(`New%s`, gstr.UcFirst(version))
  61. newFuncNameDefinition = fmt.Sprintf(`func %s()`, newFuncName)
  62. alreadyCreated bool
  63. )
  64. if !gfile.Exists(moduleFilePath) {
  65. content := gstr.ReplaceByMap(consts.TemplateGenCtrlControllerEmpty, g.MapStrStr{
  66. "{Module}": module,
  67. })
  68. if err = gfile.PutContents(moduleFilePath, gstr.TrimLeft(content)); err != nil {
  69. return err
  70. }
  71. mlog.Printf(`generated: %s`, moduleFilePath)
  72. }
  73. if !gfile.Exists(moduleFilePathNew) {
  74. content := gstr.ReplaceByMap(consts.TemplateGenCtrlControllerNewEmpty, g.MapStrStr{
  75. "{Module}": module,
  76. "{ImportPath}": fmt.Sprintf(`"%s"`, importPath),
  77. })
  78. if err = gfile.PutContents(moduleFilePathNew, gstr.TrimLeft(content)); err != nil {
  79. return err
  80. }
  81. mlog.Printf(`generated: %s`, moduleFilePathNew)
  82. }
  83. filePaths, err := gfile.ScanDir(dstModuleFolderPath, "*.go", false)
  84. if err != nil {
  85. return err
  86. }
  87. for _, filePath := range filePaths {
  88. if gstr.Contains(gfile.GetContents(filePath), newFuncNameDefinition) {
  89. alreadyCreated = true
  90. break
  91. }
  92. }
  93. if !alreadyCreated {
  94. content := gstr.ReplaceByMap(consts.TemplateGenCtrlControllerNewFunc, g.MapStrStr{
  95. "{CtrlName}": ctrlName,
  96. "{NewFuncName}": newFuncName,
  97. "{InterfaceName}": interfaceName,
  98. })
  99. err = gfile.PutContentsAppend(moduleFilePathNew, gstr.TrimLeft(content))
  100. if err != nil {
  101. return err
  102. }
  103. }
  104. return
  105. }
  106. func (c *controllerGenerator) doGenerateCtrlItem(dstModuleFolderPath string, item apiItem) (err error) {
  107. var (
  108. methodNameSnake = gstr.CaseSnake(item.MethodName)
  109. ctrlName = fmt.Sprintf(`Controller%s`, gstr.UcFirst(item.Version))
  110. methodFilePath = gfile.Join(dstModuleFolderPath, fmt.Sprintf(
  111. `%s_%s_%s.go`, item.Module, item.Version, methodNameSnake,
  112. ))
  113. )
  114. content := gstr.ReplaceByMap(consts.TemplateGenCtrlControllerMethodFunc, g.MapStrStr{
  115. "{Module}": item.Module,
  116. "{ImportPath}": item.Import,
  117. "{CtrlName}": ctrlName,
  118. "{Version}": item.Version,
  119. "{MethodName}": item.MethodName,
  120. })
  121. if err = gfile.PutContents(methodFilePath, gstr.TrimLeft(content)); err != nil {
  122. return err
  123. }
  124. mlog.Printf(`generated: %s`, methodFilePath)
  125. return
  126. }