diff --git a/execute_protoc.sh b/execute_protoc.sh index 53186da..a9ad8e5 100755 --- a/execute_protoc.sh +++ b/execute_protoc.sh @@ -1,13 +1,49 @@ #! /bin/bash -NAME=auth -GO_PB_PATH=./gen/go/service/$NAME +check_imports() { + # 获取所有导入语句中的 service.* 文件 + import_files=$(grep -E 'import ".*\.proto"' "$1" | awk -F'"' '{print $2}') + + # 定义正则表达式匹配规则 + pattern="^(\.\/)?([a-zA-Z_-]+\.proto)$" + + # 声明数组变量 + result=() + + # 列出导入的 service.* 文件 + for file in $import_files; do + if [[ $file =~ $pattern ]]; then + # 去除前缀 ./ 并添加到结果数组 + result+=("${BASH_REMATCH[2]}") + fi + done + + # 去重结果数组中的值 + unique_result=($(echo "${result[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) + + # 返回去重后的结果数组 + echo "${unique_result[@]}" +} + +PROTO_DIR=./proto +SERVICE_NAME=auth +GO_PB_PATH=./gen/go/service/$SERVICE_NAME mkdir $GO_PB_PATH -p -protoc -I ./proto --go_out $GO_PB_PATH --go_opt paths=source_relative --go-grpc_out $GO_PB_PATH --go-grpc_opt paths=source_relative --grpc-gateway_out $GO_PB_PATH --grpc-gateway_opt paths=source_relative $NAME.proto -NAME=basic -GO_PB_PATH=./gen/go/service/$NAME -mkdir $GO_PB_PATH -p -protoc -I ./proto --go_out $GO_PB_PATH --go_opt paths=source_relative --go-grpc_out $GO_PB_PATH --go-grpc_opt paths=source_relative --grpc-gateway_out $GO_PB_PATH --grpc-gateway_opt paths=source_relative $NAME.proto \ No newline at end of file +protoc -I $PROTO_DIR --go_out $GO_PB_PATH --go_opt paths=source_relative --go-grpc_out $GO_PB_PATH --go-grpc_opt paths=source_relative --grpc-gateway_out $GO_PB_PATH --grpc-gateway_opt paths=source_relative $SERVICE_NAME.proto + + +imports=$(check_imports $PROTO_DIR/$SERVICE_NAME.proto) + +# $imports=(basic aa bb) + +for import_file in $imports; do + SERVICE_NAME="${import_file%%.*}" + echo $SERVICE_NAME + GO_PB_PATH=./gen/go/service/$SERVICE_NAME + mkdir $GO_PB_PATH -p + protoc -I $PROTO_DIR --go_out $GO_PB_PATH --go_opt paths=source_relative --go-grpc_out $GO_PB_PATH --go-grpc_opt paths=source_relative --grpc-gateway_out $GO_PB_PATH --grpc-gateway_opt paths=source_relative $SERVICE_NAME.proto +done + diff --git a/proto/auth.proto b/proto/auth.proto index 36f1e3e..569c117 100644 --- a/proto/auth.proto +++ b/proto/auth.proto @@ -5,19 +5,20 @@ option go_package = "fusen-auth/gen/go/auth"; // 导入google/api/annotations.proto 注释依赖 import "google/api/annotations.proto"; -import "service/basic.proto"; +import "basic.proto"; + //定义服务 service AuthService { //定义服务方法 - rpc SayHello (AuthRequest) returns (Response) { + rpc SayHello (AuthRequest) returns (service.basic.Response) { //添加http网关注解 option (google.api.http) = { - post: "/api/auth/example/echo" + post: "/api/auth/echo" body: "*" }; } - rpc SayHelloAgain (AuthRequest) returns (Response) {} + rpc SayHelloAgain (AuthRequest) returns (service.basic.Response) {} } // 定义请求消息类型.