pkger/parser/parser.go

119 lines
2.0 KiB
Go
Raw Normal View History

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-10-15 23:40:45 +03:00
"path/filepath"
2019-10-09 20:21:54 +03:00
"sort"
2019-10-16 00:48:40 +03:00
"strings"
2019-08-01 19:03:12 +03:00
2019-08-30 05:30:00 +03:00
"github.com/markbates/pkger/here"
2019-10-15 23:40:45 +03:00
"github.com/markbates/pkger/pkging/stdos"
2019-08-01 19:03:12 +03:00
)
var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"}
2019-10-09 20:21:54 +03:00
func Parse(her here.Info) ([]here.Path, error) {
2019-08-02 00:56:37 +03:00
2019-10-09 20:21:54 +03:00
src, err := fromSource(her)
if err != nil {
return nil, err
}
2019-08-01 19:03:12 +03:00
2019-10-09 20:21:54 +03:00
return src, nil
2019-08-01 19:03:12 +03:00
}
2019-10-09 20:21:54 +03:00
func fromSource(her here.Info) ([]here.Path, error) {
2019-09-23 16:53:30 +03:00
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-10-09 20:21:54 +03:00
pm := map[string]here.Path{}
2019-09-23 16:53:30 +03:00
for _, pkg := range pkgs {
for _, pf := range pkg.Files {
2019-10-09 20:21:54 +03:00
f := &file{
fset: fset,
astFile: pf,
filename: pf.Name.Name,
2019-10-15 23:40:45 +03:00
decls: map[string]string{},
2019-10-09 20:21:54 +03:00
}
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
}
2019-10-15 23:40:45 +03:00
for _, dl := range x {
pt, err := her.Parse(dl)
if err != nil {
return nil, err
2019-08-01 19:03:12 +03:00
}
2019-10-15 23:40:45 +03:00
res, err := fromPath(pt)
2019-10-09 20:21:54 +03:00
if err != nil {
return nil, err
}
2019-10-15 23:40:45 +03:00
for _, p := range res {
pm[p.String()] = p
}
2019-08-01 19:03:12 +03:00
}
}
2019-09-23 16:53:30 +03:00
}
2019-10-09 20:21:54 +03:00
var paths []here.Path
2019-08-01 19:03:12 +03:00
2019-10-09 20:21:54 +03:00
for _, v := range pm {
paths = append(paths, v)
2019-09-23 16:53:30 +03:00
}
2019-08-01 19:03:12 +03:00
2019-10-09 20:21:54 +03:00
sort.Slice(paths, func(i, j int) bool {
return paths[i].String() < paths[j].String()
})
2019-09-23 16:53:30 +03:00
return paths, nil
2019-08-01 19:03:12 +03:00
}
2019-10-15 23:40:45 +03:00
func fromPath(pt here.Path) ([]here.Path, error) {
var paths []here.Path
her, err := here.Package(pt.Pkg)
if err != nil {
return nil, err
}
pkg, err := stdos.New(her)
if err != nil {
return nil, err
}
root := here.Path{
Pkg: pt.Pkg,
2019-10-16 00:48:40 +03:00
Name: strings.Replace(filepath.Dir(pt.Name), "\\", "/", -1),
2019-10-15 23:40:45 +03:00
}
paths = append(paths, root)
err = pkg.Walk(pt.Name, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
p, err := her.Parse(path)
if err != nil {
return err
}
paths = append(paths, p)
return nil
})
return paths, nil
2019-08-01 19:03:12 +03:00
}