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