github.com/wawandco/oxplugins@v0.7.11/tools/buffalo/model/generator.go (about) 1 package model 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "html/template" 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 12 "github.com/gobuffalo/flect" 13 "github.com/gobuffalo/flect/name" 14 "github.com/pkg/errors" 15 ) 16 17 // Generator allows to identify model as a plugin 18 type Generator struct { 19 name string 20 filename string 21 dir string 22 } 23 24 // Name returns the name of the generator plugin 25 func (g Generator) Name() string { 26 return "model" 27 } 28 29 // Generate generates an empty [name].plush.html file 30 func (g Generator) Generate(ctx context.Context, root string, args []string) error { 31 if len(args) < 3 { 32 return errors.Errorf("no name specified, please use `ox generate model [name]`") 33 } 34 35 dirPath := filepath.Join(root, "app", "models") 36 if !g.exists(dirPath) { 37 return errors.Errorf("folder '%s' do not exists on your buffalo app, please ensure the folder exists in order to proceed", dirPath) 38 } 39 40 g.name = flect.Singularize(args[2]) 41 g.filename = flect.Singularize(flect.Underscore(args[2])) 42 g.dir = dirPath 43 44 if g.exists(filepath.Join(g.dir, g.filename+".go")) { 45 return errors.Errorf("model already exists") 46 } 47 48 if err := g.generateModelFiles(args[3:]); err != nil { 49 return err 50 } 51 52 fmt.Printf("[info] Model generated in: \n-- app/models/%s.go\n-- app/models/%s_test.go\n", g.name, g.name) 53 54 return nil 55 } 56 57 func (g Generator) generateModelFiles(args []string) error { 58 if err := g.createModelFile(args); err != nil { 59 return errors.Wrap(err, "creating model file") 60 } 61 62 if err := g.createModelTestFile(); err != nil { 63 return errors.Wrap(err, "creating model test file") 64 } 65 66 return nil 67 } 68 69 func (g Generator) createModelFile(args []string) error { 70 filename := g.filename + ".go" 71 path := filepath.Join(g.dir, filename) 72 attrs := buildAttrs(args) 73 data := opts{ 74 Original: g.name, 75 Name: name.New(g.name), 76 Attrs: attrs, 77 Imports: buildImports(attrs), 78 } 79 80 tmpl, err := template.New(filename).Parse(modelTemplate) 81 if err != nil { 82 return errors.Wrap(err, "parsing new template error") 83 } 84 85 var tpl bytes.Buffer 86 if err := tmpl.Execute(&tpl, data); err != nil { 87 return errors.Wrap(err, "executing new template error") 88 } 89 90 err = ioutil.WriteFile(path, tpl.Bytes(), 0655) 91 if err != nil { 92 return errors.Wrap(err, "writing new template error") 93 } 94 95 return nil 96 } 97 98 func (g Generator) createModelTestFile() error { 99 filename := g.filename + "_test.go" 100 path := filepath.Join(g.dir, filename) 101 data := opts{ 102 Original: g.name, 103 Name: name.New(g.name), 104 } 105 106 tmpl, err := template.New(filename).Parse(modelTestTemplate) 107 if err != nil { 108 return errors.Wrap(err, "parsing new template error") 109 } 110 111 var tpl bytes.Buffer 112 if err := tmpl.Execute(&tpl, data); err != nil { 113 return errors.Wrap(err, "executing new template error") 114 } 115 116 err = ioutil.WriteFile(path, tpl.Bytes(), 0655) 117 if err != nil { 118 return errors.Wrap(err, "writing new template error") 119 } 120 121 return nil 122 } 123 124 func (g Generator) exists(path string) bool { 125 _, err := os.Stat(path) 126 127 return !os.IsNotExist(err) 128 }