-
-
Notifications
You must be signed in to change notification settings - Fork 332
/
main.go
67 lines (52 loc) · 1.4 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
package main
import (
"net/http"
"strings"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/gson"
)
func main() {
list := getDeviceList()
code := ``
for _, d := range list.Arr() {
d = d.Get("device")
name := d.Get("title").String()
code += utils.S(`
// {{.name}} device
{{.name}} = Device{gson.NewFrom({{.desc}})}`,
"name", normalizeName(name),
"title", name,
"desc", utils.EscapeGoString(strings.TrimSpace(d.JSON("\t", " "))),
)
}
code = utils.S(`// generated by "lib/devices/generate"
package devices
var (
{{.code}}
)
`, "code", code)
path := "./lib/devices/list.go"
utils.E(utils.OutputFile(path, code))
utils.Exec("gofmt", "-s", "-w", path)
utils.Exec("golangci-lint", "run", "--no-config", "--fix", "--disable-all", "-E", "gofmt,goimports,misspell", path)
}
func getDeviceList() gson.JSON {
// we use the list from the web UI of devtools
res, err := http.Get(
"https://raw.githubusercontent.com/ChromeDevTools/devtools-frontend/master/front_end/emulated_devices/module.json",
)
utils.E(err)
return gson.New(res.Body).Get("extensions")
}
func normalizeName(name string) string {
name = strings.ReplaceAll(name, "/", "or")
list := []string{}
for _, s := range strings.Split(name, " ") {
if len(s) > 1 {
list = append(list, strings.ToUpper(s[0:1])+s[1:])
} else {
list = append(list, strings.ToUpper(s))
}
}
return strings.Join(list, "")
}