github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/ox/action/generator.go (about) 1 package action 2 3 import ( 4 "context" 5 "fmt" 6 "io/fs" 7 "os" 8 "path/filepath" 9 10 "errors" 11 12 "github.com/gobuffalo/flect" 13 "github.com/wawandco/ox/internal/source" 14 ) 15 16 type Generator struct { 17 name string 18 filename string 19 dir string 20 } 21 22 // Name returns the name of the plugin 23 func (g Generator) Name() string { 24 return "buffalo/generate-action" 25 } 26 27 // InvocationName is used to identify the generator when 28 // the generate command is called. 29 func (g Generator) InvocationName() string { 30 return "action" 31 } 32 33 func (g Generator) Generate(ctx context.Context, root string, args []string) error { 34 if len(args) < 3 { 35 return fmt.Errorf("no name specified, please use `ox generate action [name]`") 36 } 37 38 dirPath := filepath.Join(root, "app", "actions") 39 if !g.exists(dirPath) { 40 err := os.MkdirAll(filepath.Dir(dirPath), 0755) 41 if err != nil { 42 return (err) 43 } 44 } 45 46 g.name = flect.Singularize(args[2]) 47 g.filename = flect.Singularize(flect.Underscore(args[2])) 48 g.dir = dirPath 49 50 if g.exists(filepath.Join(g.dir, g.filename+".go")) { 51 return errors.New("action file already exists") 52 } 53 54 if err := g.generateActionFiles(args[3:]); err != nil { 55 return err 56 } 57 58 return nil 59 } 60 61 func (g Generator) generateActionFiles(args []string) error { 62 if err := g.createActionFile(args); err != nil { 63 return fmt.Errorf("creating action file: %w", err) 64 } 65 66 if err := g.createActionTestFile(); err != nil { 67 return fmt.Errorf("creating action test file: %w", err) 68 } 69 70 return nil 71 } 72 73 func (g Generator) createActionFile(args []string) error { 74 path := filepath.Join(g.dir, g.filename+".go") 75 data := struct { 76 Name string 77 }{ 78 Name: g.name, 79 } 80 actionTemplate, err := g.callTemplate("action.go.tmpl") 81 if err != nil { 82 return fmt.Errorf("error calling template: %w", err) 83 } 84 85 err = source.Build(path, actionTemplate, data) 86 if err != nil { 87 return fmt.Errorf("error generating action: %w", err) 88 } 89 90 return nil 91 } 92 93 func (g Generator) createActionTestFile() error { 94 path := filepath.Join(g.dir, g.filename+"_test.go") 95 data := struct { 96 Name string 97 }{ 98 Name: g.name, 99 } 100 101 actionTestTemplate, err := g.callTemplate("action_test.go.tmpl") 102 if err != nil { 103 return fmt.Errorf("error calling template: %w", err) 104 } 105 106 err = source.Build(path, actionTestTemplate, data) 107 if err != nil { 108 return fmt.Errorf("error generating action: %w", err) 109 } 110 111 return nil 112 } 113 114 func (g Generator) exists(path string) bool { 115 _, err := os.Stat(path) 116 117 return !os.IsNotExist(err) 118 } 119 120 func (g Generator) callTemplate(name string) (string, error) { 121 bt, err := fs.ReadFile(templates, filepath.Join("templates", name)) 122 if err != nil { 123 return "", nil 124 } 125 126 return string(bt), nil 127 }