5 | 5 |
"os"
|
6 | 6 |
"path/filepath"
|
7 | 7 |
"regexp"
|
|
8 |
"strings"
|
8 | 9 |
"text/template"
|
9 | 10 |
|
10 | 11 |
"github.com/pkg/errors"
|
11 | 12 |
)
|
12 | 13 |
|
13 | |
var boxPattern = regexp.MustCompile(`packr.NewBox\(["` + "`" + `]([^\(\)]+)["` + "`" + `]\)`)
|
|
14 |
// ^\s*[^(//)]*\s*packr.NewBox\([\"`]([^\(\)]+)[\"`]\)
|
|
15 |
var boxPattern = regexp.MustCompile("^\\s*[^(//)]*\\s*packr.NewBox\\([\"`]([^\\(\\)]+)[\"`]\\)")
|
14 | 16 |
|
15 | 17 |
var packagePattern = regexp.MustCompile(`package\s+(\w+)`)
|
16 | 18 |
|
|
79 | 81 |
}
|
80 | 82 |
fb := string(bb)
|
81 | 83 |
|
82 | |
matches := boxPattern.FindAllStringSubmatch(fb, -1)
|
83 | |
if len(matches) == 0 {
|
84 | |
return nil
|
85 | |
}
|
|
84 |
for _, line := range strings.Split(fb, "\n") {
|
|
85 |
line = strings.TrimSpace(line)
|
|
86 |
matches := boxPattern.FindAllStringSubmatch(line, -1)
|
|
87 |
if len(matches) == 0 {
|
|
88 |
continue
|
|
89 |
}
|
86 | 90 |
|
87 | |
pk := pkg{
|
88 | |
Dir: filepath.Dir(path),
|
89 | |
Boxes: []box{},
|
90 | |
}
|
91 | |
pname := packagePattern.FindStringSubmatch(fb)
|
92 | |
pk.Name = pname[1]
|
|
91 |
pk := pkg{
|
|
92 |
Dir: filepath.Dir(path),
|
|
93 |
Boxes: []box{},
|
|
94 |
}
|
|
95 |
pname := packagePattern.FindStringSubmatch(fb)
|
|
96 |
pk.Name = pname[1]
|
93 | 97 |
|
94 | |
for _, m := range matches {
|
95 | |
n := m[1]
|
96 | |
err := func() error {
|
97 | |
for _, i := range b.IgnoredBoxes {
|
98 | |
if n == i {
|
99 | |
// this is an ignored box
|
100 | |
return nil
|
|
98 |
for _, m := range matches {
|
|
99 |
n := m[1]
|
|
100 |
err := func() error {
|
|
101 |
for _, i := range b.IgnoredBoxes {
|
|
102 |
if n == i {
|
|
103 |
// this is an ignored box
|
|
104 |
return nil
|
|
105 |
}
|
101 | 106 |
}
|
102 | |
}
|
103 | |
bx := &box{
|
104 | |
Name: n,
|
105 | |
Files: []file{},
|
106 | |
}
|
107 | |
err = bx.Walk(filepath.Join(pk.Dir, bx.Name))
|
|
107 |
bx := &box{
|
|
108 |
Name: n,
|
|
109 |
Files: []file{},
|
|
110 |
}
|
|
111 |
err = bx.Walk(filepath.Join(pk.Dir, bx.Name))
|
|
112 |
if err != nil {
|
|
113 |
return errors.WithStack(err)
|
|
114 |
}
|
|
115 |
if len(bx.Files) > 0 {
|
|
116 |
pk.Boxes = append(pk.Boxes, *bx)
|
|
117 |
}
|
|
118 |
return nil
|
|
119 |
}()
|
108 | 120 |
if err != nil {
|
109 | 121 |
return errors.WithStack(err)
|
110 | 122 |
}
|
111 | |
if len(bx.Files) > 0 {
|
112 | |
pk.Boxes = append(pk.Boxes, *bx)
|
113 | |
}
|
114 | |
return nil
|
115 | |
}()
|
116 | |
if err != nil {
|
117 | |
return errors.WithStack(err)
|
118 | 123 |
}
|
119 | |
}
|
120 | 124 |
|
121 | |
if len(pk.Boxes) > 0 {
|
122 | |
b.addPkg(pk)
|
|
125 |
if len(pk.Boxes) > 0 {
|
|
126 |
b.addPkg(pk)
|
|
127 |
}
|
123 | 128 |
}
|
124 | 129 |
return nil
|
125 | 130 |
}
|