forked from gobuffalo/buffalo
/
resource.go
84 lines (72 loc) · 2.31 KB
/
resource.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package generate
import (
"errors"
"fmt"
"path/filepath"
"github.com/markbates/gentronics"
"github.com/markbates/inflect"
"github.com/spf13/cobra"
)
// ResourceCmd generates a new actions/resource file and a stub test.
var ResourceCmd = &cobra.Command{
Use: "resource [name]",
Aliases: []string{"r"},
Short: "Generates a new actions/resource file",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("you must specify a resource name")
}
name := args[0]
data := gentronics.Data{
"name": name,
"singular": inflect.Singularize(name),
"plural": inflect.Pluralize(name),
"camel": inflect.Camelize(name),
"under": inflect.Underscore(name),
"downFirstCap": inflect.CamelizeDownFirst(name),
"actions": []string{"List", "Show", "New", "Create", "Edit", "Update", "Destroy"},
}
return NewResourceGenerator(data).Run(".", data)
},
}
// NewResourceGenerator generates a new actions/resource file and a stub test.
func NewResourceGenerator(data gentronics.Data) *gentronics.Generator {
g := gentronics.New()
g.Add(gentronics.NewFile(filepath.Join("actions", fmt.Sprintf("%s.go", data["downFirstCap"])), rAction))
g.Add(gentronics.NewFile(filepath.Join("actions", fmt.Sprintf("%s_test.go", data["under"])), rResourceTest))
g.Add(&gentronics.Func{
Should: func(data gentronics.Data) bool { return true },
Runner: func(root string, data gentronics.Data) error {
return addInsideAppBlock(fmt.Sprintf("var %sResource buffalo.Resource", data["downFirstCap"]),
fmt.Sprintf("%sResource = %sResource{&buffalo.BaseResource{}}", data["downFirstCap"], data["camel"]),
fmt.Sprintf("app.Resource(\"/%s\", %sResource)", data["under"], data["downFirstCap"]),
)
},
})
g.Add(Fmt)
return g
}
var rAction = `package actions
import "github.com/gobuffalo/buffalo"
type {{camel}}Resource struct{
buffalo.Resource
}
{{#each actions}}
// {{.}} default implementation.
func (v {{camel}}Resource) {{.}}(c buffalo.Context) error {
return c.Render(200, r.String("{{camel}}#{{.}}"))
}
{{/each}}
`
var rResourceTest = `package actions_test
import (
"testing"
"github.com/stretchr/testify/require"
)
{{#each actions}}
func Test_{{camel}}Resource_{{camelize .}}(t *testing.T) {
r := require.New(t)
r.Fail("Not Implemented!")
}
{{/each}}
`