gen-api.sh 1.22 KB
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/bin/bash

# 有命令错误直接退出脚本
set -e

# 项目类型
project_type=$1
# 项目名
project=$2

if [[ -z $project_type || -z $project ]]; then
  echo '项目类型和项目名不能为空'
  exit 1
fi

root=$(dirname $(cd $(dirname $0) && pwd))
cd $root

kzkzzzz's avatar
kzkzzzz committed
19
COMMON_PROTO_FILES=$(find api/common -name *.proto)
kzkzzzz's avatar
kzkzzzz committed
20 21
API_PROTO_FILES=$(find api/$project_type/$project -name *.proto)

kzkzzzz's avatar
kzkzzzz committed
22 23 24 25
protoc --proto_path=./api/common \
  --go_out=paths=source_relative:./api/common \
  $COMMON_PROTO_FILES

kzkzzzz's avatar
kzkzzzz committed
26
protoc --proto_path=./api/$project_type/$project \
kzkzzzz's avatar
kzkzzzz committed
27
  --proto_path=./api \
kzkzzzz's avatar
kzkzzzz committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  --proto_path=./third_party \
  --go_out=paths=source_relative:./api/$project_type/$project \
  --go-http_out=paths=source_relative:./api/$project_type/$project \
  --go-grpc_out=paths=source_relative:./api/$project_type/$project \
  --openapi_out=fq_schema_naming=true,default_response=false:. \
  $API_PROTO_FILES

protoc-go-inject-tag -input=api/$project_type/$project/*.pb.go

# 获取系统类型
os=$(uname -s)

if [[ $os = "Darwin" ]]; then
    grep -rl 'omitempty' api/$project_type/$project/*.pb.go | xargs -I {} sed -i '' "s#,omitempty##g" {} || true
else
    grep -rl 'omitempty' api/$project_type/$project/*.pb.go | xargs -I {} sed -i "s#,omitempty##g" {} || true
fi