diff --git a/.goreleaser.yml b/.goreleaser.yml index 4ca9e1cf3..c5c586cb8 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -22,6 +22,9 @@ builds: main: ./buffalo/main.go binary: buffalo-no-sqlite flags: -tags nosqlite + hooks: + pre: packr + post: packr clean - goos: - darwin @@ -31,6 +34,9 @@ builds: - "6" main: ./buffalo/main.go binary: buffalo-with-sqlite + hooks: + pre: packr + post: packr clean archive: name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' diff --git a/generators/assets/standard/standard.go b/generators/assets/standard/standard.go index a3f28ede7..b680c96cb 100644 --- a/generators/assets/standard/standard.go +++ b/generators/assets/standard/standard.go @@ -1,11 +1,10 @@ package standard import ( - "path/filepath" - "github.com/gobuffalo/buffalo/generators" "github.com/gobuffalo/buffalo/generators/assets" "github.com/gobuffalo/makr" + "github.com/gobuffalo/packr" "github.com/pkg/errors" ) @@ -16,7 +15,7 @@ var logo = &makr.RemoteFile{ // Run standard assets generator for those wishing to not use webpack func Run(root string, data makr.Data) error { - files, err := generators.Find(filepath.Join(generators.TemplatesPath, "assets", "standard")) + files, err := generators.FindByBox(packr.NewBox("./templates")) if err != nil { return errors.WithStack(err) } diff --git a/generators/assets/webpack/webpack.go b/generators/assets/webpack/webpack.go index b3157665b..dff34f257 100644 --- a/generators/assets/webpack/webpack.go +++ b/generators/assets/webpack/webpack.go @@ -8,6 +8,7 @@ import ( "github.com/gobuffalo/buffalo/generators/assets" "github.com/gobuffalo/buffalo/generators/assets/standard" "github.com/gobuffalo/makr" + "github.com/gobuffalo/packr" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -44,7 +45,7 @@ func (w Generator) Run(root string, data makr.Data) error { g.Add(logo) - files, err := generators.Find(filepath.Join(generators.TemplatesPath, "assets", "webpack")) + files, err := generators.FindByBox(packr.NewBox("./templates")) if err != nil { return errors.WithStack(err) } diff --git a/generators/docker/docker.go b/generators/docker/docker.go index 75e63eee0..eea30b8c3 100644 --- a/generators/docker/docker.go +++ b/generators/docker/docker.go @@ -1,10 +1,9 @@ package docker import ( - "path/filepath" - "github.com/gobuffalo/buffalo/generators" "github.com/gobuffalo/makr" + "github.com/gobuffalo/packr" "github.com/pkg/errors" ) @@ -17,11 +16,16 @@ func (d Generator) Run(root string, data makr.Data) error { return d.Style != "none" }, Runner: func(root string, data makr.Data) error { - style := d.Style - if style != "multi" && style != "standard" { - return errors.Errorf("unknown Docker style: %s", style) + var box packr.Box + switch d.Style { + case "standard": + box = packr.NewBox("./standard/templates") + case "multi": + box = packr.NewBox("./multi/templates") + default: + return errors.Errorf("unknown Docker style: %s", d.Style) } - files, err := generators.Find(filepath.Join(generators.TemplatesPath, "docker", style)) + files, err := generators.FindByBox(box) if err != nil { return errors.WithStack(err) } diff --git a/generators/files.go b/generators/files.go index 428ee5a3e..98d4badc3 100644 --- a/generators/files.go +++ b/generators/files.go @@ -1,17 +1,20 @@ package generators import ( + "fmt" "io/ioutil" "os" "path/filepath" + "runtime" "strings" + "sync" "github.com/gobuffalo/envy" + "github.com/gobuffalo/packr" + "github.com/sirupsen/logrus" + "golang.org/x/sync/errgroup" ) -// TemplatesPath is the "base" path for generator templates -var TemplatesPath = filepath.Join("github.com", "gobuffalo", "buffalo", "generators") - // File represents the file to be templated type File struct { ReadPath string @@ -22,36 +25,79 @@ type File struct { // Files is a slice of File type Files []File -// Find all the .tmpl files inside the buffalo GOPATH -func Find(path string) (Files, error) { - root := filepath.Join(envy.GoPath(), "src", path, "templates") +// FindByBox all the .tmpl files inside the packr.Box +func FindByBox(box packr.Box) (Files, error) { files := Files{} - err := filepath.Walk(root, func(p string, info os.FileInfo, err error) error { - if info != nil && !info.IsDir() { - if filepath.Ext(p) == ".tmpl" { - f := File{ReadPath: p} - rel := strings.TrimPrefix(p, root) - - paths := strings.Split(rel, string(os.PathSeparator)) - - li := len(paths) - 1 - base := paths[li] - base = strings.TrimSuffix(base, ".tmpl") - if strings.HasPrefix(base, "dot-") { - base = "." + strings.TrimPrefix(base, "dot-") - } - paths[li] = base - f.WritePath = filepath.Join(paths...) - - b, err := ioutil.ReadFile(p) - if err != nil { - return err - } - f.Body = string(b) - files = append(files, f) + err := box.Walk(func(p string, file packr.File) error { + if filepath.Ext(p) == ".tmpl" { + p = strings.TrimPrefix(p, "/") + f := File{ReadPath: p} + p = strings.Replace(p, "dot-", ".", 1) + p = strings.Replace(p, ".tmpl", "", 1) + f.WritePath = p + b, err := ioutil.ReadAll(file) + if err != nil { + return err } + f.Body = string(b) + files = append(files, f) } return nil }) return files, err } + +// TemplatesPath is the "base" path for generator templates +var TemplatesPath = filepath.Join("github.com", "gobuffalo", "buffalo", "generators") + +// Find all the .tmpl files inside the buffalo GOPATH +func Find(path string) (Files, error) { + warningMsg := "Find is deprecated, and will be removed in v0.12.0. Use generators.FindByBox instead." + _, file, no, ok := runtime.Caller(1) + if ok { + warningMsg = fmt.Sprintf("%s Called from %s:%d", warningMsg, file, no) + } + + logrus.Info(warningMsg) + mu := &sync.Mutex{} + wg := &errgroup.Group{} + files := Files{} + for _, gp := range envy.GoPaths() { + func(gp string) { + wg.Go(func() error { + root := filepath.Join(envy.GoPath(), "src", path, "templates") + return filepath.Walk(root, func(p string, info os.FileInfo, err error) error { + if info != nil && !info.IsDir() { + if filepath.Ext(p) == ".tmpl" { + f := File{ReadPath: p} + rel := strings.TrimPrefix(p, root) + + paths := strings.Split(rel, string(os.PathSeparator)) + + li := len(paths) - 1 + base := paths[li] + base = strings.TrimSuffix(base, ".tmpl") + if strings.HasPrefix(base, "dot-") { + base = "." + strings.TrimPrefix(base, "dot-") + } + paths[li] = base + f.WritePath = filepath.Join(paths...) + + b, err := ioutil.ReadFile(p) + if err != nil { + return err + } + f.Body = string(b) + mu.Lock() + files = append(files, f) + mu.Unlock() + } + } + return nil + }) + }) + }(gp) + } + err := wg.Wait() + return files, err +} diff --git a/generators/mail/mail.go b/generators/mail/mail.go index 78899bbd1..061a503c9 100644 --- a/generators/mail/mail.go +++ b/generators/mail/mail.go @@ -7,6 +7,7 @@ import ( "github.com/gobuffalo/buffalo/generators" "github.com/gobuffalo/buffalo/meta" "github.com/gobuffalo/makr" + "github.com/gobuffalo/packr" "github.com/pkg/errors" ) @@ -35,7 +36,7 @@ func (d Generator) Run(root string, data makr.Data) error { } func (d Generator) initGenerator(data makr.Data) error { - files, err := generators.Find(filepath.Join(generators.TemplatesPath, "mail", "init")) + files, err := generators.FindByBox(packr.NewBox("./init/templates")) if err != nil { return errors.WithStack(err) } diff --git a/generators/newapp/new.go b/generators/newapp/new.go index 76c808553..227a32a67 100644 --- a/generators/newapp/new.go +++ b/generators/newapp/new.go @@ -14,6 +14,7 @@ import ( "github.com/gobuffalo/buffalo/generators/soda" "github.com/gobuffalo/envy" "github.com/gobuffalo/makr" + "github.com/gobuffalo/packr" "github.com/pkg/errors" ) @@ -36,7 +37,7 @@ func (a Generator) Run(root string, data makr.Data) error { } g.Add(makr.NewCommand(makr.GoGet("github.com/motemen/gore", "-u"))) - files, err := generators.Find(filepath.Join(generators.TemplatesPath, "newapp")) + files, err := generators.FindByBox(packr.NewBox("./templates")) if err != nil { return errors.WithStack(err) } diff --git a/generators/refresh/refresh.go b/generators/refresh/refresh.go index 9c62cfdc9..ab9ea08d3 100644 --- a/generators/refresh/refresh.go +++ b/generators/refresh/refresh.go @@ -1,10 +1,9 @@ package refresh import ( - "path/filepath" - "github.com/gobuffalo/buffalo/generators" "github.com/gobuffalo/makr" + "github.com/gobuffalo/packr" "github.com/pkg/errors" ) @@ -12,7 +11,7 @@ import ( func Run(root string, data makr.Data) error { g := makr.New() - files, err := generators.Find(filepath.Join(generators.TemplatesPath, "refresh")) + files, err := generators.FindByBox(packr.NewBox("./templates")) if err != nil { return errors.WithStack(err) } diff --git a/generators/resource/resource.go b/generators/resource/resource.go index 10bc339bd..eb2a5a213 100644 --- a/generators/resource/resource.go +++ b/generators/resource/resource.go @@ -10,6 +10,7 @@ import ( "github.com/gobuffalo/buffalo/generators" "github.com/gobuffalo/makr" + "github.com/gobuffalo/packr" ) // Run generates a new actions/resource file and a stub test. @@ -29,7 +30,7 @@ func (res Generator) Run(root string, data makr.Data) error { tmplName = "resource-name" } - files, err := generators.Find(filepath.Join(generators.TemplatesPath, "resource")) + files, err := generators.FindByBox(packr.NewBox("./templates")) if err != nil { return errors.WithStack(err) }