12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #!/bin/bash
- curentdirname=$(basename "$PWD")
- parentfulldir=$(dirname "$PWD")
- gomodfile=go.mod
- pbdir=pb # 指定proto所在目录
- # 判断是否有go.mod文件
- if [ ! -f "$gomodfile" ]; then
- echo "No '$gomodfile' file!"
- exit 1
- fi
- # 从go.mod中取出工程包名
- packname=`awk 'NR==1{print $2}' $gomodfile`
- # 判断当前目录名是否与go.mod中的module 名称相同,不同直接提示错误
- if [ "$packname" != "$curentdirname" ]; then
- echo "The module name is not the current directory! See 'go.mod'"
- exit 1
- fi
- # 判断是否有pb目录
- if [ ! -d $pbdir ]; then
- echo "No '$pbdir' directory!"
- exit 1
- fi
- # 取出操作系统
- OS=`uname -s`
- # 将pbdir目录下所有一级目录下的所有.proto文件作统一处理并生成.pb.go文件
- dir=$(ls -l $pbdir |awk '/^d/ {print $NF}')
- for i in $dir
- do
- cd $pbdir/$i
- # 判断是否有.proto文件
- num=$(ls *.proto 2> /dev/null | wc -l)
- if [ $num != 0 ] ; then
- if [ $OS == "Linux" ]; then
- # 替换以 package 开头的整行
- sed -i "s/^package.*$/package $i;/g" *.proto
- # 替换以 option go_package 开头的整行
- sed -i "s/^option go_package.*$/option go_package = \"$packname\/$pbdir\/$i\";/g" *.proto
- elif [ $OS == "Darwin" ]; then
- # 替换以 package 开头的整行
- sed -i '' "s/^package.*$/package $i;/g" *.proto
- # 替换以 option go_package 开头的整行
- sed -i '' "s/^option go_package.*$/option go_package = \"$packname\/$pbdir\/$i\";/g" *.proto
- else
- echo "Unsupport OS: "$OS
- fi
- protoc --go_out=$parentfulldir *.proto
- fi
- cd ../..
- done
- cd $pbdir
- # 判断是否有.proto文件
- num=$(ls *.proto 2> /dev/null | wc -l)
- if [ $num != 0 ] ; then
- if [ $OS == "Linux" ]; then
- # 替换以 option go_package 开头的整行
- sed -i "s/^option go_package.*$/option go_package = \"$packname\/$pbdir\";/g" *.proto
- elif [ $OS == "Darwin" ]; then
- # 替换以 option go_package 开头的整行
- sed -i '' "s/^option go_package.*$/option go_package = \"$packname\/$pbdir\";/g" *.proto
- else
- echo "Unsupport OS: "$OS
- fi
- protoc --go_out=plugins=grpc:$parentfulldir *.proto
- fi
- cd ../
|