-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgene.go
222 lines (197 loc) · 4.69 KB
/
gene.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"encoding/json"
"go/build"
"go/parser"
"go/token"
"io/ioutil"
"log"
"path/filepath"
"strings"
g "github.com/shaomingquan/catalyst/gene"
)
var gene commandHandler
func init() {
gene = func(command string, params []string) {
routers, midwares, pkgs, ps, decorators, decoraorPkgs := getRoutersAndMidwares()
pkgs = mergePkgs(pkgs, decoraorPkgs)
// 1, update boot file
updateBootFile()
// 2, generate import file
makeImportFile(routers, midwares, pkgs, ps, decorators)
}
handlers["gene"] = gene
}
var (
pkgInfo *build.Package
)
func initBootFile() string {
conf := getConf()
rd := conf.AppRoot
data := map[string]interface{}{
"rootDir": rd,
}
content := g.MakeBootFile(data)
return string(content)
}
func updateBootFile() {
bootContentBt, err := ioutil.ReadFile(bootFileStr(rootRelatedString()))
if err != nil {
log.Fatal(err)
}
bootContent := string(bootContentBt)
if bootContent == "" {
bootContent = initBootFile()
}
pkgDir := getPkgDir()
prefix := pkgDir[5:]
if prefix == "" {
prefix = "/"
}
appid := strings.Replace(prefix, "/", "_", -1)
nextStr := `
imports.Start` + appid + `(&app)
// ###
`
bootContent = strings.Replace(bootContent, "// ###", nextStr, -1)
ioutil.WriteFile(bootFileStr(rootRelatedString()), []byte(bootContent), 0644)
}
func mergePkgs(midwaresPkgs []map[string]string, decoratorsPkgs [][]map[string]string) []map[string]string {
ret := midwaresPkgs
checker := map[string]bool{}
for _, pkg := range midwaresPkgs {
checker[pkg["pkgid"]] = true
}
for _, pkgs := range decoratorsPkgs {
for _, pkg := range pkgs {
if !checker[pkg["pkgid"]] {
ret = append(ret, pkg)
checker[pkg["pkgid"]] = true
}
}
}
return ret
}
func makeImportFile(
routers []string,
midwares []map[string]string,
pkgs []map[string]string,
params map[string][][]string,
decorators map[string][]map[string]string,
) {
conf := getConf()
rd := conf.AppRoot
appName := pkgInfo.Name
pkgDir := getPkgDir()
prefix := pkgDir[5:]
if prefix == "" {
prefix = "/"
}
appid := strings.Replace(prefix, "/", "_", -1)
filename := "app" + appid
outputDir := importFileStr(rootRelatedString(), filename)
routersWithValidator := []string{}
routersNormal := []string{}
for _, router := range routers {
if _, ok := params[router]; ok {
routersWithValidator = append(routersWithValidator, router)
} else {
routersNormal = append(routersNormal, router)
}
}
hasvalidator := len(routersWithValidator) > 0
data := map[string]interface{}{
"routers": routersNormal,
"routersWithValidator": routersWithValidator,
"hasvalidator": hasvalidator,
"appName": appName,
"pkgDir": pkgDir, // subapi
"prefix": prefix,
"appid": appid,
"midwares": midwares,
"decorators": decorators,
"pkgs": pkgs,
"rootDir": rd,
"params": params,
}
wirteMetaFile(data) // write meta file for debug GUI
content := g.MakeImporterFile(data)
ioutil.WriteFile(outputDir, content, 0644)
}
func wirteMetaFile(data map[string]interface{}) {
filename := ".pkg_meta"
datastring, err := json.Marshal(data)
if err != nil {
log.Fatal("json format wrong when trying to write meta data")
}
ioutil.WriteFile(filename, []byte(datastring), 0644)
}
func getRoutersAndMidwares() (
[]string,
[]map[string]string,
[]map[string]string,
map[string][][]string,
map[string][]map[string]string,
[][]map[string]string,
) {
var err error
pkgInfo, err = build.ImportDir(".", 0)
if err != nil {
log.Fatal(err)
}
fset := token.NewFileSet()
gintance := g.Gene{}
for _, file := range pkgInfo.GoFiles {
if !g.IsUnderscoreFile(file) {
continue
}
f, err := parser.ParseFile(fset, file, nil, 0)
if err != nil {
log.Fatal(err)
}
gintance.Collect(f)
}
a := gintance.OutputRouters()
b, c := gintance.OutputMidwares()
d := gintance.OutputParams()
e, f := gintance.OutputDecorator()
return a, b, c, d, e, f
}
var root string
func rootRelatedString() string {
if root != "" {
return root
}
_root := "../"
level := 1
for {
_, err := ioutil.ReadFile(_root + "appconf.json") // appconf is required
if level > 4 {
log.Fatal("router too deep")
}
if err != nil {
_root += "../"
level++
continue
}
break
}
root = _root
return _root
}
func getPkgDir() string {
absCurrent, _ := filepath.Abs(".")
absRoot, _ := filepath.Abs(root)
l := len(absRoot)
if len(absCurrent) < l {
log.Fatal("bad package dir")
}
ret := absCurrent[l:]
return ret
}
func bootFileStr(root string) string {
return root + "boot.go"
}
func importFileStr(root string, pkgname string) string {
return root + "imports/" + pkgname + ".go"
}