gen.go 3.7 KB

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