2013-10-30 01:11:04 +00:00
|
|
|
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
|
|
|
|
// license. Its contents can be found at:
|
|
|
|
// http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
|
|
|
|
package bindata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
// writeRelease writes the release code file.
|
2013-10-30 13:45:04 +00:00
|
|
|
func writeRelease(w io.Writer, c *Config, toc []Asset) error {
|
|
|
|
err := writeReleaseHeader(w, c)
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range toc {
|
2013-10-30 13:45:04 +00:00
|
|
|
err = writeReleaseAsset(w, c, &toc[i])
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-30 01:16:14 +00:00
|
|
|
// writeReleaseHeader writes output file headers.
|
2013-10-30 01:11:04 +00:00
|
|
|
// This targets release builds.
|
2013-10-30 12:14:58 +00:00
|
|
|
func writeReleaseHeader(w io.Writer, c *Config) error {
|
2013-10-30 13:45:04 +00:00
|
|
|
if c.NoCompress {
|
2013-10-30 01:11:04 +00:00
|
|
|
if c.NoMemCopy {
|
2013-10-30 13:45:04 +00:00
|
|
|
return header_uncompressed_nomemcopy(w)
|
2013-10-30 01:11:04 +00:00
|
|
|
} else {
|
2013-10-30 13:45:04 +00:00
|
|
|
return header_uncompressed_memcopy(w)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if c.NoMemCopy {
|
2013-10-30 13:45:04 +00:00
|
|
|
return header_compressed_nomemcopy(w)
|
2013-10-30 01:11:04 +00:00
|
|
|
} else {
|
2013-10-30 13:45:04 +00:00
|
|
|
return header_compressed_memcopy(w)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
// writeReleaseAsset write a release entry for the given asset.
|
2013-10-30 01:11:04 +00:00
|
|
|
// A release entry is a function which embeds and returns
|
|
|
|
// the file's byte content.
|
2013-10-30 12:14:58 +00:00
|
|
|
func writeReleaseAsset(w io.Writer, c *Config, asset *Asset) error {
|
2013-10-30 01:11:04 +00:00
|
|
|
fd, err := os.Open(asset.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fd.Close()
|
|
|
|
|
2013-10-30 13:45:04 +00:00
|
|
|
if c.NoCompress {
|
2013-10-30 01:11:04 +00:00
|
|
|
if c.NoMemCopy {
|
2013-10-30 13:45:04 +00:00
|
|
|
return uncompressed_nomemcopy(w, asset, fd)
|
2013-10-30 01:11:04 +00:00
|
|
|
} else {
|
2013-10-30 13:45:04 +00:00
|
|
|
return uncompressed_memcopy(w, asset, fd)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if c.NoMemCopy {
|
2013-10-30 13:45:04 +00:00
|
|
|
return compressed_nomemcopy(w, asset, fd)
|
2013-10-30 01:11:04 +00:00
|
|
|
} else {
|
2013-10-30 13:45:04 +00:00
|
|
|
return compressed_memcopy(w, asset, fd)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
func header_compressed_nomemcopy(w io.Writer) error {
|
2013-10-30 13:45:04 +00:00
|
|
|
_, err := fmt.Fprintf(w, `import (
|
2013-10-30 01:11:04 +00:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2013-11-08 15:38:45 +00:00
|
|
|
"fmt"
|
2013-10-30 01:11:04 +00:00
|
|
|
"io"
|
|
|
|
"reflect"
|
2014-05-16 17:34:04 +00:00
|
|
|
"strings"
|
2013-10-30 01:11:04 +00:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2013-11-08 15:38:45 +00:00
|
|
|
func bindata_read(data, name string) ([]byte, error) {
|
2013-10-30 01:11:04 +00:00
|
|
|
var empty [0]byte
|
|
|
|
sx := (*reflect.StringHeader)(unsafe.Pointer(&data))
|
|
|
|
b := empty[:]
|
|
|
|
bx := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
|
|
bx.Data = sx.Data
|
|
|
|
bx.Len = len(data)
|
|
|
|
bx.Cap = bx.Len
|
|
|
|
|
|
|
|
gz, err := gzip.NewReader(bytes.NewBuffer(b))
|
|
|
|
if err != nil {
|
2013-11-08 15:38:45 +00:00
|
|
|
return nil, fmt.Errorf("Read %%q: %%v", name, err)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
_, err = io.Copy(&buf, gz)
|
|
|
|
gz.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
2013-11-08 15:38:45 +00:00
|
|
|
return nil, fmt.Errorf("Read %%q: %%v", name, err)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 15:38:45 +00:00
|
|
|
return buf.Bytes(), nil
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
func header_compressed_memcopy(w io.Writer) error {
|
2013-10-30 13:45:04 +00:00
|
|
|
_, err := fmt.Fprintf(w, `import (
|
2013-10-30 01:11:04 +00:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2013-11-08 15:38:45 +00:00
|
|
|
"fmt"
|
2013-10-30 01:11:04 +00:00
|
|
|
"io"
|
2014-05-16 17:34:04 +00:00
|
|
|
"strings"
|
2013-10-30 01:11:04 +00:00
|
|
|
)
|
|
|
|
|
2013-11-08 15:38:45 +00:00
|
|
|
func bindata_read(data []byte, name string) ([]byte, error) {
|
2013-10-30 01:11:04 +00:00
|
|
|
gz, err := gzip.NewReader(bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
2013-11-08 15:38:45 +00:00
|
|
|
return nil, fmt.Errorf("Read %%q: %%v", name, err)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
_, err = io.Copy(&buf, gz)
|
|
|
|
gz.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
2013-11-08 15:38:45 +00:00
|
|
|
return nil, fmt.Errorf("Read %%q: %%v", name, err)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 15:38:45 +00:00
|
|
|
return buf.Bytes(), nil
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
func header_uncompressed_nomemcopy(w io.Writer) error {
|
2013-10-30 13:45:04 +00:00
|
|
|
_, err := fmt.Fprintf(w, `import (
|
2013-11-08 15:38:45 +00:00
|
|
|
"fmt"
|
2013-10-30 01:11:04 +00:00
|
|
|
"reflect"
|
2014-05-16 17:34:04 +00:00
|
|
|
"strings"
|
2013-10-30 01:11:04 +00:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2013-11-08 15:38:45 +00:00
|
|
|
func bindata_read(data, name string) ([]byte, error) {
|
2013-10-30 01:11:04 +00:00
|
|
|
var empty [0]byte
|
|
|
|
sx := (*reflect.StringHeader)(unsafe.Pointer(&data))
|
|
|
|
b := empty[:]
|
|
|
|
bx := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
|
|
bx.Data = sx.Data
|
|
|
|
bx.Len = len(data)
|
|
|
|
bx.Cap = bx.Len
|
2013-11-08 15:38:45 +00:00
|
|
|
return b, nil
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
func header_uncompressed_memcopy(w io.Writer) error {
|
2013-11-08 15:38:45 +00:00
|
|
|
_, err := fmt.Fprintf(w, `import (
|
|
|
|
"fmt"
|
2014-05-16 17:34:04 +00:00
|
|
|
"strings"
|
2013-11-08 15:38:45 +00:00
|
|
|
)
|
|
|
|
`)
|
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
func compressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error {
|
|
|
|
_, err := fmt.Fprintf(w, `var _%s = "`, asset.Func)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-30 01:11:04 +00:00
|
|
|
|
|
|
|
gz := gzip.NewWriter(&StringWriter{Writer: w})
|
2013-10-30 12:14:58 +00:00
|
|
|
_, err = io.Copy(gz, r)
|
2013-10-30 01:11:04 +00:00
|
|
|
gz.Close()
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fmt.Fprintf(w, `"
|
2013-10-30 01:11:04 +00:00
|
|
|
|
2013-11-08 15:38:45 +00:00
|
|
|
func %s() ([]byte, error) {
|
2013-10-30 01:11:04 +00:00
|
|
|
return bindata_read(
|
|
|
|
_%s,
|
|
|
|
%q,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
`, asset.Func, asset.Func, asset.Name)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
func compressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
|
2013-11-08 15:38:45 +00:00
|
|
|
_, err := fmt.Fprintf(w, `func %s() ([]byte, error) {
|
2013-10-30 01:11:04 +00:00
|
|
|
return bindata_read([]byte{`, asset.Func)
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-30 01:11:04 +00:00
|
|
|
gz := gzip.NewWriter(&ByteWriter{Writer: w})
|
2013-10-30 12:14:58 +00:00
|
|
|
_, err = io.Copy(gz, r)
|
2013-10-30 01:11:04 +00:00
|
|
|
gz.Close()
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fmt.Fprintf(w, `
|
2013-10-30 01:11:04 +00:00
|
|
|
},
|
|
|
|
%q,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
`, asset.Name)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
func uncompressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error {
|
|
|
|
_, err := fmt.Fprintf(w, `var _%s = "`, asset.Func)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-30 01:16:14 +00:00
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
_, err = io.Copy(&StringWriter{Writer: w}, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-30 01:16:14 +00:00
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
_, err = fmt.Fprintf(w, `"
|
2013-10-30 01:11:04 +00:00
|
|
|
|
2013-11-08 15:38:45 +00:00
|
|
|
func %s() ([]byte, error) {
|
2013-10-30 01:16:14 +00:00
|
|
|
return bindata_read(
|
|
|
|
_%s,
|
|
|
|
%q,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
`, asset.Func, asset.Func, asset.Name)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
func uncompressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
|
2013-11-08 15:38:45 +00:00
|
|
|
_, err := fmt.Fprintf(w, `func %s() ([]byte, error) {
|
2013-10-30 01:11:04 +00:00
|
|
|
return []byte{`, asset.Func)
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-30 01:11:04 +00:00
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
_, err = io.Copy(&ByteWriter{Writer: w}, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-30 01:11:04 +00:00
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
_, err = fmt.Fprintf(w, `
|
2013-11-08 15:38:45 +00:00
|
|
|
}, nil
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|