diff --git a/Makefile b/Makefile index 8e85fc8..a223d8b 100644 --- a/Makefile +++ b/Makefile @@ -75,9 +75,9 @@ docs: mod fmt .PHONY: proto -# generate *.go code from *.proto files, only for ⓶ Microservices created based on sql, ⓷ Web services created based on protobuf, ⓸ Microservices created based on protobuf, ⓹ RPC gateway service created based on protobuf +# generate *.go and template code by proto files, if you do not refer to the proto file, the default is all the proto files in the api directory. you can specify the proto file, multiple files are separated by commas, e.g. make proto FILES=api/user/v1/user.proto. only for ⓶ Microservices created based on sql, ⓷ Web services created based on protobuf, ⓸ Microservices created based on protobuf, ⓹ RPC gateway service created based on protobuf proto: mod fmt - @bash scripts/protoc.sh + @bash scripts/protoc.sh $(FILES) .PHONY: proto-doc diff --git a/scripts/protoc.sh b/scripts/protoc.sh index d60f875..fa00e85 100644 --- a/scripts/protoc.sh +++ b/scripts/protoc.sh @@ -4,6 +4,9 @@ protoBasePath="api" allProtoFiles="" +specifiedProtoFilePath=$1 +specifiedProtoFilePaths="" + function checkResult() { result=$1 if [ ${result} -ne 0 ]; then @@ -11,6 +14,25 @@ function checkResult() { fi } +# get specified proto files, if empty, return 0 else return 1 +function getSpecifiedProtoFiles() { + if [ "$specifiedProtoFilePath"x = x ];then + return 0 + fi + + specifiedProtoFilePaths=${specifiedProtoFilePath//,/ } + + for v in $specifiedProtoFilePaths; do + if [ ! -f "$v" ];then + echo "Error: not found specified proto file $v" + echo "example: make proto FILES=api/user/v1/user.proto,api/types/types.proto" + checkResult 1 + fi + done + + return 1 +} + # add the import of useless packages from the generated *.pb.go code here function deleteUnusedPkg() { file=$1 @@ -64,12 +86,19 @@ function handlePbGoFiles(){ } function generateByAllProto(){ - # get all proto file paths - listProtoFiles $protoBasePath + getSpecifiedProtoFiles + if [ $? -eq 0 ]; then + listProtoFiles $protoBasePath + else + allProtoFiles=$specifiedProtoFilePaths + fi + if [ "$allProtoFiles"x = x ];then - echo "Error: not found protobuf file in path $protoBasePath" + echo "Error: not found proto file in path $protoBasePath" exit 1 fi + echo "generate *pb.go by proto files: $allProtoFiles" + echo "" # generate files *_pb.go protoc --proto_path=. --proto_path=./third_party \ @@ -107,7 +136,24 @@ function generateBySpecifiedProto(){ allProtoFiles="" listProtoFiles ${protoBasePath}/serverNameExample cd .. - specifiedProtoFiles=$allProtoFiles + specifiedProtoFiles="" + getSpecifiedProtoFiles + if [ $? -eq 0 ]; then + specifiedProtoFiles=$allProtoFiles + else + for v1 in $specifiedProtoFilePaths; do + for v2 in $allProtoFiles; do + if [ "$v1"x = "$v2"x ];then + specifiedProtoFiles="$specifiedProtoFiles $v1" + fi + done + done + fi + + if [ "$specifiedProtoFiles"x = x ];then + return + fi + echo "generate template code by proto files: $specifiedProtoFiles" # todo generate api template code command here # delete the templates code start