-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmagefile.go
317 lines (245 loc) · 6.93 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//go:build mage
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// can be overwritten by GOEXE
var goExe = "go"
// can be overwritten by PY3EXE
var py3Exe = "python3"
// can be overwritten by CLIEXE
var cliExe = "cartridge"
var goPackageName = "github.com/tarantool/cartridge-cli/cli"
var packagePath = "./cli"
var generateModePath = filepath.Join(packagePath, "codegen", "generate_code.go")
var generatedFilesPath = fmt.Sprintf("./%s", filepath.Join(packagePath, "codegen", "static"))
var generatedFSFile = "../../create/create_vfsdata_gen.go"
var generatedModeFile = "create_cartrdige_template_filemodes_gen.go"
var completionPath = "./completion"
var tmpPath = "./tmp"
var sdkDirName = "tarantool-enterprise"
var sdkDirPath = filepath.Join(tmpPath, sdkDirName)
func getBuildEnv() map[string]string {
var err error
var curDir string
var gitTag string
var gitCommit string
if curDir, err = os.Getwd(); err != nil {
fmt.Printf("Failed to get current directory: %s\n", err)
}
if _, err := exec.LookPath("git"); err == nil {
gitTag, _ = sh.Output("git", "describe", "--tags")
gitCommit, _ = sh.Output("git", "rev-parse", "--short", "HEAD")
}
versionLabel := os.Getenv("VERSION_LABEL")
return map[string]string{
"PACKAGE": goPackageName,
"GIT_TAG": gitTag,
"GIT_COMMIT": gitCommit,
"VERSION_LABEL": versionLabel,
"PWD": curDir,
}
}
var ldflags = []string{
"-s", "-w",
"-X ${PACKAGE}/version.gitTag=${GIT_TAG}",
"-X ${PACKAGE}/version.gitCommit=${GIT_COMMIT}",
"-X ${PACKAGE}/version.versionLabel=${VERSION_LABEL}",
}
var ldflagsStr = strings.Join(ldflags, " ")
var asmflags = "all=-trimpath=${PWD}"
var gcflags = "all=-trimpath=${PWD}"
func init() {
var err error
if specifiedGoExe := os.Getenv("GOEXE"); specifiedGoExe != "" {
goExe = specifiedGoExe
}
if specifiedCliExe := os.Getenv("CLIEXE"); specifiedCliExe != "" {
cliExe = specifiedCliExe
} else {
if cliExe, err = filepath.Abs(cliExe); err != nil {
panic(err)
}
}
// We want to use Go 1.11 modules even if the source lives inside GOPATH.
// The default is "auto".
os.Setenv("GO111MODULE", "on")
}
// Generate Go code that statically implements filesystem
// and map with modes for that filesystem.
func GenerateGoCode() error {
fmt.Println("Generating Go code...")
err := sh.RunWith(
getBuildEnv(), goExe,
"generate", "-tags=dev",
generatedFilesPath,
)
if err != nil {
return err
}
err = sh.RunWith(
getBuildEnv(), goExe,
"run", generateModePath,
)
if err != nil {
return err
}
return nil
}
// Run license checker.
func CheckLicenses() error {
fmt.Println("Running license checker...")
home, err := os.UserHomeDir()
if err != nil {
return err
}
if err := sh.RunV(home+"/go/bin/lichen", "--config", ".lichen.yaml", "cartridge"); err != nil {
return err
}
return nil
}
// Run go vet and flake8
func Lint() error {
mg.Deps(GenerateGoCode)
fmt.Println("Running go vet...")
if err := sh.RunV(goExe, "vet", packagePath); err != nil {
return err
}
fmt.Println("Running flake8...")
if err := sh.RunV(py3Exe, "-m", "flake8", "test"); err != nil {
return err
}
fmt.Println("Running luacheck for test projects files...")
if err := sh.RunV(".rocks/bin/luacheck", "test/files"); err != nil {
return err
}
return nil
}
// Run unit tests
func Unit() error {
mg.Deps(GenerateGoCode)
fmt.Println("Running unit tests...")
if mg.Verbose() {
return sh.RunV(goExe, "test", "-v", "./cli/...")
} else {
return sh.RunV(goExe, "test", "./cli/...")
}
}
// Run integration tests
func Integration() error {
mg.Deps(GenerateGoCode)
build_projects_path, err := ioutil.TempDir("", "cartridge_cli_test_temp_")
defer os.RemoveAll(build_projects_path)
fmt.Println("Running integration tests...")
os.Setenv("CC_TEST_PREBUILT_PROJECTS", build_projects_path)
err = sh.RunV(py3Exe, "-m", "pytest", "test/make_preparations/test_build_projects.py")
if err != nil {
return err
}
err = sh.RunV(py3Exe, "-m", "pytest", "test/integration")
if err != nil {
return err
}
return err
}
// Run examples tests
func TestExamples() error {
mg.Deps(GenerateGoCode)
fmt.Println("Running examples tests...")
return sh.RunV(py3Exe, "-m", "pytest", "test/examples")
}
// Run e2e tests
func E2e() error {
mg.Deps(GenerateGoCode)
fmt.Println("Running e2e tests...")
return sh.RunV(py3Exe, "-m", "pytest", "test/e2e")
}
// Run all tests
func Test() {
mg.SerialDeps(Lint, CheckLicenses, Unit, Integration, TestExamples, E2e)
}
// Build cartridge-cli executable
func Build() error {
mg.Deps(GenerateGoCode)
var err error
fmt.Println("Building...")
err = sh.RunWith(
getBuildEnv(), goExe, "build",
"-o", cliExe,
"-ldflags", ldflagsStr,
"-asmflags", asmflags,
"-gcflags", gcflags,
packagePath,
)
if err != nil {
return fmt.Errorf("Failed to build cartridge-cli executable: %s", err)
}
return nil
}
// Generate completion scripts for bash and zsh
func GenCompletion() error {
if err := Build(); err != nil {
return err
}
fmt.Println("Generate autocompletion...")
if err := sh.Run(cliExe, "gen", "completion"); err != nil {
return fmt.Errorf("Failed to generate autocompletion scripts: %s", err)
}
return nil
}
// Download Tarantool Enterprise to tmp/tarantool-enterprise dir
func Sdk() error {
if _, err := os.Stat(sdkDirPath); os.IsNotExist(err) {
if err := downloadSdk(); err != nil {
return err
}
} else if err != nil {
return fmt.Errorf("Failed to check if SDK exists: %s", err)
} else {
fmt.Printf("Found Tarantool Enterprise SDK: %s\n", sdkDirPath)
}
fmt.Printf("Run `source %s/env.sh` to activate Tarantool Enterprise\n", sdkDirPath)
return nil
}
// Clean up after yourself
func Clean() {
fmt.Println("Cleaning...")
os.Remove(filepath.Join(generatedFilesPath, generatedFSFile))
os.Remove(filepath.Join(generatedFilesPath, generatedModeFile))
os.RemoveAll(cliExe)
os.RemoveAll(completionPath)
}
func downloadSdk() error {
bundleVersion := os.Getenv("BUNDLE_VERSION")
if bundleVersion == "" {
return fmt.Errorf("Please, specify BUNDLE_VERSION")
}
downloadToken := os.Getenv("DOWNLOAD_TOKEN")
if downloadToken == "" {
return fmt.Errorf("Please, specify DOWNLOAD_TOKEN")
}
archivedSDKName := fmt.Sprintf("tarantool-enterprise-bundle-%s.tar.gz", bundleVersion)
sdkDownloadUrl := fmt.Sprintf(
"https://tarantool:%[email protected]/enterprise/%s",
downloadToken,
archivedSDKName,
)
fmt.Printf("Download Tarantool Enterprise SDK %s...\n", bundleVersion)
archivedSDKPath := filepath.Join(tmpPath, archivedSDKName)
if err := downloadFile(sdkDownloadUrl, archivedSDKPath); err != nil {
return fmt.Errorf("Failed to download archived SDK: %s", err)
}
defer os.RemoveAll(archivedSDKPath)
fmt.Println("Unarchive Tarantool Enterprise SDK...")
if err := sh.RunV("tar", "-xzf", archivedSDKPath, "-C", tmpPath); err != nil {
return fmt.Errorf("Failed to unarchive SDK: %s")
}
return nil
}