add -dev flag, similar to -debug but does not emit absolute file paths

This commit is contained in:
Beyang Liu 2015-01-26 15:45:06 -08:00
parent 4a8e91e5cd
commit 0efad67aa4
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. // in the code. The default behaviour is Release mode.
Debug bool 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 // Recursively process all assets in the input directory and its
// sub directories. This defaults to false, so only files in the // sub directories. This defaults to false, so only files in the
// input directory itself are read. // input directory itself are read.

View File

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

View File

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

View File

@ -7,11 +7,12 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/jteeuwen/go-bindata"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"github.com/jteeuwen/go-bindata"
) )
func main() { 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.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.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.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.") flag.StringVar(&c.Package, "pkg", c.Package, "Package name to use in the generated code.")