cmd_fix.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 cmd
  7. import (
  8. "context"
  9. "github.com/gogf/gf/v2/os/gproc"
  10. "github.com/gogf/gf/v2/text/gregex"
  11. "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
  12. "github.com/gogf/gf/v2/errors/gerror"
  13. "github.com/gogf/gf/v2/frame/g"
  14. "github.com/gogf/gf/v2/os/gfile"
  15. "github.com/gogf/gf/v2/text/gstr"
  16. )
  17. var (
  18. Fix = cFix{}
  19. )
  20. type cFix struct {
  21. g.Meta `name:"fix" brief:"auto fixing codes after upgrading to new GoFrame version" usage:"gf fix" `
  22. }
  23. type cFixInput struct {
  24. g.Meta `name:"fix"`
  25. Path string `name:"path" short:"p" brief:"directory path, it uses current working directory in default"`
  26. Version string `name:"version" short:"v" brief:"custom specified version to fix, leave it empty to auto detect"`
  27. }
  28. type cFixOutput struct{}
  29. type cFixItem struct {
  30. Version string
  31. Func func(version string) error
  32. }
  33. func (c cFix) Index(ctx context.Context, in cFixInput) (out *cFixOutput, err error) {
  34. if in.Path == "" {
  35. in.Path = gfile.Pwd()
  36. }
  37. if in.Version == "" {
  38. in.Version, err = c.autoDetectVersion(in)
  39. if err != nil {
  40. mlog.Fatal(err)
  41. }
  42. if in.Version == "" {
  43. mlog.Print(`no GoFrame usage found, exit fixing`)
  44. return
  45. }
  46. mlog.Debugf(`current GoFrame version auto detect "%s"`, in.Version)
  47. }
  48. if !gproc.IsChild() {
  49. mlog.Printf(`start auto fixing directory path "%s"...`, in.Path)
  50. defer mlog.Print(`done!`)
  51. }
  52. err = c.doFix(in)
  53. return
  54. }
  55. func (c cFix) doFix(in cFixInput) (err error) {
  56. var items = []cFixItem{
  57. {Version: "v2.3", Func: c.doFixV23},
  58. {Version: "v2.5", Func: c.doFixV25},
  59. }
  60. for _, item := range items {
  61. if gstr.CompareVersionGo(in.Version, item.Version) < 0 {
  62. mlog.Debugf(
  63. `current GoFrame or contrib package version "%s" is lesser than "%s", nothing to do`,
  64. in.Version, item.Version,
  65. )
  66. continue
  67. }
  68. if err = item.Func(in.Version); err != nil {
  69. return
  70. }
  71. }
  72. return
  73. }
  74. // doFixV23 fixes code when upgrading to GoFrame v2.3.
  75. func (c cFix) doFixV23(version string) error {
  76. replaceFunc := func(path, content string) string {
  77. // gdb.TX from struct to interface.
  78. content = gstr.Replace(content, "*gdb.TX", "gdb.TX")
  79. // function name changes for package gtcp/gudp.
  80. if gstr.Contains(content, "/gf/v2/net/gtcp") || gstr.Contains(content, "/gf/v2/net/gudp") {
  81. content = gstr.ReplaceByMap(content, g.MapStrStr{
  82. ".SetSendDeadline": ".SetDeadlineSend",
  83. ".SetReceiveDeadline": ".SetDeadlineRecv",
  84. ".SetReceiveBufferWait": ".SetBufferWaitRecv",
  85. })
  86. }
  87. return content
  88. }
  89. return gfile.ReplaceDirFunc(replaceFunc, ".", "*.go", true)
  90. }
  91. // doFixV25 fixes code when upgrading to GoFrame v2.5.
  92. func (c cFix) doFixV25(version string) (err error) {
  93. replaceFunc := func(path, content string) string {
  94. content, err = c.doFixV25Content(content)
  95. return content
  96. }
  97. return gfile.ReplaceDirFunc(replaceFunc, ".", "*.go", true)
  98. }
  99. func (c cFix) doFixV25Content(content string) (newContent string, err error) {
  100. newContent = content
  101. if gstr.Contains(content, `.BindHookHandlerByMap(`) {
  102. var pattern = `\.BindHookHandlerByMap\((.+?), map\[string\]ghttp\.HandlerFunc`
  103. newContent, err = gregex.ReplaceString(
  104. pattern,
  105. `.BindHookHandlerByMap($1, map[ghttp.HookName]ghttp.HandlerFunc`,
  106. content,
  107. )
  108. }
  109. return
  110. }
  111. func (c cFix) autoDetectVersion(in cFixInput) (string, error) {
  112. var (
  113. err error
  114. path = gfile.Join(in.Path, "go.mod")
  115. version string
  116. )
  117. if !gfile.Exists(path) {
  118. return "", gerror.Newf(`"%s" not found in current working directory`, path)
  119. }
  120. err = gfile.ReadLines(path, func(line string) error {
  121. array := gstr.SplitAndTrim(line, " ")
  122. if len(array) > 0 {
  123. if gstr.HasPrefix(array[0], gfPackage) {
  124. version = array[1]
  125. }
  126. }
  127. return nil
  128. })
  129. if err != nil {
  130. mlog.Fatal(err)
  131. }
  132. return version, nil
  133. }