github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/build/template_walker.go (about)

     1  package build
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/gobuffalo/meta"
    10  	"github.com/gobuffalo/packd"
    11  	"github.com/karrick/godirwalk"
    12  )
    13  
    14  type dirWalker struct {
    15  	dir string
    16  }
    17  
    18  func (d dirWalker) WalkPrefix(pre string, fn packd.WalkFunc) error {
    19  	return d.Walk(func(path string, file packd.File) error {
    20  		if strings.HasPrefix(path, pre) {
    21  			return fn(path, file)
    22  		}
    23  		return nil
    24  	})
    25  }
    26  
    27  func (d dirWalker) Walk(fn packd.WalkFunc) error {
    28  	callback := func(path string, de *godirwalk.Dirent) error {
    29  		if de != nil && de.IsDir() {
    30  			base := filepath.Base(path)
    31  			for _, pre := range []string{"vendor", ".", "_"} {
    32  				if strings.HasPrefix(base, pre) {
    33  					return filepath.SkipDir
    34  				}
    35  			}
    36  			return nil
    37  		}
    38  		b, err := ioutil.ReadFile(path)
    39  		if err != nil {
    40  			return err
    41  		}
    42  		f, err := packd.NewFile(path, bytes.NewReader(b))
    43  		if err != nil {
    44  			return err
    45  		}
    46  		return fn(path, f)
    47  	}
    48  
    49  	godirwalk.Walk(d.dir, &godirwalk.Options{
    50  		FollowSymbolicLinks: true,
    51  		Callback:            callback,
    52  	})
    53  	return nil
    54  }
    55  
    56  func templateWalker(app meta.App) packd.Walkable {
    57  	return dirWalker{dir: app.Root}
    58  }