2019-08-01 19:03:12 +03:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
2019-08-30 00:05:44 +03:00
|
|
|
"fmt"
|
2019-09-23 16:53:30 +03:00
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2019-08-01 19:03:12 +03:00
|
|
|
"os"
|
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
"github.com/markbates/oncer"
|
2019-08-30 05:30:00 +03:00
|
|
|
"github.com/markbates/pkger/here"
|
2019-09-20 18:16:57 +03:00
|
|
|
"github.com/markbates/pkger/pkging"
|
2019-08-01 19:03:12 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"}
|
|
|
|
|
2019-09-21 19:51:29 +03:00
|
|
|
func Parse(her here.Info) (Results, error) {
|
2019-08-01 19:03:12 +03:00
|
|
|
var r Results
|
2019-09-23 16:53:30 +03:00
|
|
|
var err error
|
2019-08-02 00:56:37 +03:00
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
oncer.Do(her.ImportPath, func() {
|
|
|
|
pwd, err := os.Getwd()
|
2019-08-01 19:03:12 +03:00
|
|
|
if err != nil {
|
2019-09-23 16:53:30 +03:00
|
|
|
return
|
2019-08-01 19:03:12 +03:00
|
|
|
}
|
2019-09-23 16:53:30 +03:00
|
|
|
defer os.Chdir(pwd)
|
2019-08-01 19:03:12 +03:00
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
fmt.Println("cd: ", her.Dir, her.ImportPath)
|
|
|
|
os.Chdir(her.Dir)
|
2019-08-01 19:03:12 +03:00
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
// 1. search for .go files in/imported by `her.ImportPath`
|
|
|
|
src, err := fromSource(her)
|
2019-08-01 19:03:12 +03:00
|
|
|
if err != nil {
|
2019-09-23 16:53:30 +03:00
|
|
|
return
|
2019-08-01 19:03:12 +03:00
|
|
|
}
|
2019-09-23 16:53:30 +03:00
|
|
|
fmt.Println(">>>TODO parser/parser.go:30: src ", src)
|
2019-08-01 19:03:12 +03:00
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
// 2. parse .go ast's for `pkger.*` calls
|
|
|
|
// 3. find path's in those files
|
|
|
|
// 4. walk folders in those paths and add to results
|
2019-08-01 19:03:12 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
func fromSource(her here.Info) ([]pkging.Path, error) {
|
|
|
|
fmt.Println(">>>TODO parser/parser.go:201: her.ImportPath ", her.ImportPath)
|
|
|
|
fmt.Println(her)
|
|
|
|
root := her.Dir
|
|
|
|
fi, err := os.Stat(root)
|
2019-08-01 21:37:01 +03:00
|
|
|
if err != nil {
|
2019-09-23 16:53:30 +03:00
|
|
|
return nil, err
|
2019-08-01 21:37:01 +03:00
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
2019-09-23 16:53:30 +03:00
|
|
|
return nil, fmt.Errorf("%q is not a directory", root)
|
2019-08-01 21:37:01 +03:00
|
|
|
}
|
2019-08-02 00:56:37 +03:00
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
fset := token.NewFileSet()
|
2019-08-01 19:03:12 +03:00
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
pkgs, err := parser.ParseDir(fset, root, nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-01 19:03:12 +03:00
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
var paths []pkging.Path
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
for _, pf := range pkg.Files {
|
|
|
|
f := &file{fset: fset, astFile: pf, filename: pf.Name.Name}
|
|
|
|
f.decls = make(map[string]string)
|
2019-08-01 19:03:12 +03:00
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
x, err := f.find()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i, pt := range x {
|
|
|
|
if pt.Pkg == "/" || pt.Pkg == "" {
|
|
|
|
pt.Pkg = her.ImportPath
|
|
|
|
x[i] = pt
|
2019-08-01 19:03:12 +03:00
|
|
|
}
|
2019-09-23 16:53:30 +03:00
|
|
|
paths = append(paths, x[i])
|
2019-08-01 19:03:12 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-23 16:53:30 +03:00
|
|
|
}
|
2019-08-01 19:03:12 +03:00
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
for _, i := range her.Imports {
|
|
|
|
fmt.Println(">>>TODO parser/parser.go:237: i ", i)
|
|
|
|
}
|
2019-08-01 19:03:12 +03:00
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
return paths, nil
|
2019-08-01 19:03:12 +03:00
|
|
|
}
|
|
|
|
|
2019-09-23 16:53:30 +03:00
|
|
|
// func importName(pkg *ast.File) (string, error) {
|
|
|
|
// var v visitor
|
|
|
|
// var name string
|
|
|
|
// var err error
|
|
|
|
// v = func(node ast.Node) ast.Visitor {
|
|
|
|
// if node == nil {
|
|
|
|
// return v
|
|
|
|
// }
|
|
|
|
// switch t := node.(type) {
|
|
|
|
// case *ast.ImportSpec:
|
|
|
|
// s, err := strconv.Unquote(t.Path.Value)
|
|
|
|
// if err != nil {
|
|
|
|
// err = err
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
// if s != "github.com/markbates/pkger" {
|
|
|
|
// if t.Name == nil {
|
|
|
|
// name = "pkger"
|
|
|
|
// return v
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// default:
|
|
|
|
// // fmt.Printf("%#v\n", node)
|
|
|
|
// }
|
|
|
|
// return v
|
|
|
|
// }
|
|
|
|
// ast.Walk(v, pkg)
|
|
|
|
//
|
|
|
|
// if err != nil {
|
|
|
|
// return "", err
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if len(name) == 0 {
|
|
|
|
// return "", io.EOF
|
|
|
|
// }
|
|
|
|
// return name, nil
|
|
|
|
// }
|
|
|
|
|
2019-08-01 19:03:12 +03:00
|
|
|
type Results struct {
|
2019-09-20 18:16:57 +03:00
|
|
|
Paths []pkging.Path
|
|
|
|
Path pkging.Path
|
2019-08-01 19:03:12 +03:00
|
|
|
}
|