forked from rlj1202/go-wpd
/
genconst.go
97 lines (85 loc) · 1.73 KB
/
genconst.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"path/filepath"
"text/template"
)
/*
Contains names of consts in gowpd.go
*/
type Consts struct {
HRESULTS []string
CLSIDS []string
IIDS []string
PROPERTYKEYS []string
GUIDS []string
}
func main() {
fset := token.NewFileSet()
af, err := parser.ParseFile(fset, "gowpd.go", nil, 0)
if err != nil {
log.Fatal(err)
}
af.Doc = nil
af.Imports = nil
af.Comments = nil
var consts Consts
for _, decl := range af.Decls {
gd, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
if gd.Tok != token.CONST {
continue
}
var lastType ast.Expr
for _, spec := range gd.Specs {
vs, ok := spec.(*ast.ValueSpec)
if !ok {
continue
}
var curType ast.Expr = vs.Type
if curType == nil {
curType = lastType
}
if curType == nil {
continue
}
typeName := curType.(*ast.Ident).Name
name := vs.Names[0].Name
switch typeName {
case "HRESULT":
consts.HRESULTS = append(consts.HRESULTS, name)
case "CLSID":
consts.CLSIDS = append(consts.CLSIDS, name)
case "IID":
consts.IIDS = append(consts.IIDS, name)
case "PropertyKey":
consts.PROPERTYKEYS = append(consts.PROPERTYKEYS, name)
case "GUID":
consts.GUIDS = append(consts.GUIDS, name)
}
log.Println(typeName, name)
lastType = curType
}
}
templateBytes, err := ioutil.ReadFile("gen/consts.go.template")
if err != nil {
log.Fatal(err)
}
f, err := os.Create("consts.go")
if err != nil {
log.Fatal(err)
}
log.Println(filepath.Abs(f.Name()))
constsTemplate := template.Must(template.New("consts").Parse(string(templateBytes)))
err = constsTemplate.Execute(f, consts)
if err != nil {
log.Fatal(err)
}
}