-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
333 lines (273 loc) · 8.25 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//go:build mage
package main
import (
"fmt"
"os"
"path"
"runtime"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
type Build mg.Namespace
type Release mg.Namespace
type Test mg.Namespace
var Aliases = map[string]interface{}{
"start": Dev,
"build": Build.Release,
"test": Test.All,
}
// ----------------------------------------------------------------------------
// Local development
// ----------------------------------------------------------------------------
// Start development server (opens app window with live reload)
func Dev() error {
log := NewLogger()
defer log.End()
return sh.Run("wails", "dev")
}
// ----------------------------------------------------------------------------
// Test
// ----------------------------------------------------------------------------
// Runs both Go, and frontend tests
func (Test) All() error {
log := NewLogger()
defer log.End()
return RunSync([][]string{
{"gotestsum", "--format", "pkgname", "--", "--cover", "./..."},
{"yarn", "--cwd", "frontend", "test:coverage"},
})
}
// Runs Go tests
func (Test) Go() error {
log := NewLogger()
defer log.End()
return RunSync([][]string{
{"gotestsum", "--format", "pkgname", "--", "--cover", "./..."},
})
}
// Runs frontend tests
func (Test) Frontend() error {
log := NewLogger()
defer log.End()
return RunSync([][]string{
{"yarn", "--cwd", "frontend", "test:coverage"},
})
}
// ----------------------------------------------------------------------------
// Build
// ----------------------------------------------------------------------------
// Create a debug development build (expect the output filesize to be much bigger)
func (Build) Debug() error {
log := NewLogger()
defer log.End()
log.Info("compiling debug binary")
return sh.Run(
"wails",
"build",
"-trimpath",
"-race",
"-debug",
)
}
// Create a release build
func (Build) Release() error {
log := NewLogger()
defer log.End()
log.Info("compiling release binary")
return sh.Run(
"wails",
"build",
"-ldflags",
"-s -w",
"-trimpath",
// "-nsis", // Builds a Windows installer
"-upx", // Binary compression
"-upxflags", "--best",
)
}
// ----------------------------------------------------------------------------
// Release
// ----------------------------------------------------------------------------
// Prep for release (rename & zip)
func (Release) Prepare() error {
log := NewLogger()
defer log.End()
binaryFileName := os.Getenv("BINARY_FILENAME")
releaseVersion := os.Getenv("RELEASE_VERSION")
binPath := "build/bin"
binaryFilePath := fmt.Sprintf("%s/%s", binPath, binaryFileName)
if binaryFileName == "" || releaseVersion == "" {
return log.Error("required environment variables not set")
}
log.Info("checking release binary")
if _, err := os.Stat(binaryFilePath); os.IsNotExist(err) {
return log.Error("failed to find release binary file:", binaryFilePath)
}
log.Info("zip for release")
zipFileName := fmt.Sprintf("adrift-native_%s_%s_%s.zip", releaseVersion, runtime.GOOS, runtime.GOARCH)
zipFilePath := fmt.Sprintf("%s/%s", binPath, zipFileName)
err := ZipFiles(zipFilePath, binaryFilePath)
if err != nil {
return log.Error("failed to zip binary", err)
}
return nil
}
// Upload release binaries to FTP server
func (Release) FTP() error {
log := NewLogger()
defer log.End()
ftpHost := os.Getenv("FTP_HOST")
ftpUsername := os.Getenv("FTP_USERNAME")
ftpPassword := os.Getenv("FTP_PASSWORD")
ftpPath := os.Getenv("FTP_PATH")
localPath := os.Getenv("LOCAL_PATH")
releaseVersion := os.Getenv("RELEASE_VERSION")
ftpReleasePath := path.Join(ftpPath, releaseVersion)
log.Info("ftp upload path: ", ftpReleasePath)
if ftpHost == "" || ftpUsername == "" || ftpPassword == "" || ftpPath == "" || localPath == "" || releaseVersion == "" {
return log.Error("required FTP environment variables not set")
}
log.Info("connecting to ftp server")
config := &ssh.ClientConfig{
User: ftpUsername,
Auth: []ssh.AuthMethod{
ssh.Password(ftpPassword),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", ftpHost, "22"), config)
if err != nil {
return log.Error("failed to connect to SFTP server:", err)
}
defer conn.Close()
client, err := sftp.NewClient(conn)
if err != nil {
return log.Error("failed to create SFTP client:", err)
}
defer client.Close()
log.Info("create remote release directory")
err = client.Mkdir(ftpReleasePath)
if err != nil && !os.IsExist(err) {
// If the directory already exists, ignore the error
return log.Error("failed to create directory on SFTP server:", err)
}
log.Info("reading local release files")
localDir, err := os.Open(localPath)
if err != nil {
return log.Error("failed to open local directory:", err)
}
defer localDir.Close()
files, err := localDir.Readdir(-1)
if err != nil {
return log.Error("failed to read local directory:", err)
}
log.Info("uploading release files to ftp server")
for _, file := range files {
if !file.IsDir() {
localFilePath := path.Join(localPath, file.Name())
remoteFilePath := path.Join(ftpReleasePath, file.Name())
localFile, err := os.Open(localFilePath)
if err != nil {
return log.Error("failed to open local file:", err)
}
defer localFile.Close()
remoteFile, err := client.Create(remoteFilePath)
if err != nil {
return log.Error("failed to create remote file:", err)
}
defer remoteFile.Close()
_, err = remoteFile.ReadFrom(localFile)
if err != nil {
return log.Error("failed to upload file to SFTP server:", err)
}
}
}
return nil
}
// ----------------------------------------------------------------------------
// Bootstrap
// ----------------------------------------------------------------------------
// Bootstraps required packages (installs required linux/macOS packages if needed)
func Bootstrap() error {
log := NewLogger()
defer log.End()
// Install required linux packages
if runtime.GOOS == "linux" {
if ExecExists("apt") {
log.Info("installing required linux packages")
err := RunSync([][]string{
{"sudo", "apt", "update", "-y"},
{"sudo", "apt", "install", "-y", "libgtk-3-dev", "libwebkit2gtk-4.0-dev", "gcc", "g++", "upx", "ftp"},
})
if err != nil {
return log.Error(err)
}
}
}
// Install required macOS packages
if runtime.GOOS == "darwin" {
// Install Homebrew
if !ExecExists("brew") {
log.Info("installing homebrew")
if err := sh.Run("/bin/bash", "-c", `"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"`); err != nil {
return log.Error(err)
}
}
// Install xcode cli tools
if ExecExists("xcode-select") {
if err := sh.Run("xcode-select", "-p"); err != nil {
log.Info("installing xcode cli tools")
if err := sh.Run("xcode-select", "--install"); err != nil {
return log.Error(err)
}
}
}
log.Info("installing required macos packages")
err := RunSync([][]string{
{"brew", "install", "upx"},
})
if err != nil {
return log.Error(err)
}
}
// Install mage bootstrap (the recommended, as seen in https://magefile.org)
if !ExecExists("mage") && ExecExists("git") {
log.Info("installing mage")
tmpDir := "__tmp_mage"
if err := sh.Run("git", "clone", "https://github.com/magefile/mage", tmpDir); err != nil {
return log.Error("error: installing mage: ", err)
}
if err := os.Chdir(tmpDir); err != nil {
return log.Error("error: installing mage: ", err)
}
if err := sh.Run("go", "run", "bootstrap.go"); err != nil {
return log.Error("error: installing mage: ", err)
}
if err := os.Chdir("../"); err != nil {
return log.Error("error: installing mage: ", err)
}
os.RemoveAll(tmpDir)
}
// Install Go dependencies
log.Info("installing go dependencies")
return RunSync([][]string{
{"go", "mod", "vendor"},
{"go", "mod", "tidy"},
{"go", "generate", "-tags", "tools", "tools/tools.go"},
})
}
// ----------------------------------------------------------------------------
// Housekeeping
// ----------------------------------------------------------------------------
// Update all Go dependencies
func UpdateDeps() error {
log := NewLogger()
defer log.End()
return RunSync([][]string{
{"go", "get", "-u", "all"},
{"go", "mod", "vendor"},
{"go", "mod", "tidy"},
})
}