-
Notifications
You must be signed in to change notification settings - Fork 52
/
templator.go
161 lines (143 loc) · 4.67 KB
/
templator.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package templator
import (
"path/filepath"
"strings"
"sync"
"text/template"
"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/util"
"github.com/gobuffalo/packr/v2"
"github.com/gobuffalo/packr/v2/file"
)
// DockerTemplator contains the templates relevent to docker
type DockerTemplator struct {
ApplicationDocker *template.Template
HttpGatewayDocker *template.Template
DockerIgnore *template.Template
DockerCompose *template.Template
}
// GoTemplator contains the templates relevant to a go project
type GoTemplator struct {
GoMain *template.Template
GoMod *template.Template
GoModIDL *template.Template
GoServer *template.Template
GoHealthServer *template.Template
GoHTTPGW *template.Template
}
// Templator contains all the templates
type Templator struct {
Commit0 *template.Template
GitIgnore *template.Template
Readme *template.Template
MakefileTemplate *template.Template
ProtoHealthTemplate *template.Template
ProtoServiceTemplate *template.Template
Go *GoTemplator
Docker *DockerTemplator
React *DirectoryTemplator
}
func NewTemplator(box *packr.Box) *Templator {
return &Templator{
MakefileTemplate: NewSingleFileTemplator(box, "proto/makefile.tmpl"),
ProtoHealthTemplate: NewSingleFileTemplator(box, "proto/health_proto.tmpl"),
ProtoServiceTemplate: NewSingleFileTemplator(box, "proto/service_proto.tmpl"),
Go: NewGoTemplator(box),
Commit0: NewSingleFileTemplator(box, "commit0/commit0.tmpl"),
GitIgnore: NewSingleFileTemplator(box, "util/gitignore.tmpl"),
Readme: NewSingleFileTemplator(box, "util/README.tmpl"),
Docker: NewDockerFileTemplator(box),
React: NewDirectoryTemplator(box, "react"),
}
}
func NewGoTemplator(box *packr.Box) *GoTemplator {
return &GoTemplator{
GoMain: NewSingleFileTemplator(box, "golang/main.tmpl"),
GoMod: NewSingleFileTemplator(box, "golang/go_mod.tmpl"),
GoModIDL: NewSingleFileTemplator(box, "golang/go_mod_idl.tmpl"),
GoServer: NewSingleFileTemplator(box, "golang/server.tmpl"),
GoHealthServer: NewSingleFileTemplator(box, "golang/health_server.tmpl"),
GoHTTPGW: NewSingleFileTemplator(box, "golang/http_gw.tmpl"),
}
}
func NewDockerFileTemplator(box *packr.Box) *DockerTemplator {
return &DockerTemplator{
ApplicationDocker: NewSingleFileTemplator(box, "docker/dockerfile_app.tmpl"),
HttpGatewayDocker: NewSingleFileTemplator(box, "docker/dockerfile_http.tmpl"),
DockerIgnore: NewSingleFileTemplator(box, "docker/dockerignore.tmpl"),
DockerCompose: NewSingleFileTemplator(box, "docker/dockercompose.tmpl"),
}
}
// NewSingleFileTemplator returns a template struct for a given template file
func NewSingleFileTemplator(box *packr.Box, file string) *template.Template {
source, err := box.FindString(file)
if err != nil {
panic(err)
}
t, err := template.New(file).Funcs(util.FuncMap).Parse(source)
if err != nil {
panic(err)
}
return t
}
type DirectoryTemplator struct {
Templates []*template.Template
}
func (d *DirectoryTemplator) TemplateFiles(config *config.Commit0Config, overwrite bool, wg *sync.WaitGroup) {
for _, template := range d.Templates {
d, f := filepath.Split(template.Name())
if strings.HasSuffix(f, ".tmpl") {
f = strings.Replace(f, ".tmpl", "", -1)
}
if overwrite {
util.TemplateFileAndOverwrite(d, f, template, wg, config)
} else {
util.TemplateFileIfDoesNotExist(d, f, template, wg, config)
}
}
}
func NewDirectoryTemplator(box *packr.Box, dir string) *DirectoryTemplator {
templates := []*template.Template{}
for _, file := range getFileNames(box, dir) {
templateSource, _ := box.FindString(file)
template, err := template.New(file).Funcs(util.FuncMap).Parse(templateSource)
if err != nil {
panic(err)
}
templates = append(templates, template)
}
return &DirectoryTemplator{
Templates: templates,
}
}
func getFileNames(box *packr.Box, dir string) []string {
keys := []string{}
box.WalkPrefix(dir, func(path string, info file.File) error {
if info == nil {
return nil
}
finfo, _ := info.FileInfo()
if !finfo.IsDir() {
keys = append(keys, path)
}
return nil
})
return removeTmplDuplicates(keys)
}
func removeTmplDuplicates(keys []string) []string {
filteredKeys := []string{}
for _, key := range keys {
if !containsStr(keys, key+".tmpl") {
filteredKeys = append(filteredKeys, key)
}
}
return filteredKeys
}
func containsStr(arr []string, key string) bool {
for _, val := range arr {
if val == key {
return true
}
}
return false
}