Codebase list golang-github-gobuffalo-packr / 7bb4873
Merge branch 'compression' of https://github.com/jasonish/packr into jasonish-compression Mark Bates 6 years ago
7 changed file(s) with 106 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
77 "path/filepath"
88 "runtime"
99 "strings"
10
11 "compress/gzip"
1012
1113 "github.com/pkg/errors"
1214 )
7375 return true
7476 }
7577
78 func (b Box) decompress(bb []byte) []byte {
79 reader, err := gzip.NewReader(bytes.NewReader(bb))
80 if err != nil {
81 return bb
82 }
83 data, err := ioutil.ReadAll(reader)
84 if err != nil {
85 return bb
86 }
87 return data
88 }
89
7690 func (b Box) find(name string) (File, error) {
7791 name = strings.TrimPrefix(name, "/")
7892 name = filepath.ToSlash(name)
7993 if _, ok := data[b.Path]; ok {
8094 if bb, ok := data[b.Path][name]; ok {
95 bb = b.decompress(bb)
8196 return newVirtualFile(name, bb), nil
8297 }
8398 if filepath.Ext(name) != "" {
66 "path/filepath"
77 "strings"
88
9 "bytes"
10 "compress/gzip"
11
912 "github.com/pkg/errors"
1013 )
1114
1215 type box struct {
13 Name string
14 Files []file
16 Name string
17 Files []file
18 compress bool
1519 }
1620
1721 func (b *box) Walk(root string) error {
3236 if err != nil {
3337 return errors.WithStack(err)
3438 }
39 if b.compress {
40 bb, err = compressFile(bb)
41 if err != nil {
42 return errors.WithStack(err)
43 }
44 }
3545 bb, err = json.Marshal(bb)
3646 if err != nil {
3747 return errors.WithStack(err)
4252 return nil
4353 })
4454 }
55
56 func compressFile(bb []byte) ([]byte, error) {
57 var buf bytes.Buffer
58 writer := gzip.NewWriter(&buf)
59 _, err := writer.Write(bb)
60 if err != nil {
61 return bb, errors.WithStack(err)
62 }
63 err = writer.Close()
64 if err != nil {
65 return bb, errors.WithStack(err)
66 }
67 return buf.Bytes(), nil
68 }
2222 IgnoredBoxes []string
2323 pkgs map[string]pkg
2424 moot *sync.Mutex
25 Compress bool
2526 }
2627
2728 // Run the builder.
9596 }
9697 }
9798 bx := &box{
98 Name: n,
99 Files: []file{},
99 Name: n,
100 Files: []file{},
101 compress: b.Compress,
100102 }
101103 p := filepath.Join(pk.Dir, bx.Name)
102104 if err := bx.Walk(p); err != nil {
4444 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../templates", "index.html", "\"PCFET0NUWVBFIGh0bWw+CjxodG1sPgogIDxoZWFkPgogICAgPG1ldGEgY2hhcnNldD0idXRmLTgiIC8+CiAgICA8bWV0YSBuYW1lPSJ2aWV3cG9ydCIgY29udGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoIiAvPgogICAgPHRpdGxlPklOREVYPC90aXRsZT4KICAgIGxpbmsKICA8L2hlYWQ+CiAgPGJvZHk+CiAgICBib2R5CiAgPC9ib2R5Pgo8L2h0bWw+Cg==\"")`)))
4545 }
4646
47 func Test_Builder_Run_Compress(t *testing.T) {
48 r := require.New(t)
49
50 root := filepath.Join("..", "example")
51 defer Clean(root)
52
53 exPackr := filepath.Join(root, "example-packr.go")
54 r.False(fileExists(exPackr))
55
56 fooPackr := filepath.Join(root, "foo", "foo-packr.go")
57 r.False(fileExists(fooPackr))
58
59 b := New(context.Background(), root)
60 b.Compress = true
61 err := b.Run()
62 r.NoError(err)
63
64 r.True(fileExists(exPackr))
65 r.True(fileExists(fooPackr))
66
67 bb, err := ioutil.ReadFile(exPackr)
68 r.NoError(err)
69 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("./assets", "app.css", "\"H4sIAAAAAAAA/0rKT6lUqOZSUEhKTM5OL8ovzUuxUihKTbHmquUCBAAA//8hHmttHAAAAA==\"`)))
70 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("./assets", "app.js", "\"H4sIAAAAAAAA/0rMSS0q0VDKSM3JyVdU0rTmAgQAAP//8IaimBEAAAA=\"")`)))
71
72 bb, err = ioutil.ReadFile(fooPackr)
73 r.NoError(err)
74 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../assets", "app.css", "\"H4sIAAAAAAAA/0rKT6lUqOZSUEhKTM5OL8ovzUuxUihKTbHmquUCBAAA//8hHmttHAAAAA==\"")`)))
75 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../assets", "app.js", "\"H4sIAAAAAAAA/0rMSS0q0VDKSM3JyVdU0rTmAgQAAP//8IaimBEAAAA=\"")`)))
76 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../templates", "index.html", "\"H4sIAAAAAAAA/0yOvQ7CMAyEd57CZK+yMjhdaAcWYGCAMSRGicgPKqYVb4+SCMHkO3866cP1cNieLscRHMfQr7AdAHSkbQkAGIk1GKenJ7ESL751GwHyHyYdSYnZ0/LIEwswOTElVmLxlp2yNHtDXS2/JXsO1O/2w3hG2UoFwad7MZBfBbxm+26spMraC2Xz/QQAAP//5yPZVscAAAA=\"")`)))
77 }
78
4779 func Test_Binary_Builds(t *testing.T) {
4880 r := require.New(t)
4981 pwd, _ := os.Getwd()
88 )
99
1010 var input string
11 var compress bool
1112
1213 var rootCmd = &cobra.Command{
1314 Use: "packr",
1415 Short: "compiles static files into Go files",
1516 RunE: func(cmd *cobra.Command, args []string) error {
1617 b := builder.New(context.Background(), input)
18 b.Compress = compress
1719 return b.Run()
1820 },
1921 }
2123 func init() {
2224 pwd, _ := os.Getwd()
2325 rootCmd.Flags().StringVarP(&input, "input", "i", pwd, "path to scan for packr Boxes")
26 rootCmd.Flags().BoolVarP(&compress, "compress", "z", false, "compress box contents")
2427 }
2528
2629 // Execute the commands
22 import (
33 "encoding/json"
44 "sync"
5 "bytes"
6 "compress/gzip"
57 )
68
79 var gil = &sync.Mutex{}
1719 data[box][name] = bb
1820 }
1921
22 // PackBytesGzip packets the gzipped compressed bytes into a box.
23 func PackBytesGzip(box string, name string, bb []byte) error {
24 var buf bytes.Buffer
25 w := gzip.NewWriter(&buf)
26 _, err := w.Write(bb)
27 if err != nil {
28 return err
29 }
30 err = w.Close()
31 if err != nil {
32 return err
33 }
34 PackBytes(box, name, buf.Bytes())
35 return nil
36 }
37
2038 // PackJSONBytes packs JSON encoded bytes for a file into a box.
2139 func PackJSONBytes(box string, name string, jbb string) error {
2240 bb := []byte{}
3232 r.NoError(err)
3333 r.Equal([]byte("json bytes"), s)
3434 }
35
36 func Test_PackBytesGzip(t *testing.T) {
37 r := require.New(t)
38 err := PackBytesGzip(testBox.Path, "gzip", []byte("gzip foobar"))
39 r.NoError(err)
40 s := testBox.String("gzip")
41 r.Equal("gzip foobar", s)
42 }