github.com/jackgris/buffalo@v0.10.3-0.20171206174715-b1dbfd2402de/generators/files.go (about) 1 package generators 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 9 "github.com/gobuffalo/envy" 10 ) 11 12 // TemplatesPath is the "base" path for generator templates 13 var TemplatesPath = filepath.Join("github.com", "gobuffalo", "buffalo", "generators") 14 15 // File represents the file to be templated 16 type File struct { 17 ReadPath string 18 WritePath string 19 Body string 20 } 21 22 // Files is a slice of File 23 type Files []File 24 25 // Find all the .tmpl files inside the buffalo GOPATH 26 func Find(path string) (Files, error) { 27 root := filepath.Join(envy.GoPath(), "src", path, "templates") 28 files := Files{} 29 err := filepath.Walk(root, func(p string, info os.FileInfo, err error) error { 30 if info != nil && !info.IsDir() { 31 if filepath.Ext(p) == ".tmpl" { 32 f := File{ReadPath: p} 33 rel := strings.TrimPrefix(p, root) 34 35 paths := strings.Split(rel, string(os.PathSeparator)) 36 37 li := len(paths) - 1 38 base := paths[li] 39 base = strings.TrimSuffix(base, ".tmpl") 40 if strings.HasPrefix(base, "dot-") { 41 base = "." + strings.TrimPrefix(base, "dot-") 42 } 43 paths[li] = base 44 f.WritePath = filepath.Join(paths...) 45 46 b, err := ioutil.ReadFile(p) 47 if err != nil { 48 return err 49 } 50 f.Body = string(b) 51 files = append(files, f) 52 } 53 } 54 return nil 55 }) 56 return files, err 57 }