mirror of
https://github.com/beego/bee.git
synced 2025-06-11 18:40:40 +00:00
Merge branch 'develop' into feature/analyse-import-pkg
This commit is contained in:
@ -19,6 +19,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/build"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
@ -34,10 +35,9 @@ import (
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/astaxie/beego/swagger"
|
||||
"github.com/astaxie/beego/utils"
|
||||
beeLogger "github.com/beego/bee/logger"
|
||||
bu "github.com/beego/bee/utils"
|
||||
beeLogger "github.com/beego/bee/v2/logger"
|
||||
"github.com/beego/beego/v2/core/utils"
|
||||
"github.com/beego/beego/v2/server/web/swagger"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -102,8 +102,8 @@ func init() {
|
||||
astPkgs = make([]*ast.Package, 0)
|
||||
}
|
||||
|
||||
// ParsePackagesFromDir parses packages from a given directory
|
||||
func ParsePackagesFromDir(dirpath string) {
|
||||
// parsePackagesFromDir parses packages from a given directory
|
||||
func parsePackagesFromDir(dirpath string) {
|
||||
c := make(chan error)
|
||||
|
||||
go func() {
|
||||
@ -157,8 +157,14 @@ func parsePackageFromDir(path string) error {
|
||||
|
||||
// GenerateDocs generates documentations for a given path.
|
||||
func GenerateDocs(curpath string) {
|
||||
fset := token.NewFileSet()
|
||||
pkgspath := curpath
|
||||
workspace := os.Getenv("BeeWorkspace")
|
||||
if workspace != "" {
|
||||
pkgspath = workspace
|
||||
}
|
||||
parsePackagesFromDir(pkgspath)
|
||||
|
||||
fset := token.NewFileSet()
|
||||
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)
|
||||
@ -263,7 +269,7 @@ func GenerateDocs(curpath string) {
|
||||
if im.Name != nil {
|
||||
localName = im.Name.Name
|
||||
}
|
||||
analyseControllerPkg(path.Join(curpath, "vendor"), localName, im.Path.Value)
|
||||
analyseControllerPkg(localName, im.Path.Value)
|
||||
}
|
||||
for _, d := range f.Decls {
|
||||
switch specDecl := d.(type) {
|
||||
@ -418,12 +424,12 @@ func analyseNSInclude(baseurl string, ce *ast.CallExpr) string {
|
||||
return cname
|
||||
}
|
||||
|
||||
func analyseControllerPkg(vendorPath, localName, pkgpath string) {
|
||||
func analyseControllerPkg(localName, pkgpath string) {
|
||||
pkgpath = strings.Trim(pkgpath, "\"")
|
||||
if isSystemPackage(pkgpath) {
|
||||
return
|
||||
}
|
||||
if pkgpath == "github.com/astaxie/beego" {
|
||||
if pkgpath == "github.com/beego/beego/v2/server/web" {
|
||||
return
|
||||
}
|
||||
if localName != "" {
|
||||
@ -432,32 +438,19 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) {
|
||||
pps := strings.Split(pkgpath, "/")
|
||||
importlist[pps[len(pps)-1]] = pkgpath
|
||||
}
|
||||
gopaths := bu.GetGOPATHs()
|
||||
if len(gopaths) == 0 {
|
||||
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
|
||||
}
|
||||
pkgRealpath := ""
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
pkg, err := build.Default.Import(pkgpath, ".", build.FindOnly)
|
||||
if err != nil {
|
||||
beeLogger.Log.Fatalf("Package %s cannot be imported: %v", pkgpath, err)
|
||||
}
|
||||
pkgRealpath := pkg.Dir
|
||||
if pkgRealpath != "" {
|
||||
if _, ok := pkgCache[pkgpath]; ok {
|
||||
return
|
||||
}
|
||||
pkgCache[pkgpath] = localName
|
||||
} 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()
|
||||
@ -468,6 +461,7 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) {
|
||||
if err != nil {
|
||||
beeLogger.Log.Fatalf("Error while parsing dir at '%s': %s", pkgpath, err)
|
||||
}
|
||||
|
||||
for _, pkg := range astPkgs {
|
||||
for _, fl := range pkg.Files {
|
||||
for _, d := range fl.Decls {
|
||||
@ -730,7 +724,7 @@ func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
routerPath = urlReplace(routerPath)
|
||||
if routerPath != "" {
|
||||
//Go over function parameters which were not mapped and create swagger params for them
|
||||
for name, typ := range funcParamMap {
|
||||
@ -1213,8 +1207,8 @@ func parseStruct(ims []*ast.ImportSpec, st *ast.StructType, k string, m *swagger
|
||||
for _, pkg := range astPkgs {
|
||||
for _, fl := range pkg.Files {
|
||||
for nameOfObj, obj := range fl.Scope.Objects {
|
||||
if obj.Name == fmt.Sprint(field.Type) {
|
||||
parseObject(fl.Imports, obj, nameOfObj, nm, realTypes, pkg.Name)
|
||||
if pkg.Name+"."+obj.Name == realType {
|
||||
parseObject(obj, nameOfObj, nm, realTypes, astPkgs, pkg.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user