2013-10-29 17:23:52 +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-01-29 10:28:32 +00:00
|
|
|
"bufio"
|
2013-10-29 17:23:52 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2014-09-29 19:06:41 +00:00
|
|
|
"sort"
|
2013-10-29 17:23:52 +00:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Translate reads assets from an input directory, converts them
|
2013-11-01 09:30:39 +00:00
|
|
|
// to Go code and writes new files to the output specified
|
2013-10-29 17:23:52 +00:00
|
|
|
// in the given configuration.
|
2013-10-30 13:45:04 +00:00
|
|
|
func Translate(c *Config) error {
|
2013-10-30 01:11:04 +00:00
|
|
|
var toc []Asset
|
2013-10-29 17:23:52 +00:00
|
|
|
|
2013-10-30 13:45:04 +00:00
|
|
|
// Ensure our configuration has sane values.
|
|
|
|
err := c.validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-09 05:50:45 +00:00
|
|
|
var knownFuncs = make(map[string]int)
|
2013-10-30 13:45:04 +00:00
|
|
|
// Locate all the assets.
|
2013-11-08 15:21:11 +00:00
|
|
|
for _, input := range c.Input {
|
2014-12-09 05:50:45 +00:00
|
|
|
err = findFiles(input.Path, c.Prefix, input.Recursive, &toc, c.Ignore, knownFuncs)
|
2013-11-08 15:21:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-30 13:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create output file.
|
|
|
|
fd, err := os.Create(c.Output)
|
2013-10-29 17:23:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:45:04 +00:00
|
|
|
defer fd.Close()
|
|
|
|
|
2014-01-29 10:28:32 +00:00
|
|
|
// Create a buffered writer for better performance.
|
|
|
|
bfd := bufio.NewWriter(fd)
|
|
|
|
defer bfd.Flush()
|
|
|
|
|
2013-10-30 13:45:04 +00:00
|
|
|
// Write build tags, if applicable.
|
|
|
|
if len(c.Tags) > 0 {
|
2014-01-29 10:28:32 +00:00
|
|
|
_, err = fmt.Fprintf(bfd, "// +build %s\n\n", c.Tags)
|
2013-10-30 13:45:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write package declaration.
|
2014-01-29 10:28:32 +00:00
|
|
|
_, err = fmt.Fprintf(bfd, "package %s\n\n", c.Package)
|
2013-10-30 01:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:45:04 +00:00
|
|
|
// Write assets.
|
|
|
|
if c.Debug {
|
2014-01-29 10:28:32 +00:00
|
|
|
err = writeDebug(bfd, toc)
|
2013-10-30 13:45:04 +00:00
|
|
|
} else {
|
2014-01-29 10:28:32 +00:00
|
|
|
err = writeRelease(bfd, c, toc)
|
2013-10-30 13:45:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:11:04 +00:00
|
|
|
if err != nil {
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 13:45:04 +00:00
|
|
|
// Write table of contents
|
2014-07-13 06:59:24 +00:00
|
|
|
if err := writeTOC(bfd, toc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Write hierarchical tree of assets
|
2014-12-08 00:30:11 +00:00
|
|
|
if err := writeTOCTree(bfd, toc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write restore procedure
|
|
|
|
return writeRestore(bfd)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 21:00:02 +00:00
|
|
|
// Implement sort.Interface for []os.FileInfo based on Name()
|
|
|
|
type ByName []os.FileInfo
|
|
|
|
|
|
|
|
func (v ByName) Len() int { return len(v) }
|
|
|
|
func (v ByName) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
|
|
|
|
func (v ByName) Less(i, j int) bool { return v[i].Name() < v[j].Name() }
|
|
|
|
|
2013-11-01 09:30:39 +00:00
|
|
|
// findFiles recursively finds all the file paths in the given directory tree.
|
2013-10-29 17:23:52 +00:00
|
|
|
// They are added to the given map as keys. Values will be safe function names
|
|
|
|
// for each file, which will be used when generating the output code.
|
2014-12-09 05:50:45 +00:00
|
|
|
func findFiles(dir, prefix string, recursive bool, toc *[]Asset, ignore []*regexp.Regexp, knownFuncs map[string]int) error {
|
2013-10-29 17:23:52 +00:00
|
|
|
if len(prefix) > 0 {
|
|
|
|
dir, _ = filepath.Abs(dir)
|
|
|
|
prefix, _ = filepath.Abs(prefix)
|
2014-02-28 11:33:10 +00:00
|
|
|
prefix = filepath.ToSlash(prefix)
|
2013-10-29 17:23:52 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 12:23:20 +00:00
|
|
|
fi, err := os.Stat(dir)
|
2013-10-29 17:23:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 12:23:20 +00:00
|
|
|
var list []os.FileInfo
|
2013-10-29 17:23:52 +00:00
|
|
|
|
2014-08-05 12:23:20 +00:00
|
|
|
if !fi.IsDir() {
|
|
|
|
dir = ""
|
|
|
|
list = []os.FileInfo{fi}
|
|
|
|
} else {
|
|
|
|
fd, err := os.Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
list, err = fd.Readdir(0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-24 21:00:02 +00:00
|
|
|
|
|
|
|
// Sort to make output stable between invocations
|
|
|
|
sort.Sort(ByName(list))
|
2013-10-29 17:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range list {
|
2013-10-30 01:11:04 +00:00
|
|
|
var asset Asset
|
|
|
|
asset.Path = filepath.Join(dir, file.Name())
|
2014-02-28 11:33:10 +00:00
|
|
|
asset.Name = filepath.ToSlash(asset.Path)
|
2013-10-29 17:23:52 +00:00
|
|
|
|
2014-04-29 22:42:13 +00:00
|
|
|
ignoring := false
|
|
|
|
for _, re := range ignore {
|
|
|
|
if re.MatchString(asset.Path) {
|
|
|
|
ignoring = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ignoring {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-10-29 17:23:52 +00:00
|
|
|
if file.IsDir() {
|
2013-10-30 15:08:29 +00:00
|
|
|
if recursive {
|
2014-12-09 05:50:45 +00:00
|
|
|
findFiles(asset.Path, prefix, recursive, toc, ignore, knownFuncs)
|
2013-10-30 15:08:29 +00:00
|
|
|
}
|
2013-10-30 01:11:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-10-29 17:23:52 +00:00
|
|
|
|
2013-10-30 01:11:04 +00:00
|
|
|
if strings.HasPrefix(asset.Name, prefix) {
|
|
|
|
asset.Name = asset.Name[len(prefix):]
|
|
|
|
}
|
2013-10-29 17:23:52 +00:00
|
|
|
|
2013-10-30 01:11:04 +00:00
|
|
|
// If we have a leading slash, get rid of it.
|
|
|
|
if len(asset.Name) > 0 && asset.Name[0] == '/' {
|
|
|
|
asset.Name = asset.Name[1:]
|
|
|
|
}
|
2013-10-29 17:23:52 +00:00
|
|
|
|
2013-10-30 01:11:04 +00:00
|
|
|
// This shouldn't happen.
|
|
|
|
if len(asset.Name) == 0 {
|
|
|
|
return fmt.Errorf("Invalid file: %v", asset.Path)
|
2013-10-29 17:23:52 +00:00
|
|
|
}
|
2013-10-30 01:11:04 +00:00
|
|
|
|
2014-08-18 00:11:38 +00:00
|
|
|
asset.Func = safeFunctionName(asset.Name, knownFuncs)
|
2013-10-30 01:11:04 +00:00
|
|
|
asset.Path, _ = filepath.Abs(asset.Path)
|
|
|
|
*toc = append(*toc, asset)
|
2013-10-29 17:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var regFuncName = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
|
|
|
|
|
|
|
// safeFunctionName converts the given name into a name
|
2014-08-18 00:11:38 +00:00
|
|
|
// which qualifies as a valid function identifier. It
|
|
|
|
// also compares against a known list of functions to
|
|
|
|
// prevent conflict based on name translation.
|
|
|
|
func safeFunctionName(name string, knownFuncs map[string]int) string {
|
2013-10-29 17:23:52 +00:00
|
|
|
name = strings.ToLower(name)
|
|
|
|
name = regFuncName.ReplaceAllString(name, "_")
|
|
|
|
|
|
|
|
// Get rid of "__" instances for niceness.
|
|
|
|
for strings.Index(name, "__") > -1 {
|
|
|
|
name = strings.Replace(name, "__", "_", -1)
|
|
|
|
}
|
|
|
|
|
2013-10-30 01:11:04 +00:00
|
|
|
// Leading underscores are silly (unless they prefix a digit (see below)).
|
|
|
|
for len(name) > 1 && name[0] == '_' {
|
|
|
|
name = name[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Identifier can't start with a digit.
|
|
|
|
if unicode.IsDigit(rune(name[0])) {
|
|
|
|
name = "_" + name
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:11:38 +00:00
|
|
|
if num, ok := knownFuncs[name]; ok {
|
|
|
|
knownFuncs[name] = num + 1
|
|
|
|
name = fmt.Sprintf("%s%d", name, num)
|
|
|
|
} else {
|
|
|
|
knownFuncs[name] = 2
|
|
|
|
}
|
|
|
|
|
2013-10-29 17:23:52 +00:00
|
|
|
return name
|
|
|
|
}
|