diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index b68986d..a9f1c0a 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -36,6 +36,7 @@ import ( "github.com/astaxie/beego/swagger" "github.com/astaxie/beego/utils" beeLogger "github.com/beego/bee/logger" + bu "github.com/beego/bee/utils" ) const ( @@ -141,7 +142,7 @@ func parsePackageFromDir(path string) error { func GenerateDocs(curpath string) { fset := token.NewFileSet() - f, err := parser.ParseFile(fset, path.Join(curpath, "routers", "router.go"), nil, parser.ParseComments) + f, err := parser.ParseFile(fset, filepath.Join(curpath, "routers", "router.go"), nil, parser.ParseComments) if err != nil { beeLogger.Log.Fatalf("Error while parsing router.go: %s", err) } @@ -350,8 +351,8 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) { pps := strings.Split(pkgpath, "/") importlist[pps[len(pps)-1]] = pkgpath } - gopath := os.Getenv("GOPATH") - if gopath == "" { + gopaths := bu.GetGOPATHs() + if len(gopaths) == 0 { beeLogger.Log.Fatal("GOPATH environment variable is not set or empty") } pkgRealpath := "" @@ -360,7 +361,7 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) { if utils.FileExists(wg) { pkgRealpath = wg } else { - wgopath := filepath.SplitList(gopath) + wgopath := gopaths for _, wg := range wgopath { wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", pkgpath)) if utils.FileExists(wg) { diff --git a/utils/utils.go b/utils/utils.go index e1fd20d..60b3d8a 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -52,6 +52,9 @@ func IsExist(path string) bool { // GetGOPATHs returns all paths in GOPATH variable. func GetGOPATHs() []string { gopath := os.Getenv("GOPATH") + if gopath == "" && strings.Compare(runtime.Version(), "go1.8") >= 0 { + gopath = defaultGOPATH() + } return filepath.SplitList(gopath) } @@ -61,7 +64,7 @@ func IsInGOPATH(thePath string) bool { thePath = filepath.ToSlash(thePath) } for _, gopath := range GetGOPATHs() { - if strings.Contains(thePath, gopath+"/src") { + if strings.Contains(thePath, filepath.Join(gopath, "src")) { return true } } @@ -425,3 +428,16 @@ func GetFileModTime(path string) int64 { return fi.ModTime().Unix() } + +func defaultGOPATH() string { + env := "HOME" + if runtime.GOOS == "windows" { + env = "USERPROFILE" + } else if runtime.GOOS == "plan9" { + env = "home" + } + if home := os.Getenv(env); home != "" { + return filepath.Join(home, "go") + } + return "" +}