-
Notifications
You must be signed in to change notification settings - Fork 5
/
magefile.go
197 lines (167 loc) · 4.2 KB
/
magefile.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// +build mage
package main
import (
"fmt"
"os"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
type Build mg.Namespace
type Lint mg.Namespace
type Test mg.Namespace
type Run mg.Namespace
type Clean mg.Namespace
// Build the frontend and backend
func (b Build) All() {
mg.Deps(b.Backend, b.Frontend)
}
// Build the backend binaries
func (Build) Backend() error {
cleanBackend()
var version string
var err error
version = os.Getenv("VERSION")
if version == "" {
version, err = generateBackendVersion()
if err != nil {
return err
}
}
flags := fmt.Sprintf(`-X go-webapp-example/internal/app.version=%s -s -w`, version)
binary := "go-webapp-example"
env := map[string]string{
"CGO_ENABLED": "0",
"GOOS": "linux",
"GOARCH": "amd64",
}
if err := sh.RunWith(env, "go", "build", "-ldflags", flags, "-o", "output/"+binary); err != nil {
return err
}
return nil
}
// Build the frontend
func (Build) Frontend() error {
// Return this to really build your frontend.
if true {
return nil
}
env := map[string]string{"NODE_ENV": "production"}
if err := sh.RunWith(env, "yarn", "--cwd", "web", "build", "--production"); err != nil {
return err
}
return nil
}
// Lint all code
func (l Lint) All() {
mg.Deps(l.Backend, l.Frontend)
}
// Lint all backend code
func (Lint) Backend() error {
return sh.Run("golangci-lint", "run", "--fix")
}
// Lint all frontend code
func (Lint) Frontend() error {
return sh.Run("yarn", "--cwd", "web", "lint")
}
// Test all code
func (t Test) All() {
mg.Deps(t.Backend, t.Frontend)
}
// Run all backend unit tests
func (Test) Backend() error {
return sh.Run("go", "test", "./...", "-test.short")
}
// Run all backend unit tests with race detection
func (Test) Race() error {
return sh.Run("go", "test", "-race", "./...", "-test.short")
}
// Run all backend tests, include integration tests
func (Test) Integration() error {
return sh.Run("go", "test", "./...")
}
// Run all backend system tests
func (Test) System() error {
return sh.Run("go", "test", "./...")
}
// Run all frontend tests
func (Test) Frontend() error {
return sh.Run("yarn", "--cwd", "web", "test:unit")
}
// Cleanup output directories
func (Clean) Filesystem() error {
if err := cleanBackend(); err != nil {
return err
}
return nil
}
// Remove all docker dependencies
func (Clean) Docker() error {
sh.Run("pkill", "-e", "air")
if err := sh.Run("docker-compose", "-f", "deployments/docker-compose.yml", "-f", "deployments/docker-compose.dev.yml", "down"); err != nil {
return err
}
var output string
var err error
if output, err = sh.Output("docker", "ps"); err != nil {
return err
}
fmt.Println(output)
return nil
}
// Install all required dependencies
func (Run) Deps() error {
if err := sh.Run("yarn", "--cwd", "web"); err != nil {
return err
}
if err := sh.Run("go", "mod", "download"); err != nil {
return err
}
return nil
}
// Start the docker stack
func (Run) Docker() error {
return sh.Run("docker-compose", "-f", "deployments/docker-compose.yml", "-f", "deployments/docker-compose.dev.yml", "up", "-d")
}
// Start air
func (Run) Backend() error {
return sh.Run("air")
}
// Run all DB migrations
func (Run) Migrate() error {
if err := sh.Run("go", "run", "main.go", "migrate", "fresh"); err != nil {
return err
}
return nil
}
// Run all code generations
func (Run) Generate() error {
if err := sh.Run("rm", "-f", "internal/graphql/gqlresolvers/resolver.go"); err != nil {
return err
}
if err := sh.Run("go", "generate", "./..."); err != nil {
return err
}
if err := sh.Run("rm", "-f", "internal/graphql/gqlresolvers/resolver.go"); err != nil {
return err
}
if err := sh.Run("rm", "-f", "internal/graphql/gqlresolvers/*.resolvers.go"); err != nil {
return err
}
return nil
}
// Cleanup backend build output directories
func cleanBackend() error {
return sh.Run("rm", "-rf", "output/*")
}
// Fetch the version information from git
func generateBackendVersion() (string, error) {
commit, err := sh.Output("git", "rev-parse", "--short", "HEAD")
if err != nil {
return "no-repo", nil
}
branch, err := sh.Output("git", "rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return "no-repo", nil
}
return fmt.Sprintf("%s-%s", commit, branch), nil
}