From f72c4665015bb5199fd38ca35940faf2ce92c541 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 4 Jun 2013 17:05:40 -0700 Subject: [PATCH] Add -fileembed-package and -all flags The -fileembed-package flag makes genfileembed play nicely with goven. If you use goven to vendorize fileembed, the fileembed package path changes (to be relative to the current project). With -fileembed-package, you can make the generated code import the correct (vendorized) fileembed package. The -all flag forces processing of all files, not just modified files. This is useful when changing -import-prefix, when working on a filesystem with unreliable mtimes, or when being neurotic/defensive in build scripts. Addresses https://code.google.com/p/camlistore/issues/detail?id=137 Change-Id: I54215429dabd02bc1fa72c6718e1cabccb1cffaf --- pkg/fileembed/genfileembed/genfileembed.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/fileembed/genfileembed/genfileembed.go b/pkg/fileembed/genfileembed/genfileembed.go index 8b9e458d3..a4cd858f6 100644 --- a/pkg/fileembed/genfileembed/genfileembed.go +++ b/pkg/fileembed/genfileembed/genfileembed.go @@ -40,8 +40,20 @@ const ( zRatio = 0.5 ) +func usage() { + fmt.Fprintf(os.Stderr, "usage: genfileembed [flags] []\n") + flag.PrintDefaults() + os.Exit(2) +} + func main() { + var processAll bool + flag.BoolVar(&processAll, "all", false, "process all files (if false, only process modified files)") + var fileEmbedPkgPath string + flag.StringVar(&fileEmbedPkgPath, "fileembed-package", "camlistore.org/pkg/fileembed", "the Go package name for fileembed. If you have vendored fileembed (e.g. with goven), you can use this flag to ensure that generated code imports the vendored package.") + flag.Usage = usage flag.Parse() + dir := "." switch flag.NArg() { case 0: @@ -51,8 +63,7 @@ func main() { log.Fatalf("chdir(%q) = %v", dir, err) } default: - fmt.Fprintf(os.Stderr, "usage: genfileembed []\n") - os.Exit(2) + flag.Usage() } pkgName, filePattern, err := parseFileEmbed() @@ -67,7 +78,7 @@ func main() { log.Fatal(err) } efi, err := os.Stat(embedName) - if err == nil && !efi.ModTime().Before(fi.ModTime()) { + if err == nil && !efi.ModTime().Before(fi.ModTime()) && !processAll { continue } log.Printf("Updating %s (package %s)", filepath.Join(dir, embedName), pkgName) @@ -93,7 +104,7 @@ func main() { fmt.Fprintf(&b, "// DO NOT EDIT.\n\n") fmt.Fprintf(&b, "package %s\n\n", pkgName) fmt.Fprintf(&b, "import \"time\"\n\n") - fmt.Fprintf(&b, "import \"camlistore.org/pkg/fileembed\"\n\n") + fmt.Fprintf(&b, "import \""+fileEmbedPkgPath+"\"\n\n") fmt.Fprintf(&b, "func init() {\n\tFiles.Add(%q, %d, %s(%s), time.Unix(0, %d));\n}\n", fileName, fileSize, byteStreamType, qb, fi.ModTime().UnixNano())