Skip to content

Commit 17243c8

Browse files
committed
use buf for builds and refactor build process
1 parent 210226c commit 17243c8

File tree

4 files changed

+39
-270
lines changed

4 files changed

+39
-270
lines changed

.drone.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
cs3apis-build
2+
build

Dockerfile

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,14 @@ RUN apt-get update
88
RUN apt-get install build-essential curl unzip sudo -y
99
RUN apt-get install python3-pip python3-full -y
1010

11-
# deps for protoc
12-
RUN cd /tmp && curl -sSL https://github.com/protocolbuffers/protobuf/releases/download/v25.0/protoc-25.0-linux-x86_64.zip -o protoc.zip && unzip -o protoc.zip && sudo cp bin/protoc /usr/local/bin/protoc
13-
RUN cd /tmp && curl -sSL https://github.com/uber/prototool/releases/download/v1.10.0/prototool-Linux-x86_64 -o prototool && sudo cp prototool /usr/local/bin/ && sudo chmod u+x /usr/local/bin/prototool
14-
RUN cd /tmp && curl -sSL https://github.com/nilslice/protolock/releases/download/v0.16.0/protolock.20220302T184110Z.linux-amd64.tgz -o protolock.tgz && tar -xzf protolock.tgz && sudo cp protolock /usr/local/bin/
15-
RUN cd /tmp && curl -sSL https://github.com/pseudomuto/protoc-gen-doc/releases/download/v1.5.1/protoc-gen-doc_1.5.1_linux_amd64.tar.gz -o protoc-gen-doc.tar.gz && tar xzfv protoc-gen-doc.tar.gz && sudo cp protoc-gen-doc /usr/local/bin/
16-
RUN go install github.com/golang/protobuf/protoc-gen-go@v1.5.3
17-
18-
19-
# deps for python
20-
RUN pip install grpcio grpcio-tools --ignore-installed --break-system-packages
21-
22-
# deps for js
23-
RUN curl -sSL https://github.com/grpc/grpc-web/releases/download/1.5.0/protoc-gen-grpc-web-1.5.0-linux-x86_64 -o /tmp/protoc-gen-grpc-web
24-
RUN sudo mv /tmp/protoc-gen-grpc-web /usr/local/bin/ && sudo chmod u+x /usr/local/bin/protoc-gen-grpc-web
25-
2611
# deps for node.js
2712
RUN sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg
2813
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
2914
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
3015
RUN apt-get update
3116
RUN sudo apt-get install nodejs -y
3217
RUN node -v
33-
RUN npm install protoc-gen-grpc -g -unsafe-perm
18+
RUN npm install -g @bufbuild/buf
3419

3520
# compile build tool and put it into path
3621
ADD . /root/cs3apis-build

build.go

Lines changed: 37 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,14 @@ var (
1919
gitName = flag.String("git-author-name", "cs3org-bot", "Git author name")
2020
gitSSH = flag.Bool("git-ssh", false, "Use git protocol instead of https for cloning repos")
2121

22-
_only_build = flag.Bool("only-build", false, "Build all protos and languages but do not push to language repos")
23-
_all = flag.Bool("all", false, "Compile, build and publish for all available languages, mean to be run in CI platform like Drone")
24-
25-
_buildProto = flag.Bool("build-proto", false, "Compile Protobuf definitions")
26-
27-
_buildGo = flag.Bool("build-go", false, "Build Go library")
28-
_pushGo = flag.Bool("push-go", false, "Push Go library to github.com/cs3org/go-cs3apis")
29-
30-
_buildPython = flag.Bool("build-python", false, "Build Python library")
31-
_pushPython = flag.Bool("push-python", false, "Push Python library to github.com/cs3org/python-cs3apis")
32-
33-
_buildJs = flag.Bool("build-js", false, "Build Js library")
34-
_pushJs = flag.Bool("push-js", false, "Push Js library to github.com/cs3org/js-cs3apis")
35-
36-
_buildNode = flag.Bool("build-node", false, "Build Node.js library")
37-
_pushNode = flag.Bool("push-node", false, "Push Node.js library to github.com/cs3org/node-cs3apis")
22+
_pushGo = flag.Bool("push-go", false, "Push Go library to github.com/cs3org/go-cs3apis")
23+
_pushPython = flag.Bool("push-python", false, "Push Python library to github.com/cs3org/python-cs3apis")
24+
_pushJs = flag.Bool("push-js", false, "Push Js library to github.com/cs3org/js-cs3apis")
25+
_pushNode = flag.Bool("push-node", false, "Push Node.js library to github.com/cs3org/node-cs3apis")
3826
)
3927

