forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
65 lines (57 loc) · 1.68 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package resource
import (
"errors"
"os"
"strings"
"github.com/gobuffalo/buffalo/meta"
"github.com/markbates/inflect"
)
// Generator for generating a new resource
type Generator struct {
App meta.App `json:"app"`
Name meta.Name `json:"name"`
Model meta.Name `json:"model"`
SkipMigration bool `json:"skip_migration"`
SkipModel bool `json:"skip_model"`
MimeType string `json:"mime_type"`
FilesPath string `json:"files_path"`
ActionsPath string `json:"actions_path"`
Props []Prop `json:"props"`
Args []string `json:"args"`
}
// New constructs new options for generating a resource
func New(modelName string, args ...string) (Generator, error) {
o := Generator{
MimeType: "HTML",
Args: args,
}
pwd, _ := os.Getwd()
o.App = meta.New(pwd)
if len(o.Args) > 0 {
o.Name = meta.Name(o.Args[0])
o.Model = meta.Name(o.Args[0])
}
o.Props = modelPropertiesFromArgs(o.Args)
o.FilesPath = o.Name.PluralUnder()
o.ActionsPath = o.FilesPath
if strings.Contains(string(o.Name), "/") {
parts := strings.Split(string(o.Name), "/")
o.Model = meta.Name(parts[len(parts)-1])
o.ActionsPath = inflect.Underscore(o.Name.Resource())
}
if modelName != "" {
o.Model = meta.Name(modelName)
}
return o, o.Validate()
}
// Validate that the options have what you need to build a new resource
func (o Generator) Validate() error {
mt := o.MimeType
if mt != "HTML" && mt != "JSON" && mt != "XML" {
return errors.New("invalid resource type, you need to choose between \"html\", \"xml\" and \"json\"")
}
if len(o.Args) == 0 && o.Model == "" {
return errors.New("you must specify a resource name")
}
return nil
}