diff --git a/cmd/commands/generate/generate.go b/cmd/commands/generate/generate.go index 993ff3f..e427e76 100644 --- a/cmd/commands/generate/generate.go +++ b/cmd/commands/generate/generate.go @@ -81,14 +81,14 @@ func GenerateCode(cmd *commands.Command, args []string) int { beeLogger.Log.Fatal("Command is missing") } - gps := utils.GetGOPATHs() - if len(gps) == 0 { - beeLogger.Log.Fatal("GOPATH environment variable is not set or empty") - } - - gopath := gps[0] - - beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath) + //gps := utils.GetGOPATHs() + //if len(gps) == 0 { + // beeLogger.Log.Fatal("GOPATH environment variable is not set or empty") + //} + // + //gopath := gps[0] + // + //beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath) gcmd := args[0] switch gcmd { diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index 512e01c..075c1b2 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -32,11 +32,11 @@ import ( "strings" "unicode" - yaml "gopkg.in/yaml.v2" + "gopkg.in/yaml.v2" "github.com/astaxie/beego/swagger" "github.com/astaxie/beego/utils" - beeLogger "github.com/beego/bee/logger" + "github.com/beego/bee/logger" bu "github.com/beego/bee/utils" ) @@ -159,6 +159,7 @@ func parsePackageFromDir(path string) error { func GenerateDocs(curpath string) { fset := token.NewFileSet() + beeLogger.Log.Infof("curpath: %s", curpath) 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,6 +264,7 @@ func GenerateDocs(curpath string) { if im.Name != nil { localName = im.Name.Name } + beeLogger.Log.Infof("localName: %s, %v", localName, im.Path.Value) analyseControllerPkg(path.Join(curpath, "vendor"), localName, im.Path.Value) } for _, d := range f.Decls { @@ -432,25 +434,33 @@ 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 + if os.Getenv(`GO111MODULE`) == `on` { + pkgRealpath = filepath.Join(bu.GetBeeWorkPath(), "..", pkgpath) } else { - wgopath := gopaths - for _, wg := range wgopath { - wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", pkgpath)) - if utils.FileExists(wg) { - pkgRealpath = wg - break + gopaths := bu.GetGOPATHs() + if len(gopaths) == 0 { + beeLogger.Log.Fatal("GOPATH environment variable is not set or empty") + } + //首先去vendor目录里面去找源码 + 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 _, ok := pkgCache[pkgpath]; ok { return @@ -468,6 +478,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 { @@ -802,7 +813,7 @@ func setParamType(para *swagger.Parameter, typ string, pkgpath, controllerName s paraFormat = typeFormat[1] if para.In == "body" { para.Schema = &swagger.Schema{ - Type: paraType, + Type: paraType, Format: paraFormat, } } diff --git a/utils/utils.go b/utils/utils.go index 6aa2034..23017df 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -41,6 +41,14 @@ func GetBeeWorkPath() string { 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 // and returns a channel which will later return the function's return value. func Go(f func() error) chan error {