Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Go modules support for swagger document generation #699

Merged
merged 3 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 17 additions & 29 deletions generate/swaggergen/g_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"os"
Expand All @@ -37,7 +38,6 @@ import (
"github.com/astaxie/beego/swagger"
"github.com/astaxie/beego/utils"
beeLogger "github.com/beego/bee/logger"
bu "github.com/beego/bee/utils"
)

const (
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -418,7 +424,7 @@ 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
Expand All @@ -433,36 +439,18 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) {
importlist[pps[len(pps)-1]] = pkgpath
}

pkgRealpath := ""

if bu.IsGOMODULE() {
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
}
}
}
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] = struct{}{}
} 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()
Expand Down
15 changes: 0 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,10 @@ import (
"github.com/beego/bee/cmd"
"github.com/beego/bee/cmd/commands"
"github.com/beego/bee/config"
"github.com/beego/bee/generate/swaggergen"
"github.com/beego/bee/utils"
)

var (
workspace = os.Getenv("BeeWorkspace")
)

func main() {
currentpath, _ := os.Getwd()
if workspace != "" {
currentpath = workspace
}
flag.Usage = cmd.Usage
flag.Parse()
log.SetFlags(0)
Expand Down Expand Up @@ -66,12 +57,6 @@ func main() {
}

config.LoadConfig()

// Check if current directory is inside the GOPATH,
// if so parse the packages inside it.
if utils.IsInGOPATH(currentpath) && cmd.IfGenerateDocs(c.Name(), args) {
swaggergen.ParsePackagesFromDir(currentpath)
}
os.Exit(c.Run(c, args))
return
}
Expand Down