github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/buffalo/cmd/fix/plush.go (about) 1 package fix 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "path/filepath" 8 "strings" 9 ) 10 11 // Plush will update foo.html templates to foo.plush.html templates 12 func Plush(r *Runner) error { 13 templatesDir := filepath.Join(r.App.Root, "templates") 14 if _, err := os.Stat(templatesDir); os.IsNotExist(err) { 15 // Skip if the templates dir doesn't exist (e.g. API apps) 16 return nil 17 } 18 fmt.Println("~~~ Adding .plush extension to .html/.js/.md files ~~~") 19 return filepath.Walk(templatesDir, func(p string, info os.FileInfo, err error) error { 20 if err != nil { 21 return err 22 } 23 24 if info.IsDir() { 25 return nil 26 } 27 28 dir := filepath.Dir(p) 29 base := filepath.Base(p) 30 31 var exts []string 32 ext := filepath.Ext(base) 33 for len(ext) != 0 { 34 if ext == ".plush" || ext == ".fizz" { 35 return nil 36 } 37 exts = append([]string{ext}, exts...) 38 base = strings.TrimSuffix(base, ext) 39 ext = filepath.Ext(base) 40 } 41 exts = append([]string{".plush"}, exts...) 42 43 pn := filepath.Join(dir, base+strings.Join(exts, "")) 44 45 fn, err := os.Create(pn) 46 if err != nil { 47 return err 48 } 49 defer fn.Close() 50 51 fo, err := os.Open(p) 52 if err != nil { 53 return err 54 } 55 defer fo.Close() 56 _, err = io.Copy(fn, fo) 57 58 defer os.Remove(p) 59 60 return err 61 }) 62 }