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

stops buffalo new from generating incomplete projects if the templates are missing #1242

Merged
merged 7 commits into from
Aug 23, 2018
25 changes: 25 additions & 0 deletions generators/newapp/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import (
"github.com/gobuffalo/buffalo/meta"
"github.com/gobuffalo/buffalo/runtime"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/packr"
"github.com/gobuffalo/pop"
"github.com/markbates/inflect"
"github.com/pkg/errors"
)

// Templates are the templates needed by this generator
var Templates = packr.NewBox("../newapp/templates")

// ErrNotInGoPath can be asserted against
var ErrNotInGoPath = errors.New("currently not in a $GOPATH")

Expand Down Expand Up @@ -56,8 +60,29 @@ func New(name string) (Generator, error) {
return g, g.Validate()
}

const header = "Your `buffalo` binary installation is corrupted and is missing vital templates for app creation.\n"
const footer = "Please recheck your installation: https://gobuffalo.io/en/docs/installation."
const goPathAbuse = `It appears you are using multiple GOPATHs:
%s

Using multiple GOPATHs can cause issues with many third party tooling. Please try using only one GOPATH.
`

// ErrTemplatesNotFound means that the `buffalo` binary can't find the template files it needs
// to complete a task. This usually occurs when building `buffalo` locally and/or when using multipath
// GOPATHs.
var ErrTemplatesNotFound = errors.New("templates are missing")

// Validate that the app generator is good
func (g Generator) Validate() error {
if !Templates.Has("actions/app.go.tmpl") {
msg := header
if len(envy.GoPaths()) > 1 {
msg += "\n" + fmt.Sprintf(goPathAbuse, strings.Join(envy.GoPaths(), "\n"))
}
msg += "\n" + footer
return errors.Wrap(ErrTemplatesNotFound, msg)
}
if g.Name == "" {
return errors.New("you must enter a name for your new application")
}
Expand Down
33 changes: 33 additions & 0 deletions generators/newapp/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package newapp

import (
"testing"

"github.com/gobuffalo/packr"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

func Test_Validate_TemplatesFound(t *testing.T) {
r := require.New(t)

g := Generator{}
err := g.Validate()
r.Error(err)
r.NotEqual(ErrTemplatesNotFound, errors.Cause(err))
}

func Test_Validate_TemplatesMissing(t *testing.T) {
r := require.New(t)

obox := Templates
defer func() {
Templates = obox
}()
Templates = packr.NewBox(".")

g := Generator{}
err := g.Validate()
r.Error(err)
r.Equal(ErrTemplatesNotFound, errors.Cause(err))
}
3 changes: 1 addition & 2 deletions generators/newapp/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/gobuffalo/buffalo/generators/soda"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/makr"
"github.com/gobuffalo/packr"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -50,7 +49,7 @@ func (a Generator) Run(root string, data makr.Data) error {
}
}

files, err := generators.FindByBox(packr.NewBox("../newapp/templates"))
files, err := generators.FindByBox(Templates)
if err != nil {
return errors.WithStack(err)
}
Expand Down