gen.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package gogen
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. "time"
  12. "github.com/gookit/color"
  13. "github.com/spf13/cobra"
  14. "github.com/wuntsong-org/go-zero-plus/core/logx"
  15. apiformat "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/format"
  16. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/parser"
  17. apiutil "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/util"
  18. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/config"
  19. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/pkg/golang"
  20. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util"
  21. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  22. )
  23. const tmpFile = "%s-%d"
  24. var (
  25. tmpDir = path.Join(os.TempDir(), "goctlwt")
  26. // VarStringDir describes the directory.
  27. VarStringDir string
  28. // VarStringAPI describes the API.
  29. VarStringAPI string
  30. // VarStringHome describes the go home.
  31. VarStringHome string
  32. // VarStringRemote describes the remote git repository.
  33. VarStringRemote string
  34. // VarStringBranch describes the branch.
  35. VarStringBranch string
  36. // VarStringStyle describes the style of output files.
  37. VarStringStyle string
  38. )
  39. // GoCommand gen go project files from command line
  40. func GoCommand(_ *cobra.Command, _ []string) error {
  41. apiFile := VarStringAPI
  42. dir := VarStringDir
  43. namingStyle := VarStringStyle
  44. home := VarStringHome
  45. remote := VarStringRemote
  46. branch := VarStringBranch
  47. if len(remote) > 0 {
  48. repo, _ := util.CloneIntoGitHome(remote, branch)
  49. if len(repo) > 0 {
  50. home = repo
  51. }
  52. }
  53. if len(home) > 0 {
  54. pathx.RegisterGoctlHome(home)
  55. }
  56. if len(apiFile) == 0 {
  57. return errors.New("missing -api")
  58. }
  59. if len(dir) == 0 {
  60. return errors.New("missing -dir")
  61. }
  62. return DoGenProject(apiFile, dir, namingStyle)
  63. }
  64. // DoGenProject gen go project files with api file
  65. func DoGenProject(apiFile, dir, style string) error {
  66. api, err := parser.Parse(apiFile)
  67. if err != nil {
  68. return err
  69. }
  70. if err := api.Validate(); err != nil {
  71. return err
  72. }
  73. cfg, err := config.NewConfig(style)
  74. if err != nil {
  75. return err
  76. }
  77. logx.Must(pathx.MkdirIfNotExist(dir))
  78. rootPkg, err := golang.GetParentPackage(dir)
  79. if err != nil {
  80. return err
  81. }
  82. logx.Must(genEtc(dir, cfg, api))
  83. logx.Must(genConfig(dir, cfg, api))
  84. logx.Must(genMain(dir, rootPkg, cfg, api))
  85. logx.Must(genServiceContext(dir, rootPkg, cfg, api))
  86. logx.Must(genTypes(dir, cfg, api))
  87. logx.Must(genRoutes(dir, rootPkg, cfg, api))
  88. logx.Must(genHandlers(dir, rootPkg, cfg, api))
  89. logx.Must(genLogic(dir, rootPkg, cfg, api))
  90. logx.Must(genMiddleware(dir, cfg, api))
  91. if err := backupAndSweep(apiFile); err != nil {
  92. return err
  93. }
  94. if err := apiformat.ApiFormatByPath(apiFile, false); err != nil {
  95. return err
  96. }
  97. fmt.Println(color.Green.Render("Done."))
  98. return nil
  99. }
  100. func backupAndSweep(apiFile string) error {
  101. var err error
  102. var wg sync.WaitGroup
  103. wg.Add(2)
  104. _ = os.MkdirAll(tmpDir, os.ModePerm)
  105. go func() {
  106. _, fileName := filepath.Split(apiFile)
  107. _, e := apiutil.Copy(apiFile, fmt.Sprintf(path.Join(tmpDir, tmpFile), fileName, time.Now().Unix()))
  108. if e != nil {
  109. err = e
  110. }
  111. wg.Done()
  112. }()
  113. go func() {
  114. if e := sweep(); e != nil {
  115. err = e
  116. }
  117. wg.Done()
  118. }()
  119. wg.Wait()
  120. return err
  121. }
  122. func sweep() error {
  123. keepTime := time.Now().AddDate(0, 0, -7)
  124. return filepath.Walk(tmpDir, func(fpath string, info os.FileInfo, err error) error {
  125. if info.IsDir() {
  126. return nil
  127. }
  128. pos := strings.LastIndexByte(info.Name(), '-')
  129. if pos > 0 {
  130. timestamp := info.Name()[pos+1:]
  131. seconds, err := strconv.ParseInt(timestamp, 10, 64)
  132. if err != nil {
  133. // print error and ignore
  134. fmt.Println(color.Red.Sprintf("sweep ignored file: %s", fpath))
  135. return nil
  136. }
  137. tm := time.Unix(seconds, 0)
  138. if tm.Before(keepTime) {
  139. if err := os.RemoveAll(fpath); err != nil {
  140. fmt.Println(color.Red.Sprintf("failed to remove file: %s", fpath))
  141. return err
  142. }
  143. }
  144. }
  145. return nil
  146. })
  147. }