github.com/jacobsoderblom/buffalo@v0.11.0/buffalo/cmd/build/templates.go (about)

     1  package build
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/gobuffalo/packr"
    11  	"github.com/gobuffalo/plush"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  var templates = packr.NewBox("./templates")
    16  
    17  func (b *Builder) validateTemplates() error {
    18  	if b.SkipTemplateValidation {
    19  		return nil
    20  	}
    21  	errs := []string{}
    22  	err := filepath.Walk(filepath.Join(b.App.Root, "templates"), func(path string, info os.FileInfo, err error) error {
    23  		if info == nil || info.IsDir() {
    24  			return nil
    25  		}
    26  
    27  		ext := filepath.Ext(path)
    28  		if ext != ".html" && ext != ".md" {
    29  			return nil
    30  		}
    31  
    32  		b, err := ioutil.ReadFile(path)
    33  		if err != nil {
    34  			return errors.WithStack(err)
    35  		}
    36  
    37  		if _, err = plush.Parse(string(b)); err != nil {
    38  			errs = append(errs, fmt.Sprintf("template error in file %s: %s", path, err.Error()))
    39  		}
    40  
    41  		return nil
    42  	})
    43  
    44  	if err != nil {
    45  		return errors.WithStack(err)
    46  	}
    47  
    48  	if len(errs) > 0 {
    49  		return errors.New(strings.Join(errs, "\n"))
    50  	}
    51  
    52  	return nil
    53  }