ff063d28c6
We do this, because the changes in this patch fundamentally alter the way code is generated by the tool. This will, in some cases, be incompatible with older versions. * Performs cleanup and minor code fixes. * Adds two new command line flags: * -prefix: This accepts a partial path. It is used when generating a target function name, as well as the key for the Table of Contents map (see below). The specified prefix is applied to the input file name, causing the prefix section to be stripped from the input file path. E.g.: ``` input: /path/to/foo.x prefix: /path/to output: /foo.x ``` * -toc: This is a boolean flag which tells the tool to generate a table of contents for the generated data files. It creates a separate 'bindata-toc.go' file, which defines a global map named `go_bindata`. It then appends an `init` function to the generated file. This function makes the data function register itself with the global map. * Fixes the function names the tool infers from input file names to include the full path. This fixes potential name collisions when the same file name is processed from different directories. For example, we can now safely import the following two files: ``` input file: css/ie/foo.css output function: css_ie_foo_css() input file: css/chrome/foo.css output function: css_chrome_foo_css() ```
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
// 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 main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// createTOC writes a table of contents file to the given location.
|
|
func createTOC(file, pkgname string) error {
|
|
dir, _ := filepath.Split(file)
|
|
file = filepath.Join(dir, "bindata-toc.go")
|
|
code := fmt.Sprintf(`package %s
|
|
|
|
// Global Table of Contents map. Generated by go-bindata.
|
|
// After startup of the program, all generated data files will
|
|
// put themselves in this map. The key is the full filename, as
|
|
// supplied to go-bindata.
|
|
var go_bindata = make(map[string] func() []byte)`, pkgname)
|
|
|
|
return ioutil.WriteFile(file, []byte(code), 0600)
|
|
}
|
|
|
|
// writeTOCInit writes the TOC init function for a given data file.
|
|
func writeTOCInit(output io.Writer, filename, prefix, funcname string) {
|
|
filename = strings.Replace(filename, prefix, "", 1)
|
|
fmt.Fprintf(output, `
|
|
|
|
func init() {
|
|
go_bindata[%q] = %s
|
|
}
|
|
`, filename, funcname)
|
|
}
|