Merge pull request #10 from octplane/mine/master

Make bindata usable as a Library
pull/4/head v2.0.2
jimt 2013-09-30 15:07:12 -07:00
commit edbc082b2f
6 changed files with 51 additions and 47 deletions

View File

@ -2,7 +2,7 @@
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/
package main
package bindata
import (
"fmt"

View File

@ -2,7 +2,7 @@
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/
package main
package bindata
import (
"fmt"

View File

@ -2,7 +2,7 @@
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/
package main
package bindata
import (
"fmt"
@ -13,7 +13,7 @@ import (
)
// createTOC writes a table of contents file to the given location.
func createTOC(dir, pkgname string) error {
func CreateTOC(dir, pkgname string) error {
file := filepath.Join(dir, "bindata-toc.go")
code := fmt.Sprintf(`package %s
@ -26,8 +26,9 @@ 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) {
// WriteTOCInit writes the TOC init function for a given data file
// replacing the prefix in the filename by "", funcname being the translated function name
func WriteTOCInit(output io.Writer, filename, prefix, funcname string) {
filename = strings.Replace(filename, prefix, "", 1)
fmt.Fprintf(output, `

View File

@ -2,16 +2,21 @@
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/
package main
package bindata
import (
"compress/gzip"
"fmt"
"io"
"regexp"
"strings"
"unicode"
)
var regFuncName = regexp.MustCompile(`[^a-zA-Z0-9_]`)
// translate translates the input file to go source code.
func translate(input io.Reader, output io.Writer, pkgname, funcname string, uncompressed, nomemcpy bool) {
func Translate(input io.Reader, output io.Writer, pkgname, funcname string, uncompressed, nomemcpy bool) {
if nomemcpy {
if uncompressed {
translate_nomemcpy_uncomp(input, output, pkgname, funcname)
@ -151,3 +156,32 @@ func %s() []byte {
}
`, funcname, funcname, funcname, funcname)
}
// safeFuncname creates a safe function name from the input path.
func SafeFuncname(in, prefix string) string {
name := strings.Replace(in, prefix, "", 1)
if len(name) == 0 {
name = in
}
name = strings.ToLower(name)
name = regFuncName.ReplaceAllString(name, "_")
if unicode.IsDigit(rune(name[0])) {
// Identifier can't start with a digit.
name = "_" + name
}
// Get rid of "__" instances for niceness.
for strings.Index(name, "__") > -1 {
name = strings.Replace(name, "__", "_", -1)
}
// Leading underscore is silly.
if name[0] == '_' {
name = name[1:]
}
return name
}

View File

@ -2,7 +2,7 @@
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/
package main
package bindata
import (
"fmt"

45
main.go
View File

@ -7,11 +7,10 @@ package main
import (
"flag"
"fmt"
"github.com/jteeuwen/go-bindata/lib"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"unicode"
)
@ -27,14 +26,13 @@ var (
tags = flag.String("tags", "", "Optional build tags")
toc = flag.Bool("toc", false, "Generate a table of contents for this and other files. The input filepath becomes the map key. This option is only useable in non-pipe mode.")
version = flag.Bool("version", false, "Display version information.")
regFuncName = regexp.MustCompile(`[^a-zA-Z0-9_]`)
)
func main() {
parseArgs()
if pipe {
translate(os.Stdin, os.Stdout, *pkgname, *funcname, *uncompressed, *nomemcopy)
bindata.Translate(os.Stdin, os.Stdout, *pkgname, *funcname, *uncompressed, *nomemcopy)
return
}
@ -59,20 +57,20 @@ func main() {
}
// Translate binary to Go code.
translate(fs, fd, *pkgname, *funcname, *uncompressed, *nomemcopy)
bindata.Translate(fs, fd, *pkgname, *funcname, *uncompressed, *nomemcopy)
// Append the TOC init function to the end of the output file and
// write the `bindata-toc.go` file, if applicable.
if *toc {
dir, _ := filepath.Split(*out)
err := createTOC(dir, *pkgname)
err := bindata.CreateTOC(dir, *pkgname)
if err != nil {
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
return
}
writeTOCInit(fd, in, *prefix, *funcname)
bindata.WriteTOCInit(fd, in, "", *funcname)
}
}
@ -85,7 +83,7 @@ func parseArgs() {
flag.Parse()
if *version {
fmt.Printf("%s\n", Version())
fmt.Printf("%s\n", bindata.Version())
os.Exit(0)
}
@ -114,7 +112,7 @@ func parseArgs() {
os.Exit(1)
}
*funcname = safeFuncname(in, *prefix)
*funcname = bindata.SafeFuncname(in, *prefix)
fmt.Fprintf(os.Stderr, "[w] No function name specified. Using %s.\n", *funcname)
}
}
@ -157,32 +155,3 @@ func safeFilename(out, in string) string {
return filename
}
// safeFuncname creates a safe function name from the input path.
func safeFuncname(in, prefix string) string {
name := strings.Replace(in, prefix, "", 1)
if len(name) == 0 {
name = in
}
name = strings.ToLower(name)
name = regFuncName.ReplaceAllString(name, "_")
if unicode.IsDigit(rune(name[0])) {
// Identifier can't start with a digit.
name = "_" + name
}
// Get rid of "__" instances for niceness.
for strings.Index(name, "__") > -1 {
name = strings.Replace(name, "__", "_", -1)
}
// Leading underscore is silly.
if name[0] == '_' {
name = name[1:]
}
return name
}