-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmagefile.go
212 lines (172 loc) · 3.91 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
// +build mage
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const name string = "boltchat"
const buildDir string = "build"
const serverPrefix string = "server"
const clientPrefix string = "client"
const serverEntry string = "cmd/server/server.go"
const clientEntry string = "cmd/client/client.go"
type (
Build mg.Namespace
Test mg.Namespace
Docker mg.Namespace
CI mg.Namespace
Install mg.Namespace
)
type BuildOptions struct {
Static bool
Extension string
Prefix string
}
func build(os string, arch string, entry string, opts BuildOptions) error {
env := map[string]string{
"GOOS": os,
"GOARCH": arch,
}
// Build static binary
if opts.Static {
env["CGO_ENABLED"] = "0"
}
outputName := fmt.Sprintf(
"%s-%s-%s-%s", name, opts.Prefix, os, arch,
)
outputPath := path.Join(
buildDir,
outputName,
)
if opts.Extension != "" {
outputPath += fmt.Sprintf(".%s", opts.Extension)
}
args := []string{
"build",
"-o",
outputPath,
"-ldflags",
"-s -w",
entry,
}
fmt.Println(args)
return sh.RunWith(
env, "go", args...,
)
}
/*
Build
*/
// Builds all binaries
func (Build) All() {
mg.Deps(
Build.ServerDarwinAmd64,
Build.ServerLinuxAmd64,
Build.ServerWindowsAmd64,
Build.ClientDarwinAmd64,
Build.ClientLinuxAmd64,
Build.ClientWindowsAmd64,
)
}
// Builds the server binary for Linux (amd64)
func (Build) ServerLinuxAmd64() error {
return build("linux", "amd64", serverEntry, BuildOptions{Prefix: serverPrefix})
}
// Builds the server binary for Windows (amd64)
func (Build) ServerWindowsAmd64() error {
return build("windows", "amd64", serverEntry, BuildOptions{
Extension: "exe",
Prefix: serverPrefix,
})
}
// Builds the server binary for Darwin/macOS (amd64)
func (Build) ServerDarwinAmd64() error {
return build("darwin", "amd64", serverEntry, BuildOptions{Prefix: serverPrefix})
}
// Builds the server binary for Darwin/macOS (arm64, M1)
// func (Build) ServerDarwinArm64() error {
// return build("darwin", "arm64", serverEntry, false)
// }
// Builds the static server binary for use in a Docker container
func (Build) ServerStatic() error {
return build("linux", "amd64", serverEntry, BuildOptions{
Static: true,
Prefix: serverPrefix,
})
}
// Builds the client binary for Linux (amd64)
func (Build) ClientLinuxAmd64() error {
return build("linux", "amd64", clientEntry, BuildOptions{Prefix: clientPrefix})
}
// Builds the client binary for Windows (amd64)
func (Build) ClientWindowsAmd64() error {
return build("windows", "amd64", clientEntry, BuildOptions{
Extension: "exe",
Prefix: clientPrefix,
})
}
// Builds the client binary for Darwin/macOS (amd64)
func (Build) ClientDarwinAmd64() error {
return build("darwin", "amd64", clientEntry, BuildOptions{Prefix: clientPrefix})
}
/*
Test
*/
// Runs all unit tests
func (Test) Unit() error {
return sh.RunV("go", "test", "-v", "./...")
}
/*
Docker
*/
// Builds a Docker image for the server
func (Docker) Build() error {
return sh.RunV("docker", "build", ".", "-t", name)
}
/*
CI/CD
*/
// Compresses all binaries into a single tarball
func (CI) CompressBinaries() error {
return sh.Run("tar", "-cvzf", "binaries.tar.gz", "build")
}
/*
Misc
*/
// Adds license headers to source files
func License() {
paths := make([]string, 0)
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".go") {
paths = append(paths, path)
}
return nil
})
sh.Run(
"addlicense",
"-l", "apache",
"-c", "The boltchat Authors",
"client", "server", "protocol", "cmd", "util", "lib",
)
}
// Cleans up build directories
func Clean() {
sh.Rm("build")
}
// Installs the server to /usr/local/bin
// (Linux-only)
func (Install) Server() error {
return sh.Run(
"cp",
"build/boltchat-server-linux-amd64",
"/usr/local/bin/boltchat-server",
)
}