mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 12:30:54 +00:00
commit
dae2a36eb5
33
parser.go
33
parser.go
@ -23,7 +23,7 @@ import (
|
||||
"go/token"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@ -56,8 +56,9 @@ func init() {
|
||||
}
|
||||
|
||||
func parserPkg(pkgRealpath, pkgpath string) error {
|
||||
rep := strings.NewReplacer("/", "_", ".", "_")
|
||||
commentFilename = coomentPrefix + rep.Replace(pkgpath) + ".go"
|
||||
rep := strings.NewReplacer("\\", "_", "/", "_", ".", "_")
|
||||
commentFilename, _ = filepath.Rel(AppPath, pkgRealpath)
|
||||
commentFilename = coomentPrefix + rep.Replace(commentFilename) + ".go"
|
||||
if !compareFile(pkgRealpath) {
|
||||
logs.Info(pkgRealpath + " no changed")
|
||||
return nil
|
||||
@ -87,7 +88,7 @@ func parserPkg(pkgRealpath, pkgpath string) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
genRouterCode()
|
||||
genRouterCode(pkgRealpath)
|
||||
savetoFile(pkgRealpath)
|
||||
return nil
|
||||
}
|
||||
@ -130,8 +131,8 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat
|
||||
return nil
|
||||
}
|
||||
|
||||
func genRouterCode() {
|
||||
os.Mkdir(path.Join(AppPath, "routers"), 0755)
|
||||
func genRouterCode(pkgRealpath string) {
|
||||
os.Mkdir(getRouterDir(pkgRealpath), 0755)
|
||||
logs.Info("generate router from comments")
|
||||
var (
|
||||
globalinfo string
|
||||
@ -173,7 +174,7 @@ func genRouterCode() {
|
||||
}
|
||||
}
|
||||
if globalinfo != "" {
|
||||
f, err := os.Create(path.Join(AppPath, "routers", commentFilename))
|
||||
f, err := os.Create(filepath.Join(getRouterDir(pkgRealpath), commentFilename))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -183,7 +184,7 @@ func genRouterCode() {
|
||||
}
|
||||
|
||||
func compareFile(pkgRealpath string) bool {
|
||||
if !utils.FileExists(path.Join(AppPath, "routers", commentFilename)) {
|
||||
if !utils.FileExists(filepath.Join(getRouterDir(pkgRealpath), commentFilename)) {
|
||||
return true
|
||||
}
|
||||
if utils.FileExists(lastupdateFilename) {
|
||||
@ -230,3 +231,19 @@ func getpathTime(pkgRealpath string) (lastupdate int64, err error) {
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -73,6 +73,10 @@ func (e *Error) String() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
// Implement Error interface.
|
||||
// Return e.String()
|
||||
func (e *Error) Error() string { return e.String() }
|
||||
|
||||
// Result is returned from every validation method.
|
||||
// It provides an indication of success, and a pointer to the Error (if any).
|
||||
type Result struct {
|
||||
|
Loading…
Reference in New Issue
Block a user