This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
/
gen.go
168 lines (149 loc) · 3.4 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright © 2020 Hedzr Yeh.
package gen
import (
"bytes"
"fmt"
"github.com/hedzr/cmdr"
"github.com/hedzr/log/dir"
"go/ast"
"go/build"
"go/format"
"go/parser"
"go/token"
"log"
"os"
"path"
"strings"
"text/template"
)
func pr(msg string, args ...interface{}) {
fmt.Printf("%v\n", fmt.Sprintf(msg, args...))
}
func prflag(name string) {
fmt.Printf("%v => %q\n", name, cmdr.GetStringR("application."+name))
}
func genApp(cmd *cmdr.Command, args []string) (err error) {
prflag("module-name")
prflag("name")
prflag("package")
prflag("processing-filename")
prflag("processing-file-lineno")
goFile := cmdr.GetStringR("application.processing-filename")
goFilePath := path.Join(dir.GetCurrentDir(), goFile)
goPackage := cmdr.GetStringR("application.package")
goModName := goPackage
if goModName == "main" {
goModName = trimGopath(path.Dir(goFilePath))
}
var pkgInfo *build.Package
pr("goFilePath: %q", goFilePath)
pr("goPackage: %q", goPackage)
pr("goModName: %q", goModName)
pr("generating and target to %q ...", goModName)
pkgInfo, err = build.ImportDir(path.Dir(goFilePath), 0)
if err != nil {
log.Fatal(err)
}
log.Printf("pkgInfo: %v", pkgInfo)
parseAst(goModName, goPackage, pkgInfo)
return
}
func parseAst(modName, pkgName string, pkgInfo *build.Package) {
fset := token.NewFileSet()
for _, file := range pkgInfo.GoFiles {
f, err := parser.ParseFile(fset, file, nil, 0)
if err != nil {
log.Fatal(err)
}
typ := ""
ast.Inspect(f, func(n ast.Node) bool {
decl, ok := n.(*ast.GenDecl)
// 只需要const
if !ok || decl.Tok != token.CONST {
return true
}
for _, spec := range decl.Specs {
vspec := spec.(*ast.ValueSpec)
if vspec.Type == nil && len(vspec.Values) > 0 {
typ = ""
continue
}
if vspec.Type != nil {
ident, ok := vspec.Type.(*ast.Ident)
if !ok {
continue
}
typ = ident.Name
}
pr("typ = %q | vspec = %v / %v", typ, vspec.Names, vspec.Values)
////typ是否是需处理的类型
//consts, ok := typesMap[typ]
//if !ok {
// continue
//}
////将所有const变量名保存
//for _, n := range vspec.Names {
// consts = append(consts, n.Name)
//}
//typesMap[typ] = consts
}
return true
})
}
return
}
func genString(modName, pkgName string, pkgInfo *build.Package, types map[string][]string) []byte {
const strTmp = `
package {{.pkg}}
import "fmt"
{{range $typ,$consts :=.types}}
func (c {{$typ}}) String() string{
switch c { {{range $consts}}
case {{.}}:return "{{.}}"{{end}}
}
return fmt.Sprintf("Status(%d)", c)
}
{{end}}
`
//pkgName := os.Getenv("GOPACKAGE")
if pkgName == "" {
pkgName = pkgInfo.Name
}
data := map[string]interface{}{
"pkg": pkgName,
"types": types,
}
t, err := template.New("").Parse(strTmp)
if err != nil {
log.Fatal(err)
}
buff := bytes.NewBufferString("")
err = t.Execute(buff, data)
if err != nil {
log.Fatal(err)
}
//格式化
src, err := format.Source(buff.Bytes())
if err != nil {
log.Fatal(err)
}
return src
}
func trimGopath(s string) string {
gopath := os.Getenv("GOPATH")
// pr("gopath = %q", gopath)
for _, p := range strings.Split(gopath, ":") {
if strings.HasPrefix(s, p) {
v := s[len(p)+1:]
if strings.HasPrefix(v, "src/") {
v = v[4:]
}
return v
}
}
if strings.Contains(s, "src/github.com") {
idx := strings.Index(s, "src/github.com")
return s[idx+4:]
}
return s
}