From e2b4c3882762406fd3da16f5865cfc3e36e048b5 Mon Sep 17 00:00:00 2001 From: Niall Sheridan Date: Wed, 1 Aug 2018 00:37:11 +0100 Subject: Migrate from esc to packr for static files --- vendor/github.com/gobuffalo/packr/builder/box.go | 76 ++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 vendor/github.com/gobuffalo/packr/builder/box.go (limited to 'vendor/github.com/gobuffalo/packr/builder/box.go') diff --git a/vendor/github.com/gobuffalo/packr/builder/box.go b/vendor/github.com/gobuffalo/packr/builder/box.go new file mode 100644 index 0000000..8490455 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/builder/box.go @@ -0,0 +1,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 +} -- cgit v1.2.3