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 (
|
2014-11-20 15:10:11 +00:00
|
|
|
"bytes"
|
2013-10-30 01:11:04 +00:00
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-11-20 15:10:11 +00:00
|
|
|
"io/ioutil"
|
2013-10-30 01:11:04 +00:00
|
|
|
"os"
|
2014-11-20 15:10:11 +00:00
|
|
|
"unicode/utf8"
|
2013-10-30 01:11:04 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2014-12-07 22:35:15 +00:00
|
|
|
var err error
|
2013-10-30 13:45:04 +00:00
|
|
|
if c.NoCompress {
|
2013-10-30 01:11:04 +00:00
|
|
|
if c.NoMemCopy {
|
2014-12-07 22:35:15 +00:00
|
|
|
err = header_uncompressed_nomemcopy(w)
|
2013-10-30 01:11:04 +00:00
|
|
|
} else {
|
2014-12-07 22:35:15 +00:00
|
|
|
err = header_uncompressed_memcopy(w)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if c.NoMemCopy {
|
2014-12-07 22:35:15 +00:00
|
|
|
err = header_compressed_nomemcopy(w)
|
2013-10-30 01:11:04 +00:00
|
|
|
} else {
|
2014-12-07 22:35:15 +00:00
|
|
|
err = header_compressed_memcopy(w)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-07 22:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return header_release_common(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 {
|
2014-12-07 22:35:15 +00:00
|
|
|
err = uncompressed_nomemcopy(w, asset, fd)
|
2013-10-30 01:11:04 +00:00
|
|
|
} else {
|
2014-12-07 22:35:15 +00:00
|
|
|
err = uncompressed_memcopy(w, asset, fd)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if c.NoMemCopy {
|
2014-12-07 22:35:15 +00:00
|
|
|
err = compressed_nomemcopy(w, asset, fd)
|
2013-10-30 01:11:04 +00:00
|
|
|
} else {
|
2014-12-07 22:35:15 +00:00
|
|
|
err = compressed_memcopy(w, asset, fd)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-07 22:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-10 14:41:54 +00:00
|
|
|
return asset_release_common(w, c, asset)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 15:10:11 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
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 (
|
2014-05-28 12:43:02 +00:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
2014-12-07 22:35:15 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
2014-12-08 00:30:11 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2013-10-30 01:11:04 +00:00
|
|
|
)
|
|
|
|
|
2015-05-11 19:42:14 +00:00
|
|
|
func bindataRead(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)
|
2015-05-08 20:05:20 +00:00
|
|
|
clErr := gz.Close()
|
2013-10-30 01:11:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2015-05-08 20:05:20 +00:00
|
|
|
if clErr != nil {
|
|
|
|
return nil, 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 (
|
2014-05-28 12:43:02 +00:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2014-12-07 22:35:15 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
2014-12-08 00:30:11 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2013-10-30 01:11:04 +00:00
|
|
|
)
|
|
|
|
|
2015-05-11 19:42:14 +00:00
|
|
|
func bindataRead(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)
|
2015-05-08 20:05:20 +00:00
|
|
|
clErr := gz.Close()
|
2013-10-30 01:11:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2015-05-08 20:05:20 +00:00
|
|
|
if clErr != nil {
|
|
|
|
return nil, 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 (
|
2014-05-28 12:43:02 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
2014-12-07 22:35:15 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
2014-12-08 00:30:11 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2013-10-30 01:11:04 +00:00
|
|
|
)
|
|
|
|
|
2015-05-11 19:42:14 +00:00
|
|
|
func bindataRead(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 (
|
2014-05-28 12:43:02 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-12-07 22:35:15 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
2014-12-08 00:30:11 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2013-11-08 15:38:45 +00:00
|
|
|
)
|
|
|
|
`)
|
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 22:35:15 +00:00
|
|
|
func header_release_common(w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(w, `type asset struct {
|
|
|
|
bytes []byte
|
|
|
|
info os.FileInfo
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:45:16 +00:00
|
|
|
type bindataFileInfo struct {
|
2014-12-07 22:35:15 +00:00
|
|
|
name string
|
|
|
|
size int64
|
|
|
|
mode os.FileMode
|
|
|
|
modTime time.Time
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:45:16 +00:00
|
|
|
func (fi bindataFileInfo) Name() string {
|
2014-12-07 22:35:15 +00:00
|
|
|
return fi.name
|
|
|
|
}
|
2015-05-11 19:45:16 +00:00
|
|
|
func (fi bindataFileInfo) Size() int64 {
|
2014-12-07 22:35:15 +00:00
|
|
|
return fi.size
|
|
|
|
}
|
2015-05-11 19:45:16 +00:00
|
|
|
func (fi bindataFileInfo) Mode() os.FileMode {
|
2014-12-07 22:35:15 +00:00
|
|
|
return fi.mode
|
|
|
|
}
|
2015-05-11 19:45:16 +00:00
|
|
|
func (fi bindataFileInfo) ModTime() time.Time {
|
2014-12-07 22:35:15 +00:00
|
|
|
return fi.modTime
|
|
|
|
}
|
2015-05-11 19:45:16 +00:00
|
|
|
func (fi bindataFileInfo) IsDir() bool {
|
2014-12-07 22:35:15 +00:00
|
|
|
return false
|
|
|
|
}
|
2015-05-11 19:45:16 +00:00
|
|
|
func (fi bindataFileInfo) Sys() interface{} {
|
2014-12-07 22:35:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-05-12 01:44:07 +00:00
|
|
|
func %sBytes() ([]byte, error) {
|
2015-05-11 19:42:14 +00:00
|
|
|
return bindataRead(
|
2013-10-30 01:11:04 +00:00
|
|
|
_%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 {
|
2014-11-20 15:10:11 +00:00
|
|
|
_, err := fmt.Fprintf(w, `var _%s = []byte("`, asset.Func)
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
2014-11-20 15:10:11 +00:00
|
|
|
return err
|
2013-10-30 12:14:58 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 15:10:11 +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
|
|
|
|
}
|
|
|
|
|
2014-11-20 15:10:11 +00:00
|
|
|
_, err = fmt.Fprintf(w, `")
|
|
|
|
|
2015-05-12 01:44:07 +00:00
|
|
|
func %sBytes() ([]byte, error) {
|
2015-05-11 19:42:14 +00:00
|
|
|
return bindataRead(
|
2014-11-20 15:10:11 +00:00
|
|
|
_%s,
|
2013-10-30 01:11:04 +00:00
|
|
|
%q,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-11-20 15:10:11 +00:00
|
|
|
`, 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_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
|
|
|
|
2015-05-12 01:44:07 +00:00
|
|
|
func %sBytes() ([]byte, error) {
|
2015-05-11 19:42:14 +00:00
|
|
|
return bindataRead(
|
2013-10-30 01:16:14 +00:00
|
|
|
_%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 {
|
2014-11-20 15:10:11 +00:00
|
|
|
_, err := fmt.Fprintf(w, `var _%s = []byte(`, asset.Func)
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-30 01:11:04 +00:00
|
|
|
|
2014-11-20 15:10:11 +00:00
|
|
|
b, err := ioutil.ReadAll(r)
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-20 15:10:11 +00:00
|
|
|
if utf8.Valid(b) {
|
|
|
|
fmt.Fprintf(w, "`%s`", sanitize(b))
|
|
|
|
} else {
|
2015-04-01 19:44:58 +00:00
|
|
|
fmt.Fprintf(w, "%+q", b)
|
2014-11-20 15:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fmt.Fprintf(w, `)
|
2013-10-30 01:11:04 +00:00
|
|
|
|
2015-05-12 01:44:07 +00:00
|
|
|
func %sBytes() ([]byte, error) {
|
2014-11-20 15:10:11 +00:00
|
|
|
return _%s, nil
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 15:10:11 +00:00
|
|
|
`, asset.Func, asset.Func)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
2014-12-07 22:35:15 +00:00
|
|
|
|
2015-03-10 14:41:54 +00:00
|
|
|
func asset_release_common(w io.Writer, c *Config, asset *Asset) error {
|
2014-12-07 22:35:15 +00:00
|
|
|
fi, err := os.Stat(asset.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-10 14:41:54 +00:00
|
|
|
mode := uint(fi.Mode())
|
|
|
|
modTime := fi.ModTime().Unix()
|
|
|
|
|
|
|
|
if c.Mode > 0 {
|
|
|
|
mode = uint(os.ModePerm) & c.Mode
|
|
|
|
}
|
|
|
|
if c.ModTime > 0 {
|
|
|
|
modTime = c.ModTime
|
|
|
|
}
|
2014-12-07 22:35:15 +00:00
|
|
|
_, err = fmt.Fprintf(w, `func %s() (*asset, error) {
|
2015-05-12 01:44:07 +00:00
|
|
|
bytes, err := %sBytes()
|
2014-12-07 22:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:45:16 +00:00
|
|
|
info := bindataFileInfo{name: %q, size: %d, mode: os.FileMode(%d), modTime: time.Unix(%d, 0)}
|
2014-12-07 22:35:15 +00:00
|
|
|
a := &asset{bytes: bytes, info: info}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2015-03-10 14:41:54 +00:00
|
|
|
`, asset.Func, asset.Func, asset.Name, fi.Size(), mode, modTime)
|
2014-12-07 22:35:15 +00:00
|
|
|
return err
|
|
|
|
}
|