github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/ox/expressionsfixer.go (about) 1 package ox 2 3 import ( 4 "bytes" 5 "context" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/wawandco/ox/internal/info" 11 ) 12 13 var exps = map[string]string{ 14 "middleware.Database(models.DB": "buffalotools.DatabaseMiddleware(models.DB", 15 "github.com/wawandco/ox/middleware": "github.com/wawandco/ox/pkg/buffalotools", 16 ".ServeFiles(\"/\", {{.Name}}.Assets)": ".ServeFiles(\"/\", http.FS(public.FS()))", 17 ".ServeFiles(\"/\", base.Assets)": ".ServeFiles(\"/\", http.FS(public.FS()))", 18 } 19 20 // ExpressionsFixer for buffalo/ox expressions that may have changed. 21 type ExpressionsFixer struct{} 22 23 func (rf ExpressionsFixer) Name() string { 24 return "buffalo/expressionsfixer" 25 } 26 27 func (rf ExpressionsFixer) Fix(ctx context.Context, root string, args []string) error { 28 err := filepath.Walk(root, func(path string, ii os.FileInfo, _ error) error { 29 if ii.IsDir() || filepath.Ext(path) != ".go" { 30 return nil 31 } 32 33 bn, err := info.BuildName() 34 if err != nil { 35 return err 36 } 37 38 cc, err := os.ReadFile(path) 39 if err != nil { 40 41 return err 42 } 43 44 for l, r := range exps { 45 l = strings.Replace(l, "{{.Name}}", bn, -1) 46 r = strings.Replace(r, "{{.Name}}", bn, -1) 47 48 cc = bytes.ReplaceAll(cc, []byte(l), []byte(r)) 49 50 err = os.WriteFile(path, cc, 0644) 51 if err != nil { 52 return err 53 } 54 } 55 56 return nil 57 }) 58 59 return err 60 }