CreateFile.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package create
  2. import (
  3. "go-create-template/sqlload"
  4. "go-create-template/util"
  5. "os"
  6. "text/template"
  7. )
  8. // 加载配置文件
  9. func loadTemplate(templateUrl string) *template.Template {
  10. t := template.New(templateUrl)
  11. for s := range util.FuncMap {
  12. t.Funcs(template.FuncMap{s: util.FuncMap[s]})
  13. }
  14. file, err := os.ReadFile(templateUrl)
  15. if err != nil {
  16. panic(err)
  17. }
  18. return template.Must(t.Parse(string(file)))
  19. }
  20. func BaseCreate(outputPath, tmplUrl string) {
  21. tmpl := loadTemplate(tmplUrl)
  22. m := make(map[string]interface{})
  23. m["gens"] = sqlload.CGans
  24. m, err := util.StructToMap(m)
  25. if err != nil {
  26. panic(err)
  27. }
  28. file, err := os.OpenFile(outputPath, os.O_CREATE|os.O_WRONLY, 0666)
  29. os.Truncate(outputPath, 0)
  30. if err != nil {
  31. panic(err)
  32. }
  33. defer file.Close()
  34. err = tmpl.Execute(file, m)
  35. if err != nil {
  36. panic(err)
  37. }
  38. }
  39. func DomainCreate(outputPath string) {
  40. BaseCreate(outputPath, "./create/domain.go.tmpl")
  41. }
  42. func RouterCreate(outputPath string) {
  43. BaseCreate(outputPath, "./create/router.go.tmpl")
  44. }
  45. func DaoCreate(outputPath string) {
  46. BaseCreate(outputPath, "./create/dao.go.tmpl")
  47. }
  48. func ApiTsCreate(outputPath string) {
  49. BaseCreate(outputPath, "./create/api.ts.tmpl")
  50. }
  51. func QueryCreate(outputPath string) {
  52. BaseCreate(outputPath, "./create/domainQuery.go.tmpl")
  53. }
  54. func VueCreate(outputPath string) {
  55. BaseCreateVue(outputPath, "./create/back.vue.tmpl")
  56. }
  57. func BaseCreateVue(outputPath, tmplUrl string) {
  58. tmpl := loadTemplate(tmplUrl)
  59. for i := range sqlload.CGans {
  60. fileName := outputPath + sqlload.CGans[i].Table.Name + "View.vue"
  61. m := make(map[string]interface{})
  62. m["gen"] = sqlload.CGans[i]
  63. m, err := util.StructToMap(m)
  64. if err != nil {
  65. panic(err)
  66. }
  67. file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, 0666)
  68. os.Truncate(fileName, 0)
  69. if err != nil {
  70. panic(err)
  71. }
  72. defer file.Close()
  73. err = tmpl.Execute(file, m)
  74. if err != nil {
  75. panic(err)
  76. }
  77. }
  78. }