github.com/kotovmak/go-admin@v1.1.1/adm/compile_tmpl.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path" 7 "strings" 8 ) 9 10 func compileTmpl(rootPath, outputPath, packageName, varName string) { 11 content := `package ` + packageName + ` 12 13 var ` + varName + ` = map[string]string{` 14 15 content = getContentFromDir(content, fixPath(rootPath), fixPath(rootPath)) 16 17 content += `}` 18 19 _ = ioutil.WriteFile(outputPath, []byte(content), 0644) 20 } 21 22 func fixPath(p string) string { 23 if p[len(p)-1] != '/' { 24 return p + "/" 25 } 26 return p 27 } 28 29 func getContentFromDir(content, dirPath, rootPath string) string { 30 files, _ := ioutil.ReadDir(dirPath) 31 32 for _, f := range files { 33 34 if f.IsDir() { 35 content = getContentFromDir(content, dirPath+f.Name()+"/", rootPath) 36 continue 37 } 38 39 b, err := ioutil.ReadFile(dirPath + f.Name()) 40 if err != nil { 41 fmt.Print(err) 42 } 43 str := string(b) 44 45 suffix := path.Ext(f.Name()) 46 onlyName := strings.TrimSuffix(f.Name(), suffix) 47 48 if suffix == ".tmpl" { 49 fmt.Println(dirPath + f.Name()) 50 content += `"` + strings.ReplaceAll(dirPath, rootPath, "") + onlyName + `":` + "`" + str + "`," 51 } 52 } 53 54 return content 55 }