From 2065c36b3fa2798e616f4211093ecd2450852d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Art=C5=ABrs=20J=C4=81nis=20P=C4=93tersons?= Date: Sun, 12 May 2019 20:46:06 +0100 Subject: [PATCH 1/2] Search for boxes in vendor directory --- v2/jam/parser/finder.go | 22 ++++++++++++---------- v2/jam/parser/prospect.go | 2 +- v2/jam/parser/prospect_test.go | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/v2/jam/parser/finder.go b/v2/jam/parser/finder.go index 2ad808e..4462a15 100644 --- a/v2/jam/parser/finder.go +++ b/v2/jam/parser/finder.go @@ -30,6 +30,9 @@ func (fd *finder) findAllGoFiles(dir string) ([]string, error) { plog.Debug(fd, "findAllGoFiles", "dir", dir) callback := func(path string, do *godirwalk.Dirent) error { + if filepath.Base(path) == "vendor" { + return filepath.SkipDir + } ext := filepath.Ext(path) if ext != ".go" { return nil @@ -57,9 +60,13 @@ func (fd *finder) findAllGoFiles(dir string) ([]string, error) { } func (fd *finder) findAllGoFilesImports(dir string) ([]string, error) { + return fd.findAllGoFilesImportsIn(".", dir) +} + +func (fd *finder) findAllGoFilesImportsIn(path, dir string) ([]string, error) { var err error var names []string - oncer.Do(fd.key("findAllGoFilesImports", dir), func() { + oncer.Do(fd.key("findAllGoFilesImports", filepath.Join(dir, path)), func() { ctx := build.Default if len(ctx.SrcDirs()) == 0 { @@ -67,8 +74,8 @@ func (fd *finder) findAllGoFilesImports(dir string) ([]string, error) { return } - pkg, err := ctx.ImportDir(dir, 0) - if strings.HasPrefix(pkg.ImportPath, "github.com/gobuffalo/packr") { + pkg, err := ctx.Import(path, dir, 0) + if strings.Contains(pkg.ImportPath, "github.com/gobuffalo/packr") { return } @@ -90,17 +97,12 @@ func (fd *finder) findAllGoFilesImports(dir string) ([]string, error) { plog.Debug(fd, "findAllGoFilesImports", "dir", dir) - names, _ = fd.findAllGoFiles(dir) + names, _ = fd.findAllGoFiles(pkg.Dir) for _, n := range pkg.GoFiles { names = append(names, filepath.Join(pkg.Dir, n)) } for _, imp := range pkg.Imports { - if len(ctx.SrcDirs()) == 0 { - continue - } - d := ctx.SrcDirs()[len(ctx.SrcDirs())-1] - ip := filepath.Join(d, imp) - n, err := fd.findAllGoFilesImports(ip) + n, err := fd.findAllGoFilesImportsIn(imp, dir) if err != nil && len(n) != 0 { names = n return diff --git a/v2/jam/parser/prospect.go b/v2/jam/parser/prospect.go index 652db5b..afb5cf2 100644 --- a/v2/jam/parser/prospect.go +++ b/v2/jam/parser/prospect.go @@ -9,7 +9,7 @@ import ( "github.com/gobuffalo/packr/v2/plog" ) -var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"} +var DefaultIgnoredFolders = []string{".", "_", "node_modules", "_fixtures", "testdata"} func IsProspect(path string, ignore ...string) (status bool) { // plog.Debug("parser", "IsProspect", "path", path, "ignore", ignore) diff --git a/v2/jam/parser/prospect_test.go b/v2/jam/parser/prospect_test.go index f023f81..1699b91 100644 --- a/v2/jam/parser/prospect_test.go +++ b/v2/jam/parser/prospect_test.go @@ -18,7 +18,7 @@ func Test_IsProspect(t *testing.T) { {"a/b.go", true}, {"a/b_test.go", false}, {"a/b-packr.go", false}, - {"a/vendor/b.go", false}, + {"a/vendor/b.go", true}, {"a/_c/c.go", false}, {"a/_c/e/fe/f/c.go", false}, {"a/d/_d.go", false}, From bb79106ba7dc546f7c3d414affdefbad608733e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Art=C5=ABrs=20J=C4=81nis=20P=C4=93tersons?= Date: Tue, 29 Dec 2020 11:17:41 +0200 Subject: [PATCH 2/2] Add flag to look for boxes in vendor directory --- packr/cmd/root.go | 6 ++++++ v2/jam/parser/parser_test.go | 2 +- v2/jam/parser/prospect.go | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packr/cmd/root.go b/packr/cmd/root.go index 94f1c55..57f077f 100644 --- a/packr/cmd/root.go +++ b/packr/cmd/root.go @@ -6,12 +6,14 @@ import ( "os" "github.com/gobuffalo/packr/builder" + "github.com/gobuffalo/packr/v2/jam/parser" "github.com/spf13/cobra" ) var input string var compress bool var verbose bool +var includeVendored bool var rootCmd = &cobra.Command{ Use: "packr", @@ -36,6 +38,9 @@ var rootCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { b := builder.New(context.Background(), input) b.Compress = compress + if !includeVendored { + parser.DefaultIgnoredFolders = append(parser.DefaultIgnoredFolders, "vendor") + } return b.Run() }, } @@ -44,6 +49,7 @@ func init() { pwd, _ := os.Getwd() rootCmd.Flags().StringVarP(&input, "input", "i", pwd, "path to scan for packr Boxes") rootCmd.Flags().BoolVarP(&compress, "compress", "z", false, "compress box contents") + rootCmd.Flags().BoolVar(&includeVendored, "vendor", false, "look for boxes in vendor directory") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "print verbose logging information") } diff --git a/v2/jam/parser/parser_test.go b/v2/jam/parser/parser_test.go index 2a04411..e904833 100644 --- a/v2/jam/parser/parser_test.go +++ b/v2/jam/parser/parser_test.go @@ -12,7 +12,7 @@ import ( ) func init() { - parser.DefaultIgnoredFolders = []string{"vendor", ".git", "node_modules", ".idea"} + parser.DefaultIgnoredFolders = []string{".git", "node_modules", ".idea"} } func Test_Parser_Run(t *testing.T) { diff --git a/v2/jam/parser/prospect.go b/v2/jam/parser/prospect.go index afb5cf2..ab15eca 100644 --- a/v2/jam/parser/prospect.go +++ b/v2/jam/parser/prospect.go @@ -9,6 +9,8 @@ import ( "github.com/gobuffalo/packr/v2/plog" ) +// DefaultIgnoredFolders is a list of directories where box lookup is not +// performed. var DefaultIgnoredFolders = []string{".", "_", "node_modules", "_fixtures", "testdata"} func IsProspect(path string, ignore ...string) (status bool) {