1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 17:50:58 +00:00

Merge pull request #1924 from ysqi/develop

implement some features
This commit is contained in:
astaxie 2016-05-06 13:58:29 +08:00
commit dae2a36eb5
2 changed files with 29 additions and 8 deletions

View File

@ -23,7 +23,7 @@ import (
"go/token" "go/token"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path/filepath"
"sort" "sort"
"strings" "strings"
@ -56,8 +56,9 @@ func init() {
} }
func parserPkg(pkgRealpath, pkgpath string) error { func parserPkg(pkgRealpath, pkgpath string) error {
rep := strings.NewReplacer("/", "_", ".", "_") rep := strings.NewReplacer("\\", "_", "/", "_", ".", "_")
commentFilename = coomentPrefix + rep.Replace(pkgpath) + ".go" commentFilename, _ = filepath.Rel(AppPath, pkgRealpath)
commentFilename = coomentPrefix + rep.Replace(commentFilename) + ".go"
if !compareFile(pkgRealpath) { if !compareFile(pkgRealpath) {
logs.Info(pkgRealpath + " no changed") logs.Info(pkgRealpath + " no changed")
return nil return nil
@ -87,7 +88,7 @@ func parserPkg(pkgRealpath, pkgpath string) error {
} }
} }
} }
genRouterCode() genRouterCode(pkgRealpath)
savetoFile(pkgRealpath) savetoFile(pkgRealpath)
return nil return nil
} }
@ -130,8 +131,8 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat
return nil return nil
} }
func genRouterCode() { func genRouterCode(pkgRealpath string) {
os.Mkdir(path.Join(AppPath, "routers"), 0755) os.Mkdir(getRouterDir(pkgRealpath), 0755)
logs.Info("generate router from comments") logs.Info("generate router from comments")
var ( var (
globalinfo string globalinfo string
@ -173,7 +174,7 @@ func genRouterCode() {
} }
} }
if globalinfo != "" { if globalinfo != "" {
f, err := os.Create(path.Join(AppPath, "routers", commentFilename)) f, err := os.Create(filepath.Join(getRouterDir(pkgRealpath), commentFilename))
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -183,7 +184,7 @@ func genRouterCode() {
} }
func compareFile(pkgRealpath string) bool { func compareFile(pkgRealpath string) bool {
if !utils.FileExists(path.Join(AppPath, "routers", commentFilename)) { if !utils.FileExists(filepath.Join(getRouterDir(pkgRealpath), commentFilename)) {
return true return true
} }
if utils.FileExists(lastupdateFilename) { if utils.FileExists(lastupdateFilename) {
@ -230,3 +231,19 @@ func getpathTime(pkgRealpath string) (lastupdate int64, err error) {
} }
return lastupdate, nil return lastupdate, nil
} }
func getRouterDir(pkgRealpath string) string {
dir := filepath.Dir(pkgRealpath)
for {
d := filepath.Join(dir, "routers")
if utils.FileExists(d) {
return d
}
if r, _ := filepath.Rel(dir, AppPath); r == "." {
return d
}
// Parent dir.
dir = filepath.Dir(dir)
}
}

View File

@ -73,6 +73,10 @@ func (e *Error) String() string {
return e.Message return e.Message
} }
// Implement Error interface.
// Return e.String()
func (e *Error) Error() string { return e.String() }
// Result is returned from every validation method. // Result is returned from every validation method.
// It provides an indication of success, and a pointer to the Error (if any). // It provides an indication of success, and a pointer to the Error (if any).
type Result struct { type Result struct {