genctrl_generate_ctrl_clear.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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/utility/mlog"
  10. "github.com/gogf/gf/v2/os/gfile"
  11. "github.com/gogf/gf/v2/text/gregex"
  12. "github.com/gogf/gf/v2/text/gstr"
  13. )
  14. type controllerClearer struct{}
  15. func newControllerClearer() *controllerClearer {
  16. return &controllerClearer{}
  17. }
  18. func (c *controllerClearer) Clear(dstModuleFolderPath string, extraApiItemsInCtrl []apiItem) (err error) {
  19. for _, item := range extraApiItemsInCtrl {
  20. if err = c.doClear(dstModuleFolderPath, item); err != nil {
  21. return err
  22. }
  23. }
  24. return
  25. }
  26. func (c *controllerClearer) doClear(dstModuleFolderPath string, item apiItem) (err error) {
  27. var (
  28. methodNameSnake = gstr.CaseSnake(item.MethodName)
  29. methodFilePath = gfile.Join(dstModuleFolderPath, fmt.Sprintf(
  30. `%s_%s_%s.go`, item.Module, item.Version, methodNameSnake,
  31. ))
  32. fileContent = gstr.Trim(gfile.GetContents(methodFilePath))
  33. )
  34. match, err := gregex.MatchString(`.+?Req.+?Res.+?{([\s\S]+?)}`, fileContent)
  35. if err != nil {
  36. return err
  37. }
  38. if len(match) > 1 {
  39. implements := gstr.Trim(match[1])
  40. // One line.
  41. if !gstr.Contains(implements, "\n") && gstr.Contains(implements, `CodeNotImplemented`) {
  42. mlog.Printf(
  43. `remove unimplemented and of no api definitions controller file: %s`,
  44. methodFilePath,
  45. )
  46. err = gfile.Remove(methodFilePath)
  47. }
  48. }
  49. return
  50. }