package create import ( "go-create-template/sqlload" "go-create-template/util" "os" "text/template" ) // 加载配置文件 func loadTemplate(templateUrl string) *template.Template { t := template.New(templateUrl) for s := range util.FuncMap { t.Funcs(template.FuncMap{s: util.FuncMap[s]}) } file, err := os.ReadFile(templateUrl) if err != nil { panic(err) } return template.Must(t.Parse(string(file))) } func BaseCreate(outputPath, tmplUrl string) { tmpl := loadTemplate(tmplUrl) m := make(map[string]interface{}) m["gens"] = sqlload.CGans m, err := util.StructToMap(m) if err != nil { panic(err) } file, err := os.OpenFile(outputPath, os.O_CREATE|os.O_WRONLY, 0666) os.Truncate(outputPath, 0) if err != nil { panic(err) } defer file.Close() err = tmpl.Execute(file, m) if err != nil { panic(err) } } func DomainCreate(outputPath string) { BaseCreate(outputPath, "./create/domain.go.tmpl") } func RouterCreate(outputPath string) { BaseCreate(outputPath, "./create/router.go.tmpl") } func DaoCreate(outputPath string) { BaseCreate(outputPath, "./create/dao.go.tmpl") } func ApiTsCreate(outputPath string) { BaseCreate(outputPath, "./create/api.ts.tmpl") } func QueryCreate(outputPath string) { BaseCreate(outputPath, "./create/domainQuery.go.tmpl") } func VueCreate(outputPath string) { BaseCreateVue(outputPath, "./create/back.vue.tmpl") } func BaseCreateVue(outputPath, tmplUrl string) { tmpl := loadTemplate(tmplUrl) for i := range sqlload.CGans { fileName := outputPath + sqlload.CGans[i].Table.Name + "View.vue" m := make(map[string]interface{}) m["gen"] = sqlload.CGans[i] m, err := util.StructToMap(m) if err != nil { panic(err) } file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, 0666) os.Truncate(fileName, 0) if err != nil { panic(err) } defer file.Close() err = tmpl.Execute(file, m) if err != nil { panic(err) } } }