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.

158 lines
3.9 KiB

  1. package model
  2. import (
  3. "gin-vue-admin/utils"
  4. "html/template"
  5. "os"
  6. )
  7. // 初始版本自动化代码工具
  8. type AutoCodeStruct struct {
  9. StructName string `json:"structName"`
  10. PackageName string `json:"packageName"`
  11. Abbreviation string `json:"abbreviation"`
  12. Fields []Field `json:"fields"`
  13. }
  14. type Field struct {
  15. FieldName string `json:"fieldName"`
  16. FieldType string `json:"fieldType"`
  17. FieldJson string `json:"fieldJson"`
  18. ColumnName string `json:"columnName"`
  19. }
  20. // @title CreateTemp
  21. // @description 函数的详细描述
  22. // @auth (2020/04/05 20:22 )
  23. // @return err error
  24. func (a *AutoCodeStruct) CreateTemp() (err error) {
  25. basePath := "./resource/template"
  26. modelTmpl, err := template.ParseFiles(basePath + "/te/model.go.tpl")
  27. if err != nil {
  28. return err
  29. }
  30. apiTmpl, err := template.ParseFiles(basePath + "/te/api.go.tpl")
  31. if err != nil {
  32. return err
  33. }
  34. routerTmpl, err := template.ParseFiles(basePath + "/te/router.go.tpl")
  35. if err != nil {
  36. return err
  37. }
  38. feapiTmpl, err := template.ParseFiles(basePath + "/fe/api.js.tpl")
  39. if err != nil {
  40. return err
  41. }
  42. readmeTmpl, err := template.ParseFiles(basePath + "/readme.txt.tpl")
  43. if err != nil {
  44. return err
  45. }
  46. //自动化总目录
  47. _autoCode := "./autoCode/"
  48. //自动化后台代码目录
  49. _te := "./autoCode/te/"
  50. _dir := _te + a.PackageName
  51. _modeldir := _te + a.PackageName + "/model"
  52. _apidir := _te + a.PackageName + "/api"
  53. _routerdir := _te + a.PackageName + "/router"
  54. //自动化前台代码目录
  55. _fe := "./autoCode/fe/"
  56. _fe_dir := _fe + a.PackageName
  57. _fe_apidir := _fe + a.PackageName + "/api"
  58. err = createDir(_autoCode, _te, _dir, _modeldir, _apidir, _routerdir, _fe, _fe_dir, _fe_apidir)
  59. if err != nil {
  60. return err
  61. }
  62. model, err := os.OpenFile(_te+a.PackageName+"/model/model.go", os.O_CREATE|os.O_WRONLY, 0755)
  63. if err != nil {
  64. return err
  65. }
  66. api, err := os.OpenFile(_te+a.PackageName+"/api/api.go", os.O_CREATE|os.O_WRONLY, 0755)
  67. if err != nil {
  68. return err
  69. }
  70. router, err := os.OpenFile(_te+a.PackageName+"/router/router.go", os.O_CREATE|os.O_WRONLY, 0755)
  71. if err != nil {
  72. return err
  73. }
  74. feapi, err := os.OpenFile(_fe+a.PackageName+"/api/api.js", os.O_CREATE|os.O_WRONLY, 0755)
  75. if err != nil {
  76. return err
  77. }
  78. readme, err := os.OpenFile(_autoCode+"readme.txt", os.O_CREATE|os.O_WRONLY, 0755)
  79. if err != nil {
  80. return err
  81. }
  82. // 生成代码
  83. {
  84. err = modelTmpl.Execute(model, a)
  85. if err != nil {
  86. return err
  87. }
  88. err = apiTmpl.Execute(api, a)
  89. if err != nil {
  90. return err
  91. }
  92. err = routerTmpl.Execute(router, a)
  93. if err != nil {
  94. return err
  95. }
  96. err = feapiTmpl.Execute(feapi, a)
  97. if err != nil {
  98. return err
  99. }
  100. err = readmeTmpl.Execute(readme, a)
  101. if err != nil {
  102. return err
  103. }
  104. }
  105. _ = model.Close()
  106. _ = api.Close()
  107. _ = router.Close()
  108. _ = feapi.Close()
  109. _ = readme.Close()
  110. fileList := []string{
  111. _te + a.PackageName + "/model/model.go",
  112. _te + a.PackageName + "/api/api.go",
  113. _te + a.PackageName + "/router/router.go",
  114. _fe + a.PackageName + "/api/api.js",
  115. _autoCode + "readme.txt",
  116. }
  117. err = utils.ZipFiles("./ginvueadmin.zip", fileList, ".", ".")
  118. if err != nil {
  119. return err
  120. }
  121. err = os.RemoveAll(_autoCode)
  122. if err != nil {
  123. return err
  124. }
  125. return nil
  126. }
  127. // @title createDir
  128. // @description 批量创建文件夹
  129. // @auth (2020/04/05 20:22 )
  130. // @param dirs string
  131. // @return err error
  132. func createDir(dirs ...string) (err error) {
  133. for _, v := range dirs {
  134. exist, err := utils.PathExists(v)
  135. if err != nil {
  136. //log.L.Info(fmt.Sprintf("get dir error![%v]\n", err))
  137. return err
  138. }
  139. if exist {
  140. //log.L.Info(fmt.Sprintf("has dir![%v]\n"+_dir))
  141. } else {
  142. //log.L.Info(fmt.Sprintf("no dir![%v]\n"+_dir))
  143. // 创建文件夹
  144. err = os.Mkdir(v, os.ModePerm)
  145. if err != nil {
  146. //log.L.Error(fmt.Sprintf("mkdir error![%v]\n",err))
  147. } else {
  148. //log.L.Info("mkdir success!\n")
  149. }
  150. }
  151. }
  152. return err
  153. }