This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
magefile-dev.go
274 lines (244 loc) · 7.2 KB
/
magefile-dev.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//go:build mage
// +build mage
package main
import (
"bufio"
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/fs"
"os"
"path"
"strings"
"github.com/magefile/mage/sh"
)
//###################################//
// Mage Commands used my a developer //
//###################################//
//////////////////
// Dependencies //
//////////////////
// GetDependencies installs needed Go CLI Tools and Dependencies. Required to run go:generate or other generators.
func GetDependencies() error {
// Check Required Tools
requirements := [][]string{
{"go", "version"},
{"node", "--version"},
{"yarn", "--version"},
}
for _, requirement := range requirements {
if err := sh.Run(requirement[0], requirement[1:]...); err != nil {
fmt.Println("Please make sure this tool is installed. It is a prerequisite.")
return err
}
}
// Install Doc Dependencies
if err := sh.Run("yarn", "install", "--cwd", "./docs"); err != nil {
return err
}
// Get Go Mod Packages
if err := sh.Run("go", "get", "--tags=psql,unit,integration", "./..."); err != nil {
return err
}
// Install CLIs
packages := []string{
"github.com/golang/mock/mockgen",
"github.com/golangci/golangci-lint/cmd/golangci-lint",
"github.com/swaggo/swag/cmd/swag",
"github.com/princjef/gomarkdoc/cmd/gomarkdoc",
"github.com/volatiletech/sqlboiler/v4",
"github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-psql",
"github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-mysql",
}
for _, pkg := range packages {
if err := sh.Run("go", "install", pkg); err != nil {
return err
}
}
return nil
}
///////////////////////////
// Software Dev Commands //
///////////////////////////
// GenerateSQLBoiler generates sql builerplate code from the specified Database in the config and places it in ./internal/storage/db/models. Needs a running mysql and psql instance with ccdb database and a fully deployed schema to generate the code from.
func GenerateSQLBoiler() error {
if err := sh.Rm("./internal/storage/db/models"); err != nil {
return err
}
if err := os.MkdirAll("internal/storage/db/models/mocks", 0755); err != nil {
return err
}
if err := sh.Run("sqlboiler", "psql", "-c", "sqlboiler_psql.toml",
"--no-driver-templates",
"--templates", "sqlboiler-templates/shared",
"--templates", "sqlboiler-templates/psql",
); err != nil {
return err
}
if err := sh.Run("sqlboiler", "mysql", "-c", "sqlboiler_mysql.toml",
"--no-driver-templates",
"--templates", "sqlboiler-templates/shared",
"--templates", "sqlboiler-templates/mysql",
); err != nil {
return err
}
if err := modifySQLBoilerFiles("psql", true); err != nil {
return err
}
if err := modifySQLBoilerFiles("mysql", false); err != nil {
return err
}
if err := sh.Rm("./internal/storage/db/models/psql"); err != nil {
return err
}
if err := sh.Rm("./internal/storage/db/models/mysql"); err != nil {
return err
}
if err := sh.Run("go", "generate", "--tags=psql,mysql,unit", "./..."); err != nil {
return err
}
return nil
}
// modifySQLBoilerFiles adds buildtags for multi db support. Also add go generated for test mocks. TODO -> Do this with SQLBoiler Natively(Needs Contribution)
func modifySQLBoilerFiles(dbEngine string, addGoGenerate bool) error {
// Append build tags to every generated file so we can use e.g. "go test -tags=db,psql" to switch between unit and db tests
fileSet := token.NewFileSet()
packages, err := parser.ParseDir(
fileSet,
fmt.Sprintf("internal/storage/db/models/%s", dbEngine),
func(fi fs.FileInfo) bool { return true },
parser.ParseComments,
)
for filepath, file := range packages["models"].Files {
commentMap := ast.NewCommentMap(fileSet, file, file.Comments)
_, filename := path.Split(filepath)
comments := []*ast.Comment{{Text: fmt.Sprintf("// +build %s", dbEngine)}}
if addGoGenerate && !strings.HasPrefix(filename, "boil_") {
comments = append(comments, &ast.Comment{
Text: fmt.Sprintf("//go:generate sh -c \"echo '\\x2bbuild unit' > ../../../../buildtags.txt && mockgen -source=$GOFILE -destination=mocks/%s -copyright_file=../../../../buildtags.txt && rm -f ../../../../buildtags.txt\"", filename),
})
}
commentMap[file] = append([]*ast.CommentGroup{{List: comments}}, commentMap[file]...)
file.Comments = commentMap.Comments()
buf := &bytes.Buffer{}
err = format.Node(buf, fileSet, file)
if err != nil {
return err
}
err = os.WriteFile(fmt.Sprintf("internal/storage/db/models/%s_%s", dbEngine, filename), buf.Bytes(), 0644)
if err != nil {
return err
}
}
return err
}
///////////////////////
// Software Building //
///////////////////////
// Build Runs go:generate and builds both binary(psql and mysql).
func Build() error {
if err := sh.Rm("./build/cfapi*"); err != nil {
return err
}
if err := sh.Run("go", "generate", "./..."); err != nil {
return err
}
if err := sh.RunV("go", "build", "--tags=mysql", "-o", "build/cfapi_mysql", "cmd/main.go"); err != nil {
return err
}
return sh.RunV("go", "build", "--tags=psql", "-o", "build/cfapi_psql", "cmd/main.go")
}
// Run runs go:generate and starts cfapi with config_psql.yaml.
func Run() error {
if err := sh.Run("go", "generate", "./..."); err != nil {
return err
}
return sh.RunV("go", "run", "--tags=psql", "cmd/main.go", "config_psql.yaml")
}
///////////////////////////
// DOCUMENTATION HELPERS //
///////////////////////////
// GenerateDocs generates Markdown Docs for Go Packages in ./docs/godocs
func GenerateDocs() error {
if err := sh.Run("go", "generate", "./..."); err != nil {
return err
}
const godocdir = "./docs/godocs/Packages"
if err := sh.Rm(godocdir); err != nil {
return err
}
if err := sh.Run("mkdir", "-p", godocdir); err != nil {
return err
}
if err := sh.Run("gomarkdoc", "-u", "--output", fmt.Sprintf("%s/{{.ImportPath}}.md", godocdir), "./..."); err != nil {
return err
}
if err := sh.Copy("./docs/docs/intro.md", "./README.md"); err != nil {
return err
}
if err := prependToFile("---\nsidebar_position: 1\ntitle: Intro\n---", "./docs/docs/intro.md"); err != nil {
return err
}
return nil
}
func prependToFile(text string, fileName string) error {
tmpFileName := fmt.Sprintf("%s_tmp", fileName)
tmpFile, err := os.Create(tmpFileName)
defer tmpFile.Close()
if err != nil {
return err
}
inFile, err := os.Open(fileName)
defer inFile.Close()
if err != nil {
return err
}
_, err = tmpFile.WriteString(text)
if err != nil {
return err
}
scanner := bufio.NewScanner(inFile)
for scanner.Scan() {
_, err = tmpFile.WriteString(scanner.Text())
_, err = tmpFile.WriteString("\n")
}
if err := scanner.Err(); err != nil {
return err
}
tmpFile.Sync()
err = os.Rename(tmpFileName, fileName)
if err != nil {
return err
}
return nil
}
//////////
// MISC //
//////////
// Clean removes all generated files from the project
func Clean() error {
paths := []string{
"./build",
"./vendor",
"./repl",
"./internal/api/docs/docs.go",
"./internal/api/docs/swagger.json",
"./internal/api/docs/swagger.yaml",
"./docs/swagger.yaml",
"./docs/build",
"./docs/.docusaurus",
"./docs/godocs/Packages",
"./docs/godocs/nmode_modules",
"./main.go",
"./mage_output_file.go",
}
for _, p := range paths {
if err := sh.Rm(p); err != nil {
return err
}
}
return nil
}