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.

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