diff --git a/buffalo/cmd/build.go b/buffalo/cmd/build.go index 94362d01e..b7e6fe74c 100644 --- a/buffalo/cmd/build.go +++ b/buffalo/cmd/build.go @@ -77,5 +77,6 @@ func init() { xbuildCmd.Flags().StringVar(&buildOptions.LDFlags, "ldflags", "", "set any ldflags to be passed to the go build") xbuildCmd.Flags().BoolVarP(&buildOptions.Debug, "debug", "d", false, "print debugging information") xbuildCmd.Flags().BoolVarP(&buildOptions.Compress, "compress", "c", true, "compress static files in the binary") + xbuildCmd.Flags().BoolVar(&buildOptions.SkipTemplateValidation, "skip-template-validation", false, "skip validating plush templates") xbuildCmd.Flags().StringVarP(&buildOptions.Environment, "environment", "", "development", "set the environment for the binary") } diff --git a/buffalo/cmd/build/builder.go b/buffalo/cmd/build/builder.go index 00d280028..2341bc995 100644 --- a/buffalo/cmd/build/builder.go +++ b/buffalo/cmd/build/builder.go @@ -29,6 +29,7 @@ func New(ctx context.Context, opts Options) *Builder { b.steps = []func() error{ b.prepTarget, + b.validateTemplates, b.transformMain, b.createBuildMain, b.prepAPackage, diff --git a/buffalo/cmd/build/options.go b/buffalo/cmd/build/options.go index fd835a728..6402fe631 100644 --- a/buffalo/cmd/build/options.go +++ b/buffalo/cmd/build/options.go @@ -9,14 +9,15 @@ import ( // Options for a build type Options struct { meta.App - ExtractAssets bool `json:"extract_assets"` - WithAssets bool `json:"with_assets"` - LDFlags string `json:"ld_flags"` - Tags []string `json:"tags"` - Static bool `json:"static"` - Debug bool `json:"debug"` - Compress bool `json:"compress"` - Environment string `json:"environment"` + ExtractAssets bool `json:"extract_assets"` + WithAssets bool `json:"with_assets"` + LDFlags string `json:"ld_flags"` + Tags []string `json:"tags"` + Static bool `json:"static"` + Debug bool `json:"debug"` + Compress bool `json:"compress"` + Environment string `json:"environment"` + SkipTemplateValidation bool `json:"skip_template_validation"` } func (o Options) String() string { diff --git a/buffalo/cmd/build/templates.go b/buffalo/cmd/build/templates.go index 978ef3820..7524a6379 100644 --- a/buffalo/cmd/build/templates.go +++ b/buffalo/cmd/build/templates.go @@ -1,7 +1,53 @@ package build import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" + "github.com/gobuffalo/packr" + "github.com/gobuffalo/plush" + "github.com/pkg/errors" ) var templates = packr.NewBox("./templates") + +func (b *Builder) validateTemplates() error { + if b.SkipTemplateValidation { + return nil + } + errs := []string{} + err := filepath.Walk(filepath.Join(b.App.Root, "templates"), func(path string, info os.FileInfo, err error) error { + if info == nil || info.IsDir() { + return nil + } + + ext := filepath.Ext(path) + if ext != ".html" && ext != ".md" { + return nil + } + + b, err := ioutil.ReadFile(path) + if err != nil { + return errors.WithStack(err) + } + + if _, err = plush.Parse(string(b)); err != nil { + errs = append(errs, fmt.Sprintf("template error in file %s: %s", path, err.Error())) + } + + return nil + }) + + if err != nil { + return errors.WithStack(err) + } + + if len(errs) > 0 { + return errors.New(strings.Join(errs, "\n")) + } + + return nil +}