diff --git a/parser.go b/parser.go index 24f7549b..3bf3cf6b 100644 --- a/parser.go +++ b/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) + } +} diff --git a/validation/validation.go b/validation/validation.go index 2b020aa8..489dfa5e 100644 --- a/validation/validation.go +++ b/validation/validation.go @@ -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 {