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.

134 lines
4.6 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/fe/api.js.tpl -> autoCode/fe/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. // 生成压缩包
  82. if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil {
  83. return err
  84. }
  85. // 移除中间文件
  86. if err := os.RemoveAll(autoPath); err != nil {
  87. return err
  88. }
  89. return nil
  90. }
  91. // GetAllTplFile 用来获取 pathName 文件夹下所有 tpl 文件
  92. func GetAllTplFile(pathName string, fileList []string) ([]string, error) {
  93. files, err := ioutil.ReadDir(pathName)
  94. for _, fi := range files {
  95. if fi.IsDir() {
  96. fileList, err = GetAllTplFile(pathName+"/"+fi.Name(), fileList)
  97. if err != nil {
  98. return nil, err
  99. }
  100. } else {
  101. if strings.HasSuffix(fi.Name(), ".tpl") {
  102. fileList = append(fileList, pathName+"/"+fi.Name())
  103. }
  104. }
  105. }
  106. return fileList, err
  107. }
  108. func GetTables(dbName string) (err error, TableNames []request.TableReq) {
  109. err = global.GVA_DB.Raw("select table_name as table_name from information_schema.tables where table_schema = ?", dbName).Scan(&TableNames).Error
  110. return err, TableNames
  111. }
  112. func GetDB() (err error, DBNames []request.DBReq) {
  113. err = global.GVA_DB.Raw("SELECT SCHEMA_NAME AS `database` FROM INFORMATION_SCHEMA.SCHEMATA;").Scan(&DBNames).Error
  114. return err, DBNames
  115. }
  116. func GetColume(tableName string, dbName string) (err error, Columes []request.ColumeReq) {
  117. err = global.GVA_DB.Raw("SELECT COLUMN_NAME colume_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 colume_comment FROM INFORMATION_SCHEMA.COLUMNS c WHERE table_name = ? AND table_schema = ?", tableName, dbName).Scan(&Columes).Error
  118. return err, Columes
  119. }