Fix extreme memory bloat during compile time. When using uncompressed mode show raw UTF-8 strings for better diffs.

This commit is contained in:
Jessica Forrester 2014-11-20 10:10:11 -05:00
parent da92400a92
commit 2c5bf09f1b

View File

@ -5,10 +5,13 @@
package bindata package bindata
import ( import (
"bytes"
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"unicode/utf8"
) )
// writeRelease writes the release code file. // writeRelease writes the release code file.
@ -72,6 +75,19 @@ func writeReleaseAsset(w io.Writer, c *Config, asset *Asset) error {
} }
} }
// sanitize prepares a valid UTF-8 string as a raw string constant.
// Based on https://code.google.com/p/go/source/browse/godoc/static/makestatic.go?repo=tools
func sanitize(b []byte) []byte {
// Replace ` with `+"`"+`
b = bytes.Replace(b, []byte("`"), []byte("`+\"`\"+`"), -1)
// Replace BOM with `+"\xEF\xBB\xBF"+`
// (A BOM is valid UTF-8 but not permitted in Go source files.
// I wouldn't bother handling this, but for some insane reason
// jquery.js has a BOM somewhere in the middle.)
return bytes.Replace(b, []byte("\xEF\xBB\xBF"), []byte("`+\"\\xEF\\xBB\\xBF\"+`"), -1)
}
func header_compressed_nomemcopy(w io.Writer) error { func header_compressed_nomemcopy(w io.Writer) error {
_, err := fmt.Fprintf(w, `import ( _, err := fmt.Fprintf(w, `import (
"bytes" "bytes"
@ -202,14 +218,12 @@ func %s() ([]byte, error) {
} }
func compressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error { func compressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err := fmt.Fprintf(w, `func %s() ([]byte, error) { _, err := fmt.Fprintf(w, `var _%s = []byte("`, asset.Func)
return bindata_read([]byte{`, asset.Func)
if err != nil { if err != nil {
return nil return err
} }
gz := gzip.NewWriter(&ByteWriter{Writer: w}) gz := gzip.NewWriter(&StringWriter{Writer: w})
_, err = io.Copy(gz, r) _, err = io.Copy(gz, r)
gz.Close() gz.Close()
@ -217,13 +231,16 @@ func compressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
return err return err
} }
_, err = fmt.Fprintf(w, ` _, err = fmt.Fprintf(w, `")
},
func %s() ([]byte, error) {
return bindata_read(
_%s,
%q, %q,
) )
} }
`, asset.Name) `, asset.Func, asset.Func, asset.Name)
return err return err
} }
@ -252,21 +269,27 @@ func %s() ([]byte, error) {
} }
func uncompressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error { func uncompressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err := fmt.Fprintf(w, `func %s() ([]byte, error) { _, err := fmt.Fprintf(w, `var _%s = []byte(`, asset.Func)
return []byte{`, asset.Func)
if err != nil { if err != nil {
return err return err
} }
_, err = io.Copy(&ByteWriter{Writer: w}, r) b, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
return err return err
} }
if utf8.Valid(b) {
fmt.Fprintf(w, "`%s`", sanitize(b))
} else {
fmt.Fprintf(w, "%q", b)
}
_, err = fmt.Fprintf(w, ` _, err = fmt.Fprintf(w, `)
}, nil
func %s() ([]byte, error) {
return _%s, nil
} }
`) `, asset.Func, asset.Func)
return err return err
} }