bindata/release.go

271 lines
4.8 KiB
Go
Raw Permalink Normal View History

// 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"
)
// writeRelease writes the release code file.
func writeRelease(w io.Writer, c *Config, toc []Asset) error {
err := writeReleaseHeader(w, c)
if err != nil {
return err
}
for i := range toc {
err = writeReleaseAsset(w, c, &toc[i])
if err != nil {
return err
}
}
return nil
}
// writeReleaseHeader writes output file headers.
// This targets release builds.
func writeReleaseHeader(w io.Writer, c *Config) error {
if c.NoCompress {
if c.NoMemCopy {
return header_uncompressed_nomemcopy(w)
} else {
return header_uncompressed_memcopy(w)
}
} else {
if c.NoMemCopy {
return header_compressed_nomemcopy(w)
} else {
return header_compressed_memcopy(w)
}
}
}
// writeReleaseAsset write a release entry for the given asset.
// A release entry is a function which embeds and returns
// the file's byte content.
func writeReleaseAsset(w io.Writer, c *Config, asset *Asset) error {
fd, err := os.Open(asset.Path)
if err != nil {
return err
}
defer fd.Close()
if c.NoCompress {
if c.NoMemCopy {
return uncompressed_nomemcopy(w, asset, fd)
} else {
return uncompressed_memcopy(w, asset, fd)
}
} else {
if c.NoMemCopy {
return compressed_nomemcopy(w, asset, fd)
} else {
return compressed_memcopy(w, asset, fd)
}
}
return nil
}
func header_compressed_nomemcopy(w io.Writer) error {
_, err := fmt.Fprintf(w, `import (
"bytes"
"compress/gzip"
"fmt"
"io"
"reflect"
"unsafe"
)
func bindata_read(data, name string) ([]byte, error) {
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 {
return nil, fmt.Errorf("Read %%q: %%v", name, err)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, gz)
gz.Close()
if err != nil {
return nil, fmt.Errorf("Read %%q: %%v", name, err)
}
return buf.Bytes(), nil
}
`)
return err
}
func header_compressed_memcopy(w io.Writer) error {
_, err := fmt.Fprintf(w, `import (
"bytes"
"compress/gzip"
"fmt"
"io"
)
func bindata_read(data []byte, name string) ([]byte, error) {
gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("Read %%q: %%v", name, err)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, gz)
gz.Close()
if err != nil {
return nil, fmt.Errorf("Read %%q: %%v", name, err)
}
return buf.Bytes(), nil
}
`)
return err
}
func header_uncompressed_nomemcopy(w io.Writer) error {
_, err := fmt.Fprintf(w, `import (
"fmt"
"reflect"
"unsafe"
)
func bindata_read(data, name string) ([]byte, error) {
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
return b, nil
}
`)
return err
}
func header_uncompressed_memcopy(w io.Writer) error {
_, err := fmt.Fprintf(w, `import (
"fmt"
)
`)
return err
}
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
}
gz := gzip.NewWriter(&StringWriter{Writer: w})
_, err = io.Copy(gz, r)
gz.Close()
if err != nil {
return err
}
_, err = fmt.Fprintf(w, `"
func %s() ([]byte, error) {
return bindata_read(
_%s,
%q,
)
}
`, asset.Func, asset.Func, asset.Name)
return err
}
func compressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err := fmt.Fprintf(w, `func %s() ([]byte, error) {
return bindata_read([]byte{`, asset.Func)
if err != nil {
return nil
}
gz := gzip.NewWriter(&ByteWriter{Writer: w})
_, err = io.Copy(gz, r)
gz.Close()
if err != nil {
return err
}
_, err = fmt.Fprintf(w, `
},
%q,
)
}
`, asset.Name)
return err
}
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
}
_, err = io.Copy(&StringWriter{Writer: w}, r)
if err != nil {
return err
}
_, err = fmt.Fprintf(w, `"
func %s() ([]byte, error) {
return bindata_read(
_%s,
%q,
)
}
`, asset.Func, asset.Func, asset.Name)
return err
}
func uncompressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err := fmt.Fprintf(w, `func %s() ([]byte, error) {
return []byte{`, asset.Func)
if err != nil {
return err
}
_, err = io.Copy(&ByteWriter{Writer: w}, r)
if err != nil {
return err
}
_, err = fmt.Fprintf(w, `
}, nil
}
`)
return err
}