mirror of
https://github.com/astaxie/beego.git
synced 2024-11-21 21:40:55 +00:00
fix comment router generate issue
This commit is contained in:
parent
2fce8f9d1b
commit
993ccac2bd
1
go.mod
1
go.mod
@ -32,6 +32,7 @@ require (
|
||||
github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b // indirect
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
|
||||
golang.org/x/tools v0.0.0-20200117065230-39095c1d176c
|
||||
google.golang.org/grpc v1.31.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.8
|
||||
)
|
||||
|
@ -97,6 +97,7 @@ func initBeforeHTTPRun() {
|
||||
registerTemplate,
|
||||
registerAdmin,
|
||||
registerGzip,
|
||||
registerCommentRouter,
|
||||
)
|
||||
|
||||
for _, hk := range hooks {
|
||||
|
@ -86,6 +86,7 @@ type WebConfig struct {
|
||||
TemplateLeft string
|
||||
TemplateRight string
|
||||
ViewsPath string
|
||||
CommentRouterPath string
|
||||
EnableXSRF bool
|
||||
XSRFKey string
|
||||
XSRFExpire int
|
||||
@ -245,6 +246,7 @@ func newBConfig() *Config {
|
||||
TemplateLeft: "{{",
|
||||
TemplateRight: "}}",
|
||||
ViewsPath: "views",
|
||||
CommentRouterPath: "controllers",
|
||||
EnableXSRF: false,
|
||||
XSRFKey: "beegoxsrf",
|
||||
XSRFExpire: 0,
|
||||
|
10
pkg/hooks.go
10
pkg/hooks.go
@ -102,3 +102,13 @@ func registerGzip() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func registerCommentRouter() error {
|
||||
if BConfig.RunMode == DEV {
|
||||
if err := parserPkg(filepath.Join(WorkPath, BConfig.WebConfig.CommentRouterPath)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -19,8 +19,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"golang.org/x/tools/go/packages"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -76,7 +75,7 @@ func init() {
|
||||
pkgLastupdate = make(map[string]int64)
|
||||
}
|
||||
|
||||
func parserPkg(pkgRealpath, pkgpath string) error {
|
||||
func parserPkg(pkgRealpath string) error {
|
||||
rep := strings.NewReplacer("\\", "_", "/", "_", ".", "_")
|
||||
commentFilename, _ = filepath.Rel(AppPath, pkgRealpath)
|
||||
commentFilename = commentPrefix + rep.Replace(commentFilename) + ".go"
|
||||
@ -85,24 +84,23 @@ func parserPkg(pkgRealpath, pkgpath string) error {
|
||||
return nil
|
||||
}
|
||||
genInfoList = make(map[string][]ControllerComments)
|
||||
fileSet := token.NewFileSet()
|
||||
astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
|
||||
name := info.Name()
|
||||
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
|
||||
}, parser.ParseComments)
|
||||
pkgs, err := packages.Load(&packages.Config{
|
||||
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedSyntax,
|
||||
Dir: pkgRealpath,
|
||||
}, "./...")
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, pkg := range astPkgs {
|
||||
for _, fl := range pkg.Files {
|
||||
for _, pkg := range pkgs {
|
||||
for _, fl := range pkg.Syntax {
|
||||
for _, d := range fl.Decls {
|
||||
switch specDecl := d.(type) {
|
||||
case *ast.FuncDecl:
|
||||
if specDecl.Recv != nil {
|
||||
exp, ok := specDecl.Recv.List[0].Type.(*ast.StarExpr) // Check that the type is correct first beforing throwing to parser
|
||||
if ok {
|
||||
parserComments(specDecl, fmt.Sprint(exp.X), pkgpath)
|
||||
parserComments(specDecl, fmt.Sprint(exp.X), pkg.PkgPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -566,8 +564,17 @@ func getpathTime(pkgRealpath string) (lastupdate int64, err error) {
|
||||
return lastupdate, err
|
||||
}
|
||||
for _, f := range fl {
|
||||
if lastupdate < f.ModTime().UnixNano() {
|
||||
lastupdate = f.ModTime().UnixNano()
|
||||
var t int64
|
||||
if f.IsDir() {
|
||||
t, err = getpathTime(filepath.Join(pkgRealpath, f.Name()))
|
||||
if err != nil {
|
||||
return lastupdate, err
|
||||
}
|
||||
} else {
|
||||
t = f.ModTime().UnixNano()
|
||||
}
|
||||
if lastupdate < t {
|
||||
lastupdate = t
|
||||
}
|
||||
}
|
||||
return lastupdate, nil
|
||||
|
@ -18,9 +18,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -257,45 +255,6 @@ func (p *ControllerRegister) addToRouter(method, pattern string, r *ControllerIn
|
||||
// Include only when the Runmode is dev will generate router file in the router/auto.go from the controller
|
||||
// Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
|
||||
func (p *ControllerRegister) Include(cList ...ControllerInterface) {
|
||||
if BConfig.RunMode == DEV {
|
||||
skip := make(map[string]bool, 10)
|
||||
wgopath := utils.GetGOPATHs()
|
||||
go111module := os.Getenv(`GO111MODULE`)
|
||||
for _, c := range cList {
|
||||
reflectVal := reflect.ValueOf(c)
|
||||
t := reflect.Indirect(reflectVal).Type()
|
||||
// for go modules
|
||||
if go111module == `on` {
|
||||
pkgpath := filepath.Join(WorkPath, "..", t.PkgPath())
|
||||
if utils.FileExists(pkgpath) {
|
||||
if pkgpath != "" {
|
||||
if _, ok := skip[pkgpath]; !ok {
|
||||
skip[pkgpath] = true
|
||||
parserPkg(pkgpath, t.PkgPath())
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if len(wgopath) == 0 {
|
||||
panic("you are in dev mode. So please set gopath")
|
||||
}
|
||||
pkgpath := ""
|
||||
for _, wg := range wgopath {
|
||||
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", t.PkgPath()))
|
||||
if utils.FileExists(wg) {
|
||||
pkgpath = wg
|
||||
break
|
||||
}
|
||||
}
|
||||
if pkgpath != "" {
|
||||
if _, ok := skip[pkgpath]; !ok {
|
||||
skip[pkgpath] = true
|
||||
parserPkg(pkgpath, t.PkgPath())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, c := range cList {
|
||||
reflectVal := reflect.ValueOf(c)
|
||||
t := reflect.Indirect(reflectVal).Type()
|
||||
|
Loading…
Reference in New Issue
Block a user