compress packages

This commit is contained in:
Mark Bates 2019-08-01 22:34:32 -04:00
parent fa7d8c2b96
commit a63d5995a3
20 changed files with 93 additions and 142 deletions

View File

@ -3,12 +3,12 @@ package main
import ( import (
"fmt" "fmt"
"github.com/markbates/pkger"
"github.com/markbates/pkger/parser" "github.com/markbates/pkger/parser"
"github.com/markbates/pkger/pkgs"
) )
func list(args []string) error { func list(args []string) error {
info, err := pkgs.Current() info, err := pkger.Current()
if err != nil { if err != nil {
return err return err
} }

View File

@ -11,14 +11,12 @@ import (
"github.com/markbates/pkger" "github.com/markbates/pkger"
"github.com/markbates/pkger/parser" "github.com/markbates/pkger/parser"
"github.com/markbates/pkger/paths"
"github.com/markbates/pkger/pkgs"
) )
const outName = "pkged.go" const outName = "pkged.go"
func pack(args []string) error { func pack(args []string) error {
info, err := pkgs.Current() info, err := pkger.Current()
if err != nil { if err != nil {
return err return err
} }
@ -41,7 +39,7 @@ func pack(args []string) error {
return nil return nil
} }
func Package(p string, paths []paths.Path) error { func Package(p string, paths []pkger.Path) error {
os.RemoveAll(p) os.RemoveAll(p)
var files []*pkger.File var files []*pkger.File

View File

@ -5,11 +5,10 @@ import (
"os" "os"
"github.com/markbates/pkger" "github.com/markbates/pkger"
"github.com/markbates/pkger/paths"
) )
func walk(args []string) error { func walk(args []string) error {
err := pkger.Walk(".", func(path paths.Path, info os.FileInfo) error { err := pkger.Walk(".", func(path pkger.Path, info os.FileInfo) error {
fmt.Println(path) fmt.Println(path)
return nil return nil
}) })

View File

@ -1,6 +1,6 @@
// TODO: need to populate in memory cache when packed. // TODO: need to populate in memory cache when packed.
// you can't use go list, etc... in prod // you can't use go list, etc... in prod
package pkgs package pkger
import ( import (
"github.com/gobuffalo/here" "github.com/gobuffalo/here"

60
file.go
View File

@ -8,11 +8,9 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"path"
"time" "time"
"github.com/gobuffalo/here" "github.com/gobuffalo/here"
"github.com/markbates/pkger/paths"
) )
const timeFmt = time.RFC3339Nano const timeFmt = time.RFC3339Nano
@ -20,9 +18,8 @@ const timeFmt = time.RFC3339Nano
type File struct { type File struct {
info *FileInfo info *FileInfo
her here.Info her here.Info
path paths.Path path Path
data []byte data []byte
index *index
writer io.ReadWriter writer io.ReadWriter
Source io.ReadCloser Source io.ReadCloser
} }
@ -95,7 +92,6 @@ func (f File) MarshalJSON() ([]byte, error) {
m["info"] = f.info m["info"] = f.info
m["her"] = f.her m["her"] = f.her
m["path"] = f.path m["path"] = f.path
m["index"] = f.index
m["data"] = f.data m["data"] = f.data
if len(f.data) == 0 { if len(f.data) == 0 {
b, err := ioutil.ReadAll(&f) b, err := ioutil.ReadAll(&f)
@ -138,30 +134,16 @@ func (f *File) UnmarshalJSON(b []byte) error {
return err return err
} }
ind, ok := m["index"]
if !ok {
return fmt.Errorf("missing index")
}
f.index = newIndex()
if err := json.Unmarshal(ind, f.index); err != nil {
return err
}
return nil return nil
} }
func (f *File) Open(name string) (http.File, error) { func (f *File) Open(name string) (http.File, error) {
if f.index == nil {
f.index = newIndex()
}
// name = strings.TrimPrefix(name, "/") // name = strings.TrimPrefix(name, "/")
pt, err := paths.Parse(name) pt, err := Parse(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
pt.Name = path.Join(f.Path().Name, pt.Name)
if len(pt.Pkg) == 0 { if len(pt.Pkg) == 0 {
pt.Pkg = f.path.Pkg pt.Pkg = f.path.Pkg
} }
@ -170,22 +152,36 @@ func (f *File) Open(name string) (http.File, error) {
crs: &byteCRS{bytes.NewReader(f.data)}, crs: &byteCRS{bytes.NewReader(f.data)},
} }
if pt == f.path {
h.File = f
} else {
of, err := f.index.Open(pt)
if err != nil {
return nil, err
}
defer of.Close()
h.File = of
}
if len(f.data) > 0 { if len(f.data) > 0 {
return h, nil return h, nil
} }
if pt == f.path {
h.File = f
}
if h.File == nil {
of, err := rootIndex.Open(pt)
if err != nil {
return nil, err
}
h.File = of
}
bf, err := f.her.Open(h.File.FilePath()) bf, err := f.her.Open(h.File.FilePath())
if err != nil {
if _, ok := err.(*os.PathError); ok {
return h, nil
}
b, err := ioutil.ReadAll(h.File)
if err != nil {
return h, err
}
h.crs = &byteCRS{bytes.NewReader(b)}
return h, err
}
if err != nil { if err != nil {
return h, err return h, err
} }
@ -222,7 +218,7 @@ func (f File) FilePath() string {
return f.her.FilePath(f.Name()) return f.her.FilePath(f.Name())
} }
func (f File) Path() paths.Path { func (f File) Path() Path {
return f.path return f.path
} }

View File

@ -6,7 +6,6 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/markbates/pkger/paths"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -55,7 +54,7 @@ func Test_File_Write(t *testing.T) {
i := newIndex() i := newIndex()
f, err := i.Create(paths.Path{ f, err := i.Create(Path{
Name: "/hello.txt", Name: "/hello.txt",
}) })
r.NoError(err) r.NoError(err)

View File

@ -9,23 +9,19 @@ import (
"time" "time"
"github.com/gobuffalo/here" "github.com/gobuffalo/here"
"github.com/markbates/pkger/paths"
"github.com/markbates/pkger/pkgs"
) )
type index struct { type index struct {
Pkg string Files map[Path]*File
Files map[paths.Path]*File
} }
func (i *index) Create(pt paths.Path) (*File, error) { func (i *index) Create(pt Path) (*File, error) {
her, err := pkgs.Pkg(pt.Pkg) her, err := Pkg(pt.Pkg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
f := &File{ f := &File{
path: pt, path: pt,
index: newIndex(),
her: her, her: her,
info: &FileInfo{ info: &FileInfo{
name: strings.TrimPrefix(pt.Name, "/"), name: strings.TrimPrefix(pt.Name, "/"),
@ -39,9 +35,7 @@ func (i *index) Create(pt paths.Path) (*File, error) {
} }
func (i index) MarshalJSON() ([]byte, error) { func (i index) MarshalJSON() ([]byte, error) {
m := map[string]interface{}{ m := map[string]interface{}{}
"pkg": i.Pkg,
}
fm := map[string]File{} fm := map[string]File{}
@ -54,10 +48,7 @@ func (i index) MarshalJSON() ([]byte, error) {
return json.Marshal(m) return json.Marshal(m)
} }
func (i index) Walk(pt paths.Path, wf WalkFunc) error { func (i index) Walk(pt Path, wf WalkFunc) error {
if len(pt.Pkg) == 0 {
pt.Pkg = i.Pkg
}
if len(i.Files) > 0 { if len(i.Files) > 0 {
for k, v := range i.Files { for k, v := range i.Files {
if k.Pkg != pt.Pkg { if k.Pkg != pt.Pkg {
@ -72,7 +63,7 @@ func (i index) Walk(pt paths.Path, wf WalkFunc) error {
var info here.Info var info here.Info
var err error var err error
if pt.Pkg == "." { if pt.Pkg == "." {
info, err = pkgs.Current() info, err = Current()
if err != nil { if err != nil {
return err return err
} }
@ -80,7 +71,7 @@ func (i index) Walk(pt paths.Path, wf WalkFunc) error {
} }
if info.IsZero() { if info.IsZero() {
info, err = pkgs.Pkg(pt.Pkg) info, err = Pkg(pt.Pkg)
if err != nil { if err != nil {
return fmt.Errorf("%s: %s", pt, err) return fmt.Errorf("%s: %s", pt, err)
} }
@ -92,7 +83,7 @@ func (i index) Walk(pt paths.Path, wf WalkFunc) error {
} }
path = strings.TrimPrefix(path, info.Dir) path = strings.TrimPrefix(path, info.Dir)
pt, err := paths.Parse(fmt.Sprintf("%s:%s", pt.Pkg, path)) pt, err := Parse(fmt.Sprintf("%s:%s", pt.Pkg, path))
if err != nil { if err != nil {
return err return err
} }
@ -102,10 +93,7 @@ func (i index) Walk(pt paths.Path, wf WalkFunc) error {
return err return err
} }
func (i index) Open(pt paths.Path) (*File, error) { func (i *index) Open(pt Path) (*File, error) {
if len(pt.Pkg) == 0 {
pt.Pkg = i.Pkg
}
f, ok := i.Files[pt] f, ok := i.Files[pt]
if !ok { if !ok {
return i.openDisk(pt) return i.openDisk(pt)
@ -115,15 +103,11 @@ func (i index) Open(pt paths.Path) (*File, error) {
path: f.path, path: f.path,
data: f.data, data: f.data,
her: f.her, her: f.her,
index: newIndex(),
}, nil }, nil
} }
func (i index) openDisk(pt paths.Path) (*File, error) { func (i index) openDisk(pt Path) (*File, error) {
if len(pt.Pkg) == 0 { info, err := Pkg(pt.Pkg)
pt.Pkg = i.Pkg
}
info, err := pkgs.Pkg(pt.Pkg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -140,30 +124,17 @@ func (i index) openDisk(pt paths.Path) (*File, error) {
info: WithName(strings.TrimPrefix(pt.Name, "/"), NewFileInfo(fi)), info: WithName(strings.TrimPrefix(pt.Name, "/"), NewFileInfo(fi)),
her: info, her: info,
path: pt, path: pt,
index: &index{
Files: map[paths.Path]*File{},
},
} }
return f, nil return f, nil
} }
func (i index) Parse(p string) (paths.Path, error) {
pt, err := paths.Parse(p)
if err != nil {
return pt, err
}
if len(pt.Pkg) == 0 {
pt.Pkg = i.Pkg
}
return pt, nil
}
func newIndex() *index { func newIndex() *index {
return &index{ return &index{
Files: map[paths.Path]*File{}, Files: map[Path]*File{},
} }
} }
var rootIndex = newIndex() var rootIndex = func() *index {
i := newIndex()
return i
}()

View File

@ -6,7 +6,6 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/markbates/pkger/paths"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -15,7 +14,7 @@ func Test_index_Create(t *testing.T) {
i := newIndex() i := newIndex()
f, err := i.Create(paths.Path{ f, err := i.Create(Path{
Name: "/hello.txt", Name: "/hello.txt",
}) })
r.NoError(err) r.NoError(err)
@ -38,7 +37,7 @@ func Test_index_Create_Write(t *testing.T) {
i := newIndex() i := newIndex()
f, err := i.Create(paths.Path{ f, err := i.Create(Path{
Name: "/hello.txt", Name: "/hello.txt",
}) })
r.NoError(err) r.NoError(err)

View File

@ -5,6 +5,8 @@ github.com/gobuffalo/envy v1.7.0 h1:GlXgaiBkmrYMHco6t4j7SacKO4XUjvh5pwXh0f4uxXU=
github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI= github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI=
github.com/gobuffalo/here v0.2.1 h1:YWZUvrHnxNCIY2nnHPnF5Ob99Z5Iq29wHioLgcY+2G0= github.com/gobuffalo/here v0.2.1 h1:YWZUvrHnxNCIY2nnHPnF5Ob99Z5Iq29wHioLgcY+2G0=
github.com/gobuffalo/here v0.2.1/go.mod h1:2a6G14FaAKOGJMK/5UNa4Og/+iyFS5cq3MnlvFR7YDk= github.com/gobuffalo/here v0.2.1/go.mod h1:2a6G14FaAKOGJMK/5UNa4Og/+iyFS5cq3MnlvFR7YDk=
github.com/gobuffalo/here v0.2.2 h1:AXEK2ApOb4F5cKZ46Ofi8inGWa0qy5ChmJXAK5/IDmo=
github.com/gobuffalo/here v0.2.2/go.mod h1:2a6G14FaAKOGJMK/5UNa4Og/+iyFS5cq3MnlvFR7YDk=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=

View File

@ -6,11 +6,10 @@ import (
"os" "os"
"github.com/markbates/pkger" "github.com/markbates/pkger"
"github.com/markbates/pkger/paths"
) )
func main() { func main() {
err := pkger.Walk("github.com/gobuffalo/envy", func(path paths.Path, info os.FileInfo) error { err := pkger.Walk("github.com/gobuffalo/envy", func(path pkger.Path, info os.FileInfo) error {
fmt.Println(path) fmt.Println(path)
return nil return nil
}) })

View File

@ -1,12 +1,10 @@
package paths package pkger
import ( import (
"fmt" "fmt"
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
"github.com/markbates/pkger/pkgs"
) )
var cache = pathsMap{ var cache = pathsMap{
@ -44,7 +42,7 @@ func build(p, pkg, name string) (Path, error) {
Name: name, Name: name,
} }
info, err := pkgs.Current() info, err := Current()
if err != nil { if err != nil {
return pt, err return pt, err
} }

View File

@ -1,4 +1,4 @@
package paths package pkger
import ( import (
"testing" "testing"

View File

@ -6,15 +6,14 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/markbates/pkger/paths" "github.com/markbates/pkger"
"github.com/markbates/pkger/pkgs"
) )
var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"} var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"}
func Parse(name string) (Results, error) { func Parse(name string) (Results, error) {
var r Results var r Results
c, err := pkgs.Current() c, err := pkger.Current()
if err != nil { if err != nil {
return r, err return r, err
} }
@ -23,19 +22,19 @@ func Parse(name string) (Results, error) {
name = c.ImportPath name = c.ImportPath
} }
pt, err := paths.Parse(name) pt, err := pkger.Parse(name)
if err != nil { if err != nil {
return r, err return r, err
} }
r.Path = pt r.Path = pt
her, err := pkgs.Pkg(r.Path.Pkg) her, err := pkger.Pkg(r.Path.Pkg)
if err != nil { if err != nil {
return r, err return r, err
} }
m := map[paths.Path]bool{} m := map[pkger.Path]bool{}
root := r.Path.Name root := r.Path.Name
if !strings.HasPrefix(root, string(filepath.Separator)) { if !strings.HasPrefix(root, string(filepath.Separator)) {
@ -116,7 +115,7 @@ func Parse(name string) (Results, error) {
return nil return nil
}) })
var found []paths.Path var found []pkger.Path
for k := range m { for k := range m {
if len(k.String()) == 0 { if len(k.String()) == 0 {
@ -132,10 +131,10 @@ func Parse(name string) (Results, error) {
return r, err return r, err
} }
func sourceFiles(pt paths.Path) ([]paths.Path, error) { func sourceFiles(pt pkger.Path) ([]pkger.Path, error) {
var res []paths.Path var res []pkger.Path
her, err := pkgs.Pkg(pt.Pkg) her, err := pkger.Pkg(pt.Pkg)
if err != nil { if err != nil {
return res, err return res, err
@ -150,7 +149,7 @@ func sourceFiles(pt paths.Path) ([]paths.Path, error) {
return res, nil return res, nil
} }
c, err := pkgs.Current() c, err := pkger.Current()
if err != nil { if err != nil {
return res, err return res, err
} }
@ -189,7 +188,7 @@ func sourceFiles(pt paths.Path) ([]paths.Path, error) {
} }
n := strings.TrimPrefix(strings.TrimPrefix(p, her.Dir), "/") n := strings.TrimPrefix(strings.TrimPrefix(p, her.Dir), "/")
pt := paths.Path{ pt := pkger.Path{
Name: n, Name: n,
} }
res = append(res, pt) res = append(res, pt)
@ -200,6 +199,6 @@ func sourceFiles(pt paths.Path) ([]paths.Path, error) {
} }
type Results struct { type Results struct {
Paths []paths.Path Paths []pkger.Path
Path paths.Path Path pkger.Path
} }

View File

@ -5,23 +5,23 @@ import (
"go/ast" "go/ast"
"strconv" "strconv"
"github.com/markbates/pkger/paths" "github.com/markbates/pkger"
) )
type Visitor struct { type Visitor struct {
File string File string
Found map[paths.Path]bool Found map[pkger.Path]bool
errors []error errors []error
} }
func NewVisitor(p string) (*Visitor, error) { func NewVisitor(p string) (*Visitor, error) {
return &Visitor{ return &Visitor{
File: p, File: p,
Found: map[paths.Path]bool{}, Found: map[pkger.Path]bool{},
}, nil }, nil
} }
func (v *Visitor) Run() ([]paths.Path, error) { func (v *Visitor) Run() ([]pkger.Path, error) {
pf, err := parseFile(v.File) pf, err := parseFile(v.File)
if err != nil { if err != nil {
return nil, err return nil, err
@ -29,7 +29,7 @@ func (v *Visitor) Run() ([]paths.Path, error) {
ast.Walk(v, pf.Ast) ast.Walk(v, pf.Ast)
var found []paths.Path var found []pkger.Path
for k := range v.Found { for k := range v.Found {
found = append(found, k) found = append(found, k)
@ -40,7 +40,7 @@ func (v *Visitor) Run() ([]paths.Path, error) {
func (v *Visitor) addPath(p string) error { func (v *Visitor) addPath(p string) error {
p, _ = strconv.Unquote(p) p, _ = strconv.Unquote(p)
pt, err := paths.Parse(p) pt, err := pkger.Parse(p)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,4 +1,4 @@
package paths package pkger
import ( import (
"encoding/json" "encoding/json"

View File

@ -1,4 +1,4 @@
package paths package pkger
import ( import (
"testing" "testing"

View File

@ -1,6 +1,6 @@
// Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT. // Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT.
package paths package pkger
import ( import (
"sort" "sort"

View File

@ -1,10 +1,8 @@
package pkger package pkger
import "github.com/markbates/pkger/paths"
// Open opens the named file for reading. // Open opens the named file for reading.
func Open(p string) (*File, error) { func Open(p string) (*File, error) {
pt, err := paths.Parse(p) pt, err := Parse(p)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -13,7 +11,7 @@ func Open(p string) (*File, error) {
// Create creates the named file with mode 0666 (before umask), truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. If there is an error, it will be of type *PathError. // Create creates the named file with mode 0666 (before umask), truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. If there is an error, it will be of type *PathError.
func Create(p string) (*File, error) { func Create(p string) (*File, error) {
pt, err := paths.Parse(p) pt, err := Parse(p)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -5,17 +5,12 @@ import (
"strings" "strings"
) )
func createFile(i *index, p string, body ...string) (*File, error) { func createFile(p string, body ...string) (*File, error) {
pt, err := i.Parse(p)
if err != nil {
return nil, err
}
if len(body) == 0 { if len(body) == 0 {
body = append(body, radio) body = append(body, radio)
} }
f, err := i.Create(pt) f, err := Create(p)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -2,14 +2,12 @@ package pkger
import ( import (
"os" "os"
"github.com/markbates/pkger/paths"
) )
type WalkFunc func(paths.Path, os.FileInfo) error type WalkFunc func(Path, os.FileInfo) error
func Walk(p string, wf WalkFunc) error { func Walk(p string, wf WalkFunc) error {
pt, err := paths.Parse(p) pt, err := Parse(p)
if err != nil { if err != nil {
return err return err
} }