forked from cathiele/evcc-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
208 lines (172 loc) · 4.23 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"
"github.com/andig/evcc-config/registry"
flag "github.com/spf13/pflag"
"gopkg.in/yaml.v2"
)
const (
ext = ".yaml"
summary = "template.md"
)
var (
confYaml string
confGo bool
confOutGo string
confSummary bool
confOutSummary string
confHelp bool
tmpl *template.Template
)
func init() {
flag.StringVarP(&confYaml, "yaml", "y", "yaml", "yaml path")
flag.StringVarP(&confOutGo, "output-go", "o", "", "output go files path")
flag.StringVarP(&confOutSummary, "output-summary", "f", "", "output summary file")
flag.BoolVarP(&confGo, "go", "g", false, "generate go files")
flag.BoolVarP(&confSummary, "summary", "s", false, "generate summary")
flag.BoolVarP(&confHelp, "help", "h", false, "help")
flag.Parse()
}
var sourceTemplate = `package templates {{/* Define backtick variable */}}{{$tick := "` + "`" + `"}}
import (
"github.com/andig/evcc-config/registry"
)
func init() {
template := registry.Template{
Class: "{{.Class}}",
Type: "{{.Type}}",
Name: "{{.Name}}",
Sample: {{$tick}}{{escape .Sample}}{{$tick}},
}
registry.Add(template)
}
`
func scanFolder(root string) (files []string) {
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(info.Name()) == ext {
files = append(files, path)
}
return nil
})
if err != nil {
panic(err)
}
return files
}
func parseSample(file string) registry.Template {
src, err := os.ReadFile(file)
if err != nil {
panic(err)
}
var sample registry.Template
if err := yaml.Unmarshal(src, &sample); err != nil {
panic(err)
}
// trim trailing linebreaks
sample.Sample = strings.TrimRight(sample.Sample, "\r\n")
return sample
}
func render(wr io.Writer, sample registry.Template) {
if tmpl == nil {
var err error
tmpl, err = template.New("test").Funcs(template.FuncMap{
// escape backticks in raw strings
"escape": func(s string) string {
return strings.ReplaceAll(s, "`", "`+\"`\"+`")
},
}).Parse(sourceTemplate)
if err != nil {
panic(err)
}
}
tmpl.Execute(wr, sample)
}
func renderSummary(wr io.Writer, samples []registry.Template) {
summaryTemplate, err := os.ReadFile(summary)
if err != nil {
panic(err)
}
// prepare outside of loop
re, err := regexp.Compile("[^a-zA-ZäöüÄÖÜ0-9]")
if err != nil {
panic(err)
}
tmpl, err := template.New("test").Funcs(template.FuncMap{
// filter samples by class
"filter": func(class string, samples []registry.Template) (reg []registry.Template) {
for _, sample := range samples {
if sample.Class == class {
reg = append(reg, sample)
}
}
return
},
// https://github.com/Masterminds/sprig/blob/48e6b77026913419ba1a4694dde186dc9c4ad74d/strings.go#L109
"indent": func(spaces int, v string) string {
pad := strings.Repeat(" ", spaces)
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
},
// unique link target
"href": func(class, name string) string {
link := strings.ReplaceAll(re.ReplaceAllString(strings.ToLower(name), "-"), "--", "-")
return strings.Trim(strings.ToLower(class)+"-"+link, "-")
},
}).Parse(string(summaryTemplate))
if err != nil {
panic(err)
}
tmpl.Execute(wr, samples)
}
func output(file string, fun func(io.Writer)) {
wr := os.Stdout
if file != "" {
var err error
wr, err = os.Create(file)
if err != nil {
panic(err)
}
}
fun(wr)
wr.Close()
}
func main() {
if confHelp {
flag.PrintDefaults()
os.Exit(0)
}
var samples []registry.Template
files := scanFolder(confYaml)
for _, file := range files {
sample := parseSample(file)
// example type
dir := filepath.Dir(file)
typ := filepath.Base(dir)
typ = strings.TrimRight(typ, "s") // de-pluralize
sample.Class = typ
samples = append(samples, sample)
if confGo {
var out string
if confOutGo != "" {
name := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
out = fmt.Sprintf("%s/%s-%s.go", confOutGo, typ, name)
}
println(out)
output(out, func(wr io.Writer) {
render(wr, sample)
})
}
}
if confSummary {
sort.Sort(registry.Templates(samples))
output(confOutSummary, func(wr io.Writer) {
renderSummary(wr, samples)
})
}
}