Debian Codegraph @

lib/proto/generate/main.go

raw
1 // Package main ...
2 package main
3
4 import (
5 "fmt"
6 "os"
7 "path/filepath"
8 "regexp"
9 "strings"
10
11 "github.com/go-rod/rod/lib/utils"
12 )
13
14 func main() {
15 comment := `// This file is generated by "./lib/proto/generate"`
16
17 schema := getSchema()
18
19 cleanup()
20
21 init := comment + utils.S(`
22
23 package proto
24
25 import (
26 "reflect"
27 "github.com/ysmood/gson"
28 )
29
30 // Version of cdp protocol
31 const Version = "v{{.major}}.{{.minor}}"
32
33 var types = map[string]reflect.Type{
34 `, "major", schema.Get("version.major").Str(), "minor", schema.Get("version.minor").Str())
35
36 testsCode := comment + `
37
38 package proto_test
39
40 import (
41 "github.com/go-rod/rod/lib/proto"
42 )
43 `
44
45 for _, domain := range parse(schema) {
46 code := comment + `
47
48 package proto
49
50 import (
51 "github.com/ysmood/gson"
52 )
53 `
54
55 code += fmt.Sprintf("/*\n\n%s\n\n", domain.name)
56
57 if domain.description != "" {
58 code += domain.description + "\n\n"
59 }
60 code += "*/\n\n"
61
62 for _, definition := range domain.definitions {
63 if definition.skip {
64 continue
65 }
66
67 code += definition.format()
68 testsCode += definition.formatTests()
69
70 if definition.originName != "" {
71 init += utils.S(`
72 "{{.name}}": reflect.TypeOf({{.type}}{}),`,
73 "name", definition.domain.name+"."+definition.originName,
74 "type", definition.name,
75 )
76 }
77 }
78
79 utils.E(utils.OutputFile(
80 filepath.FromSlash(
81 fmt.Sprintf("lib/proto/%s.go", toSnakeCase(domain.name))),
82 code))
83 }
84
85 init += `
86 }
87 `
88
89 utils.E(utils.OutputFile(filepath.FromSlash("lib/proto/definitions.go"), init))
90 utils.E(utils.OutputFile(filepath.FromSlash("lib/proto/definitions_test.go"), testsCode))
91
92 path := "./lib/proto"
93 utils.Exec("gofumpt -w", path)
94 utils.Exec("go run golang.org/x/tools/cmd/goimports@latest -w", path)
95 utils.Exec(
96 "go run github.com/ysmood/golangci-lint@latest -- run --fix",
97 path,
98 )
99 }
100
101 func (d *definition) comment() string {
102 comment := d.description
103
104 if comment == "<nil>" {
105 comment = "..."
106 }
107
108 if d.optional {
109 comment = "(optional) " + comment
110 }
111 if d.experimental {
112 comment = "(experimental) " + comment
113 }
114 if d.deprecated {
115 comment = "(deprecated) " + comment
116 }
117
118 name := symbol(d.name)
119
120 if !strings.HasPrefix(strings.TrimSpace(comment), name) {
121 comment = name + " " + comment
122 }
123
124 return regexp.MustCompile(`(?m)^`).ReplaceAllString(comment, "// ")
125 }
126
127 func (d *definition) format() (code string) {
128 switch d.objType {
129 case objTypePrimitive:
130 code = utils.S(`
131 {{.comment}}
132 type {{.name}} {{.type}}
133 `, "name", d.name, "type", d.typeName, "comment", d.comment())
134
135 if d.enum != nil {
136 code += "const ("
137 for _, value := range d.enum {
138 name := d.name + symbol(value)
139 code += utils.S(`
140 // {{.name}} enum const
141 {{.name}} {{.type}} = "{{.value}}"
142 `, "name", name, "type", d.name, "value", value)
143 }
144 code += ")\n"
145 }
146
147 case objTypeStruct:
148 code = utils.S(`
149 {{.comment}}
150 type {{.name}} struct {
151 `, "name", d.name, "comment", d.comment())
152
153 for _, prop := range d.props {
154 tag := jsonTag(prop.originName, prop.optional)
155
156 t := prop.typeName
157 if prop.optional {
158 switch prop.typeName {
159 case "float64", "int":
160 if !strings.Contains(prop.comment(), "(default: 0)") {
161 t = "*" + t
162 }
163 }
164 }
165
166 code += utils.S(`
167 {{.comment}}
168 {{.name}} {{.type}} {{.tag}}
169 `, "comment", prop.comment(), "name", prop.name, "type", t, "tag", tag)
170 }
171
172 code += "}\n"
173
174 if d.command {
175 method := d.domain.name + "." + d.originName
176 if d.returnValue {
177 code += utils.S(`
178 // ProtoReq name
179 func (m {{.name}}) ProtoReq() string { return "{{.method}}" }
180
181 // Call the request
182 func (m {{.name}}) Call(c Client) (*{{.name}}Result, error) {
183 var res {{.name}}Result
184 return &res, call(m.ProtoReq(), m, &res, c)
185 }
186 `, "name", d.name, "method", method)
187 } else {
188 code += utils.S(`
189 // ProtoReq name
190 func (m {{.name}}) ProtoReq() string { return "{{.method}}" }
191
192 // Call sends the request
193 func (m {{.name}}) Call(c Client) error {
194 return call(m.ProtoReq(), m, nil, c)
195 }
196 `, "name", d.name, "method", method)
197 }
198 }
199
200 if d.cdpType == cdpTypeEvents {
201 code += utils.S(`
202 // ProtoEvent name
203 func (evt {{.name}}) ProtoEvent() string {
204 return "{{.event}}"
205 }
206 `, "name", d.name, "event", d.domain.name+"."+d.originName)
207 }
208 }
209
210 return
211 }
212
213 func (d *definition) formatTests() (code string) {
214 switch d.cdpType {
215 case cdpTypeCommands:
216 if !d.command {
217 return ""
218 }
219
220 if d.returnValue {
221 return utils.S(`
222 func (t T) {{.name}}() {
223 c := &Client{}
224 _, err := proto.{{.name}}{}.Call(c)
225 t.Nil(err)
226 }
227 `, "name", d.name)
228 }
229
230 return utils.S(`
231 func (t T) {{.name}}() {
232 c := &Client{}
233 err := proto.{{.name}}{}.Call(c)
234 t.Nil(err)
235 }
236 `, "name", d.name)
237
238 case cdpTypeEvents:
239 return utils.S(`
240 func (t T) {{.name}}() {
241 e := proto.{{.name}}{}
242 t.Regex("", e.ProtoEvent())
243 }
244 `, "name", d.name)
245 }
246
247 return ""
248 }
249
250 // The "a_" prefixed files won't removed, other go files will be removed before the generation.
251 func cleanup() {
252 d := filepath.Join("lib", "proto")
253 list, err := os.ReadDir(d)
254 utils.E(err)
255
256 for _, f := range list {
257 if !f.IsDir() && !strings.HasPrefix(f.Name(), "a_") && strings.HasSuffix(f.Name(), ".go") {
258 utils.E(os.Remove(filepath.Join(d, f.Name())))
259 }
260 }
261 }