-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobby.go
179 lines (166 loc) · 3.76 KB
/
globby.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package globby
import (
"fmt"
"os"
"path/filepath"
"regexp"
)
type Option struct {
BaseDir string
CheckDot bool
RelativeReturn bool
excludes []string
}
/*
* Glob all patterns
*/
func Match(patterns []string, opt Option) []string {
var allFiles []string
patterns, opt, err := completeOpt(patterns, opt)
if err != nil {
fmt.Printf("Magth err: [%v]\n", err)
return allFiles
}
for _, pattern := range patterns {
files := find(pattern, opt)
if files == nil || len(*files) == 0 {
continue
}
allFiles = append(allFiles, *files...)
}
return allFiles
}
func find(pattern string, opt Option) *[]string {
// match ./some/path/**/*
if regexTest("\\*\\*", pattern) ||
!regexTest("\\*", pattern) { // Dirname
return findRecr(pattern, opt)
}
// match ./some/path/*
if regexTest("\\*", pattern) {
return findDir(pattern, opt)
}
return nil
}
// find under centain directory
func findDir(pattern string, opt Option) *[]string {
var list []string
files, err := filepath.Glob(pattern)
if err != nil {
fmt.Printf("err: [%v]\n", err)
return &list
}
for _, fullpath := range files {
path, err := filepath.Rel(opt.BaseDir, fullpath)
if err != nil {
continue
}
if checkExclude(opt, path) {
continue
}
if opt.RelativeReturn {
list = append(list, path)
} else {
list = append(list, fullpath)
}
}
return &list
}
// find recursively
func findRecr(pattern string, opt Option) *[]string {
dir := strReplace(pattern, "\\*\\*.+", "")
afterMacth := ""
matchAfterFlag := false
if regexTest("\\*", pattern) {
afterMacth = strReplace(pattern, ".+\\*", "")
matchAfterFlag = len(afterMacth) > 0
}
var list []string
err := filepath.Walk(dir, func(fullpath string, f os.FileInfo, err error) error {
if !opt.CheckDot && regexTest("^\\.", f.Name()) {
if f.IsDir() {
return filepath.SkipDir
}
return nil
}
if f.IsDir() {
return nil
}
path, _ := filepath.Rel(opt.BaseDir, fullpath)
if checkExclude(opt, path) {
return nil
}
if !opt.RelativeReturn {
path = fullpath
}
if !matchAfterFlag {
list = append(list, path)
return nil
}
if regexTest(afterMacth+"$", path) {
list = append(list, path)
}
return nil
})
if err != nil {
fmt.Printf("err: [%v]\n", err)
}
return &list
}
// check and complete the options
func completeOpt(srcPatterns []string, opt Option) ([]string, Option, error) {
if len(opt.BaseDir) == 0 {
curDir, err := os.Getwd()
if err != nil {
panic(err)
}
opt.BaseDir = curDir
}
var patterns []string
for _, pattern := range srcPatterns {
// TODO: check no "tmp/*", use "tmp" or "tmp/*.ext" instead
if regexTest("^\\!", pattern) {
opt.excludes = append(opt.excludes, strReplace(pattern, "^\\!", ""))
continue
}
if regexTest("^\\.", pattern) || // like ./dist
!regexTest("^\\/", pattern) { // like dist
patterns = append(patterns, filepath.Join(opt.BaseDir, pattern))
continue
}
patterns = append(patterns, pattern)
}
return patterns, opt, nil
}
// check if path should be excluded
func checkExclude(opt Option, path string) bool {
// if exludes dirs
for _, exclude := range opt.excludes {
rule := exclude
if regexTest("\\*\\*", exclude) {
rule = strReplace(exclude, "\\*\\*/\\*+?", ".+")
} else if regexTest("\\*", exclude) {
rule = strReplace(exclude, "\\*", "[^/]+")
}
if regexTest("^"+rule, path) {
return true // ignore
}
}
return false
}
// Check if regex match the "src" string
func regexTest(re string, src string) bool {
matched, err := regexp.MatchString(re, src)
if err != nil {
return false
}
if matched {
return true
}
return false
}
// "dest" replace "text" pattern with "repl"
func strReplace(dest, text, repl string) string {
re := regexp.MustCompile(text)
return re.ReplaceAllString(dest, repl)
}