forked from GeertJohan/go.rice
/
find.go
58 lines (49 loc) · 1.43 KB
/
find.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
package main
import (
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"regexp"
)
func findBoxes(pkg *build.Package) map[string]bool {
// create one list of files for this package
filenames := make([]string, 0, len(pkg.GoFiles)+len(pkg.CgoFiles))
filenames = append(filenames, pkg.GoFiles...)
filenames = append(filenames, pkg.CgoFiles...)
// prepare regex to find calls to rice.FindBox(..)
regexpBox, err := regexp.Compile(`rice\.(?:Must)?FindBox\(["` + "`" + `]{1}([a-zA-Z0-9\\/\.\-_]+)["` + "`" + `]{1}\)`)
if err != nil {
fmt.Printf("error compiling rice.FindBox regexp: %s\n", err)
os.Exit(1)
}
// create map of boxes to embed
var boxMap = make(map[string]bool)
// loop over files, search for rice.FindBox(..) calls
for _, filename := range filenames {
// find full filepath
fullpath := filepath.Join(pkg.Dir, filename)
verbosef("scanning file %s\n", fullpath)
// open source file
file, err := os.Open(fullpath)
if err != nil {
fmt.Printf("error opening file '%s': %s\n", filename, err)
os.Exit(1)
}
defer file.Close()
// slurp source code
fileData, err := ioutil.ReadAll(file)
if err != nil {
fmt.Printf("error reading file '%s': %s\n", filename, err)
os.Exit(1)
}
// find rice.FindBox(..) calls
matches := regexpBox.FindAllStringSubmatch(string(fileData), -1)
for _, match := range matches {
boxMap[match[1]] = true
verbosef("\tfound box '%s'\n", match[1])
}
}
return boxMap
}