forked from kevinburke/go-bindata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_finder.go
103 lines (87 loc) · 2.34 KB
/
convert_finder.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
package xbindata
import (
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/moisespsena-go/xbindata/walker"
"github.com/gobwas/glob"
)
// Finder recursively finds all the file paths in the given directory tree.
// They are added to the given map as keys. Values will be safe function names
// for each file, which will be used when generating the output code.
type Finder struct {
toc *tocRegister
ignore []*regexp.Regexp
ignoreGlob []glob.Glob
knownFuncs map[string]int
visitedPaths map[string]bool
mu *sync.Mutex
production bool
}
// find now
func (this Finder) find(input *InputConfig, prefix string) (err error) {
var dirpath string
if len(prefix) > 0 {
dirpath, _ = filepath.Abs(input.Path)
prefix = filepath.ToSlash(prefix)
}
_, err = os.Stat(dirpath)
if err != nil {
return err
}
return input.Walk(&this.visitedPaths, this.production, func(info walker.FileInfo) (err error) {
if info.IsDir() {
return nil
}
for _, re := range this.ignore {
if re.MatchString(info.Path) {
return nil
}
}
for _, ignore := range this.ignoreGlob {
if ignore.Match(info.Path) {
return nil
}
}
var asset Asset
asset.Name = filepath.ToSlash(info.Path)
if strings.HasPrefix(asset.Name, prefix) {
asset.Name = asset.Name[len(prefix):]
}
// If we have a leading slash, get rid of it.
if len(asset.Name) > 0 && asset.Name[0] == '/' {
asset.Name = asset.Name[1:]
}
if len(info.NamePrefix) > 0 {
var namePrefix []string
for i := 0; i < input.DirReplacesCount; i++ {
if pos := strings.IndexByte(asset.Name, '/'); pos > 0 {
namePrefix = append(namePrefix, asset.Name[0:pos])
asset.Name = strings.TrimPrefix(asset.Name[pos+1:], "/")
}
}
asset.Name = path.Join(append(info.NamePrefix, asset.Name)...)
if len(namePrefix) > 0 {
asset.Name = strings.ReplaceAll(asset.Name, fmt.Sprintf("(%d)", input.DirReplacesCount), path.Join(namePrefix...))
}
}
// This shouldn't happen.
if len(asset.Name) == 0 {
return fmt.Errorf("Invalid file: %v", asset.Path)
}
if asset.Path, err = filepath.Abs(info.Path); err != nil {
return err
}
asset.info = info
asset.Size = info.Size()
this.mu.Lock()
defer this.mu.Unlock()
asset.Func = safeFunctionName(asset.Name, this.knownFuncs)
this.toc.Append(asset)
return nil
})
}