From dc1ded5433bb80bd9f76b5bb74d72a60ec25a0db Mon Sep 17 00:00:00 2001 From: "Jip J. Dekker" Date: Fri, 18 Mar 2016 16:18:35 +0100 Subject: [PATCH] Adds function to process files that are on the compilation path --- compiler/file_discovery.go | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/compiler/file_discovery.go b/compiler/file_discovery.go index db4ad80..4666eb5 100644 --- a/compiler/file_discovery.go +++ b/compiler/file_discovery.go @@ -13,3 +13,50 @@ // limitations under the License. package compiler + +import ( + "os" + "path/filepath" + + log "github.com/Sirupsen/logrus" + "github.com/jjdekker/ponder/helpers" + "github.com/jjdekker/ponder/settings" +) + +// compilePath calls the given function on all files that aren't in any +// of the ignored directories +func compilePath(root string, opts *settings.Settings, + f func(string, os.FileInfo) error) filepath.WalkFunc { + return func(path string, info os.FileInfo, err error) error { + // Handle walking error + if err != nil { + log.withFields(log.Fields{ + "error": err, + "path": path, + }).Warning("error occurred transversing project path") + return nil + } + + if info.IsDir() { + // Skip directories that are ignored + relPath, err = filepath.Rel(root, path) + helpers.Check(err, "Unable to create relative Path") + for dir := range append(append(opts.IgnoreDirs, opts.LilypondIncludes), []string{opts.OutputDir}) { + if relPath == dir || (filepath.IsAbs(dir) && path == dir) { + log.WithFields(log.Fields{"path": path}).Info("Ignoring directory") + return filepath.SkipDir + } + } + } else { + // Call function on non-directory + err = f(path, info) + if err != nil { + log.WithFields(log.Fields{ + "error": err, + "path": path, + }).Error("error occured processing files") + } + } + return nil + } +}