Skip to content

Commit

Permalink
[now-go] Ignore folders in analyze.go (#503)(#504) (#506)
Browse files Browse the repository at this point in the history
* [now-go] Ignore folders in `analyze.go` (#503)(#504)

This commit adds some changes to the way the AST for the source is
built.

The `analyze.go` program now ignores every `vendor`, `.git` and
`testdata` folder. This improves performance, since `vendor` and `.git`
are usually large folders (#504).

By ignoring `testdata`, we mimick the behaviour of `go build` and avoid
failing the parsing because of invalid Go code inside of `testdata` (#503)

* [now-go] Don't ignore `.git` in analyze (#506)

If the user wants to ignore `.git`, he should put it into `.nowignore`
  • Loading branch information
marhaupe authored and thasophearak committed May 17, 2019
1 parent 008b044 commit 4deb426
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/now-go/util/analyze.go
Expand Up @@ -10,9 +10,22 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strings"
)

var ignoredFoldersRegex []*regexp.Regexp

func init() {
ignoredFolders := []string{"vendor", "testdata"}

// Build the regex that matches if a path contains the respective ignored folder
// The pattern will look like: (.*/)?vendor/.*, which matches every path that contains a vendor folder
for _, folder := range ignoredFolders {
ignoredFoldersRegex = append(ignoredFoldersRegex, regexp.MustCompile("(.*/)?"+folder+"/.*"))
}
}

type analyze struct {
PackageName string `json:"packageName"`
FuncName string `json:"functionName"`
Expand Down Expand Up @@ -40,8 +53,9 @@ func visit(files *[]string) filepath.WalkFunc {
}

// we don't need Dirs, or test files
// we only want `.go` files
if info.IsDir() || itf || filepath.Ext(path) != ".go" {
// we only want `.go` files. Further, we ignore
// every file that is in one of the ignored folders.
if info.IsDir() || itf || filepath.Ext(path) != ".go" || isInIgnoredFolder(path) {
return nil
}

Expand All @@ -50,6 +64,19 @@ func visit(files *[]string) filepath.WalkFunc {
}
}

// isInIgnoredFolder checks if the given path is in one of the ignored folders.
func isInIgnoredFolder(path string) bool {
// Make sure the regex works for Windows paths
path = filepath.ToSlash(path)

for _, pattern := range ignoredFoldersRegex {
if pattern.MatchString(path) {
return true
}
}
return false
}

// return unique file
func unique(files []string) []string {
encountered := map[string]bool{}
Expand Down

0 comments on commit 4deb426

Please sign in to comment.