1package parser
2
3import (
4	"os"
5	"path/filepath"
6	"strings"
7
8	"github.com/gobuffalo/packr/v2/file/resolver"
9	"github.com/gobuffalo/packr/v2/plog"
10)
11
12var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"}
13
14func IsProspect(path string, ignore ...string) (status bool) {
15	// plog.Debug("parser", "IsProspect", "path", path, "ignore", ignore)
16	defer func() {
17		if status {
18			plog.Debug("parser", "IsProspect (TRUE)", "path", path, "status", status)
19		}
20	}()
21	if path == "." {
22		return true
23	}
24
25	ext := filepath.Ext(path)
26	dir := filepath.Dir(path)
27
28	fi, _ := os.Stat(path)
29	if fi != nil {
30		if fi.IsDir() {
31			dir = filepath.Base(path)
32		} else {
33			if len(ext) > 0 {
34				dir = filepath.Base(filepath.Dir(path))
35			}
36		}
37	}
38
39	path = strings.ToLower(path)
40	dir = strings.ToLower(dir)
41
42	if strings.HasSuffix(path, "-packr.go") {
43		return false
44	}
45
46	if strings.HasSuffix(path, "_test.go") {
47		return false
48	}
49
50	ignore = append(ignore, DefaultIgnoredFolders...)
51	for i, x := range ignore {
52		ignore[i] = strings.TrimSpace(strings.ToLower(x))
53	}
54
55	parts := strings.Split(resolver.OsPath(path), string(filepath.Separator))
56	if len(parts) == 0 {
57		return false
58	}
59
60	for _, i := range ignore {
61		for _, p := range parts {
62			if strings.HasPrefix(p, i) {
63				return false
64			}
65		}
66	}
67
68	un := filepath.Base(path)
69	if len(ext) != 0 {
70		un = filepath.Base(filepath.Dir(path))
71	}
72	if strings.HasPrefix(un, "_") {
73		return false
74	}
75
76	return ext == ".go"
77}
78