-
Notifications
You must be signed in to change notification settings - Fork 0
/
templating.go
136 lines (114 loc) · 3.48 KB
/
templating.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package codegen
import (
"bytes"
"embed"
"fmt"
"io/fs"
"regexp"
"strings"
"text/template"
"github.com/huandu/xstrings"
"github.com/iancoleman/strcase"
"github.com/bmatcuk/doublestar/v4"
"github.com/golang-cz/textcase"
)
var templateFuncs = template.FuncMap{
"add": func(left int, right int) int {
return left + right
},
"toUpper": strings.ToUpper,
"toKebabCase": textcase.KebabCase,
"toSnakeCase": xstrings.ToSnakeCase,
"toLowerCamelCase": strcase.ToLowerCamel,
"toPascalCase": textcase.PascalCase,
"sanitizeProtoFieldName": SanitizeProtoFieldName,
}
type GenerateConfig struct {
FS embed.FS
Files map[string]string
}
// GenerateTemplateTree will read from both the given templateFS and the commonTemplate folder for files prefixed with 'common-templates/'
func GenerateTemplateTree(projectData any, templatesFS embed.FS, templateFiles map[string]string) ReturnGenerate {
projFiles, err := generateTemplateTree(projectData, templatesFS, templateFiles)
if err != nil {
return ReturnGenerate{Err: err}
}
return ReturnGenerate{ProjectFiles: projFiles}
}
func generateTemplateTree(projectData any, templatesFS embed.FS, templateFiles map[string]string) (map[string][]byte, error) {
projectFiles := map[string][]byte{}
tpls, err := ParseFS(templatesFS, "**/*.gotmpl") // TODO: close when refactored
if err != nil {
return nil, fmt.Errorf("parse templates: %w", err)
}
for templateFile, finalFileName := range templateFiles {
//zlog.Debug("reading template file", zap.String("filename", templateFile))
var content []byte
if strings.HasSuffix(templateFile, ".gotmpl") {
buffer := &bytes.Buffer{}
var err error
if strings.HasPrefix(templateFile, "common-templates/") {
err = commonTemplates.ExecuteTemplate(buffer, templateFile, projectData)
} else {
err = tpls.ExecuteTemplate(buffer, templateFile, projectData)
}
if err != nil {
return nil, fmt.Errorf("embed render entry template %q: %w", templateFile, err)
}
content = buffer.Bytes()
} else {
if strings.HasPrefix(templateFile, "common-templates/") {
content, err = commonTemplatesFS.ReadFile(templateFile)
} else {
content, err = templatesFS.ReadFile("templates/" + templateFile)
}
if err != nil {
return nil, fmt.Errorf("reading %q: %w", templateFile, err)
}
}
projectFiles[finalFileName] = content
}
return projectFiles, nil
}
// ParseFS reads the files from the embedded FS and parses them into named templates.
func ParseFS(fsys fs.FS, pattern string) (*template.Template, error) {
t, err := commonTemplates.Clone()
if err != nil {
return nil, err
}
filenames, err := doublestar.Glob(fsys, pattern)
if err != nil {
return nil, err
}
if len(filenames) == 0 {
return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
}
for _, filename := range filenames {
b, err := fs.ReadFile(fsys, filename)
if err != nil {
return nil, err
}
name, _ := strings.CutPrefix(filename, "templates/")
_, err = t.New(name).Parse(string(b))
if err != nil {
return nil, err
}
}
return t, nil
}
func SanitizeProtoFieldName(name string) string {
regex := regexp.MustCompile("(\\d+)(_*)")
name = regex.ReplaceAllStringFunc(name, func(match string) string {
if strings.HasSuffix(match, "_") {
return match
}
return match + "_"
})
if strings.HasPrefix(name, "_") {
name = "u" + name
}
if strings.HasSuffix(name, "_") {
name = strings.TrimSuffix(name, "_")
}
return name
}