github.com/jacobsoderblom/buffalo@v0.11.0/generators/action/action.go (about) 1 package action 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path/filepath" 7 "strings" 8 9 "github.com/markbates/inflect" 10 "github.com/pkg/errors" 11 "github.com/sirupsen/logrus" 12 13 "github.com/gobuffalo/buffalo/generators" 14 "github.com/gobuffalo/makr" 15 ) 16 17 // Run action generator 18 func (act Generator) Run(root string, data makr.Data) error { 19 g := makr.New() 20 defer g.Fmt(root) 21 22 filePath := filepath.Join("actions", fmt.Sprintf("%v.go", act.Name.File())) 23 actionsTemplate := act.buildActionsTemplate(filePath) 24 testFilePath := filepath.Join("actions", fmt.Sprintf("%v_test.go", act.Name.File())) 25 testsTemplate := act.buildTestsTemplate(testFilePath) 26 actionsToAdd := act.findActionsToAdd(filePath) 27 testsToAdd := act.findTestsToAdd(testFilePath) 28 handlersToAdd := act.findHandlersToAdd(filepath.Join("actions", "app.go")) 29 30 data["opts"] = act 31 data["actions"] = actionsToAdd 32 data["tests"] = testsToAdd 33 34 g.Add(makr.NewFile(filePath, actionsTemplate)) 35 g.Add(makr.NewFile(testFilePath, testsTemplate)) 36 g.Add(&makr.Func{ 37 Should: func(data makr.Data) bool { return true }, 38 Runner: func(root string, data makr.Data) error { 39 routes := []string{} 40 for _, a := range handlersToAdd { 41 routes = append(routes, fmt.Sprintf("app.%s(\"/%s/%s\", %s)", strings.ToUpper(act.Method), act.Name, a, act.Name.Camel()+a.Camel())) 42 } 43 return generators.AddInsideAppBlock(routes...) 44 }, 45 }) 46 if !act.SkipTemplate { 47 if err := act.addTemplateFiles(actionsToAdd, data); err != nil { 48 return errors.WithStack(err) 49 } 50 } 51 return g.Run(root, data) 52 } 53 54 func (act Generator) buildActionsTemplate(filePath string) string { 55 actionsTemplate := actionsHeaderTmpl 56 fileContents, err := ioutil.ReadFile(filePath) 57 if err == nil { 58 actionsTemplate = string(fileContents) 59 } 60 61 actionsTemplate = actionsTemplate + actionsTmpl 62 return actionsTemplate 63 } 64 65 func (act Generator) buildTestsTemplate(filePath string) string { 66 testsTemplate := testHeaderTmpl 67 68 fileContents, err := ioutil.ReadFile(filePath) 69 if err == nil { 70 testsTemplate = string(fileContents) 71 } 72 73 testsTemplate = testsTemplate + testsTmpl 74 return testsTemplate 75 } 76 77 func (act Generator) addTemplateFiles(actionsToAdd []inflect.Name, data makr.Data) error { 78 for _, action := range actionsToAdd { 79 vg := makr.New() 80 viewPath := filepath.Join("templates", fmt.Sprintf("%s", act.Name.File()), fmt.Sprintf("%s.html", action.File())) 81 vg.Add(makr.NewFile(viewPath, viewTmpl)) 82 err := vg.Run(".", makr.Data{ 83 "opts": act, 84 "action": action.Camel(), 85 }) 86 if err != nil { 87 return errors.WithStack(err) 88 } 89 } 90 return nil 91 } 92 93 func (act Generator) findActionsToAdd(path string) []inflect.Name { 94 fileContents, err := ioutil.ReadFile(path) 95 if err != nil { 96 fileContents = []byte("") 97 } 98 99 actionsToAdd := []inflect.Name{} 100 101 for _, action := range act.Actions { 102 funcSignature := fmt.Sprintf("func %s%s(c buffalo.Context) error", act.Name.Camel(), action.Camel()) 103 if strings.Contains(string(fileContents), funcSignature) { 104 logrus.Warnf("--> skipping %v%v since it already exists\n", act.Name.Camel(), action.Camel()) 105 continue 106 } 107 108 actionsToAdd = append(actionsToAdd, action) 109 } 110 111 return actionsToAdd 112 } 113 114 func (act Generator) findHandlersToAdd(path string) []inflect.Name { 115 fileContents, err := ioutil.ReadFile(path) 116 if err != nil { 117 fileContents = []byte("") 118 } 119 120 handlersToAdd := []inflect.Name{} 121 122 for _, action := range act.Actions { 123 funcSignature := fmt.Sprintf("app.GET(\"/%s/%s\", %s%s)", act.Name.URL(), action.URL(), act.Name.Camel(), action.Camel()) 124 if strings.Contains(string(fileContents), funcSignature) { 125 logrus.Warnf("--> skipping %s from app.go since it already exists\n", funcSignature) 126 continue 127 } 128 129 handlersToAdd = append(handlersToAdd, action) 130 } 131 132 return handlersToAdd 133 } 134 135 func (act Generator) findTestsToAdd(path string) []inflect.Name { 136 fileContents, err := ioutil.ReadFile(path) 137 if err != nil { 138 fileContents = []byte("") 139 } 140 141 actionsToAdd := []inflect.Name{} 142 143 for _, action := range act.Actions { 144 funcSignature := fmt.Sprintf("func (as *ActionSuite) Test_%v_%v() {", act.Name.Camel(), action.Camel()) 145 if strings.Contains(string(fileContents), funcSignature) { 146 logrus.Warnf("--> skipping Test_%v_%v since it already exists\n", act.Name.Camel(), action.Camel()) 147 continue 148 } 149 150 actionsToAdd = append(actionsToAdd, action) 151 } 152 153 return actionsToAdd 154 }