|
| 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 "$@" |
0 commit comments