forked from livekit/livekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
264 lines (229 loc) · 5.92 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
//go:build mage
// +build mage
package main
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/magefile/mage/mg"
"github.com/livekit/livekit-server/version"
"github.com/livekit/mageutil"
_ "github.com/livekit/psrpc"
)
const (
goChecksumFile = ".checksumgo"
imageName = "livekit/livekit-server"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
var Default = Build
var checksummer = mageutil.NewChecksummer(".", goChecksumFile, ".go", ".mod")
func init() {
checksummer.IgnoredPaths = []string{
"pkg/service/wire_gen.go",
"pkg/rtc/types/typesfakes",
}
}
// explicitly reinstall all deps
func Deps() error {
return installTools(true)
}
// builds LiveKit server
func Build() error {
mg.Deps(generateWire)
if !checksummer.IsChanged() {
fmt.Println("up to date")
return nil
}
fmt.Println("building...")
if err := os.MkdirAll("bin", 0755); err != nil {
return err
}
if err := mageutil.RunDir(context.Background(), "cmd/server", "go build -o ../../bin/livekit-server"); err != nil {
return err
}
checksummer.WriteChecksum()
return nil
}
// builds binary that runs on linux amd64
func BuildLinux() error {
mg.Deps(generateWire)
if !checksummer.IsChanged() {
fmt.Println("up to date")
return nil
}
fmt.Println("building...")
if err := os.MkdirAll("bin", 0755); err != nil {
return err
}
cmd := mageutil.CommandDir(context.Background(), "cmd/server", "go build -buildvcs=false -o ../../bin/livekit-server-amd64")
cmd.Env = []string{
"GOOS=linux",
"GOARCH=amd64",
"HOME=" + os.Getenv("HOME"),
"GOPATH=" + os.Getenv("GOPATH"),
}
if err := cmd.Run(); err != nil {
return err
}
checksummer.WriteChecksum()
return nil
}
func Deadlock() error {
ctx := context.Background()
if err := mageutil.InstallTool("golang.org/x/tools/cmd/goimports", "latest", false); err != nil {
return err
}
if err := mageutil.Run(ctx, "go get github.com/sasha-s/go-deadlock"); err != nil {
return err
}
if err := mageutil.Pipe("grep -rl sync.Mutex ./pkg", "xargs sed -i -e s/sync.Mutex/deadlock.Mutex/g"); err != nil {
return err
}
if err := mageutil.Pipe("grep -rl sync.RWMutex ./pkg", "xargs sed -i -e s/sync.RWMutex/deadlock.RWMutex/g"); err != nil {
return err
}
if err := mageutil.Pipe("grep -rl deadlock.Mutex\\|deadlock.RWMutex ./pkg", "xargs goimports -w"); err != nil {
return err
}
if err := mageutil.Run(ctx, "go mod tidy"); err != nil {
return err
}
return nil
}
func Sync() error {
if err := mageutil.Pipe("grep -rl deadlock.Mutex ./pkg", "xargs sed -i -e s/deadlock.Mutex/sync.Mutex/g"); err != nil {
return err
}
if err := mageutil.Pipe("grep -rl deadlock.RWMutex ./pkg", "xargs sed -i -e s/deadlock.RWMutex/sync.RWMutex/g"); err != nil {
return err
}
if err := mageutil.Pipe("grep -rl sync.Mutex\\|sync.RWMutex ./pkg", "xargs goimports -w"); err != nil {
return err
}
if err := mageutil.Run(context.Background(), "go mod tidy"); err != nil {
return err
}
return nil
}
// builds and publish snapshot docker image
func PublishDocker() error {
// don't publish snapshot versions as latest or minor version
if !strings.Contains(version.Version, "SNAPSHOT") {
return errors.New("Cannot publish non-snapshot versions")
}
versionImg := fmt.Sprintf("%s:v%s", imageName, version.Version)
cmd := exec.Command("docker", "buildx", "build",
"--push", "--platform", "linux/amd64,linux/arm64",
"--tag", versionImg,
".")
mageutil.ConnectStd(cmd)
if err := cmd.Run(); err != nil {
return err
}
return nil
}
// regenerate psrpc service definitions
func Psrpc() error {
psrpcProtoFiles := []string{
"pkg/service/rpc/egress.proto",
}
fmt.Println("generating psrpc")
protocolDir, err := mageutil.GetPkgDir("github.com/livekit/protocol")
if err != nil {
return err
}
psrpcDir, err := mageutil.GetPkgDir("github.com/livekit/psrpc")
if err != nil {
return err
}
protoc, err := mageutil.GetToolPath("protoc")
if err != nil {
return err
}
protocGoPath, err := mageutil.GetToolPath("protoc-gen-go")
if err != nil {
return err
}
psrpcPath, err := mageutil.GetToolPath("protoc-gen-psrpc")
if err != nil {
return err
}
fmt.Println("generating psrpc protobuf")
args := append([]string{
"--go_out", ".",
"--psrpc_out", ".",
"--go_opt=paths=source_relative",
"--psrpc_opt=paths=source_relative",
"--plugin=go=" + protocGoPath,
"--plugin=psrpc=" + psrpcPath,
"-I" + protocolDir,
"-I" + psrpcDir + "/protoc-gen-psrpc/options",
"-I=.",
}, psrpcProtoFiles...)
cmd := exec.Command(protoc, args...)
mageutil.ConnectStd(cmd)
if err := cmd.Run(); err != nil {
return err
}
return nil
}
// run unit tests, skipping integration
func Test() error {
mg.Deps(generateWire, setULimit)
return mageutil.Run(context.Background(), "go test -short ./... -count=1")
}
// run all tests including integration
func TestAll() error {
mg.Deps(generateWire, setULimit)
return mageutil.Run(context.Background(), "go test ./... -count=1 -timeout=4m -v")
}
// cleans up builds
func Clean() {
fmt.Println("cleaning...")
os.RemoveAll("bin")
os.Remove(goChecksumFile)
}
// regenerate code
func Generate() error {
mg.Deps(installDeps, generateWire)
fmt.Println("generating...")
return mageutil.Run(context.Background(), "go generate ./...")
}
// code generation for wiring
func generateWire() error {
mg.Deps(installDeps)
if !checksummer.IsChanged() {
return nil
}
fmt.Println("wiring...")
wire, err := mageutil.GetToolPath("wire")
if err != nil {
return err
}
cmd := exec.Command(wire)
cmd.Dir = "pkg/service"
mageutil.ConnectStd(cmd)
if err := cmd.Run(); err != nil {
return err
}
return nil
}
// implicitly install deps
func installDeps() error {
return installTools(false)
}
func installTools(force bool) error {
tools := map[string]string{
"github.com/google/wire/cmd/wire": "latest",
}
for t, v := range tools {
if err := mageutil.InstallTool(t, v, force); err != nil {
return err
}
}
return nil
}