bee genenrate

This commit is contained in:
qiantao 2020-05-24 12:59:02 +08:00
parent 8360965cc7
commit 92de521f36
3 changed files with 43 additions and 24 deletions

View File

@ -81,14 +81,14 @@ func GenerateCode(cmd *commands.Command, args []string) int {
beeLogger.Log.Fatal("Command is missing") beeLogger.Log.Fatal("Command is missing")
} }
gps := utils.GetGOPATHs() //gps := utils.GetGOPATHs()
if len(gps) == 0 { //if len(gps) == 0 {
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty") // beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
} //}
//
gopath := gps[0] //gopath := gps[0]
//
beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath) //beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath)
gcmd := args[0] gcmd := args[0]
switch gcmd { switch gcmd {

View File

@ -32,11 +32,11 @@ import (
"strings" "strings"
"unicode" "unicode"
yaml "gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"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" "github.com/beego/bee/logger"
bu "github.com/beego/bee/utils" bu "github.com/beego/bee/utils"
) )
@ -159,6 +159,7 @@ func parsePackageFromDir(path string) error {
func GenerateDocs(curpath string) { func GenerateDocs(curpath string) {
fset := token.NewFileSet() fset := token.NewFileSet()
beeLogger.Log.Infof("curpath: %s", curpath)
f, err := parser.ParseFile(fset, filepath.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 { if err != nil {
beeLogger.Log.Fatalf("Error while parsing router.go: %s", err) beeLogger.Log.Fatalf("Error while parsing router.go: %s", err)
@ -263,6 +264,7 @@ func GenerateDocs(curpath string) {
if im.Name != nil { if im.Name != nil {
localName = im.Name.Name localName = im.Name.Name
} }
beeLogger.Log.Infof("localName: %s, %v", localName, im.Path.Value)
analyseControllerPkg(path.Join(curpath, "vendor"), localName, im.Path.Value) analyseControllerPkg(path.Join(curpath, "vendor"), localName, im.Path.Value)
} }
for _, d := range f.Decls { for _, d := range f.Decls {
@ -432,25 +434,33 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) {
pps := strings.Split(pkgpath, "/") pps := strings.Split(pkgpath, "/")
importlist[pps[len(pps)-1]] = 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 := "" pkgRealpath := ""
wg, _ := filepath.EvalSymlinks(filepath.Join(vendorPath, pkgpath)) if os.Getenv(`GO111MODULE`) == `on` {
if utils.FileExists(wg) { pkgRealpath = filepath.Join(bu.GetBeeWorkPath(), "..", pkgpath)
pkgRealpath = wg
} else { } else {
wgopath := gopaths gopaths := bu.GetGOPATHs()
for _, wg := range wgopath { if len(gopaths) == 0 {
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", pkgpath)) beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
if utils.FileExists(wg) { }
pkgRealpath = wg //首先去vendor目录里面去找源码
break wg, _ := filepath.EvalSymlinks(filepath.Join(vendorPath, pkgpath))
if utils.FileExists(wg) {
pkgRealpath = wg
} else {
wgopath := gopaths
// 然后去gopath下面去找代码
for _, wg := range wgopath {
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", pkgpath))
if utils.FileExists(wg) {
pkgRealpath = wg
break
}
} }
} }
} }
beeLogger.Log.Infof("pkgRealpath:%v", pkgRealpath)
if pkgRealpath != "" { if pkgRealpath != "" {
if _, ok := pkgCache[pkgpath]; ok { if _, ok := pkgCache[pkgpath]; ok {
return return
@ -468,6 +478,7 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) {
if err != nil { if err != nil {
beeLogger.Log.Fatalf("Error while parsing dir at '%s': %s", pkgpath, err) beeLogger.Log.Fatalf("Error while parsing dir at '%s': %s", pkgpath, err)
} }
for _, pkg := range astPkgs { for _, pkg := range astPkgs {
for _, fl := range pkg.Files { for _, fl := range pkg.Files {
for _, d := range fl.Decls { for _, d := range fl.Decls {
@ -802,7 +813,7 @@ func setParamType(para *swagger.Parameter, typ string, pkgpath, controllerName s
paraFormat = typeFormat[1] paraFormat = typeFormat[1]
if para.In == "body" { if para.In == "body" {
para.Schema = &swagger.Schema{ para.Schema = &swagger.Schema{
Type: paraType, Type: paraType,
Format: paraFormat, Format: paraFormat,
} }
} }

View File

@ -41,6 +41,14 @@ func GetBeeWorkPath() string {
return beePath return beePath
} }
func GetBeeWorkPath() string {
beePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
return beePath
}
// Go is a basic promise implementation: it wraps calls a function in a goroutine // Go is a basic promise implementation: it wraps calls a function in a goroutine
// and returns a channel which will later return the function's return value. // and returns a channel which will later return the function's return value.
func Go(f func() error) chan error { func Go(f func() error) chan error {