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.

150 lines
3.6 KiB

  1. package autoCodeModel
  2. import (
  3. "gin-vue-admin/tools"
  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. func (a *AutoCodeStruct) CreateTemp() (err error) {
  21. basePath := "./tpl"
  22. modelTmpl, err := template.ParseFiles(basePath + "/te/model.go.tpl")
  23. if err != nil {
  24. return err
  25. }
  26. apiTmpl, err := template.ParseFiles(basePath + "/te/api.go.tpl")
  27. if err != nil {
  28. return err
  29. }
  30. routerTmpl, err := template.ParseFiles(basePath + "/te/router.go.tpl")
  31. if err != nil {
  32. return err
  33. }
  34. feapiTmpl, err := template.ParseFiles(basePath + "/fe/api.js.tpl")
  35. if err != nil {
  36. return err
  37. }
  38. readmeTmpl, err := template.ParseFiles(basePath + "/readme.txt.tpl")
  39. if err != nil {
  40. return err
  41. }
  42. //自动化总目录
  43. _autoCode := "./autoCode/"
  44. //自动化后台代码目录
  45. _te := "./autoCode/te/"
  46. _dir := _te + a.PackageName
  47. _modeldir := _te + a.PackageName + "/model"
  48. _apidir := _te + a.PackageName + "/api"
  49. _routerdir := _te + a.PackageName + "/router"
  50. //自动化前台代码目录
  51. _fe := "./autoCode/fe/"
  52. _fe_dir := _fe + a.PackageName
  53. _fe_apidir := _fe + a.PackageName + "/api"
  54. err = createDir(_autoCode, _te, _dir, _modeldir, _apidir, _routerdir, _fe, _fe_dir, _fe_apidir)
  55. if err != nil {
  56. return err
  57. }
  58. model, err := os.OpenFile(_te+a.PackageName+"/model/model.go", os.O_CREATE|os.O_WRONLY, 0755)
  59. if err != nil {
  60. return err
  61. }
  62. api, err := os.OpenFile(_te+a.PackageName+"/api/api.go", os.O_CREATE|os.O_WRONLY, 0755)
  63. if err != nil {
  64. return err
  65. }
  66. router, err := os.OpenFile(_te+a.PackageName+"/router/router.go", os.O_CREATE|os.O_WRONLY, 0755)
  67. if err != nil {
  68. return err
  69. }
  70. feapi, err := os.OpenFile(_fe+a.PackageName+"/api/api.js", os.O_CREATE|os.O_WRONLY, 0755)
  71. if err != nil {
  72. return err
  73. }
  74. readme, err := os.OpenFile(_autoCode+"readme.txt", os.O_CREATE|os.O_WRONLY, 0755)
  75. if err != nil {
  76. return err
  77. }
  78. // 生成代码
  79. {
  80. err = modelTmpl.Execute(model, a)
  81. if err != nil {
  82. return err
  83. }
  84. err = apiTmpl.Execute(api, a)
  85. if err != nil {
  86. return err
  87. }
  88. err = routerTmpl.Execute(router, a)
  89. if err != nil {
  90. return err
  91. }
  92. err = feapiTmpl.Execute(feapi, a)
  93. if err != nil {
  94. return err
  95. }
  96. err = readmeTmpl.Execute(readme, a)
  97. if err != nil {
  98. return err
  99. }
  100. }
  101. _ = model.Close()
  102. _ = api.Close()
  103. _ = router.Close()
  104. _ = feapi.Close()
  105. _ = readme.Close()
  106. fileList := []string{
  107. _te + a.PackageName + "/model/model.go",
  108. _te + a.PackageName + "/api/api.go",
  109. _te + a.PackageName + "/router/router.go",
  110. _fe + a.PackageName + "/api/api.js",
  111. _autoCode + "readme.txt",
  112. }
  113. err = tools.ZipFiles("./ginvueadmin.zip", fileList, ".", ".")
  114. if err != nil {
  115. return err
  116. }
  117. err = os.RemoveAll(_autoCode)
  118. if err != nil {
  119. return err
  120. }
  121. return nil
  122. }
  123. //批量创建文件夹
  124. func createDir(dirs ...string) (err error) {
  125. for _, v := range dirs {
  126. exist, err := tools.PathExists(v)
  127. if err != nil {
  128. //log.L.Info(fmt.Sprintf("get dir error![%v]\n", err))
  129. return err
  130. }
  131. if exist {
  132. //log.L.Info(fmt.Sprintf("has dir![%v]\n"+_dir))
  133. } else {
  134. //log.L.Info(fmt.Sprintf("no dir![%v]\n"+_dir))
  135. // 创建文件夹
  136. err = os.Mkdir(v, os.ModePerm)
  137. if err != nil {
  138. //log.L.Error(fmt.Sprintf("mkdir error![%v]\n",err))
  139. } else {
  140. //log.L.Info("mkdir success!\n")
  141. }
  142. }
  143. }
  144. return err
  145. }