forked from gobuffalo/buffalo
/
soda.go
99 lines (75 loc) · 1.7 KB
/
soda.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package soda
import (
"github.com/gobuffalo/makr"
sg "github.com/gobuffalo/pop/soda/cmd/generate"
)
// Run the soda generator
func (sd Generator) Run(root string, data makr.Data) error {
g := makr.New()
defer g.Fmt(root)
should := func(data makr.Data) bool {
return sd.App.WithPop
}
f := makr.NewFile("models/models.go", nModels)
f.Should = should
g.Add(f)
f = makr.NewFile("models/models_test.go", nModelsTest)
f.Should = should
g.Add(f)
f = makr.NewFile("grifts/db.go", nSeedGrift)
f.Should = should
g.Add(f)
c := makr.NewCommand(makr.GoGet("github.com/gobuffalo/pop/..."))
c.Should = should
g.Add(c)
g.Add(&makr.Func{
Should: should,
Runner: func(rootPath string, data makr.Data) error {
data["dialect"] = sd.Dialect
return sg.GenerateConfig("./database.yml", data)
},
})
return g.Run(root, data)
}
const nModels = `package models
import (
"log"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/pop"
)
// DB is a connection to your database to be used
// throughout your application.
var DB *pop.Connection
func init() {
var err error
env := envy.Get("GO_ENV", "development")
DB, err = pop.Connect(env)
if err != nil {
log.Fatal(err)
}
pop.Debug = env == "development"
}
`
const nModelsTest = `package models_test
import (
"testing"
"github.com/gobuffalo/suite"
)
type ModelSuite struct {
*suite.Model
}
func Test_ModelSuite(t *testing.T) {
as := &ModelSuite{suite.NewModel()}
suite.Run(t, as)
}`
const nSeedGrift = `package grifts
import (
"github.com/markbates/grift/grift"
)
var _ = grift.Namespace("db", func() {
grift.Desc("seed", "Seeds a database")
grift.Add("seed", func(c *grift.Context) error {
// Add DB seeding stuff here
return nil
})
})`