From 0efad67aa4674c117084de47d0a7cde6339ba668 Mon Sep 17 00:00:00 2001 From: Beyang Liu Date: Mon, 26 Jan 2015 15:45:06 -0800 Subject: [PATCH] add -dev flag, similar to -debug but does not emit absolute file paths --- config.go | 10 ++++++++++ convert.go | 4 ++-- debug.go | 15 ++++++++++----- go-bindata/main.go | 4 +++- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index 2b05389..774dedd 100644 --- a/config.go +++ b/config.go @@ -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. diff --git a/convert.go b/convert.go index 7e6246d..8489f62 100644 --- a/convert.go +++ b/convert.go @@ -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) } diff --git a/debug.go b/debug.go index 884b4df..5d96e9d 100644 --- a/debug.go +++ b/debug.go @@ -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 } diff --git a/go-bindata/main.go b/go-bindata/main.go index 2b93d33..f2e00e9 100644 --- a/go-bindata/main.go +++ b/go-bindata/main.go @@ -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.")