2019-07-31 00:21:26 +03:00
|
|
|
package pkger
|
|
|
|
|
|
|
|
import (
|
2019-07-31 18:53:36 +03:00
|
|
|
"encoding/json"
|
2019-07-31 00:21:26 +03:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-08-02 07:22:17 +03:00
|
|
|
"sync"
|
2019-07-31 23:29:49 +03:00
|
|
|
"time"
|
2019-07-31 00:21:26 +03:00
|
|
|
|
|
|
|
"github.com/gobuffalo/here"
|
|
|
|
)
|
|
|
|
|
|
|
|
type index struct {
|
2019-08-02 21:47:59 +03:00
|
|
|
Files *filesMap
|
2019-08-02 21:51:09 +03:00
|
|
|
Infos *infosMap
|
2019-08-02 07:22:17 +03:00
|
|
|
current here.Info
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *index) Info(p string) (here.Info, error) {
|
2019-08-02 21:51:09 +03:00
|
|
|
info, ok := i.Infos.Load(p)
|
|
|
|
if ok {
|
2019-08-02 07:22:17 +03:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := here.Cache(p, here.Package)
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
2019-08-02 21:51:09 +03:00
|
|
|
i.Infos.Store(p, info)
|
2019-08-02 07:22:17 +03:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *index) Current() (here.Info, error) {
|
|
|
|
var err error
|
|
|
|
i.once.Do(func() {
|
|
|
|
i.current, err = here.Cache("", func(string) (here.Info, error) {
|
|
|
|
return here.Current()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return i.current, err
|
2019-07-31 00:21:26 +03:00
|
|
|
}
|
|
|
|
|
2019-08-02 05:34:32 +03:00
|
|
|
func (i *index) Create(pt Path) (*File, error) {
|
2019-08-02 06:21:37 +03:00
|
|
|
her, err := Info(pt.Pkg)
|
2019-07-31 23:29:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f := &File{
|
2019-08-02 05:34:32 +03:00
|
|
|
path: pt,
|
|
|
|
her: her,
|
2019-07-31 23:29:49 +03:00
|
|
|
info: &FileInfo{
|
2019-08-02 00:35:42 +03:00
|
|
|
name: strings.TrimPrefix(pt.Name, "/"),
|
2019-07-31 23:29:49 +03:00
|
|
|
mode: 0666,
|
|
|
|
modTime: time.Now(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:47:59 +03:00
|
|
|
i.Files.Store(pt, f)
|
2019-07-31 23:29:49 +03:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2019-08-02 07:22:17 +03:00
|
|
|
func (i *index) MarshalJSON() ([]byte, error) {
|
2019-08-02 05:34:32 +03:00
|
|
|
m := map[string]interface{}{}
|
2019-07-31 18:53:36 +03:00
|
|
|
|
2019-08-02 07:22:17 +03:00
|
|
|
fm := map[string]json.RawMessage{}
|
2019-07-31 18:53:36 +03:00
|
|
|
|
2019-08-02 21:47:59 +03:00
|
|
|
var err error
|
|
|
|
i.Files.Range(func(key Path, value *File) bool {
|
|
|
|
b, err := value.MarshalJSON()
|
2019-08-02 07:22:17 +03:00
|
|
|
if err != nil {
|
2019-08-02 21:47:59 +03:00
|
|
|
return false
|
2019-08-02 07:22:17 +03:00
|
|
|
}
|
2019-08-02 21:47:59 +03:00
|
|
|
fm[key.String()] = b
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-31 18:53:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m["files"] = fm
|
2019-08-02 07:22:17 +03:00
|
|
|
m["infos"] = i.Infos
|
|
|
|
m["current"] = i.current
|
2019-07-31 18:53:36 +03:00
|
|
|
|
|
|
|
return json.Marshal(m)
|
|
|
|
}
|
|
|
|
|
2019-08-02 07:22:17 +03:00
|
|
|
func (i *index) UnmarshalJSON(b []byte) error {
|
|
|
|
// fmt.Println(string(b))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:34:32 +03:00
|
|
|
func (i index) Walk(pt Path, wf WalkFunc) error {
|
2019-08-02 21:47:59 +03:00
|
|
|
var err error
|
|
|
|
i.Files.Range(func(k Path, v *File) bool {
|
|
|
|
if k.Pkg != pt.Pkg {
|
|
|
|
return true
|
2019-07-31 00:21:26 +03:00
|
|
|
}
|
2019-08-02 21:47:59 +03:00
|
|
|
if err = wf(k, v.info); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-31 00:21:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var info here.Info
|
|
|
|
if pt.Pkg == "." {
|
2019-08-02 05:34:32 +03:00
|
|
|
info, err = Current()
|
2019-07-31 00:21:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pt.Pkg = info.ImportPath
|
|
|
|
}
|
2019-08-01 19:03:12 +03:00
|
|
|
|
2019-07-31 00:21:26 +03:00
|
|
|
if info.IsZero() {
|
2019-08-02 06:21:37 +03:00
|
|
|
info, err = Info(pt.Pkg)
|
2019-07-31 00:21:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", pt, err)
|
|
|
|
}
|
|
|
|
}
|
2019-08-01 19:03:12 +03:00
|
|
|
fp := filepath.Join(info.Dir, pt.Name)
|
|
|
|
err = filepath.Walk(fp, func(path string, fi os.FileInfo, err error) error {
|
2019-07-31 00:21:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path = strings.TrimPrefix(path, info.Dir)
|
2019-08-02 05:34:32 +03:00
|
|
|
pt, err := Parse(fmt.Sprintf("%s:%s", pt.Pkg, path))
|
2019-07-31 00:21:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-01 19:03:12 +03:00
|
|
|
return wf(pt, NewFileInfo(fi))
|
2019-07-31 00:21:26 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:34:32 +03:00
|
|
|
func (i *index) Open(pt Path) (*File, error) {
|
2019-08-02 21:47:59 +03:00
|
|
|
f, ok := i.Files.Load(pt)
|
2019-07-31 00:21:26 +03:00
|
|
|
if !ok {
|
|
|
|
return i.openDisk(pt)
|
|
|
|
}
|
2019-08-02 06:21:37 +03:00
|
|
|
nf := &File{
|
2019-08-02 05:34:32 +03:00
|
|
|
info: f.info,
|
|
|
|
path: f.path,
|
|
|
|
data: f.data,
|
|
|
|
her: f.her,
|
2019-08-02 06:21:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nf, nil
|
2019-07-31 00:21:26 +03:00
|
|
|
}
|
|
|
|
|
2019-08-02 05:34:32 +03:00
|
|
|
func (i index) openDisk(pt Path) (*File, error) {
|
2019-08-02 06:21:37 +03:00
|
|
|
info, err := Info(pt.Pkg)
|
2019-07-31 00:21:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fp := info.Dir
|
|
|
|
if len(pt.Name) > 0 {
|
|
|
|
fp = filepath.Join(fp, pt.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(fp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f := &File{
|
2019-08-02 00:35:42 +03:00
|
|
|
info: WithName(strings.TrimPrefix(pt.Name, "/"), NewFileInfo(fi)),
|
2019-07-31 00:21:26 +03:00
|
|
|
her: info,
|
|
|
|
path: pt,
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:29:49 +03:00
|
|
|
func newIndex() *index {
|
|
|
|
return &index{
|
2019-08-02 21:47:59 +03:00
|
|
|
Files: &filesMap{},
|
2019-08-02 21:51:09 +03:00
|
|
|
Infos: &infosMap{},
|
2019-07-31 23:29:49 +03:00
|
|
|
}
|
2019-07-31 00:21:26 +03:00
|
|
|
}
|
2019-07-31 23:29:49 +03:00
|
|
|
|
2019-08-02 05:34:32 +03:00
|
|
|
var rootIndex = func() *index {
|
|
|
|
i := newIndex()
|
|
|
|
return i
|
|
|
|
}()
|