github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/refresh_protos.sh (about) 1 #!/usr/bin/env bash 2 # Copyright (c) 2022 Arista Networks, Inc. 3 # Use of this source code is governed by the Apache License 2.0 4 # that can be found in the COPYING file. 5 6 # This script is run periodically by Jenkins in the update-deps job 7 # It is used to ensure that .pb.go files are generated with the most 8 # up-to-date protoc compiler 9 10 11 if [[ "$USER" != jenkins ]]; then 12 echo >&2 "warning: This script is mostly for Jenkins' benefit." 13 echo >&2 " It updates dependencies and posts a review to Gerrit." 14 echo >&2 " Hit enter to proceed anyway, Ctrl-C to cancel." 15 test -t 0 && read -rs # Read enter if stdin is a terminal 16 fi 17 18 set -euo pipefail 19 cd "$(dirname "$0")" 20 21 # find the latest release of protoc compiler, download and unzip 22 curl -s https://api.github.com/repos/protocolbuffers/protobuf/releases/latest \ 23 | grep -o -m 1 \ 24 "https://github.com/protocolbuffers/protobuf/releases/download/v.*/protoc\-.*-linux-x86_64.zip" \ 25 | xargs curl -LO && echo "protoc downloaded" 26 python3 -c \ 27 "from glob import glob as g;from zipfile import ZipFile as z; 28 p=g('protoc-*-linux-x86_64.zip')[0];f=z(p,'r');f.extractall('protoc');f.close()" 29 rm protoc-*-linux-x86_64.zip 30 protocBin=$(realpath .)"/protoc/bin/protoc" 31 chmod +x "$protocBin" 32 33 # install the latest protoc-gen-go and add to path 34 go install google.golang.org/protobuf/cmd/protoc-gen-go@latest 35 protoc-gen-go --version 36 go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest 37 protoc-gen-go-grpc --version 38 39 protocBin=$(realpath ./protoc/bin/protoc); 40 startPath=$(realpath .); 41 42 # Copy dependencies into a vendor folder 43 go mod vendor 44 # Add the two places where we could import proto files from: Anywhere under the GOPATH/src, 45 # and anything under the vendor directory. 46 # We can't use the GOPATH/pkg/mod directory as its package names are not in the format that 47 # protoc is expecting. 48 # E.g. protoc is expecting the github.com/openconfig/gnmi module to exist at a directory like: 49 # github.com/openconfig/gnmi 50 # When it really exists at a transient directory like: 51 # github.com/openconfig/gnmi@v0.0.0-20220503232738-6eb133c65a13 52 # By vendoring the dependencies we create a copy of them which has the expected naming 53 protoImports="${GOPATH}/src:${startPath}/vendor"; 54 55 # filePaths is a space-seperated string of all .proto files in goarista 56 # exclude ./.git, ./modLink/ and the downloaded protoc directory 57 filePaths=$(find . -not \( -path ./.git -prune \) -not \( -path ./protoc -prune \) \ 58 -not \( -path ./vendor -prune \) -type f -name "*.proto") ; 59 60 # This loop recompiles every .proto file under goarista, and if 61 # significant changes in any of the generated files are observed, it will 62 # add the updated .pb.go and _grpc.pb.go files to git 63 for filePath in $filePaths; do 64 echo "filePath is $filePath" 65 66 protoFile="${filePath##*/}"; 67 pkgPath="${filePath%/*}"; 68 cd "${pkgPath}"; 69 pkg="${pkgPath##*/}"; 70 $protocBin -I="${protoImports}" \ 71 --proto_path=. \ 72 --go_out=. \ 73 --go_opt=paths=source_relative \ 74 --go_opt="M${protoFile}=./${pkg}" \ 75 --go-grpc_out=. \ 76 --go-grpc_opt=paths=source_relative \ 77 --go-grpc_opt="M${protoFile}=./${pkg}"\ 78 "${protoFile}"; 79 80 echo "proto compiled successfully" 81 82 # determine if there are any significant differences in the newly generated files. 83 # diffs which _only_ involve updates to the version numbers are deemed not-significant. 84 # If the diff is significant, diffExists = 1, else diffExists = 0 85 read -r diffExists < <(git --no-pager diff --no-color --unified=0 ./ | grep -Ev \ 86 -e "^(\+|-)//\s+(-|)\s*protoc(|-gen-go)\s+v[0-9\.]" \ 87 -e "^(@@|diff|index|--- a/|\+\+\+ b/)" | head -c1 | wc -c) 88 89 echo "diffExists is $diffExists" 90 91 git add ./\*.pb.go; 92 if [ "$diffExists" -eq "0" ];then 93 echo "restoring .pb.go files - no significant diff after re-compilation" 94 git reset HEAD ./\*.pb.go; 95 git checkout -- ./\*.pb.go; 96 fi; 97 98 cd "${startPath}"; 99 git status -uno; 100 done 101 102 rm -rf protoc/ 103 rm -rf vendor/