diff --git a/scripts/vet-common.sh b/scripts/common.sh similarity index 100% rename from scripts/vet-common.sh rename to scripts/common.sh diff --git a/scripts/install-protoc.sh b/scripts/install-protoc.sh new file mode 100755 index 000000000000..e2424675aeee --- /dev/null +++ b/scripts/install-protoc.sh @@ -0,0 +1,91 @@ +#!/bin/bash +# +# Copyright 2024 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# install-protoc.sh +# +# This script installs the Protocol Buffers compiler (protoc) on the local +# machine. It is used to generate code from .proto files for gRPC communication. +# The script downloads the protoc binary from the official GitHub repository and +# installs it in the system. +# +# Usage: ./install-protoc.sh [INSTALL_PATH] +# +# Arguments: +# INSTALL_PATH: The path where the protoc binary will be installed (optional). +# If not provided, the script will install the binary in the the +# directory named by the GOBIN environment variable, which +# defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH +# environment variable is not set. +# +# Note: This script requires internet connectivity to download the protoc binary. + +set -eu -o pipefail + +source "$(dirname $0)/common.sh" + +# The version of protoc that will be installed. +PROTOC_VERSION="27.1" + +INSTALL_PATH="${1:-${GOBIN:-${GOPATH:-$HOME/go}}}" + +# downloads the pre-built binaries for Linux to $INSTALL_PATH. +# Usage: +# download_binary ARCH OS +# Arguments: +# ARCH: The architecture of the system. Accepted values: [x86_64, aarch_64] +# OS: The operating system of the system. Accepted values: [osx, linux] +download_binary() { + DOWNLOAD_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-$2-$1.zip" + # -L follows redirects. + # -O writes output to a file. + curl -LO "${DOWNLOAD_URL}" + unzip "protoc-${PROTOC_VERSION}-$2-$1.zip" -d "${INSTALL_PATH}" + rm "protoc-${PROTOC_VERSION}-$2-$1.zip" + rm "${INSTALL_PATH}/readme.txt" +} + +main() { + # Check if protoc is already available. + if command -v protoc &> /dev/null; then + if INSTALL_VERSION=$(protoc --version | cut -d' ' -f2 2>/dev/null); then + if [ "$INSTALL_VERSION" = "$PROTOC_VERSION" ]; then + echo "protoc version $PROTOC_VERSION is already installed." + return + else + die "Existing protoc version ($INSTALL_VERSION) differs. Kindly make sure you have $PROTOC_VERSION installed." + fi + fi + fi + + # Detect the architecture + case "$(uname -m)" in + "x86_64") ARCH="x86_64";; + "aarch64") ARCH="aarch_64";; + "arm64") ARCH="aarch_64";; + *) die "Unsupported architecture. Please consider manual installation from "\ + "https://github.com/protocolbuffers/protobuf/releases/ and add to PATH." + esac + + # Detect the Operating System + case "$(uname -s)" in + "Darwin") download_binary $ARCH "osx";; + "Linux") download_binary $ARCH "linux";; + *) die "Unsupported OS. Please consider manual installation from "\ + "https://github.com/protocolbuffers/protobuf/releases/ and add to PATH" ;; + esac +} + +main "$@" diff --git a/scripts/install_protoc.sh b/scripts/install_protoc.sh deleted file mode 100755 index 4daa8160cc26..000000000000 --- a/scripts/install_protoc.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/bin/bash -# Copyright 2024 gRPC authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# This script ensures the installation of protobuf on client machine. -# In case of manual run of this script, make sure you pass the args -# expected at -# https://github.com/grpc/grpc-go/blob/master/scripts/install_protoc.sh#L60 - -set -eu -o pipefail - -source "$(dirname $0)/vet-common.sh" - -# The version of protoc that will be installed. -PROTOC_VERSION="27.1" - -# Function to download pre-built binaries for Linux with -# ARCH as $1, OS as $2, and INSTALL_PATH as $3 arguments. -download_binary() { - # Check if protoc is already available. - if command -v protoc &> /dev/null; then - if INSTALL_VERSION=$(protoc --version | cut -d' ' -f2 2>/dev/null); then - if [ "$INSTALL_VERSION" = "$PROTOC_VERSION" ]; then - echo "protoc version $PROTOC_VERSION is already installed." - return - else - die "Existing protoc version ($INSTALL_VERSION) differs. Kindly make sure you have $PROTOC_VERSION installed." - fi - else - echo "Unable to determine installed protoc version. Starting the installation." - fi - fi - DOWNLOAD_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-$2-$1.zip" - # Download and unzip - curl -LO "$DOWNLOAD_URL" - INSTALL_DIR="${3:-${GOBIN:-${GOPATH:-$HOME/go}}}" - unzip "protoc-${PROTOC_VERSION}-$2-$1.zip" -d $INSTALL_DIR - rm "protoc-${PROTOC_VERSION}-$2-$1.zip" - rm "${INSTALL_DIR}/readme.txt" -} - -# Detect the architecture -case "$(uname -m)" in - "x86_64") ARCH="x86_64";; - "aarch64") ARCH="aarch_64";; - "arm64") ARCH="aarch_64";; -*) die "Unsupported architecture. Please consider manual installation from \ - https://github.com/protocolbuffers/protobuf/releases/ and add to PATH." -esac - -# Detect the Operating System -INSTALL_PATH=${1:+"$1"} -case "$(uname -s)" in - "Darwin") download_binary $ARCH "osx" "$INSTALL_PATH";; - "Linux") download_binary $ARCH "linux" "$INSTALL_PATH";; -*) die "Unsupported OS. Please consider manual installation from \ - https://github.com/protocolbuffers/protobuf/releases/ and add to PATH" ;; -esac diff --git a/scripts/regenerate.sh b/scripts/regenerate.sh index c5d7b1a645f0..d63f88d812c7 100755 --- a/scripts/regenerate.sh +++ b/scripts/regenerate.sh @@ -19,13 +19,12 @@ WORKDIR=$(mktemp -d) function finish { rm -rf "$WORKDIR" - # Revert back the PATH to client's original value - export PATH=$ORIGINAL_PATH } trap finish EXIT -GOBIN="${WORKDIR}"/bin -ORIGINAL_PATH=$PATH +./scripts/install-protoc.sh "${WORKDIR}" + +export GOBIN="${WORKDIR}"/bin export PATH="${GOBIN}:${PATH}" mkdir -p "${GOBIN}" @@ -51,7 +50,6 @@ mkdir -p "${WORKDIR}/googleapis/google/rpc" echo "curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto" curl --silent https://raw.githubusercontent.com/googleapis/googleapis/master/google/rpc/code.proto > "${WORKDIR}/googleapis/google/rpc/code.proto" -source ./scripts/install_protoc.sh $WORKDIR mkdir -p "${WORKDIR}/out" @@ -98,7 +96,7 @@ Mgrpc/testing/empty.proto=google.golang.org/grpc/interop/grpc_testing for src in ${SOURCES[@]}; do echo "protoc ${src}" - protoc --go_out=${OPTS}:${WORKDIR}/out --go-grpc_out=${OPTS},use_generic_streams_experimental=true:${WORKDIR}/out \ + protoc --go_out=${OPTS}:${WORKDIR}/out --go-grpc_out=${OPTS}:${WORKDIR}/out \ -I"." \ -I"${WORKDIR}/grpc-proto" \ -I"${WORKDIR}/googleapis" \ diff --git a/scripts/vet-proto.sh b/scripts/vet-proto.sh index 56b423593cb9..e46be0c8e73d 100755 --- a/scripts/vet-proto.sh +++ b/scripts/vet-proto.sh @@ -4,7 +4,7 @@ set -ex # Exit on error; debugging enabled. set -o pipefail # Fail a pipe if any sub-command fails. # - Source them sweet sweet helpers. -source "$(dirname $0)/vet-common.sh" +source "$(dirname $0)/common.sh" # - Check to make sure it's safe to modify the user's git repo. git status --porcelain | fail_on_output @@ -21,9 +21,9 @@ trap cleanup EXIT # go.) if [[ "$1" = "-install" ]]; then if [[ "${GITHUB_ACTIONS}" = "true" ]]; then - source ./scripts/install_protoc.sh "/home/runner/go" + source ./scripts/install-protoc.sh "/home/runner/go" else - die "run protoc installer https://github.com/grpc/grpc-go/blob/master/scripts/install_protoc.sh" + die "run protoc installer https://github.com/grpc/grpc-go/blob/master/scripts/install-protoc.sh" fi echo SUCCESS exit 0 diff --git a/scripts/vet.sh b/scripts/vet.sh index 18368261f7b5..9abb08ff52ff 100755 --- a/scripts/vet.sh +++ b/scripts/vet.sh @@ -3,7 +3,7 @@ set -ex # Exit on error; debugging enabled. set -o pipefail # Fail a pipe if any sub-command fails. -source "$(dirname $0)/vet-common.sh" +source "$(dirname $0)/common.sh" # Check to make sure it's safe to modify the user's git repo. git status --porcelain | fail_on_output