Updated scanner

Removed scanning all the import statements, this is not needed
Added recursive scan for revel import path to pick up testunits
This commit is contained in:
notzippy@gmail.com
2020-05-08 15:41:20 -07:00
parent 49eef29bb5
commit 741f49236a
4 changed files with 11 additions and 85 deletions

View File

@@ -174,8 +174,10 @@ func (c *CommandConfig) initAppFolder() (err error) {
// Use app folder to read the go.mod if it exists and extract the package information // Use app folder to read the go.mod if it exists and extract the package information
goModFile := filepath.Join(appFolder, "go.mod") goModFile := filepath.Join(appFolder, "go.mod")
utils.Logger.Info("Checking gomod, extracting from file", "path", goModFile,"exists", utils.Exists(goModFile))
if utils.Exists(goModFile) { if utils.Exists(goModFile) {
c.Vendored = true c.Vendored = true
utils.Logger.Info("Found go mod, extracting from file", "path", goModFile)
file, err := ioutil.ReadFile(goModFile) file, err := ioutil.ReadFile(goModFile)
if err != nil { if err != nil {
return err return err
@@ -220,7 +222,7 @@ func (c *CommandConfig) initAppFolder() (err error) {
c.AppPath = appFolder c.AppPath = appFolder
} }
utils.Logger.Info("Set application path", "path", c.AppPath) utils.Logger.Info("Set application path", "path", c.AppPath, "vendored",c.Vendored, "importpath",c.ImportPath)
return nil return nil
} }

View File

@@ -57,7 +57,6 @@ func (s *SourceInfo) TypesThatEmbed(targetType, packageFilter string) (filtered
// Look through the embedded types to see if the current type is among them. // Look through the embedded types to see if the current type is among them.
for _, embeddedType := range spec.EmbeddedTypes { for _, embeddedType := range spec.EmbeddedTypes {
// If so, add this type's simple name to the nodeQueue, and its spec to // If so, add this type's simple name to the nodeQueue, and its spec to
// the filtered list. // the filtered list.
if typeSimpleName == embeddedType.String() { if typeSimpleName == embeddedType.String() {
@@ -111,6 +110,7 @@ func (s *SourceInfo) TypesThatEmbed(targetType, packageFilter string) (filtered
// ControllerSpecs returns the all the controllers that embeds // ControllerSpecs returns the all the controllers that embeds
// `revel.Controller` // `revel.Controller`
func (s *SourceInfo) ControllerSpecs() []*TypeInfo { func (s *SourceInfo) ControllerSpecs() []*TypeInfo {
utils.Logger.Infof("Scanning controller specifications for types ","typePath",RevelImportPath + ".Controller", "speclen",len(s.controllerSpecs))
if s.controllerSpecs == nil { if s.controllerSpecs == nil {
s.controllerSpecs = s.TypesThatEmbed(RevelImportPath + ".Controller", "controllers") s.controllerSpecs = s.TypesThatEmbed(RevelImportPath + ".Controller", "controllers")
} }

View File

@@ -282,6 +282,7 @@ func (s *SourceInfoProcessor) getControllerSpec(spec *ast.TypeSpec, p *packages.
ImportPath: p.PkgPath, ImportPath: p.PkgPath,
PackageName: p.Name, PackageName: p.Name,
} }
log := s.sourceProcessor.log.New("file", p.Fset.Position(spec.Pos()).Filename,"position", p.Fset.Position(spec.Pos()).Line)
for _, field := range structType.Fields.List { for _, field := range structType.Fields.List {
// If field.Names is set, it's not an embedded type. // If field.Names is set, it's not an embedded type.
if field.Names != nil { if field.Names != nil {
@@ -330,7 +331,7 @@ func (s *SourceInfoProcessor) getControllerSpec(spec *ast.TypeSpec, p *packages.
} else { } else {
var ok bool var ok bool
if importPath, ok = s.sourceProcessor.importMap[pkgName]; !ok { if importPath, ok = s.sourceProcessor.importMap[pkgName]; !ok {
s.sourceProcessor.log.Error("Error: Failed to find import path for ", "package", pkgName, "type", typeName, "map", s.sourceProcessor.importMap) log.Error("Error: Failed to find import path for ", "package", pkgName, "type", typeName, "map", s.sourceProcessor.importMap, "usedin", )
continue continue
} }
} }

View File

@@ -1,8 +1,6 @@
package parser2 package parser2
import ( import (
"go/ast"
"go/token"
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
@@ -74,7 +72,7 @@ func (s *SourceProcessor) parse() (compileError error) {
return return
} }
func (s *SourceProcessor) addPackages() (err error) { func (s *SourceProcessor) addPackages() (err error) {
allPackages := []string{s.revelContainer.ImportPath + "/...", model.RevelImportPath} allPackages := []string{s.revelContainer.ImportPath + "/...", model.RevelImportPath + "/..."}
for _, module := range s.revelContainer.ModulePathMap { for _, module := range s.revelContainer.ModulePathMap {
allPackages = append(allPackages, module.ImportPath + "/...") // +"/app/controllers/...") allPackages = append(allPackages, module.ImportPath + "/...") // +"/app/controllers/...")
} }
@@ -105,6 +103,9 @@ func (s *SourceProcessor) addPackages() (err error) {
s.log.Info("Loaded packages ", "len results", len(s.packageList), "error", err) s.log.Info("Loaded packages ", "len results", len(s.packageList), "error", err)
return return
} }
// This function is used to populate a map so that we can lookup controller embedded types in order to determine
// if a Struct inherits from from revel.Controller
func (s *SourceProcessor) addImportMap() (err error) { func (s *SourceProcessor) addImportMap() (err error) {
s.importMap = map[string]string{} s.importMap = map[string]string{}
s.packageMap = map[string]string{} s.packageMap = map[string]string{}
@@ -119,35 +120,7 @@ func (s *SourceProcessor) addImportMap() (err error) {
} }
} }
for _, tree := range p.Syntax { for _, tree := range p.Syntax {
for _, decl := range tree.Decls { s.importMap[tree.Name.Name] = p.PkgPath
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
if genDecl.Tok == token.IMPORT {
for _, spec := range genDecl.Specs {
importSpec := spec.(*ast.ImportSpec)
//fmt.Printf("*** import specification %#v\n", importSpec)
var pkgAlias string
if importSpec.Name != nil {
pkgAlias = importSpec.Name.Name
if pkgAlias == "_" {
continue
}
}
quotedPath := importSpec.Path.Value // e.g. "\"sample/app/models\""
fullPath := quotedPath[1 : len(quotedPath) - 1] // Remove the quotes
if pkgAlias == "" {
pkgAlias = fullPath
if index := strings.LastIndex(pkgAlias, "/"); index > 0 {
pkgAlias = pkgAlias[index + 1:]
}
}
s.importMap[pkgAlias] = fullPath
}
}
}
} }
} }
return return
@@ -165,53 +138,3 @@ func (s *SourceProcessor) addSourceInfo() (err error) {
} }
return return
} }
// Add imports to the map from the source dir
//func addImports(imports map[string]string, decl ast.Decl, srcDir string) {
// genDecl, ok := decl.(*ast.GenDecl)
// if !ok {
// return
// }
//
// if genDecl.Tok != token.IMPORT {
// return
// }
//
// for _, spec := range genDecl.Specs {
// importSpec := spec.(*ast.ImportSpec)
// var pkgAlias string
// if importSpec.Name != nil {
// pkgAlias = importSpec.Name.Name
// if pkgAlias == "_" {
// continue
// }
// }
// quotedPath := importSpec.Path.Value // e.g. "\"sample/app/models\""
// fullPath := quotedPath[1 : len(quotedPath)-1] // Remove the quotes
//
// // If the package was not aliased (common case), we have to import it
// // to see what the package name is.
// // TODO: Can improve performance here a lot:
// // 1. Do not import everything over and over again. Keep a cache.
// // 2. Exempt the standard library; their directories always match the package name.
// // 3. Can use build.FindOnly and then use parser.ParseDir with mode PackageClauseOnly
// if pkgAlias == "" {
//
// utils.Logger.Debug("Reading from build", "path", fullPath, "srcPath", srcDir, "gopath", build.Default.GOPATH)
// pkg, err := build.Import(fullPath, srcDir, 0)
// if err != nil {
// // We expect this to happen for apps using reverse routing (since we
// // have not yet generated the routes). Don't log that.
// if !strings.HasSuffix(fullPath, "/app/routes") {
// utils.Logger.Warn("Could not find import:", "path", fullPath, "srcPath", srcDir, "error", err)
// }
// continue
// } else {
// utils.Logger.Debug("Found package in dir", "dir", pkg.Dir, "name", pkg.ImportPath)
// }
// pkgAlias = pkg.Name
// }
//
// imports[pkgAlias] = fullPath
// }
//}