1
0
mirror of https://github.com/beego/bee.git synced 2024-11-21 18:40:54 +00:00

Use go/build to resolve package path for bee generate docs

This commit is contained in:
Hanjiang Yu 2020-07-26 10:15:26 +08:00 committed by askuy
parent 6a2f44720e
commit e0ada8860d

View File

@ -19,6 +19,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"go/ast" "go/ast"
"go/build"
"go/parser" "go/parser"
"go/token" "go/token"
"os" "os"
@ -37,7 +38,6 @@ import (
"github.com/astaxie/beego/swagger" "github.com/astaxie/beego/swagger"
"github.com/astaxie/beego/utils" "github.com/astaxie/beego/utils"
beeLogger "github.com/beego/bee/logger" beeLogger "github.com/beego/bee/logger"
bu "github.com/beego/bee/utils"
) )
const ( const (
@ -263,7 +263,7 @@ func GenerateDocs(curpath string) {
if im.Name != nil { if im.Name != nil {
localName = im.Name.Name localName = im.Name.Name
} }
analyseControllerPkg(path.Join(curpath, "vendor"), localName, im.Path.Value) analyseControllerPkg(localName, im.Path.Value)
} }
for _, d := range f.Decls { for _, d := range f.Decls {
switch specDecl := d.(type) { switch specDecl := d.(type) {
@ -418,7 +418,7 @@ func analyseNSInclude(baseurl string, ce *ast.CallExpr) string {
return cname return cname
} }
func analyseControllerPkg(vendorPath, localName, pkgpath string) { func analyseControllerPkg(localName, pkgpath string) {
pkgpath = strings.Trim(pkgpath, "\"") pkgpath = strings.Trim(pkgpath, "\"")
if isSystemPackage(pkgpath) { if isSystemPackage(pkgpath) {
return return
@ -433,36 +433,18 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) {
importlist[pps[len(pps)-1]] = pkgpath importlist[pps[len(pps)-1]] = pkgpath
} }
pkgRealpath := "" pkg, err := build.Default.Import(pkgpath, ".", build.FindOnly)
if err != nil {
if bu.IsGOMODULE() { beeLogger.Log.Fatalf("Package %s cannot be imported: %v", pkgpath, err)
pkgRealpath = filepath.Join(bu.GetBeeWorkPath(), "..", pkgpath)
} else {
gopaths := bu.GetGOPATHs()
if len(gopaths) == 0 {
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
}
wg, _ := filepath.EvalSymlinks(filepath.Join(vendorPath, pkgpath))
if utils.FileExists(wg) {
pkgRealpath = wg
} else {
wgopath := gopaths
for _, wg := range wgopath {
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", pkgpath))
if utils.FileExists(wg) {
pkgRealpath = wg
break
}
}
}
} }
pkgRealpath := pkg.Dir
if pkgRealpath != "" { if pkgRealpath != "" {
if _, ok := pkgCache[pkgpath]; ok { if _, ok := pkgCache[pkgpath]; ok {
return return
} }
pkgCache[pkgpath] = struct{}{} pkgCache[pkgpath] = struct{}{}
} else { } else {
beeLogger.Log.Fatalf("Package '%s' does not exist in the GOPATH or vendor path", pkgpath) beeLogger.Log.Fatalf("Package '%s' does not have source directory", pkgpath)
} }
fileSet := token.NewFileSet() fileSet := token.NewFileSet()