Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #879 from gobuffalo/use-packr-for-generator-templates
Browse files Browse the repository at this point in the history
use packr to manage templates for generators. hopefully this fixes issues like #629
  • Loading branch information
markbates authored Jan 26, 2018
2 parents 5e5c9a6 + f7bfdab commit 43153e7
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 45 deletions.
6 changes: 6 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ builds:
main: ./buffalo/main.go
binary: buffalo-no-sqlite
flags: -tags nosqlite
hooks:
pre: packr
post: packr clean
-
goos:
- darwin
Expand All @@ -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 }}'
Expand Down
5 changes: 2 additions & 3 deletions generators/assets/standard/standard.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion generators/assets/webpack/webpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 10 additions & 6 deletions generators/docker/docker.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)
}
Expand Down
104 changes: 75 additions & 29 deletions generators/files.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion generators/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion generators/newapp/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions generators/refresh/refresh.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package refresh

import (
"path/filepath"

"github.com/gobuffalo/buffalo/generators"
"github.com/gobuffalo/makr"
"github.com/gobuffalo/packr"
"github.com/pkg/errors"
)

// Run generator for a .buffalo.dev.yml file
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)
}
Expand Down
3 changes: 2 additions & 1 deletion generators/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand Down

0 comments on commit 43153e7

Please sign in to comment.