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
4.2 KiB

  1. package service
  2. import (
  3. "gin-vue-admin/model"
  4. "gin-vue-admin/utils"
  5. "os"
  6. "text/template"
  7. )
  8. // @title CreateTemp
  9. // @description 函数的详细描述
  10. // @auth (2020/04/05 20:22)
  11. // @param autoCode model.AutoCodeStruct
  12. // @return err error
  13. func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
  14. basePath := "./resource/template"
  15. modelTmpl, err := template.ParseFiles(basePath + "/te/model.go.tpl")
  16. if err != nil {
  17. return err
  18. }
  19. apiTmpl, err := template.ParseFiles(basePath + "/te/api.go.tpl")
  20. if err != nil {
  21. return err
  22. }
  23. routerTmpl, err := template.ParseFiles(basePath + "/te/router.go.tpl")
  24. if err != nil {
  25. return err
  26. }
  27. serviceTmpl, err := template.ParseFiles(basePath + "/te/service.go.tpl")
  28. if err != nil {
  29. return err
  30. }
  31. feapiTmpl, err := template.ParseFiles(basePath + "/fe/api.js.tpl")
  32. if err != nil {
  33. return err
  34. }
  35. feTableTmpl, err := template.ParseFiles(basePath + "/fe/table.vue.tpl")
  36. if err != nil {
  37. return err
  38. }
  39. readmeTmpl, err := template.ParseFiles(basePath + "/readme.txt.tpl")
  40. if err != nil {
  41. return err
  42. }
  43. //自动化总目录
  44. _autoCode := "./autoCode/"
  45. //自动化后台代码目录
  46. _te := "./autoCode/te/"
  47. _dir := _te + autoCode.PackageName
  48. _modeldir := _te + autoCode.PackageName + "/model"
  49. _apidir := _te + autoCode.PackageName + "/api"
  50. _routerdir := _te + autoCode.PackageName + "/router"
  51. _servicedir := _te + autoCode.PackageName + "/service"
  52. //自动化前台代码目录
  53. _fe := "./autoCode/fe/"
  54. _fe_dir := _fe + autoCode.PackageName
  55. _fe_apidir := _fe + autoCode.PackageName + "/api"
  56. _fe_tabledir := _fe + autoCode.PackageName + "/table"
  57. err = utils.CreateDir(_autoCode, _te, _dir, _modeldir, _apidir, _routerdir, _servicedir, _fe, _fe_dir, _fe_apidir, _fe_tabledir)
  58. if err != nil {
  59. return err
  60. }
  61. model, err := os.OpenFile(_te+autoCode.PackageName+"/model/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
  62. if err != nil {
  63. return err
  64. }
  65. api, err := os.OpenFile(_te+autoCode.PackageName+"/api/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
  66. if err != nil {
  67. return err
  68. }
  69. router, err := os.OpenFile(_te+autoCode.PackageName+"/router/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
  70. if err != nil {
  71. return err
  72. }
  73. service, err := os.OpenFile(_te+autoCode.PackageName+"/service/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
  74. if err != nil {
  75. return err
  76. }
  77. feapi, err := os.OpenFile(_fe+autoCode.PackageName+"/api/"+autoCode.PackageName+".js", os.O_CREATE|os.O_WRONLY, 0755)
  78. if err != nil {
  79. return err
  80. }
  81. fetable, err := os.OpenFile(_fe+autoCode.PackageName+"/table/"+autoCode.PackageName+".vue", os.O_CREATE|os.O_WRONLY, 0755)
  82. if err != nil {
  83. return err
  84. }
  85. readme, err := os.OpenFile(_autoCode+"readme.txt", os.O_CREATE|os.O_WRONLY, 0755)
  86. if err != nil {
  87. return err
  88. }
  89. // 生成代码
  90. {
  91. err = modelTmpl.Execute(model, autoCode)
  92. if err != nil {
  93. return err
  94. }
  95. err = apiTmpl.Execute(api, autoCode)
  96. if err != nil {
  97. return err
  98. }
  99. err = routerTmpl.Execute(router, autoCode)
  100. if err != nil {
  101. return err
  102. }
  103. err = serviceTmpl.Execute(service, autoCode)
  104. if err != nil {
  105. return err
  106. }
  107. err = feapiTmpl.Execute(feapi, autoCode)
  108. if err != nil {
  109. return err
  110. }
  111. err = feTableTmpl.Execute(fetable, autoCode)
  112. if err != nil {
  113. return err
  114. }
  115. err = readmeTmpl.Execute(readme, autoCode)
  116. if err != nil {
  117. return err
  118. }
  119. }
  120. _ = model.Close()
  121. _ = api.Close()
  122. _ = router.Close()
  123. _ = service.Close()
  124. _ = feapi.Close()
  125. _ = fetable.Close()
  126. _ = readme.Close()
  127. fileList := []string{
  128. _te + autoCode.PackageName + "/model/" + autoCode.PackageName + ".go",
  129. _te + autoCode.PackageName + "/api/" + autoCode.PackageName + ".go",
  130. _te + autoCode.PackageName + "/router/" + autoCode.PackageName + ".go",
  131. _te + autoCode.PackageName + "/service/" + autoCode.PackageName + ".go",
  132. _fe + autoCode.PackageName + "/api/" + autoCode.PackageName + ".js",
  133. _fe + autoCode.PackageName + "/table/" + autoCode.PackageName + ".vue",
  134. _autoCode + "readme.txt",
  135. }
  136. err = utils.ZipFiles("./ginvueadmin.zip", fileList, ".", ".")
  137. if err != nil {
  138. return err
  139. }
  140. err = os.RemoveAll(_autoCode)
  141. if err != nil {
  142. return err
  143. }
  144. return nil
  145. }