From 5dec5f37be4a67db3de39d403b4a2e321b2f559d Mon Sep 17 00:00:00 2001 From: Ryosuke IWANAGA Date: Mon, 8 Dec 2014 07:35:15 +0900 Subject: [PATCH] Adjust release mode for AssetInfo() --- release.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 86 insertions(+), 12 deletions(-) diff --git a/release.go b/release.go index 527d928..cb8db86 100644 --- a/release.go +++ b/release.go @@ -34,19 +34,24 @@ func writeRelease(w io.Writer, c *Config, toc []Asset) error { // writeReleaseHeader writes output file headers. // This targets release builds. func writeReleaseHeader(w io.Writer, c *Config) error { + var err error if c.NoCompress { if c.NoMemCopy { - return header_uncompressed_nomemcopy(w) + err = header_uncompressed_nomemcopy(w) } else { - return header_uncompressed_memcopy(w) + err = header_uncompressed_memcopy(w) } } else { if c.NoMemCopy { - return header_compressed_nomemcopy(w) + err = header_compressed_nomemcopy(w) } else { - return header_compressed_memcopy(w) + err = header_compressed_memcopy(w) } } + if err != nil { + return err + } + return header_release_common(w) } // writeReleaseAsset write a release entry for the given asset. @@ -62,17 +67,21 @@ func writeReleaseAsset(w io.Writer, c *Config, asset *Asset) error { if c.NoCompress { if c.NoMemCopy { - return uncompressed_nomemcopy(w, asset, fd) + err = uncompressed_nomemcopy(w, asset, fd) } else { - return uncompressed_memcopy(w, asset, fd) + err = uncompressed_memcopy(w, asset, fd) } } else { if c.NoMemCopy { - return compressed_nomemcopy(w, asset, fd) + err = compressed_nomemcopy(w, asset, fd) } else { - return compressed_memcopy(w, asset, fd) + err = compressed_memcopy(w, asset, fd) } } + if err != nil { + return err + } + return asset_release_common(w, asset) } // sanitize prepares a valid UTF-8 string as a raw string constant. @@ -97,6 +106,8 @@ func header_compressed_nomemcopy(w io.Writer) error { "reflect" "strings" "unsafe" + "os" + "time" ) func bindata_read(data, name string) ([]byte, error) { @@ -135,6 +146,8 @@ func header_compressed_memcopy(w io.Writer) error { "fmt" "io" "strings" + "os" + "time" ) func bindata_read(data []byte, name string) ([]byte, error) { @@ -164,6 +177,8 @@ func header_uncompressed_nomemcopy(w io.Writer) error { "reflect" "strings" "unsafe" + "os" + "time" ) func bindata_read(data, name string) ([]byte, error) { @@ -185,11 +200,49 @@ func header_uncompressed_memcopy(w io.Writer) error { _, err := fmt.Fprintf(w, `import ( "fmt" "strings" + "os" + "time" ) `) return err } +func header_release_common(w io.Writer) error { + _, err := fmt.Fprintf(w, `type asset struct { + bytes []byte + info os.FileInfo +} + +type bindata_file_info struct { + name string + size int64 + mode os.FileMode + modTime time.Time +} + +func (fi bindata_file_info) Name() string { + return fi.name +} +func (fi bindata_file_info) Size() int64 { + return fi.size +} +func (fi bindata_file_info) Mode() os.FileMode { + return fi.mode +} +func (fi bindata_file_info) ModTime() time.Time { + return fi.modTime +} +func (fi bindata_file_info) IsDir() bool { + return false +} +func (fi bindata_file_info) Sys() interface{} { + return nil +} + +`) + 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 { @@ -206,7 +259,7 @@ func compressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error { _, err = fmt.Fprintf(w, `" -func %s() ([]byte, error) { +func %s_bytes() ([]byte, error) { return bindata_read( _%s, %q, @@ -233,7 +286,7 @@ func compressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error { _, err = fmt.Fprintf(w, `") -func %s() ([]byte, error) { +func %s_bytes() ([]byte, error) { return bindata_read( _%s, %q, @@ -257,7 +310,7 @@ func uncompressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error { _, err = fmt.Fprintf(w, `" -func %s() ([]byte, error) { +func %s_bytes() ([]byte, error) { return bindata_read( _%s, %q, @@ -286,10 +339,31 @@ func uncompressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error { _, err = fmt.Fprintf(w, `) -func %s() ([]byte, error) { +func %s_bytes() ([]byte, error) { return _%s, nil } `, asset.Func, asset.Func) return err } + +func asset_release_common(w io.Writer, asset *Asset) error { + fi, err := os.Stat(asset.Path) + if err != nil { + return err + } + + _, err = fmt.Fprintf(w, `func %s() (*asset, error) { + bytes, err := %s_bytes() + if err != nil { + return nil, err + } + + info := bindata_file_info{name: %q, size: %d, mode: os.FileMode(%d), modTime: time.Unix(%d, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + +`, asset.Func, asset.Func, asset.Name, fi.Size(), uint32(fi.Mode()), fi.ModTime().Unix()) + return err +}