Skip to content

Commit c5c0e18

Browse files
authored
scripts: minor refactor to scripts (#7403)
1 parent e7d8822 commit c5c0e18

File tree

6 files changed

+99
-79
lines changed

6 files changed

+99
-79
lines changed
File renamed without changes.

scripts/install-protoc.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2024 gRPC authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# install-protoc.sh
18+
#
19+
# This script installs the Protocol Buffers compiler (protoc) on the local
20+
# machine. It is used to generate code from .proto files for gRPC communication.
21+
# The script downloads the protoc binary from the official GitHub repository and
22+
# installs it in the system.
23+
#
24+
# Usage: ./install-protoc.sh [INSTALL_PATH]
25+
#
26+
# Arguments:
27+
# INSTALL_PATH: The path where the protoc binary will be installed (optional).
28+
# If not provided, the script will install the binary in the the
29+
# directory named by the GOBIN environment variable, which
30+
# defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH
31+
# environment variable is not set.
32+
#
33+
# Note: This script requires internet connectivity to download the protoc binary.
34+
35+
set -eu -o pipefail
36+
37+
source "$(dirname $0)/common.sh"
38+
39+
# The version of protoc that will be installed.
40+
PROTOC_VERSION="27.1"
41+
42+
INSTALL_PATH="${1:-${GOBIN:-${GOPATH:-$HOME/go}}}"
43+
44+
# downloads the pre-built binaries for Linux to $INSTALL_PATH.
45+
# Usage:
46+
# download_binary ARCH OS
47+
# Arguments:
48+
# ARCH: The architecture of the system. Accepted values: [x86_64, aarch_64]
49+
# OS: The operating system of the system. Accepted values: [osx, linux]
50+
download_binary() {
51+
DOWNLOAD_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-$2-$1.zip"
52+
# -L follows redirects.
53+
# -O writes output to a file.
54+
curl -LO "${DOWNLOAD_URL}"
55+
unzip "protoc-${PROTOC_VERSION}-$2-$1.zip" -d "${INSTALL_PATH}"
56+
rm "protoc-${PROTOC_VERSION}-$2-$1.zip"
57+
rm "${INSTALL_PATH}/readme.txt"
58+
}
59+
60+
main() {
61+
# Check if protoc is already available.
62+
if command -v protoc &> /dev/null; then
63+
if INSTALL_VERSION=$(protoc --version | cut -d' ' -f2 2>/dev/null); then
64+
if [ "$INSTALL_VERSION" = "$PROTOC_VERSION" ]; then
65+
echo "protoc version $PROTOC_VERSION is already installed."
66+
return
67+
else
68+
die "Existing protoc version ($INSTALL_VERSION) differs. Kindly make sure you have $PROTOC_VERSION installed."
69+
fi
70+
fi
71+
fi
72+
73+
# Detect the architecture
74+
case "$(uname -m)" in
75+
"x86_64") ARCH="x86_64";;
76+
"aarch64") ARCH="aarch_64";;
77+
"arm64") ARCH="aarch_64";;
78+
*) die "Unsupported architecture. Please consider manual installation from "\
79+
"https://github.com/protocolbuffers/protobuf/releases/ and add to PATH."
80+
esac
81+
82+
# Detect the Operating System
83+
case "$(uname -s)" in
84+
"Darwin") download_binary $ARCH "osx";;
85+
"Linux") download_binary $ARCH "linux";;
86+
*) die "Unsupported OS. Please consider manual installation from "\
87+
"https://github.com/protocolbuffers/protobuf/releases/ and add to PATH" ;;
88+
esac
89+
}
90+
91+
main "$@"

scripts/install_protoc.sh

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

scripts/regenerate.sh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ WORKDIR=$(mktemp -d)
1919

2020
function finish {
2121
rm -rf "$WORKDIR"
22-
# Revert back the PATH to client's original value
23-
export PATH=$ORIGINAL_PATH
2422
}
2523
trap finish EXIT
2624

27-
GOBIN="${WORKDIR}"/bin
28-
ORIGINAL_PATH=$PATH
25+
./scripts/install-protoc.sh "${WORKDIR}"
26+
27+
export GOBIN="${WORKDIR}"/bin
2928
export PATH="${GOBIN}:${PATH}"
3029
mkdir -p "${GOBIN}"
3130

@@ -51,7 +50,6 @@ mkdir -p "${WORKDIR}/googleapis/google/rpc"
5150
echo "curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto"
5251
curl --silent https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto > "${WORKDIR}/googleapis/google/rpc/code.proto"
5352

54-
source ./scripts/install_protoc.sh $WORKDIR
5553

5654
mkdir -p "${WORKDIR}/out"
5755

@@ -98,7 +96,7 @@ Mgrpc/testing/empty.proto=google.golang.org/grpc/interop/grpc_testing
9896

9997
for src in ${SOURCES[@]}; do
10098
echo "protoc ${src}"
101-
protoc --go_out=${OPTS}:${WORKDIR}/out --go-grpc_out=${OPTS},use_generic_streams_experimental=true:${WORKDIR}/out \
99+
protoc --go_out=${OPTS}:${WORKDIR}/out --go-grpc_out=${OPTS}:${WORKDIR}/out \
102100
-I"." \
103101
-I"${WORKDIR}/grpc-proto" \
104102
-I"${WORKDIR}/googleapis" \

scripts/vet-proto.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -ex # Exit on error; debugging enabled.
44
set -o pipefail # Fail a pipe if any sub-command fails.
55

66
# - Source them sweet sweet helpers.
7-
source "$(dirname $0)/vet-common.sh"
7+
source "$(dirname $0)/common.sh"
88

99
# - Check to make sure it's safe to modify the user's git repo.
1010
git status --porcelain | fail_on_output
@@ -21,9 +21,9 @@ trap cleanup EXIT
2121
# go.)
2222
if [[ "$1" = "-install" ]]; then
2323
if [[ "${GITHUB_ACTIONS}" = "true" ]]; then
24-
source ./scripts/install_protoc.sh "/home/runner/go"
24+
source ./scripts/install-protoc.sh "/home/runner/go"
2525
else
26-
die "run protoc installer https://github.com/grpc/grpc-go/blob/master/scripts/install_protoc.sh"
26+
die "run protoc installer https://github.com/grpc/grpc-go/blob/master/scripts/install-protoc.sh"
2727
fi
2828
echo SUCCESS
2929
exit 0

scripts/vet.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -ex # Exit on error; debugging enabled.
44
set -o pipefail # Fail a pipe if any sub-command fails.
55

6-
source "$(dirname $0)/vet-common.sh"
6+
source "$(dirname $0)/common.sh"
77

88
# Check to make sure it's safe to modify the user's git repo.
99
git status --porcelain | fail_on_output

0 commit comments

Comments
 (0)