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.

133 lines
3.7 KiB

  1. package service
  2. import (
  3. "gin-vue-admin/model"
  4. "gin-vue-admin/utils"
  5. "html/template"
  6. "os"
  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. readmeTmpl, err := template.ParseFiles(basePath + "/readme.txt.tpl")
  36. if err != nil {
  37. return err
  38. }
  39. //自动化总目录
  40. _autoCode := "./autoCode/"
  41. //自动化后台代码目录
  42. _te := "./autoCode/te/"
  43. _dir := _te + autoCode.PackageName
  44. _modeldir := _te + autoCode.PackageName + "/model"
  45. _apidir := _te + autoCode.PackageName + "/api"
  46. _routerdir := _te + autoCode.PackageName + "/router"
  47. _servicedir := _te + autoCode.PackageName + "/service"
  48. //自动化前台代码目录
  49. _fe := "./autoCode/fe/"
  50. _fe_dir := _fe + autoCode.PackageName
  51. _fe_apidir := _fe + autoCode.PackageName + "/api"
  52. err = utils.CreateDir(_autoCode, _te, _dir, _modeldir, _apidir, _routerdir, _servicedir, _fe, _fe_dir, _fe_apidir)
  53. if err != nil {
  54. return err
  55. }
  56. model, err := os.OpenFile(_te+autoCode.PackageName+"/model/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
  57. if err != nil {
  58. return err
  59. }
  60. api, err := os.OpenFile(_te+autoCode.PackageName+"/api/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
  61. if err != nil {
  62. return err
  63. }
  64. router, err := os.OpenFile(_te+autoCode.PackageName+"/router/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
  65. if err != nil {
  66. return err
  67. }
  68. service, err := os.OpenFile(_te+autoCode.PackageName+"/service/"+autoCode.PackageName+".go", os.O_CREATE|os.O_WRONLY, 0755)
  69. if err != nil {
  70. return err
  71. }
  72. feapi, err := os.OpenFile(_fe+autoCode.PackageName+"/api/"+autoCode.PackageName+".js", os.O_CREATE|os.O_WRONLY, 0755)
  73. if err != nil {
  74. return err
  75. }
  76. readme, err := os.OpenFile(_autoCode+"readme.txt", os.O_CREATE|os.O_WRONLY, 0755)
  77. if err != nil {
  78. return err
  79. }
  80. // 生成代码
  81. {
  82. err = modelTmpl.Execute(model, autoCode)
  83. if err != nil {
  84. return err
  85. }
  86. err = apiTmpl.Execute(api, autoCode)
  87. if err != nil {
  88. return err
  89. }
  90. err = routerTmpl.Execute(router, autoCode)
  91. if err != nil {
  92. return err
  93. }
  94. err = serviceTmpl.Execute(service, autoCode)
  95. if err != nil {
  96. return err
  97. }
  98. err = feapiTmpl.Execute(feapi, autoCode)
  99. if err != nil {
  100. return err
  101. }
  102. err = readmeTmpl.Execute(readme, autoCode)
  103. if err != nil {
  104. return err
  105. }
  106. }
  107. _ = model.Close()
  108. _ = api.Close()
  109. _ = router.Close()
  110. _ = service.Close()
  111. _ = feapi.Close()
  112. _ = readme.Close()
  113. fileList := []string{
  114. _te + autoCode.PackageName + "/model/" + autoCode.PackageName + ".go",
  115. _te + autoCode.PackageName + "/api/" + autoCode.PackageName + ".go",
  116. _te + autoCode.PackageName + "/router/" + autoCode.PackageName + ".go",
  117. _te + autoCode.PackageName + "/service/" + autoCode.PackageName + ".go",
  118. _fe + autoCode.PackageName + "/api/" + autoCode.PackageName + ".js",
  119. _autoCode + "readme.txt",
  120. }
  121. err = utils.ZipFiles("./ginvueadmin.zip", fileList, ".", ".")
  122. if err != nil {
  123. return err
  124. }
  125. err = os.RemoveAll(_autoCode)
  126. if err != nil {
  127. return err
  128. }
  129. return nil
  130. }