-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
77 lines (66 loc) · 1.76 KB
/
gen.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
// Copyright (c) 2023, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// This program generates constants for all of the icon
// svg file names in outlined
package main
import (
"bytes"
"io/fs"
"log"
"os"
"strings"
"text/template"
"unicode"
"github.com/iancoleman/strcase"
"goki.dev/icons"
)
const preamble = `// Code generated by "gen.go"; DO NOT EDIT.
package icons
const (`
// iconData contains the data for an icon
type iconData struct {
Dir string // Dir is the directory in which the icon is contained
Snake string // Snake is the snake_case name of the icon
Camel string // Camel is the CamelCase name of the icon
}
var iconTmpl = template.Must(template.New("icon").Parse(
`
// {{.Camel}} is https://github.com/goki/icons/blob/main/{{.Dir}}{{.Snake}}.svg
{{.Camel}} Icon = "{{.Snake}}"
`,
))
func main() {
buf := bytes.NewBufferString(preamble)
fs.WalkDir(icons.Icons, ".", func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
name := strings.TrimSuffix(path, ".svg")
// ignore fill icons as they are handled separately
if strings.HasSuffix(name, "-fill") {
return nil
}
// ignore blank icon, as we define the constant for that separately
if name == "blank" {
return nil
}
camel := strcase.ToCamel(name)
// identifier names can't start with a digit
if unicode.IsDigit([]rune(camel)[0]) {
camel = "X" + camel
}
data := iconData{
Dir: "svg/",
Snake: name,
Camel: camel,
}
return iconTmpl.Execute(buf, data)
})
buf.WriteString(")\n")
err := os.WriteFile("iconnames.go", buf.Bytes(), 0666)
if err != nil {
log.Fatalln("error writing result to iconnames.go:", err)
}
}