github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/fix/plush.go (about)

     1  package fix
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/gobuffalo/genny/v2"
    10  )
    11  
    12  // UpdatePlushTemplates will update foo.html templates to foo.plush.html templates
    13  func UpdatePlushTemplates(opts *Options) genny.RunFn {
    14  	return func(r *genny.Runner) error {
    15  		fmt.Println("~~~ Adding .plush extension to .html/.js/.md files ~~~")
    16  		err := walkDisk(r.Disk, "templates", func(path string, info os.FileInfo, err error) error {
    17  			if err != nil {
    18  				return err
    19  			}
    20  
    21  			if info.IsDir() {
    22  				return nil
    23  			}
    24  
    25  			dir := filepath.Dir(path)
    26  			base := filepath.Base(path)
    27  
    28  			var exts []string
    29  			ext := filepath.Ext(base)
    30  			switch ext {
    31  			case ".html":
    32  			case ".js":
    33  			case ".md":
    34  			default:
    35  				return nil
    36  			}
    37  
    38  			for len(ext) != 0 {
    39  				if ext == ".plush" || ext == ".fizz" {
    40  					return nil
    41  				}
    42  				exts = append([]string{ext}, exts...)
    43  				base = strings.TrimSuffix(base, ext)
    44  				ext = filepath.Ext(base)
    45  			}
    46  			exts = append([]string{base, ".plush"}, exts...)
    47  			pathNew := filepath.Join(dir, strings.Join(exts, ""))
    48  
    49  			fo, err := r.FindFile(path)
    50  			if err != nil {
    51  				return err
    52  			}
    53  
    54  			fn := genny.NewFile(pathNew, fo)
    55  			if err := r.File(fn); err != nil {
    56  				return err
    57  			}
    58  			return r.Delete(path)
    59  		})
    60  		return err
    61  	}
    62  }