2014-06-08 12:24:01 +00:00
|
|
|
// Beego (http://beego.me/)
|
2014-06-25 02:39:37 +00:00
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
2014-06-25 02:39:37 +00:00
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
2014-06-25 02:39:37 +00:00
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
2014-06-25 02:39:37 +00:00
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
// @authors astaxie
|
2014-06-25 02:39:37 +00:00
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
package beego
|
2014-06-09 02:11:37 +00:00
|
|
|
|
|
|
|
import (
|
2014-06-10 09:11:02 +00:00
|
|
|
"encoding/json"
|
2014-06-09 09:33:04 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2014-06-10 09:11:02 +00:00
|
|
|
"io/ioutil"
|
2014-06-09 02:11:37 +00:00
|
|
|
"os"
|
2014-06-09 09:33:04 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
2014-06-10 09:11:02 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/utils"
|
2014-06-09 02:11:37 +00:00
|
|
|
)
|
|
|
|
|
2014-06-09 09:33:04 +00:00
|
|
|
var globalRouterTemplate = `package routers
|
2014-06-09 02:11:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
{{.globalinfo}}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-06-10 09:11:02 +00:00
|
|
|
var (
|
|
|
|
lastupdateFilename string = "lastupdate.tmp"
|
|
|
|
pkgLastupdate map[string]int64
|
|
|
|
genInfoList map[string][]ControllerComments
|
|
|
|
)
|
2014-06-09 02:11:37 +00:00
|
|
|
|
2014-06-09 09:33:04 +00:00
|
|
|
func init() {
|
2014-06-10 09:11:02 +00:00
|
|
|
pkgLastupdate = make(map[string]int64)
|
2014-06-09 09:33:04 +00:00
|
|
|
genInfoList = make(map[string][]ControllerComments)
|
|
|
|
}
|
2014-06-09 02:11:37 +00:00
|
|
|
|
2014-06-09 09:33:04 +00:00
|
|
|
func parserPkg(pkgRealpath, pkgpath string) error {
|
2014-06-10 09:11:02 +00:00
|
|
|
if !compareFile(pkgRealpath) {
|
|
|
|
Info(pkgRealpath + " don't has updated")
|
|
|
|
return nil
|
|
|
|
}
|
2014-06-09 09:33:04 +00:00
|
|
|
fileSet := token.NewFileSet()
|
|
|
|
astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
|
|
|
|
name := info.Name()
|
|
|
|
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
|
|
|
|
}, parser.ParseComments)
|
2014-06-09 02:11:37 +00:00
|
|
|
|
2014-06-09 09:33:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, pkg := range astPkgs {
|
|
|
|
for _, fl := range pkg.Files {
|
|
|
|
for _, d := range fl.Decls {
|
|
|
|
switch specDecl := d.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
parserComments(specDecl.Doc, specDecl.Name.String(), fmt.Sprint(specDecl.Recv.List[0].Type.(*ast.StarExpr).X), pkgpath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
genRouterCode()
|
2014-06-10 09:11:02 +00:00
|
|
|
savetoFile(pkgRealpath)
|
2014-06-09 09:33:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpath string) error {
|
|
|
|
if comments != nil && comments.List != nil {
|
|
|
|
for _, c := range comments.List {
|
|
|
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
|
|
|
if strings.HasPrefix(t, "@router") {
|
|
|
|
elements := strings.TrimLeft(t, "@router ")
|
|
|
|
e1 := strings.SplitN(elements, " ", 2)
|
|
|
|
if len(e1) < 1 {
|
|
|
|
return errors.New("you should has router infomation")
|
|
|
|
}
|
|
|
|
key := pkgpath + ":" + controllerName
|
|
|
|
cc := ControllerComments{}
|
2014-06-09 09:46:13 +00:00
|
|
|
cc.Method = funcName
|
|
|
|
cc.Router = e1[0]
|
2014-06-09 09:33:04 +00:00
|
|
|
if len(e1) == 2 && e1[1] != "" {
|
|
|
|
e1 = strings.SplitN(e1[1], " ", 2)
|
|
|
|
if len(e1) >= 1 {
|
2014-06-09 09:46:13 +00:00
|
|
|
cc.AllowHTTPMethods = strings.Split(strings.Trim(e1[0], "[]"), ",")
|
2014-06-09 09:33:04 +00:00
|
|
|
} else {
|
2014-06-09 09:46:13 +00:00
|
|
|
cc.AllowHTTPMethods = append(cc.AllowHTTPMethods, "get")
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-06-09 09:46:13 +00:00
|
|
|
cc.AllowHTTPMethods = append(cc.AllowHTTPMethods, "get")
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
|
|
|
if len(e1) == 2 && e1[1] != "" {
|
|
|
|
keyval := strings.Split(strings.Trim(e1[1], "[]"), " ")
|
|
|
|
for _, kv := range keyval {
|
|
|
|
kk := strings.Split(kv, ":")
|
2014-06-09 09:46:13 +00:00
|
|
|
cc.Params = append(cc.Params, map[string]string{strings.Join(kk[:len(kk)-1], ":"): kk[len(kk)-1]})
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
genInfoList[key] = append(genInfoList[key], cc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func genRouterCode() {
|
|
|
|
os.Mkdir(path.Join(AppPath, "routers"), 0755)
|
|
|
|
Info("generate router from comments")
|
|
|
|
var globalinfo string
|
|
|
|
for k, cList := range genInfoList {
|
|
|
|
for _, c := range cList {
|
|
|
|
allmethod := "nil"
|
2014-06-09 09:46:13 +00:00
|
|
|
if len(c.AllowHTTPMethods) > 0 {
|
2014-06-09 09:33:04 +00:00
|
|
|
allmethod = "[]string{"
|
2014-06-09 09:46:13 +00:00
|
|
|
for _, m := range c.AllowHTTPMethods {
|
2014-06-09 09:33:04 +00:00
|
|
|
allmethod += `"` + m + `",`
|
|
|
|
}
|
|
|
|
allmethod = strings.TrimRight(allmethod, ",") + "}"
|
|
|
|
}
|
|
|
|
params := "nil"
|
2014-06-09 09:46:13 +00:00
|
|
|
if len(c.Params) > 0 {
|
2014-06-09 09:33:04 +00:00
|
|
|
params = "[]map[string]string{"
|
2014-06-09 09:46:13 +00:00
|
|
|
for _, p := range c.Params {
|
2014-06-09 09:33:04 +00:00
|
|
|
for k, v := range p {
|
|
|
|
params = params + `map[string]string{` + k + `:"` + v + `"},`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
params = strings.TrimRight(params, ",") + "}"
|
|
|
|
}
|
2014-06-10 09:11:02 +00:00
|
|
|
globalinfo = globalinfo + `
|
|
|
|
beego.GlobalControllerRouter["` + k + `"] = append(beego.GlobalControllerRouter["` + k + `"],
|
|
|
|
beego.ControllerComments{
|
|
|
|
"` + strings.TrimSpace(c.Method) + `",
|
|
|
|
"` + c.Router + `",
|
|
|
|
` + allmethod + `,
|
|
|
|
` + params + `})
|
|
|
|
`
|
2014-06-10 03:02:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if globalinfo != "" {
|
|
|
|
f, err := os.Create(path.Join(AppPath, "routers", "commentsRouter.go"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
2014-06-10 03:02:41 +00:00
|
|
|
defer f.Close()
|
|
|
|
f.WriteString(strings.Replace(globalRouterTemplate, "{{.globalinfo}}", globalinfo, -1))
|
2014-06-09 09:33:04 +00:00
|
|
|
}
|
2014-06-09 02:11:37 +00:00
|
|
|
}
|
2014-06-10 09:11:02 +00:00
|
|
|
|
|
|
|
func compareFile(pkgRealpath string) bool {
|
|
|
|
if utils.FileExists(path.Join(AppPath, lastupdateFilename)) {
|
|
|
|
content, err := ioutil.ReadFile(path.Join(AppPath, lastupdateFilename))
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
json.Unmarshal(content, &pkgLastupdate)
|
|
|
|
ft, err := os.Lstat(pkgRealpath)
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if v, ok := pkgLastupdate[pkgRealpath]; ok {
|
2014-06-23 07:52:30 +00:00
|
|
|
if ft.ModTime().UnixNano() <= v {
|
2014-06-10 09:11:02 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func savetoFile(pkgRealpath string) {
|
|
|
|
ft, err := os.Lstat(pkgRealpath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pkgLastupdate[pkgRealpath] = ft.ModTime().UnixNano()
|
|
|
|
d, err := json.Marshal(pkgLastupdate)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ioutil.WriteFile(path.Join(AppPath, lastupdateFilename), d, os.ModePerm)
|
|
|
|
}
|