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 (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-10-30 12:14:58 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-10-30 01:11:04 +00:00
|
|
|
)
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
// writeDebug writes the debug code file.
|
|
|
|
func writeDebug(c *Config, toc []Asset) error {
|
|
|
|
fd, err := os.Create(filepath.Join(c.Output, "bindata_debug.go"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
err = writeDebugHeader(fd, c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range toc {
|
|
|
|
err = writeDebugAsset(fd, c, &toc[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-30 01:16:14 +00:00
|
|
|
// writeDebugHeader writes output file headers.
|
2013-10-30 01:11:04 +00:00
|
|
|
// This targets debug builds.
|
2013-10-30 12:14:58 +00:00
|
|
|
func writeDebugHeader(w io.Writer, c *Config) error {
|
|
|
|
var err error
|
|
|
|
|
2013-10-30 01:11:04 +00:00
|
|
|
// Write build tags, if applicable.
|
|
|
|
if len(c.Tags) > 0 {
|
2013-10-30 12:14:58 +00:00
|
|
|
_, err = fmt.Fprintf(w, "// +build !release %s\n\n", c.Tags)
|
2013-10-30 01:11:04 +00:00
|
|
|
} else {
|
2013-10-30 12:14:58 +00:00
|
|
|
_, err = fmt.Fprintf(w, "// +build !release\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write package declaration
|
2013-10-30 12:14:58 +00:00
|
|
|
_, err = fmt.Fprintf(w, "package %s\n\n", c.Package)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-30 01:11:04 +00:00
|
|
|
|
|
|
|
// Define packages we need to import.
|
|
|
|
// And add the asset_read function. This is called
|
|
|
|
// from asset-specific functions.
|
2013-10-30 12:14:58 +00:00
|
|
|
_, err = fmt.Fprintf(w, `import (
|
2013-10-30 01:11:04 +00:00
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// bindata_read reads the given file from disk.
|
|
|
|
// It panics if anything went wrong.
|
|
|
|
func bindata_read(path, name string) []byte {
|
|
|
|
fd, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Read %%s: %%v", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
_, err = io.Copy(&buf, fd)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Read %%s: %%v", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
`)
|
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
|
|
|
// writeDebugAsset write a debug entry for the given asset.
|
2013-10-30 01:11:04 +00:00
|
|
|
// A debug entry is simply a function which reads the asset from
|
|
|
|
// the original file (e.g.: from disk).
|
2013-10-30 12:14:58 +00:00
|
|
|
func writeDebugAsset(w io.Writer, c *Config, asset *Asset) error {
|
|
|
|
_, err := fmt.Fprintf(w, `func %s() []byte {
|
2013-10-30 01:11:04 +00:00
|
|
|
return bindata_read(
|
|
|
|
%q,
|
|
|
|
%q,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
`, asset.Func, asset.Path, asset.Name)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|