2019-10-21 17:27:04 +03:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
2019-10-22 21:50:48 +03:00
|
|
|
"encoding/json"
|
2019-10-21 17:27:04 +03:00
|
|
|
"go/token"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-03 23:07:51 +03:00
|
|
|
"github.com/gobuffalo/here"
|
2019-10-21 17:27:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ Decl = WalkDecl{}
|
|
|
|
|
|
|
|
type WalkDecl struct {
|
|
|
|
file *File
|
2019-10-26 00:11:34 +03:00
|
|
|
pos token.Position
|
2019-10-21 17:27:04 +03:00
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:19:39 +03:00
|
|
|
func (d WalkDecl) String() string {
|
|
|
|
b, _ := json.Marshal(d)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2019-10-22 21:50:48 +03:00
|
|
|
func (d WalkDecl) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"type": "pkger.Walk",
|
|
|
|
"file": d.file,
|
|
|
|
"pos": d.pos,
|
|
|
|
"value": d.value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-21 17:27:04 +03:00
|
|
|
func (d WalkDecl) File() (*File, error) {
|
|
|
|
if d.file == nil {
|
|
|
|
return nil, os.ErrNotExist
|
|
|
|
}
|
|
|
|
return d.file, nil
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:11:34 +03:00
|
|
|
func (d WalkDecl) Position() (token.Position, error) {
|
2019-10-21 17:27:04 +03:00
|
|
|
return d.pos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d WalkDecl) Value() (string, error) {
|
|
|
|
if d.value == "" {
|
|
|
|
return "", os.ErrNotExist
|
|
|
|
}
|
|
|
|
return d.value, nil
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:19:39 +03:00
|
|
|
func (d WalkDecl) Files(virtual map[string]string) ([]*File, error) {
|
2019-10-26 00:11:34 +03:00
|
|
|
var files []*File
|
|
|
|
|
2019-11-01 22:53:54 +03:00
|
|
|
her := d.file.Here
|
|
|
|
pt := d.file.Path
|
2019-10-21 17:27:04 +03:00
|
|
|
|
2019-10-30 18:31:39 +03:00
|
|
|
root := filepath.Join(her.Module.Dir, pt.Name)
|
2019-10-21 17:27:04 +03:00
|
|
|
|
2019-11-01 22:53:54 +03:00
|
|
|
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
2019-10-21 17:27:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:11:34 +03:00
|
|
|
if info.IsDir() {
|
|
|
|
her, err = here.Dir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-23 20:19:39 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-21 17:27:04 +03:00
|
|
|
|
2019-11-01 22:53:54 +03:00
|
|
|
n := strings.TrimPrefix(path, her.Module.Dir)
|
|
|
|
|
|
|
|
pt, err := her.Parse(n)
|
2019-10-21 17:27:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-26 00:11:34 +03:00
|
|
|
|
|
|
|
if _, ok := virtual[n]; ok {
|
|
|
|
if info.IsDir() {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-21 17:27:04 +03:00
|
|
|
|
|
|
|
files = append(files, &File{
|
|
|
|
Abs: path,
|
|
|
|
Path: pt,
|
2019-10-26 00:11:34 +03:00
|
|
|
Here: her,
|
2019-10-21 17:27:04 +03:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return files, err
|
|
|
|
}
|