CreateFile.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. }