-
Notifications
You must be signed in to change notification settings - Fork 47
/
generator.go
81 lines (66 loc) · 1.16 KB
/
generator.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
package main
import (
"flag"
"path/filepath"
"strings"
"github.com/ungerik/go-dry"
)
func main() {
list := []string{
"ApplicationWindow",
"ScrolledWindow",
"AboutDialog",
"CheckButton",
"ColorButton",
"MenuButton",
"HeaderBar",
"MenuItem",
"Notebook",
"TextView",
"Viewport",
"ListBox",
"Spinner",
"Button",
"Switch",
"Window",
"Entry",
"Label",
"Stack",
"Box",
}
target := ""
flag.StringVar(&target, "target", "", "-target=./gui")
flag.Parse()
if err := Generate(list, target); err != nil {
panic(err)
}
}
func Generate(list []string, target string) error {
txt := `package get
import (
"errors"
"github.com/gotk3/gotk3/gtk"
)
var Builder *gtk.Builder
`
for _, el := range list {
vel := strings.ToLower(el)
txt += `
func ` + el + `(name string) (*gtk.` + el + `, error) {
obj, err := Builder.GetObject(name)
if err != nil {
return nil, err
}
` + vel + `1, ok := obj.(*gtk.` + el + `)
if !ok {
return nil, errors.New("cant get *gtk.` + el + `: " + name)
}
return ` + vel + `1, nil
}
`
}
if err := dry.FileSetString(filepath.Join(target, "./gtk.go"), txt); err != nil {
return err
}
return nil
}