aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/gobuffalo/packr/builder/box.go
blob: 8490455dcbbe9ec785e77281cead887f36ae60ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package builder

import (
	"bytes"
	"compress/gzip"
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/pkg/errors"
)

type box struct {
	Name     string
	Files    []file
	compress bool
}

func (b *box) Walk(root string) error {
	root, err := filepath.EvalSymlinks(root)
	if err != nil {
		return errors.WithStack(err)
	}
	if _, err := os.Stat(root); err != nil {
		// return nil
		return errors.Errorf("could not find folder for box: %s", root)
	}
	return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if info == nil || info.IsDir() || strings.HasSuffix(info.Name(), "-packr.go") {
			return nil
		}
		name := strings.Replace(path, root+string(os.PathSeparator), "", 1)
		name = strings.Replace(name, "\\", "/", -1)
		f := file{
			Name: name,
		}

		DebugLog("packing file %s\n", f.Name)

		bb, err := ioutil.ReadFile(path)
		if err != nil {
			return errors.WithStack(err)
		}
		if b.compress {
			bb, err = compressFile(bb)
			if err != nil {
				return errors.WithStack(err)
			}
		}
		bb, err = json.Marshal(bb)
		if err != nil {
			return errors.WithStack(err)
		}
		f.Contents = strings.Replace(string(bb), "\"", "\\\"", -1)

		DebugLog("packed file %s\n", f.Name)
		b.Files = append(b.Files, f)
		return nil
	})
}

func compressFile(bb []byte) ([]byte, error) {
	var buf bytes.Buffer
	writer := gzip.NewWriter(&buf)
	_, err := writer.Write(bb)
	if err != nil {
		return bb, errors.WithStack(err)
	}
	err = writer.Close()
	if err != nil {
		return bb, errors.WithStack(err)
	}
	return buf.Bytes(), nil
}