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.

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