4028
func init() {
4129
flag.Parse()
42-
43-
if *_all {
44-
*_buildProto = true
45-
*_buildGo = true
46-
*_buildPython = true
47-
*_buildJs = true
48-
*_buildNode = true
49-
50-
*_pushGo = true
51-
*_pushPython = true
52-
*_pushJs = true
53-
*_pushNode = true
54-
}
55-
56-
if *_only_build {
57-
*_buildProto = true
58-
*_buildGo = true
59-
*_buildPython = true
60-
*_buildJs = true
61-
*_buildNode = true
62-
}
6330
}
6431

6532
func getProtoOS() string {
@@ -280,185 +247,57 @@ func findFolders() []string {
280247
return folders
281248
}
282249

283-
func buildProto() {
284-
dir := "."
285-
cmd := exec.Command("prototool", "compile", "--walk-timeout", "10s")
286-
cmd.Dir = dir
287-
run(cmd)
288-
289-
cmd = exec.Command("protolock", "status")
290-
cmd.Dir = dir
291-
run(cmd)
292-
293-
// lint
294-
cmd = exec.Command("prototool", "format", "-w", "--walk-timeout", "10s")
295-
cmd.Dir = dir
296-
run(cmd)
297-
cmd = exec.Command("prototool", "lint", "--walk-timeout", "10s")
298-
cmd.Dir = dir
299-
run(cmd)
300-
cmd = exec.Command("go", "run", "tools/check-license/check-license.go")
301-
cmd.Dir = dir
302-
run(cmd)
303-
304-
os.RemoveAll("docs")
305-
os.MkdirAll("docs", 0755)
306-
307-
files := findProtos()
308-
fmt.Println(files)
309-
310-
args := []string{"--doc_out=./docs", "--doc_opt=html,index.html", "-I=.", "-I=./third_party"}
311-
args = append(args, files...)
312-
cmd = exec.Command("protoc", args...)
313-
run(cmd)
314-
}
315-
316-
func buildGo() {
317-
318-
// Remove build dir
319-
os.RemoveAll("build/go-cs3apis")
320-
os.MkdirAll("build", 0755)
321-
322-
// Clone Go repo and set branch to current branch
323-
clone("cs3org/go-cs3apis", "build")
324-
protoBranch := getGitBranch(".")
325-
goBranch := getGitBranch("build/go-cs3apis")
326-
fmt.Printf("Proto branch: %s\nGo branch: %s\n", protoBranch, goBranch)
327-
328-
if goBranch != protoBranch {
329-
checkout(protoBranch, "build/go-cs3apis")
250+
func generate() {
251+
cwd, err := os.Getwd()
252+
if err != nil {
253+
panic(err)
330254
}
255+
fmt.Printf("current working directory: %s\n", cwd)
331256

332-
// remove leftovers (existing defs)
333-
os.RemoveAll("build/go-cs3apis/cs3")
334-
335-
cmd := exec.Command("prototool", "generate", "--walk-timeout", "10s")
257+
cmd := exec.Command("git", "config", "--global", "--add", "safe.directory", cwd)
336258
run(cmd)
337259

338-
sed("build/go-cs3apis", ".go", "github.com/cs3org/go-cs3apis/build/go-cs3apis/cs3/", "github.com/cs3org/go-cs3apis/cs3/")
339-
340-
if !isRepoDirty("build/go-cs3apis") {
341-
fmt.Println("Repo is clean, nothing to do")
342-
}
343-
344-
// get proto repo commit id
345-
hash := getCommitID(".")
346-
repo := "build/go-cs3apis"
347-
msg := "Synced to https://github.com/cs3org/cs3apis/tree/" + hash
348-
commit(repo, msg)
349-
}
350-
351-
func buildPython() {
352-
353260
// Remove build dir
354-
os.RemoveAll("build/python-cs3apis")
261+
os.RemoveAll("build")
355262
os.MkdirAll("build", 0755)
356263

357-
// Clone Go repo and set branch to current branch
358-
clone("cs3org/python-cs3apis", "build")
359-
protoBranch := getGitBranch(".")
360-
buildBranch := getGitBranch("build/python-cs3apis")
361-
fmt.Printf("Proto branch: %s\nBuild branch: %s\n", protoBranch, buildBranch)
362-
363-
if buildBranch != protoBranch {
364-
checkout(protoBranch, "build/python-cs3apis")
365-
}
366-
367-
// remove leftovers (existing defs)
368-
os.RemoveAll("build/python-cs3apis/cs3")
369-
370-
files := findProtos()
371-
372-
args := []string{"-m", "grpc_tools.protoc", "--python_out=./build/python-cs3apis", "-I.", "-I./third_party", "--grpc_python_out=./build/python-cs3apis"}
373-
args = append(args, files...)
374-
cmd := exec.Command("python3", args...)
375-
run(cmd)
376-
377-
modules := findFolders()
378-
379-
var initFiles []string
380-
for _, f := range modules {
381-
initPy := fmt.Sprintf("%s/%s/%s", "build/python-cs3apis", f, "__init__.py")
382-
initFiles = append(initFiles, initPy)
383-
}
264+
languages := []string{"go", "js", "node", "python"}
384265

385-
cmd = exec.Command("touch", initFiles...)
386-
run(cmd)
266+
// prepare language git repos
267+
for _, l := range languages {
268+
target := fmt.Sprintf("%s-cs3apis", l)
387269

388-
// get proto repo commit id
389-
hash := getCommitID(".")
390-
repo := "build/python-cs3apis"
391-
msg := "Synced to https://github.com/cs3org/cs3apis/tree/" + hash
392-
commit(repo, msg)
393-
}
270+
// Clone Go repo and set branch to current branch
271+
clone("cs3org/"+target, "build")
272+
protoBranch := getGitBranch(".")
273+
targetBranch := getGitBranch("build/" + target)
274+
fmt.Printf("Proto branch: %s\n%s branch: %s\n", l, protoBranch, targetBranch)
394275

395-
func buildJS() {
396-
// Remove build dir
397-
os.RemoveAll("build/js-cs3apis")
398-
os.MkdirAll("build", 0755)
276+
if targetBranch != protoBranch {
277+
checkout(protoBranch, "build/"+target)
278+
}
399279

400-
// Clone repo and set branch to current branch
401-
clone("cs3org/js-cs3apis", "build")
402-
protoBranch := getGitBranch(".")
403-
buildBranch := getGitBranch("build/js-cs3apis")
404-
fmt.Printf("Proto branch: %s\nBuild branch: %s\n", protoBranch, buildBranch)
280+
// remove leftovers (existing defs)
281+
os.RemoveAll(fmt.Sprintf("build/%s/cs3", target))
405282

406-
if buildBranch != protoBranch {
407-
checkout(protoBranch, "build/js-cs3apis")
408283
}
409284

410-
// remove leftovers (existing defs)
411-
os.RemoveAll("build/js-cs3apis/cs3")
412-
413-
files := findProtos()
414-
415-
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"}
416-
args = append(args, files...)
417-
cmd := exec.Command("protoc", args...)
285+
cmd = exec.Command("buf", "generate")
418286
run(cmd)
419287

420-
// get proto repo commit id
421-
hash := getCommitID(".")
422-
repo := "build/js-cs3apis"
423-
msg := "Synced to https://github.com/cs3org/cs3apis/tree/" + hash
424-
commit(repo, msg)
425-
}
288+
for _, l := range languages {
289+
target := fmt.Sprintf("%s-cs3apis", l)
426290

427-
func buildNode() {
428-
// Remove build dir
429-
os.RemoveAll("build/node-cs3apis")
430-
os.MkdirAll("build", 0755)
431-
432-
// Clone repo and set branch to current branch
433-
clone("cs3org/node-cs3apis", "build")
434-
protoBranch := getGitBranch(".")
435-
buildBranch := getGitBranch("build/node-cs3apis")
436-
fmt.Printf("Proto branch: %s\nBuild branch: %s\n", protoBranch, buildBranch)
291+
if !isRepoDirty("build/" + target) {
292+
fmt.Println("Repo is clean, nothing to do")
293+
}
437294

438-
if buildBranch != protoBranch {
439-
checkout(protoBranch, "build/node-cs3apis")
295+
// get proto repo commit id
296+
hash := getCommitID(".")
297+
repo := "build/" + target
298+
msg := "Synced to https://github.com/cs3org/cs3apis/tree/" + hash
299+
commit(repo, msg)
440300
}
441-
442-
// remove leftovers (existing defs)
443-
os.RemoveAll("build/node-cs3apis/cs3")
444-
445-
files := findProtos()
446-
447-
args1 := []string{"--ts_out=grpc_js:./build/node-cs3apis", "--proto_path=.", "--proto_path=./third_party"}
448-
args1 = append(args1, files...)
449-
cmd1 := exec.Command("protoc-gen-grpc-ts", args1...)
450-
run(cmd1)
451-
452-
args2 := []string{"--js_out=import_style=commonjs,binary:./build/node-cs3apis", "--grpc_out=grpc_js:./build/node-cs3apis/", "--proto_path=.", "--proto_path=./third_party"}
453-
args2 = append(args2, files...)
454-
cmd2 := exec.Command("protoc-gen-grpc", args2...)
455-
run(cmd2)
456-
457-
// get proto repo commit id
458-
hash := getCommitID(".")
459-
repo := "build/node-cs3apis"
460-
msg := "Synced to https://github.com/cs3org/cs3apis/tree/" + hash
461-
commit(repo, msg)
462301
}
463302

464303
func pushPython() {
@@ -478,46 +317,23 @@ func pushNode() {
478317
}
479318

480319
func main() {
481-
if *_buildProto {
482-
fmt.Println("Compiling and linting protobufs ...")
483-
buildProto()
484-
}
485-
486-
if *_buildGo {
487-
fmt.Println("Building Go ...")
488-
buildGo()
489-
}
320+
generate()
490321

491322
if *_pushGo {
492323
fmt.Println("Pushing Go ...")
493324
pushGo()
494325
}
495326

496-
if *_buildPython {
497-
fmt.Println("Building Python ...")
498-
buildPython()
499-
}
500-
501327
if *_pushPython {
502328
fmt.Println("Pushing Python ...")
503329
pushPython()
504330
}
505331

506-
if *_buildJs {
507-
fmt.Println("Building JS ...")
508-
buildJS()
509-
}
510-
511332
if *_pushJs {
512333
fmt.Println("Pushing Js ...")
513334
pushJS()
514335
}
515336

516-
if *_buildNode {
517-
fmt.Println("Building Node.js ...")
518-
buildNode()
519-
}
520-
521337
if *_pushNode {
522338
fmt.Println("Pushing Node.js ...")
523339
pushNode()

0 commit comments

Comments
 (0)