| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- 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")
- }
|