You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.4 KiB

  1. package service
  2. import (
  3. "gin-vue-admin/model"
  4. "gin-vue-admin/utils"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. "text/template"
  9. )
  10. type tplData struct {
  11. template *template.Template
  12. locationPath string
  13. autoCodePath string
  14. }
  15. // @title CreateTemp
  16. // @description 函数的详细描述
  17. // @auth (2020/04/05 20:22)
  18. // @param autoCode model.AutoCodeStruct
  19. // @return err error
  20. func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
  21. basePath := "resource/template"
  22. // 获取 basePath 文件夹下所有tpl文件
  23. tplFileList, err := GetAllTplFile(basePath, nil)
  24. if err != nil {
  25. return err
  26. }
  27. dataList := make([]tplData, 0, len(tplFileList))
  28. fileList := make([]string, 0, len(tplFileList))
  29. needMkdir := make([]string, 0, len(tplFileList)) // 当文件夹下存在多个tpl文件时,改为map更合理
  30. // 根据文件路径生成 tplData 结构体,待填充数据
  31. for _, value := range tplFileList {
  32. dataList = append(dataList, tplData{locationPath: value})
  33. }
  34. // 生成 *Template, 填充 template 字段
  35. for index, value := range dataList {
  36. dataList[index].template, err = template.ParseFiles(value.locationPath)
  37. if err != nil {
  38. return err
  39. }
  40. }
  41. // 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理
  42. // resource/template/fe/api.js.tpl -> autoCode/fe/autoCode.PackageName/api/autoCode.PackageName.js
  43. // resource/template/readme.txt.tpl -> autoCode/readme.txt
  44. autoPath := "autoCode/"
  45. for index, value := range dataList {
  46. trimBase := strings.TrimPrefix(value.locationPath, basePath+"/")
  47. if trimBase == "readme.txt.tpl" {
  48. dataList[index].autoCodePath = autoPath + "readme.txt"
  49. continue
  50. }
  51. if lastSeparator := strings.LastIndex(trimBase, "/"); lastSeparator != -1 {
  52. origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl")
  53. firstDot := strings.Index(origFileName, ".")
  54. if firstDot != -1 {
  55. dataList[index].autoCodePath = autoPath + trimBase[:lastSeparator] + "/" + autoCode.PackageName + "/" +
  56. origFileName[:firstDot] + "/" + autoCode.PackageName + origFileName[firstDot:]
  57. }
  58. }
  59. if lastSeparator := strings.LastIndex(dataList[index].autoCodePath, "/"); lastSeparator != -1 {
  60. needMkdir = append(needMkdir, dataList[index].autoCodePath[:lastSeparator])
  61. }
  62. }
  63. // 写入文件前,先创建文件夹
  64. if err = utils.CreateDir(needMkdir...); err != nil {
  65. return err
  66. }
  67. // 生成文件
  68. for _, value := range dataList {
  69. fileList = append(fileList, value.autoCodePath)
  70. f, err := os.OpenFile(value.autoCodePath, os.O_CREATE|os.O_WRONLY, 0755)
  71. if err != nil {
  72. return err
  73. }
  74. if err = value.template.Execute(f, autoCode); err != nil {
  75. return err
  76. }
  77. _ = f.Close()
  78. }
  79. // 生成压缩包
  80. if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil {
  81. return err
  82. }
  83. // 移除中间文件
  84. if err := os.RemoveAll(autoPath); err != nil {
  85. return err
  86. }
  87. return nil
  88. }
  89. // GetAllTplFile 用来获取 pathName 文件夹下所有 tpl 文件
  90. func GetAllTplFile(pathName string, fileList []string) ([]string, error) {
  91. files, err := ioutil.ReadDir(pathName)
  92. for _, fi := range files {
  93. if fi.IsDir() {
  94. fileList, err = GetAllTplFile(pathName+"/"+fi.Name(), fileList)
  95. if err != nil {
  96. return nil, err
  97. }
  98. } else {
  99. if strings.HasSuffix(fi.Name(), ".tpl") {
  100. fileList = append(fileList, pathName+"/"+fi.Name())
  101. }
  102. }
  103. }
  104. return fileList, err
  105. }