utils.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 utils
  7. import (
  8. "context"
  9. "fmt"
  10. "golang.org/x/tools/imports"
  11. "github.com/gogf/gf/cmd/gf/v2/internal/consts"
  12. "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
  13. "github.com/gogf/gf/v2/os/gfile"
  14. "github.com/gogf/gf/v2/os/gproc"
  15. "github.com/gogf/gf/v2/text/gregex"
  16. "github.com/gogf/gf/v2/text/gstr"
  17. )
  18. // GoFmt formats the source file and adds or removes import statements as necessary.
  19. func GoFmt(path string) {
  20. replaceFunc := func(path, content string) string {
  21. res, err := imports.Process(path, []byte(content), nil)
  22. if err != nil {
  23. mlog.Printf(`error format "%s" go files: %v`, path, err)
  24. return content
  25. }
  26. return string(res)
  27. }
  28. var err error
  29. if gfile.IsFile(path) {
  30. // File format.
  31. if gfile.ExtName(path) != "go" {
  32. return
  33. }
  34. err = gfile.ReplaceFileFunc(replaceFunc, path)
  35. } else {
  36. // Folder format.
  37. err = gfile.ReplaceDirFunc(replaceFunc, path, "*.go", true)
  38. }
  39. if err != nil {
  40. mlog.Printf(`error format "%s" go files: %v`, path, err)
  41. }
  42. }
  43. // GoModTidy executes `go mod tidy` at specified directory `dirPath`.
  44. func GoModTidy(ctx context.Context, dirPath string) error {
  45. command := fmt.Sprintf(`cd %s && go mod tidy`, dirPath)
  46. err := gproc.ShellRun(ctx, command)
  47. return err
  48. }
  49. // IsFileDoNotEdit checks and returns whether file contains `do not edit` key.
  50. func IsFileDoNotEdit(filePath string) bool {
  51. if !gfile.Exists(filePath) {
  52. return true
  53. }
  54. return gstr.Contains(gfile.GetContents(filePath), consts.DoNotEditKey)
  55. }
  56. // ReplaceGeneratedContentGFV2 replaces generated go content from goframe v1 to v2.
  57. func ReplaceGeneratedContentGFV2(folderPath string) (err error) {
  58. return gfile.ReplaceDirFunc(func(path, content string) string {
  59. if gstr.Contains(content, `"github.com/gogf/gf`) && !gstr.Contains(content, `"github.com/gogf/gf/v2`) {
  60. content = gstr.Replace(content, `"github.com/gogf/gf"`, `"github.com/gogf/gf/v2"`)
  61. content = gstr.Replace(content, `"github.com/gogf/gf/`, `"github.com/gogf/gf/v2/`)
  62. content = gstr.Replace(content, `"github.com/gogf/gf/v2/contrib/`, `"github.com/gogf/gf/contrib/`)
  63. return content
  64. }
  65. return content
  66. }, folderPath, "*.go", true)
  67. }
  68. // GetImportPath calculates and returns the golang import path for given `filePath`.
  69. // Note that it needs a `go.mod` in current working directory or parent directories to detect the path.
  70. func GetImportPath(filePath string) string {
  71. // If `filePath` does not exist, create it firstly to find the import path.
  72. var realPath = gfile.RealPath(filePath)
  73. if realPath == "" {
  74. _ = gfile.Mkdir(filePath)
  75. realPath = gfile.RealPath(filePath)
  76. }
  77. var (
  78. newDir = gfile.Dir(realPath)
  79. oldDir string
  80. suffix string
  81. goModName = "go.mod"
  82. goModPath string
  83. importPath string
  84. )
  85. if gfile.IsDir(filePath) {
  86. suffix = gfile.Basename(filePath)
  87. }
  88. for {
  89. goModPath = gfile.Join(newDir, goModName)
  90. if gfile.Exists(goModPath) {
  91. match, _ := gregex.MatchString(`^module\s+(.+)\s*`, gfile.GetContents(goModPath))
  92. importPath = gstr.Trim(match[1]) + "/" + suffix
  93. importPath = gstr.Replace(importPath, `\`, `/`)
  94. importPath = gstr.TrimRight(importPath, `/`)
  95. return importPath
  96. }
  97. oldDir = newDir
  98. newDir = gfile.Dir(oldDir)
  99. if newDir == oldDir {
  100. return ""
  101. }
  102. suffix = gfile.Basename(oldDir) + "/" + suffix
  103. }
  104. }
  105. // GetModPath retrieves and returns the file path of go.mod for current project.
  106. func GetModPath() string {
  107. var (
  108. oldDir = gfile.Pwd()
  109. newDir = gfile.Dir(oldDir)
  110. goModName = "go.mod"
  111. goModPath string
  112. )
  113. for {
  114. goModPath = gfile.Join(newDir, goModName)
  115. if gfile.Exists(goModPath) {
  116. return goModPath
  117. }
  118. oldDir = newDir
  119. newDir = gfile.Dir(oldDir)
  120. if newDir == oldDir {
  121. break
  122. }
  123. }
  124. return ""
  125. }