Codebase list golang-github-gobuffalo-packr / f0f7731
Builder should ignore commented lines fixes #9 Mark Bates 6 years ago
3 changed file(s) with 42 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
55 "os"
66 "path/filepath"
77 "regexp"
8 "strings"
89 "text/template"
910
1011 "github.com/pkg/errors"
1112 )
1213
13 var boxPattern = regexp.MustCompile(`packr.NewBox\(["` + "`" + `]([^\(\)]+)["` + "`" + `]\)`)
14 // ^\s*[^(//)]*\s*packr.NewBox\([\"`]([^\(\)]+)[\"`]\)
15 var boxPattern = regexp.MustCompile("^\\s*[^(//)]*\\s*packr.NewBox\\([\"`]([^\\(\\)]+)[\"`]\\)")
1416
1517 var packagePattern = regexp.MustCompile(`package\s+(\w+)`)
1618
7981 }
8082 fb := string(bb)
8183
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 }
8690
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]
9397
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 }
101106 }
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 }()
108120 if err != nil {
109121 return errors.WithStack(err)
110122 }
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)
118123 }
119 }
120124
121 if len(pk.Boxes) > 0 {
122 b.addPkg(pk)
125 if len(pk.Boxes) > 0 {
126 b.addPkg(pk)
127 }
123128 }
124129 return nil
125130 }
22 import "github.com/gobuffalo/packr"
33
44 func init() {
5 // packr.NewBox("../idontexists")
56 packr.NewBox("./assets")
67 }
1919 }
2020
2121 func init() {
22 rootCmd.Flags().StringVarP(&input, "input", "i", ".", "path to scan for packr Boxes")
22 pwd, _ := os.Getwd()
23 rootCmd.Flags().StringVarP(&input, "input", "i", pwd, "path to scan for packr Boxes")
2324 }
2425
2526 // Execute the commands