Merge pull request #69 from beyang/master

add -dev flag, similar to -debug but does not emit absolute file paths
This commit is contained in:
Jim Teeuwen 2015-01-27 01:35:24 +01:00
commit 8ec170e704
4 changed files with 25 additions and 8 deletions

View File

@ -117,6 +117,16 @@ type Config struct {
// in the code. The default behaviour is Release mode.
Debug bool
// Perform a dev build, which is nearly identical to the debug option. The
// only difference is that instead of absolute file paths in generated code,
// it expects a variable, `rootDir`, to be set in the generated code's
// package (the author needs to do this manually), which it then prepends to
// an asset's name to construct the file path on disk.
//
// This is mainly so you can push the generated code file to a shared
// repository.
Dev bool
// Recursively process all assets in the input directory and its
// sub directories. This defaults to false, so only files in the
// input directory itself are read.

View File

@ -63,8 +63,8 @@ func Translate(c *Config) error {
}
// Write assets.
if c.Debug {
err = writeDebug(bfd, toc)
if c.Debug || c.Dev {
err = writeDebug(bfd, c, toc)
} else {
err = writeRelease(bfd, c, toc)
}

View File

@ -10,14 +10,14 @@ import (
)
// writeDebug writes the debug code file.
func writeDebug(w io.Writer, toc []Asset) error {
func writeDebug(w io.Writer, c *Config, toc []Asset) error {
err := writeDebugHeader(w)
if err != nil {
return err
}
for i := range toc {
err = writeDebugAsset(w, &toc[i])
err = writeDebugAsset(w, c, &toc[i])
if err != nil {
return err
}
@ -59,10 +59,15 @@ type asset struct {
// writeDebugAsset write a debug entry for the given asset.
// A debug entry is simply a function which reads the asset from
// the original file (e.g.: from disk).
func writeDebugAsset(w io.Writer, asset *Asset) error {
func writeDebugAsset(w io.Writer, c *Config, asset *Asset) error {
pathExpr := fmt.Sprintf("%q", asset.Path)
if c.Dev {
pathExpr = fmt.Sprintf("filepath.Join(rootDir, %q)", asset.Name)
}
_, err := fmt.Fprintf(w, `// %s reads file data from disk. It returns an error on failure.
func %s() (*asset, error) {
path := %q
path := %s
name := %q
bytes, err := bindata_read(path, name)
if err != nil {
@ -78,6 +83,6 @@ func %s() (*asset, error) {
return a, err
}
`, asset.Func, asset.Func, asset.Path, asset.Name)
`, asset.Func, asset.Func, pathExpr, asset.Name)
return err
}

View File

@ -7,11 +7,12 @@ package main
import (
"flag"
"fmt"
"github.com/jteeuwen/go-bindata"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/jteeuwen/go-bindata"
)
func main() {
@ -40,6 +41,7 @@ func parseArgs() *bindata.Config {
}
flag.BoolVar(&c.Debug, "debug", c.Debug, "Do not embed the assets, but provide the embedding API. Contents will still be loaded from disk.")
flag.BoolVar(&c.Dev, "dev", c.Dev, "Similar to debug, but does not emit absolute paths. Expects a rootDir variable to already exist in the generated code's package.")
flag.StringVar(&c.Tags, "tags", c.Tags, "Optional set of build tags to include.")
flag.StringVar(&c.Prefix, "prefix", c.Prefix, "Optional path prefix to strip off asset names.")
flag.StringVar(&c.Package, "pkg", c.Package, "Package name to use in the generated code.")