mirror of
https://github.com/beego/bee.git
synced 2024-11-22 10:10:53 +00:00
Enhances the package dir parser
The function parsePackagesFromDir() was panicing when it encounter some "invalid" Go files. Instead, this will allow it to display a warning and continue the execution of the command.
This commit is contained in:
parent
eac9dc25d3
commit
ab39926c41
40
g_docs.go
40
g_docs.go
@ -32,8 +32,6 @@ import (
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/astaxie/beego/swagger"
|
||||
"github.com/astaxie/beego/utils"
|
||||
)
|
||||
@ -87,39 +85,57 @@ func init() {
|
||||
parsePackagesFromDir(curPath)
|
||||
}
|
||||
|
||||
func parsePackagesFromDir(path string) {
|
||||
parsePackageFromDir(path)
|
||||
list, err := ioutil.ReadDir(path)
|
||||
func parsePackagesFromDir(dirpath string) {
|
||||
c := make(chan error)
|
||||
|
||||
go func() {
|
||||
filepath.Walk(dirpath, func(fpath string, fileInfo os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
logger.Fatalf("Cannot read directory '%s': %s", path, err)
|
||||
return nil
|
||||
}
|
||||
for _, item := range list {
|
||||
if item.IsDir() && item.Name() != "vendor" {
|
||||
parsePackagesFromDir(path + "/" + item.Name())
|
||||
if !fileInfo.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if fileInfo.Name() != "vendor" {
|
||||
err = parsePackageFromDir(fpath)
|
||||
if err != nil {
|
||||
// Send the error to through the channel and continue walking
|
||||
c <- fmt.Errorf("Error while parsing directory: %s", err.Error())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
close(c)
|
||||
}()
|
||||
|
||||
for err := range c {
|
||||
logger.Warnf("%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func parsePackageFromDir(path string) {
|
||||
func parsePackageFromDir(path string) error {
|
||||
fileSet := token.NewFileSet()
|
||||
folderPkgs, err := parser.ParseDir(fileSet, path, func(info os.FileInfo) bool {
|
||||
name := info.Name()
|
||||
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
|
||||
}, parser.ParseComments)
|
||||
if err != nil {
|
||||
logger.Fatalf("Error while parsing dir at '%s': %s", path, err)
|
||||
return err
|
||||
}
|
||||
|
||||
for k, v := range folderPkgs {
|
||||
astPkgs[k] = v
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateDocs(curpath string) {
|
||||
fset := token.NewFileSet()
|
||||
|
||||
f, err := parser.ParseFile(fset, path.Join(curpath, "routers", "router.go"), nil, parser.ParseComments)
|
||||
|
||||
if err != nil {
|
||||
logger.Fatalf("Error while parsing router.go: %s", err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user