Codebase list golang-github-gobuffalo-packr / 0dfae2a
add option to gzip compress files Jason Ish 7 years ago
7 changed file(s) with 103 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
77 "path/filepath"
88 "runtime"
99 "strings"
10 "compress/gzip"
1011 )
1112
1213 // NewBox returns a Box that can be used to
7172 return true
7273 }
7374
75 func (b Box) decompress(bb []byte) []byte {
76 reader, err := gzip.NewReader(bytes.NewReader(bb))
77 if err != nil {
78 return bb
79 }
80 data, err := ioutil.ReadAll(reader)
81 if err != nil {
82 return bb
83 }
84 return data
85 }
86
7487 func (b Box) find(name string) (File, error) {
7588 name = strings.TrimPrefix(name, "/")
7689 name = strings.Replace(name, "\\", "/", -1)
7790 if _, ok := data[b.Path]; ok {
7891 if bb, ok := data[b.Path][name]; ok {
92 bb = b.decompress(bb)
7993 return newVirtualFile(name, bb), nil
8094 }
8195 }
77 "strings"
88
99 "github.com/pkg/errors"
10 "bytes"
11 "compress/gzip"
1012 )
1113
1214 type box struct {
1416 Files []file
1517 }
1618
17 func (b *box) Walk(root string) error {
19 func (b *box) Walk(root string, compress bool) error {
1820 return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
1921 if info == nil || info.IsDir() {
2022 return nil
2931 if err != nil {
3032 return errors.WithStack(err)
3133 }
34 if compress {
35 bb, err = compressFile(bb)
36 if err != nil {
37 return errors.WithStack(err)
38 }
39 }
3240 bb, err = json.Marshal(bb)
3341 if err != nil {
3442 return errors.WithStack(err)
3947 return nil
4048 })
4149 }
50
51 func compressFile(bb []byte) ([]byte, error) {
52 var buf bytes.Buffer
53 writer := gzip.NewWriter(&buf)
54 _, err := writer.Write(bb)
55 if err != nil {
56 return bb, errors.WithStack(err)
57 }
58 err = writer.Close()
59 if err != nil {
60 return bb, errors.WithStack(err)
61 }
62 return buf.Bytes(), nil
63 }
2424 type Builder struct {
2525 context.Context
2626 RootPath string
27 compress bool
2728 pkgs map[string]pkg
2829 }
2930
9798 Name: m[1],
9899 Files: []file{},
99100 }
100 err = bx.Walk(filepath.Join(pk.Dir, bx.Name))
101 err = bx.Walk(filepath.Join(pk.Dir, bx.Name), b.compress)
101102 if err != nil {
102103 return errors.WithStack(err)
103104 }
123124 }
124125
125126 // New Builder with a given context and path
126 func New(ctx context.Context, path string) *Builder {
127 func New(ctx context.Context, path string, compress bool) *Builder {
127128 return &Builder{
128129 Context: ctx,
129130 RootPath: path,
130131 pkgs: map[string]pkg{},
132 compress: compress,
131133 }
132134 }
2323 fooPackr := filepath.Join(root, "foo", "foo-packr.go")
2424 r.False(fileExists(fooPackr))
2525
26 b := New(context.Background(), root)
26 b := New(context.Background(), root, false)
2727 err := b.Run()
2828 r.NoError(err)
2929
4242 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../templates", "index.html", "\"PCFET0NUWVBFIGh0bWw+CjxodG1sPgogIDxoZWFkPgogICAgPG1ldGEgY2hhcnNldD0idXRmLTgiIC8+CiAgICA8bWV0YSBuYW1lPSJ2aWV3cG9ydCIgY29udGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoIiAvPgogICAgPHRpdGxlPklOREVYPC90aXRsZT4KICAgIGxpbmsKICA8L2hlYWQ+CiAgPGJvZHk+CiAgICBib2R5CiAgPC9ib2R5Pgo8L2h0bWw+Cg==\"")`)))
4343 }
4444
45 func Test_Builder_Run_Compress(t *testing.T) {
46 r := require.New(t)
47
48 root := filepath.Join("..", "example")
49 defer Clean(root)
50
51 exPackr := filepath.Join(root, "example-packr.go")
52 r.False(fileExists(exPackr))
53
54 fooPackr := filepath.Join(root, "foo", "foo-packr.go")
55 r.False(fileExists(fooPackr))
56
57 b := New(context.Background(), root, true)
58 err := b.Run()
59 r.NoError(err)
60
61 r.True(fileExists(exPackr))
62 r.True(fileExists(fooPackr))
63
64 bb, err := ioutil.ReadFile(exPackr)
65 r.NoError(err)
66 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("./assets", "app.css", "\"H4sIAAAAAAAA/0rKT6lUqOZSUEhKTM5OL8ovzUuxUihKTbHmquUCBAAA//8hHmttHAAAAA==\"`)))
67 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("./assets", "app.js", "\"H4sIAAAAAAAA/0rMSS0q0VDKSM3JyVdU0rTmAgQAAP//8IaimBEAAAA=\"")`)))
68
69 bb, err = ioutil.ReadFile(fooPackr)
70 r.NoError(err)
71 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../assets", "app.css", "\"H4sIAAAAAAAA/0rKT6lUqOZSUEhKTM5OL8ovzUuxUihKTbHmquUCBAAA//8hHmttHAAAAA==\"")`)))
72 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../assets", "app.js", "\"H4sIAAAAAAAA/0rMSS0q0VDKSM3JyVdU0rTmAgQAAP//8IaimBEAAAA=\"")`)))
73 r.True(bytes.Contains(bb, []byte(`packr.PackJSONBytes("../templates", "index.html", "\"H4sIAAAAAAAA/0yOvQ7CMAyEd57CZK+yMjhdaAcWYGCAMSRGicgPKqYVb4+SCMHkO3866cP1cNieLscRHMfQr7AdAHSkbQkAGIk1GKenJ7ESL751GwHyHyYdSYnZ0/LIEwswOTElVmLxlp2yNHtDXS2/JXsO1O/2w3hG2UoFwad7MZBfBbxm+26spMraC2Xz/QQAAP//5yPZVscAAAA=\"")`)))
74 }
75
4576 func Test_Binary_Builds(t *testing.T) {
4677 r := require.New(t)
4778 pwd, _ := os.Getwd()
5182 defer Clean(root)
5283 defer os.RemoveAll(filepath.Join(root, "bin"))
5384
54 b := New(context.Background(), root)
85 b := New(context.Background(), root, false)
5586 err := b.Run()
5687 r.NoError(err)
5788
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 {
16 b := builder.New(context.Background(), input)
17 b := builder.New(context.Background(), input, compress)
1718 return b.Run()
1819 },
1920 }
2021
2122 func init() {
2223 rootCmd.Flags().StringVarP(&input, "input", "i", ".", "path to scan for packr Boxes")
24 rootCmd.Flags().BoolVarP(&compress, "compress", "z", false, "compress box contents")
2325 }
2426
2527 // 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 }