-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.go
526 lines (434 loc) · 12.2 KB
/
build.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package main
import (
"bytes"
"flag"
"fmt"
"go/build"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
var (
gitMail = flag.String("git-author-mail", "cs3org-bot@hugo.labkode.com", "Git author mail")
gitName = flag.String("git-author-name", "cs3org-bot", "Git author name")
gitSSH = flag.Bool("git-ssh", false, "Use git protocol instead of https for cloning repos")
_only_build = flag.Bool("only-build", false, "Build all protos and languages but do not push to language repos")
_all = flag.Bool("all", false, "Compile, build and publish for all available languages, mean to be run in CI platform like Drone")
_buildProto = flag.Bool("build-proto", false, "Compile Protobuf definitions")
_buildGo = flag.Bool("build-go", false, "Build Go library")
_pushGo = flag.Bool("push-go", false, "Push Go library to github.com/cs3org/go-cs3apis")
_buildPython = flag.Bool("build-python", false, "Build Python library")
_pushPython = flag.Bool("push-python", false, "Push Python library to github.com/cs3org/python-cs3apis")
_buildJs = flag.Bool("build-js", false, "Build Js library")
_pushJs = flag.Bool("push-js", false, "Push Js library to github.com/cs3org/js-cs3apis")
_buildNode = flag.Bool("build-node", false, "Build Node.js library")
_pushNode = flag.Bool("push-node", false, "Push Node.js library to github.com/cs3org/node-cs3apis")
)
func init() {
flag.Parse()
if *_all {
*_buildProto = true
*_buildGo = true
*_buildPython = true
*_buildJs = true
*_buildNode = true
*_pushGo = true
*_pushPython = true
*_pushJs = true
*_pushNode = true
}
if *_only_build {
*_buildProto = true
*_buildGo = true
*_buildPython = true
*_buildJs = true
*_buildNode = true
}
}
func getProtoOS() string {
switch runtime.GOOS {
case "darwin":
return "osx"
case "linux":
return "linux"
default:
panic("no build procedure for " + runtime.GOOS)
}
}
func clone(repo, dir string) {
repo = getRepo(repo) // get git or https repo location
cmd := exec.Command("git", "clone", "--quiet", repo)
cmd.Dir = dir
run(cmd)
}
func checkout(branch, dir string) {
// See https://stackoverflow.com/questions/26961371/switch-on-another-branch-create-if-not-exists-without-checking-if-already-exi
cmd := exec.Command("bash", "-c", fmt.Sprintf("git checkout %s || git checkout -b %s", branch, branch))
cmd.Dir = dir
run(cmd)
}
func update(dir string) error {
cmd := exec.Command("git", "pull", "--quiet")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
return cmd.Run()
}
func isRepoDirty(repo string) bool {
cmd := exec.Command("git", "status", "-s")
cmd.Dir = repo
changes := runAndGet(cmd)
if changes != "" {
fmt.Println("repo is dirty")
fmt.Println(changes)
}
return changes != ""
}
func getCommitID(dir string) string {
if os.Getenv("BUILD_GIT_COMMIT") != "" {
return os.Getenv("BUILD_GIT_COMMIT")
}
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = dir
commit := runAndGet(cmd)
return commit
}
func getRepo(repo string) string {
if *gitSSH {
return fmt.Sprintf("git@github.com:%s", repo)
}
return fmt.Sprintf("https://github.com/%s", repo)
}
func commit(repo, msg string) {
// set correct author name and mail
cmd := exec.Command("git", "config", "user.email", *gitMail)
cmd.Dir = repo
run(cmd)
cmd = exec.Command("git", "config", "user.name", *gitName)
cmd.Dir = repo
run(cmd)
// check if repo is dirty
if !isRepoDirty(repo) {
// nothing to do
return
}
cmd = exec.Command("git", "add", ".")
cmd.Dir = repo
run(cmd)
cmd = exec.Command("git", "commit", "-m", msg)
cmd.Dir = repo
run(cmd)
}
func push(repo string) {
protoBranch := getGitBranch(".")
cmd := exec.Command("git", "push", "--set-upstream", "origin", protoBranch)
cmd.Dir = repo
run(cmd)
}
func getGitBranch(repo string) string {
// check if branch is provided by env variable
if os.Getenv("BUILD_GIT_BRANCH") != "" {
return os.Getenv("BUILD_GIT_BRANCH")
}
// obtain branch from repo
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = repo
branch := runAndGet(cmd)
return branch
}
// getVersionFromGit returns a version string that identifies the currently
// checked out git commit.
func getVersionFromGit(repodir string) string {
cmd := exec.Command("git", "describe",
"--long", "--tags", "--dirty", "--always")
cmd.Dir = repodir
out, err := cmd.Output()
if err != nil {
panic(fmt.Sprintf("git describe returned error: %v\n", err))
}
version := strings.TrimSpace(string(out))
return version
}
func run(cmd *exec.Cmd) {
var b bytes.Buffer
mw := io.MultiWriter(os.Stdout, &b)
cmd.Stdout = mw
cmd.Stderr = mw
err := cmd.Run()
fmt.Println(cmd.Dir, cmd.Args)
fmt.Println(b.String())
if err != nil {
fmt.Println("ERROR: ", err.Error())
os.Exit(1)
}
}
func runAndGet(cmd *exec.Cmd) string {
var b bytes.Buffer
mw := io.MultiWriter(os.Stdout, &b)
cmd.Stderr = mw
out, err := cmd.Output()
fmt.Println(cmd.Dir, cmd.Args)
fmt.Println(b.String())
if err != nil {
fmt.Println("ERROR: ", err.Error())
os.Exit(1)
}
return strings.TrimSpace(string(out))
}
// Works with Go 1.8+
// https://stackoverflow.com/questions/32649770/how-to-get-current-gopath-from-code
func getGoPath() string {
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
return gopath
}
func sed(dir, suffix, old, new string) {
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if strings.HasSuffix(path, suffix) {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
newData := strings.ReplaceAll(string(data), old, new)
err = ioutil.WriteFile(path, []byte(newData), 0)
if err != nil {
return err
}
}
return nil
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func find(patterns ...string) []string {
var files []string
for _, p := range patterns {
fs, err := filepath.Glob(p)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
files = append(files, fs...)
}
return files
}
func findProtos() []string {
return find("cs3/*/*.proto", "cs3/*/*/*.proto", "cs3/*/*/*/*.proto")
}
func findFolders() []string {
var folders []string
err := filepath.Walk("cs3",
func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if info.IsDir() {
folders = append(folders, path)
}
return nil
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return folders
}
func buildProto() {
dir := "."
cmd := exec.Command("prototool", "compile", "--walk-timeout", "10s")
cmd.Dir = dir
run(cmd)
cmd = exec.Command("protolock", "status")
cmd.Dir = dir
run(cmd)
// lint
cmd = exec.Command("prototool", "format", "-w", "--walk-timeout", "10s")
cmd.Dir = dir
run(cmd)
cmd = exec.Command("prototool", "lint", "--walk-timeout", "10s")
cmd.Dir = dir
run(cmd)
cmd = exec.Command("go", "run", "tools/check-license/check-license.go")
cmd.Dir = dir
run(cmd)
os.RemoveAll("docs")
os.MkdirAll("docs", 0755)
files := findProtos()
fmt.Println(files)
args := []string{"--doc_out=./docs", "--doc_opt=html,index.html", "-I=.", "-I=./third_party"}
args = append(args, files...)
cmd = exec.Command("protoc", args...)
run(cmd)
}
func buildGo() {
// Remove build dir
os.RemoveAll("build/go-cs3apis")
os.MkdirAll("build", 0755)
// Clone Go repo and set branch to current branch
clone("cs3org/go-cs3apis", "build")
protoBranch := getGitBranch(".")
goBranch := getGitBranch("build/go-cs3apis")
fmt.Printf("Proto branch: %s\nGo branch: %s\n", protoBranch, goBranch)
if goBranch != protoBranch {
checkout(protoBranch, "build/go-cs3apis")
}
// remove leftovers (existing defs)
os.RemoveAll("build/go-cs3apis/cs3")
cmd := exec.Command("prototool", "generate", "--walk-timeout", "10s")
run(cmd)
sed("build/go-cs3apis", ".go", "github.com/cs3org/go-cs3apis/build/go-cs3apis/cs3/", "github.com/cs3org/go-cs3apis/cs3/")
if !isRepoDirty("build/go-cs3apis") {
fmt.Println("Repo is clean, nothing to do")
}
// get proto repo commit id
hash := getCommitID(".")
repo := "build/go-cs3apis"
msg := "Synced to https://github.com/cs3org/cs3apis/tree/" + hash
commit(repo, msg)
}
func buildPython() {
// Remove build dir
os.RemoveAll("build/python-cs3apis")
os.MkdirAll("build", 0755)
// Clone Go repo and set branch to current branch
clone("cs3org/python-cs3apis", "build")
protoBranch := getGitBranch(".")
buildBranch := getGitBranch("build/python-cs3apis")
fmt.Printf("Proto branch: %s\nBuild branch: %s\n", protoBranch, buildBranch)
if buildBranch != protoBranch {
checkout(protoBranch, "build/python-cs3apis")
}
// remove leftovers (existing defs)
os.RemoveAll("build/python-cs3apis/cs3")
files := findProtos()
args := []string{"-m", "grpc_tools.protoc", "--python_out=./build/python-cs3apis", "-I.", "-I./third_party", "--grpc_python_out=./build/python-cs3apis"}
args = append(args, files...)
cmd := exec.Command("python3", args...)
run(cmd)
modules := findFolders()
var initFiles []string
for _, f := range modules {
initPy := fmt.Sprintf("%s/%s/%s", "build/python-cs3apis", f, "__init__.py")
initFiles = append(initFiles, initPy)
}
cmd = exec.Command("touch", initFiles...)
run(cmd)
// get proto repo commit id
hash := getCommitID(".")
repo := "build/python-cs3apis"
msg := "Synced to https://github.com/cs3org/cs3apis/tree/" + hash
commit(repo, msg)
}
func buildJS() {
// Remove build dir
os.RemoveAll("build/js-cs3apis")
os.MkdirAll("build", 0755)
// Clone repo and set branch to current branch
clone("cs3org/js-cs3apis", "build")
protoBranch := getGitBranch(".")
buildBranch := getGitBranch("build/js-cs3apis")
fmt.Printf("Proto branch: %s\nBuild branch: %s\n", protoBranch, buildBranch)
if buildBranch != protoBranch {
checkout(protoBranch, "build/js-cs3apis")
}
// remove leftovers (existing defs)
os.RemoveAll("build/js-cs3apis/cs3")
files := findProtos()
args := []string{"--js_out=import_style=commonjs:./build/js-cs3apis", "--grpc-web_out=import_style=commonjs,mode=grpcwebtext:./build/js-cs3apis/", "-I.", "-I./third_party"}
args = append(args, files...)
cmd := exec.Command("protoc", args...)
run(cmd)
// get proto repo commit id
hash := getCommitID(".")
repo := "build/js-cs3apis"
msg := "Synced to https://github.com/cs3org/cs3apis/tree/" + hash
commit(repo, msg)
}
func buildNode() {
// Remove build dir
os.RemoveAll("build/node-cs3apis")
os.MkdirAll("build", 0755)
// Clone repo and set branch to current branch
clone("cs3org/node-cs3apis", "build")
protoBranch := getGitBranch(".")
buildBranch := getGitBranch("build/node-cs3apis")
fmt.Printf("Proto branch: %s\nBuild branch: %s\n", protoBranch, buildBranch)
if buildBranch != protoBranch {
checkout(protoBranch, "build/node-cs3apis")
}
nodeProtocPlugin, err := exec.LookPath("grpc_tools_node_protoc_plugin")
if err != nil {
panic(fmt.Sprintf("grpc_tools_node_protoc_plugin binary not found in PATH: %v\n", err))
}
// remove leftovers (existing defs)
os.RemoveAll("build/node-cs3apis/cs3")
files := findProtos()
args := []string{"--js_out=import_style=commonjs,binary:./build/node-cs3apis", "--grpc_out=./build/node-cs3apis/", "--plugin=protoc-gen-grpc=" + nodeProtocPlugin}
args = append(args, files...)
cmd := exec.Command("grpc_tools_node_protoc", args...)
run(cmd)
// get proto repo commit id
hash := getCommitID(".")
repo := "build/node-cs3apis"
msg := "Synced to https://github.com/cs3org/cs3apis/tree/" + hash
commit(repo, msg)
}
func pushPython() {
push("build/python-cs3apis")
}
func pushGo() {
push("build/go-cs3apis")
}
func pushJS() {
push("build/js-cs3apis")
}
func pushNode() {
push("build/node-cs3apis")
}
func main() {
if *_buildProto {
fmt.Println("Compiling and linting protobufs ...")
buildProto()
}
if *_buildGo {
fmt.Println("Building Go ...")
buildGo()
}
if *_pushGo {
fmt.Println("Pushing Go ...")
pushGo()
}
if *_buildPython {
fmt.Println("Building Python ...")
buildPython()
}
if *_pushPython {
fmt.Println("Pushing Python ...")
pushPython()
}
if *_buildJs {
fmt.Println("Building JS ...")
buildJS()
}
if *_pushJs {
fmt.Println("Pushing Js ...")
pushJS()
}
if *_buildNode {
fmt.Println("Building Node.js ...")
buildNode()
}
if *_pushNode {
fmt.Println("Pushing Node.js ...")
pushNode()
}
}