genctrl_calculate.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
  9. "github.com/gogf/gf/v2/os/gfile"
  10. "github.com/gogf/gf/v2/text/gregex"
  11. "github.com/gogf/gf/v2/text/gstr"
  12. )
  13. func (c CGenCtrl) getApiItemsInSrc(apiModuleFolderPath string) (items []apiItem, err error) {
  14. var (
  15. fileContent string
  16. importPath string
  17. )
  18. // The second level folders: versions.
  19. apiVersionFolderPaths, err := gfile.ScanDir(apiModuleFolderPath, "*", false)
  20. if err != nil {
  21. return nil, err
  22. }
  23. for _, apiVersionFolderPath := range apiVersionFolderPaths {
  24. if !gfile.IsDir(apiVersionFolderPath) {
  25. continue
  26. }
  27. // The second level folders: versions.
  28. apiFileFolderPaths, err := gfile.ScanDir(apiVersionFolderPath, "*.go", false)
  29. if err != nil {
  30. return nil, err
  31. }
  32. importPath = utils.GetImportPath(apiVersionFolderPath)
  33. for _, apiFileFolderPath := range apiFileFolderPaths {
  34. if gfile.IsDir(apiFileFolderPath) {
  35. continue
  36. }
  37. fileContent = gfile.GetContents(apiFileFolderPath)
  38. matches, err := gregex.MatchAllString(PatternApiDefinition, fileContent)
  39. if err != nil {
  40. return nil, err
  41. }
  42. for _, match := range matches {
  43. item := apiItem{
  44. Import: gstr.Trim(importPath, `"`),
  45. Module: gfile.Basename(apiModuleFolderPath),
  46. Version: gfile.Basename(apiVersionFolderPath),
  47. MethodName: match[1],
  48. }
  49. items = append(items, item)
  50. }
  51. }
  52. }
  53. return
  54. }
  55. func (c CGenCtrl) getApiItemsInDst(dstFolder string) (items []apiItem, err error) {
  56. if !gfile.Exists(dstFolder) {
  57. return nil, nil
  58. }
  59. type importItem struct {
  60. Path string
  61. Alias string
  62. }
  63. var fileContent string
  64. filePaths, err := gfile.ScanDir(dstFolder, "*.go", true)
  65. if err != nil {
  66. return nil, err
  67. }
  68. for _, filePath := range filePaths {
  69. fileContent = gfile.GetContents(filePath)
  70. match, err := gregex.MatchString(`import\s+\(([\s\S]+?)\)`, fileContent)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if len(match) < 2 {
  75. continue
  76. }
  77. var (
  78. array []string
  79. importItems []importItem
  80. importLines = gstr.SplitAndTrim(match[1], "\n")
  81. module = gfile.Basename(gfile.Dir(filePath))
  82. )
  83. // retrieve all imports.
  84. for _, importLine := range importLines {
  85. array = gstr.SplitAndTrim(importLine, " ")
  86. if len(array) == 2 {
  87. importItems = append(importItems, importItem{
  88. Path: gstr.Trim(array[1], `"`),
  89. Alias: array[0],
  90. })
  91. } else {
  92. importItems = append(importItems, importItem{
  93. Path: gstr.Trim(array[0], `"`),
  94. })
  95. }
  96. }
  97. // retrieve all api usages.
  98. matches, err := gregex.MatchAllString(PatternCtrlDefinition, fileContent)
  99. if err != nil {
  100. return nil, err
  101. }
  102. for _, match = range matches {
  103. // try to find the import path of the api.
  104. var (
  105. importPath string
  106. version = match[1]
  107. methodName = match[2] // not the function name, but the method name in api definition.
  108. )
  109. for _, item := range importItems {
  110. if item.Alias != "" {
  111. if item.Alias == version {
  112. importPath = item.Path
  113. break
  114. }
  115. continue
  116. }
  117. if gfile.Basename(item.Path) == version {
  118. importPath = item.Path
  119. break
  120. }
  121. }
  122. item := apiItem{
  123. Import: gstr.Trim(importPath, `"`),
  124. Module: module,
  125. Version: gfile.Basename(importPath),
  126. MethodName: methodName,
  127. }
  128. items = append(items, item)
  129. }
  130. }
  131. return
  132. }