-
-
Notifications
You must be signed in to change notification settings - Fork 250
/
file.go
267 lines (222 loc) · 5.76 KB
/
file.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package processor
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/dbaggerman/cuba"
"github.com/monochromegane/go-gitignore"
)
// Used as quick lookup for files with the same name to avoid some processing
// needs to be sync.Map as it potentially could be called by many GoRoutines
var extensionCache sync.Map
// A custom version of extracting extensions for a file
// which also has a case insensitive cache in order to save
// some needless processing
func getExtension(name string) string {
name = strings.ToLower(name)
extension, ok := extensionCache.Load(name)
if ok {
return extension.(string)
}
ext := filepath.Ext(name)
if ext == "" || strings.LastIndex(name, ".") == 0 {
extension = name
} else {
// Handling multiple dots or multiple extensions only needs to delete the last extension
// and then call filepath.Ext.
// If there are multiple extensions, it is the value of subExt,
// otherwise subExt is an empty string.
subExt := filepath.Ext(strings.TrimSuffix(name, ext))
extension = strings.TrimPrefix(subExt+ext, ".")
}
extensionCache.Store(name, extension)
return extension.(string)
}
type DirectoryJob struct {
root string
path string
ignores []gitignore.IgnoreMatcher
}
type DirectoryWalker struct {
buffer *cuba.Pool
output chan<- *FileJob
excludes []*regexp.Regexp
}
func NewDirectoryWalker(output chan<- *FileJob) *DirectoryWalker {
directoryWalker := &DirectoryWalker{
output: output,
}
for _, exclude := range Exclude {
directoryWalker.excludes = append(directoryWalker.excludes, regexp.MustCompile(exclude))
}
directoryWalker.buffer = cuba.New(directoryWalker.Readdir, cuba.NewStack())
return directoryWalker
}
func (dw *DirectoryWalker) Walk(root string) error {
root = filepath.Clean(root)
fileInfo, err := os.Stat(root)
if err != nil {
return err
}
if !fileInfo.IsDir() {
fileJob := newFileJob(root, filepath.Base(root))
if fileJob != nil {
dw.output <- fileJob
}
return nil
}
_ = dw.buffer.Push(
&DirectoryJob{
root: root,
path: root,
ignores: nil,
},
)
return nil
}
func (dw *DirectoryWalker) Run() {
dw.buffer.Finish()
close(dw.output)
}
func (dw *DirectoryWalker) Readdir(handle *cuba.Handle) {
job := handle.Item().(*DirectoryJob)
ignores := job.ignores
file, err := os.Open(job.path)
if err != nil {
printError(fmt.Sprintf("failed to open %s: %v", job.path, err))
return
}
defer file.Close()
dirents, err := file.Readdir(-1)
if err != nil {
printError(fmt.Sprintf("failed to read %s: %v", job.path, err))
return
}
for _, dirent := range dirents {
name := dirent.Name()
if (!GitIgnore && name == ".gitignore") || (!Ignore && name == ".ignore") {
path := filepath.Join(job.path, name)
ignore, err := gitignore.NewGitIgnore(path)
if err != nil {
printError(fmt.Sprintf("failed to load gitignore %s: %v", job.path, err))
}
ignores = append(ignores, ignore)
}
}
DIRENTS:
for _, dirent := range dirents {
name := dirent.Name()
path := filepath.Join(job.path, name)
isDir := dirent.IsDir()
for _, black := range PathBlacklist {
if strings.HasSuffix(path, black) {
if Verbose {
printWarn(fmt.Sprintf("skipping directory due to being in blacklist: %s", path))
}
continue DIRENTS
}
}
for _, exclude := range dw.excludes {
if exclude.Match([]byte(name)) {
if Verbose {
printWarn("skipping directory due to match exclude: " + name)
}
continue DIRENTS
}
}
for _, ignore := range ignores {
if ignore.Match(path, isDir) {
if Verbose {
printWarn("skipping directory due to ignore: " + path)
}
continue DIRENTS
}
}
if isDir {
handle.Push(
&DirectoryJob{
root: job.root,
path: path,
ignores: ignores,
},
)
} else {
fileJob := newFileJob(path, name)
if fileJob != nil {
dw.output <- fileJob
}
}
}
}
func newFileJob(path, name string) *FileJob {
extension := ""
t := strings.Count(name, ".")
// If there is no . in the filename or it starts with one then check if #! or other
if (t == 0 || (name[0] == '.' && t == 1)) && len(WhiteListExtensions) == 0 {
return checkFullName(name, path, extension)
}
// Lookup in case the full name matches
language, ok := ExtensionToLanguage[strings.ToLower(name)]
// If no match check if we have a matching extension
if !ok {
extension = getExtension(name)
language, ok = ExtensionToLanguage[extension]
}
// Convert from d.ts to ts and check that in case of multiple extensions
if !ok {
language, ok = ExtensionToLanguage[getExtension(extension)]
}
if ok {
if len(WhiteListExtensions) != 0 {
ok = false
for _, x := range WhiteListExtensions {
if x == extension {
ok = true
}
}
if !ok {
if Verbose {
printWarn(fmt.Sprintf("skipping file as not whitelisted: %s", name))
}
return nil
}
}
for _, l := range language {
LoadLanguageFeature(l)
}
return &FileJob{
Location: path,
Filename: name,
Extension: extension,
PossibleLanguages: language,
}
} else if Verbose {
printWarn(fmt.Sprintf("skipping file unknown extension: %s", name))
}
return nil
}
func checkFullName(name string, path string, extension string) *FileJob {
// Need to check if special type
language, ok := FilenameToLanguage[strings.ToLower(name)]
if ok {
return &FileJob{
Location: path,
Filename: name,
Extension: extension,
PossibleLanguages: []string{language},
}
}
if Verbose {
printWarn(fmt.Sprintf("possible #! file: %s", name))
}
// No extension indicates possible #! so mark as such for processing
return &FileJob{
Location: path,
Filename: name,
Extension: extension,
PossibleLanguages: []string{SheBang},
}
}