-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmain.go
More file actions
215 lines (177 loc) Β· 4.73 KB
/
main.go
File metadata and controls
215 lines (177 loc) Β· 4.73 KB
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
209
210
211
212
213
214
215
package main
import (
"encoding/json"
"fmt"
"os"
"path"
"slices"
"strings"
"github.com/evcc-io/evcc/util/templates"
"github.com/gosimple/slug"
)
const (
docsPath = "../../../templates/docs"
websitePath = "../../../templates/evcc.io"
iconsPath = "../../../templates/icons"
)
//go:generate go run main.go
func main() {
slug.CustomSub = map[string]string{"+": "plus"}
for _, lang := range []string{"de", "en"} {
if err := generateDocs(lang); err != nil {
panic(err)
}
}
if err := generateBrandJSON(); err != nil {
panic(err)
}
if err := generateProductJSON(); err != nil {
panic(err)
}
}
func generateDocs(lang string) error {
for _, class := range templates.ClassValues() {
path := fmt.Sprintf("%s/%s/%s", docsPath, lang, strings.ToLower(class.String()))
_, err := os.Stat(path)
if os.IsNotExist(err) {
if err := os.MkdirAll(path, 0o755); err != nil {
return err
}
}
if err := clearDir(path); err != nil {
fmt.Printf("Could not clear directory for %s: %s\n", class, err)
}
if err := generateClass(class, lang); err != nil {
return err
}
}
return nil
}
func generateClass(class templates.Class, lang string) error {
for _, tmpl := range templates.ByClass(class) {
if err := tmpl.Validate(); err != nil {
return err
}
for _, product := range tmpl.Products {
fmt.Println(tmpl.Template + ": " + product.Title(lang))
b, err := tmpl.RenderDocumentation(product, lang)
if err != nil {
return err
}
filename := fmt.Sprintf("%s/%s/%s/%s.yaml", docsPath, lang, strings.ToLower(class.String()), product.Identifier())
if _, err := os.Stat(filename); err == nil {
return fmt.Errorf("file already exists: %s - product titles must be unique", filename)
}
if err := os.WriteFile(filename, b, 0o644); err != nil {
return err
}
}
}
return nil
}
func clearDir(dir string) error {
names, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, entry := range names {
if err := os.RemoveAll(path.Join([]string{dir, entry.Name()}...)); err != nil {
return err
}
}
return nil
}
func sorted(keys []string) []string {
slices.SortFunc(keys, func(i, j string) int {
return strings.Compare(strings.ToLower(i), strings.ToLower(j))
})
return slices.Compact(keys)
}
func generateBrandJSON() error {
var chargers, smartPlugs []string
for _, tmpl := range templates.ByClass(templates.Charger) {
for _, product := range tmpl.Products {
if product.Brand == "" {
continue
}
if tmpl.Group == "switchsockets" {
smartPlugs = append(smartPlugs, product.Brand)
} else {
chargers = append(chargers, product.Brand)
}
}
}
var vehicles []string
for _, tmpl := range templates.ByClass(templates.Vehicle) {
for _, product := range tmpl.Products {
if product.Brand != "" {
vehicles = append(vehicles, product.Brand)
}
}
}
var meters, pvBattery []string
for _, tmpl := range templates.ByClass(templates.Meter) {
for i := range tmpl.Params {
if tmpl.Params[i].Name != templates.ParamUsage {
continue
}
for j := range tmpl.Params[i].Choice {
usage, _ := templates.UsageString(tmpl.Params[i].Choice[j])
for _, product := range tmpl.Products {
if product.Brand == "" {
continue
}
switch usage {
case templates.UsageGrid, templates.UsageCharge, templates.UsageAux:
meters = append(meters, product.Brand)
case templates.UsagePV, templates.UsageBattery:
pvBattery = append(pvBattery, product.Brand)
}
}
}
}
}
brands := struct {
Chargers, SmartPlugs, Meters, PVBattery, Vehicles []string
}{
Chargers: sorted(chargers),
SmartPlugs: sorted(smartPlugs),
Meters: sorted(meters),
PVBattery: sorted(pvBattery),
Vehicles: sorted(vehicles),
}
file, err := json.MarshalIndent(brands, "", " ")
if err == nil {
err = os.WriteFile(websitePath+"/brands.json", file, 0o644)
}
return err
}
func generateProductJSON() error {
type ProductInfo struct {
Brand string `json:"brand"`
Description string `json:"description"`
}
products := make(map[string]map[string]ProductInfo)
for _, class := range templates.ClassValues() {
classKey := strings.ToLower(class.String())
products[classKey] = make(map[string]ProductInfo)
for _, tmpl := range templates.ByClass(class) {
for _, product := range tmpl.Products {
products[classKey][product.Identifier()] = ProductInfo{
Brand: product.Brand,
Description: product.Description.String("en"),
}
}
}
}
if _, err := os.Stat(iconsPath); os.IsNotExist(err) {
if err := os.MkdirAll(iconsPath, 0o755); err != nil {
return err
}
}
file, err := json.MarshalIndent(products, "", " ")
if err == nil {
err = os.WriteFile(iconsPath+"/products.json", file, 0o644)
}
return err
}