11#! /bin/bash
2- #   
2+ # 
33#  Copyright 2024 gRPC authors.
44# 
55#  Licensed under the Apache License, Version 2.0 (the "License");
1414#  See the License for the specific language governing permissions and
1515#  limitations under the License.
1616# 
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] 
17+ #  install-protoc.sh
18+ # 
19+ #  This script installs the Protocol Buffers compiler (protoc) to  the specified 
20+ #  directory.   It is used to generate code from .proto files for gRPC
21+ #  communication.  The script downloads the protoc binary from the official GitHub
22+ #  repository and  installs it in the system.
23+ # 
24+ #  Usage: ./install-protoc.sh INSTALL_PATH
2525# 
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. 
26+ #  Arguments:
27+ #    INSTALL_PATH: The path where the protoc binary will be installed.
3228# 
3329#  Note: This script requires internet connectivity to download the protoc binary.
3430
@@ -39,53 +35,60 @@ source "$(dirname $0)/common.sh"
3935#  The version of protoc that will be installed.
4036PROTOC_VERSION=" 27.1" 
4137
42- INSTALL_PATH=" ${1:- ${GOBIN:- ${GOPATH:- $HOME / go} } } " 
38+ main () {
39+   if  [[ " $# " -ne  1 ]];  then 
40+     die " Usage: $0  INSTALL_PATH" 
41+   fi 
4342
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" " ${INSTALL_PATH} " 
56-   rm " protoc-${PROTOC_VERSION} -$2 -$1 .zip" 
57-   rm " ${INSTALL_PATH} /readme.txt" 
58- }
43+   INSTALL_PATH=" ${1} " 
5944
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'  ' 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 
45+   if  [[ !  -d  " ${INSTALL_PATH} " ;  then 
46+     die " INSTALL_PATH (${INSTALL_PATH} ) does not exist." 
7147  fi 
7248
73-   #  Detect the architecture
49+   echo  " Installing protoc version $PROTOC_VERSION  to ${INSTALL_PATH} ..." 
50+ 
51+   #  Detect the hardware platform.
7452  case  " $( uname -m) " in 
75-     " x86_64" " x86_64" 
76-     " aarch64" " aarch_64" 
77-     " arm64" " aarch_64" 
78-   * ) die " Unsupported architecture. Please consider manual installation from " 
79-           " https://github.com/protocolbuffers/protobuf/releases/ and add to PATH." 
53+     " x86_64" " x86_64" 
54+     " aarch64" " aarch_64" 
55+     " arm64" " aarch_64" 
56+     * )          die " Install unsuccessful. Hardware platform not supported by installer: $1 " 
8057  esac 
8158
82-   #  Detect the Operating System
59+   #  Detect the Operating System. 
8360  case  " $( uname -s) " in 
84-     " Darwin" $ARCH  " osx" 
85-     " Linux" $ARCH  " linux" 
86-   * ) die " Unsupported OS. Please consider manual installation from " 
87-           " https://github.com/protocolbuffers/protobuf/releases/ and add to PATH" 
61+     " Darwin" " osx" 
62+     " Linux" " linux" 
63+     * )        die " Install unsuccessful. OS not supported by installer: $2 " 
8864  esac 
65+ 
66+   #  Check if the protoc binary with the right version is already installed.
67+   if  [[ -f  " ${INSTALL_PATH} /bin/protoc" ;  then 
68+     if  [[ " $( " ${INSTALL_PATH} /bin/protoc" ) " ==  " libprotoc ${PROTOC_VERSION} " ;  then 
69+       echo  " protoc version ${PROTOC_VERSION}  is already installed in ${INSTALL_PATH} " 
70+       return 
71+     fi 
72+   fi 
73+ 
74+   DOWNLOAD_URL=" https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION} /protoc-${PROTOC_VERSION} -${OS} -${ARCH} .zip" 
75+ 
76+   #  -L follows redirects.
77+   #  -O writes output to a file.
78+   curl -LO " ${DOWNLOAD_URL} " 
79+ 
80+   #  Unzip the downloaded file and except readme.txt.
81+   #  The file structure should look like:
82+   #  INSTALL_PATH
83+   #  ├── bin
84+   #  │   └── protoc
85+   #  └── include
86+   #      └── other files...
87+   unzip " protoc-${PROTOC_VERSION} -${OS} -${ARCH} .zip" " ${INSTALL_PATH} " " readme.txt" 
88+   rm " protoc-${PROTOC_VERSION} -${OS} -${ARCH} .zip" 
89+ 
90+   #  Make the protoc binary executable. ¯\_(ツ)_/¯  crazy, right?
91+   chmod +x " ${INSTALL_PATH} /bin/protoc" 
8992}
9093
9194main " $@ " 
0 commit comments