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.

146 lines
4.8 KiB

  1. package service
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model"
  5. "gin-vue-admin/model/request"
  6. "gin-vue-admin/utils"
  7. "io/ioutil"
  8. "os"
  9. "strings"
  10. "text/template"
  11. )
  12. type tplData struct {
  13. template *template.Template
  14. locationPath string
  15. autoCodePath string
  16. }
  17. // @title CreateTemp
  18. // @description 函数的详细描述
  19. // @auth (2020/04/05 20:22)
  20. // @param autoCode model.AutoCodeStruct
  21. // @return err error
  22. func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
  23. basePath := "resource/template"
  24. // 获取 basePath 文件夹下所有tpl文件
  25. tplFileList, err := GetAllTplFile(basePath, nil)
  26. if err != nil {
  27. return err
  28. }
  29. dataList := make([]tplData, 0, len(tplFileList))
  30. fileList := make([]string, 0, len(tplFileList))
  31. needMkdir := make([]string, 0, len(tplFileList)) // 当文件夹下存在多个tpl文件时,改为map更合理
  32. // 根据文件路径生成 tplData 结构体,待填充数据
  33. for _, value := range tplFileList {
  34. dataList = append(dataList, tplData{locationPath: value})
  35. }
  36. // 生成 *Template, 填充 template 字段
  37. for index, value := range dataList {
  38. dataList[index].template, err = template.ParseFiles(value.locationPath)
  39. if err != nil {
  40. return err
  41. }
  42. }
  43. // 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理
  44. // resource/template/web/api.js.tpl -> autoCode/web/autoCode.PackageName/api/autoCode.PackageName.js
  45. // resource/template/readme.txt.tpl -> autoCode/readme.txt
  46. autoPath := "autoCode/"
  47. for index, value := range dataList {
  48. trimBase := strings.TrimPrefix(value.locationPath, basePath+"/")
  49. if trimBase == "readme.txt.tpl" {
  50. dataList[index].autoCodePath = autoPath + "readme.txt"
  51. continue
  52. }
  53. if lastSeparator := strings.LastIndex(trimBase, "/"); lastSeparator != -1 {
  54. origFileName := strings.TrimSuffix(trimBase[lastSeparator+1:], ".tpl")
  55. firstDot := strings.Index(origFileName, ".")
  56. if firstDot != -1 {
  57. dataList[index].autoCodePath = autoPath + trimBase[:lastSeparator] + "/" + autoCode.PackageName + "/" +
  58. origFileName[:firstDot] + "/" + autoCode.PackageName + origFileName[firstDot:]
  59. }
  60. }
  61. if lastSeparator := strings.LastIndex(dataList[index].autoCodePath, "/"); lastSeparator != -1 {
  62. needMkdir = append(needMkdir, dataList[index].autoCodePath[:lastSeparator])
  63. }
  64. }
  65. // 写入文件前,先创建文件夹
  66. if err = utils.CreateDir(needMkdir...); err != nil {
  67. return err
  68. }
  69. // 生成文件
  70. for _, value := range dataList {
  71. fileList = append(fileList, value.autoCodePath)
  72. f, err := os.OpenFile(value.autoCodePath, os.O_CREATE|os.O_WRONLY, 0755)
  73. if err != nil {
  74. return err
  75. }
  76. if err = value.template.Execute(f, autoCode); err != nil {
  77. return err
  78. }
  79. _ = f.Close()
  80. }
  81. defer func() {
  82. // 移除中间文件
  83. if err := os.RemoveAll(autoPath); err != nil {
  84. return
  85. }
  86. }()
  87. if autoCode.AutoMoveFile {
  88. // 判断是否需要自动转移
  89. for _, value := range dataList {
  90. // 转移
  91. err := utils.FileMove(value.locationPath, value.autoCodePath)
  92. if err != nil {
  93. return err
  94. }
  95. }
  96. } else {
  97. // 打包
  98. if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil {
  99. return err
  100. }
  101. }
  102. return nil
  103. }
  104. // GetAllTplFile 用来获取 pathName 文件夹下所有 tpl 文件
  105. func GetAllTplFile(pathName string, fileList []string) ([]string, error) {
  106. files, err := ioutil.ReadDir(pathName)
  107. for _, fi := range files {
  108. if fi.IsDir() {
  109. fileList, err = GetAllTplFile(pathName+"/"+fi.Name(), fileList)
  110. if err != nil {
  111. return nil, err
  112. }
  113. } else {
  114. if strings.HasSuffix(fi.Name(), ".tpl") {
  115. fileList = append(fileList, pathName+"/"+fi.Name())
  116. }
  117. }
  118. }
  119. return fileList, err
  120. }
  121. func GetTables(dbName string) (err error, TableNames []request.TableReq) {
  122. err = global.GVA_DB.Raw("select table_name as table_name from information_schema.tables where table_schema = ?", dbName).Scan(&TableNames).Error
  123. return err, TableNames
  124. }
  125. func GetDB() (err error, DBNames []request.DBReq) {
  126. err = global.GVA_DB.Raw("SELECT SCHEMA_NAME AS `database` FROM INFORMATION_SCHEMA.SCHEMATA;").Scan(&DBNames).Error
  127. return err, DBNames
  128. }
  129. func GetColumn(tableName string, dbName string) (err error, Columns []request.ColumnReq) {
  130. err = global.GVA_DB.Raw("SELECT COLUMN_NAME column_name,DATA_TYPE data_type,CASE DATA_TYPE WHEN 'longtext' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'varchar' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'double' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'decimal' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'int' THEN c.NUMERIC_PRECISION WHEN 'bigint' THEN c.NUMERIC_PRECISION ELSE '' END AS data_type_long,COLUMN_COMMENT column_comment FROM INFORMATION_SCHEMA.COLUMNS c WHERE table_name = ? AND table_schema = ?", tableName, dbName).Scan(&Columns).Error
  131. return err, Columns
  132. }