From 5b37b1c354ab2658e98db2f6415b5fc6ed6de079 Mon Sep 17 00:00:00 2001 From: fish Date: Sat, 28 Mar 2026 18:30:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20proto=20?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E9=80=BB=E8=BE=91=EF=BC=8C=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=9C=A8=20Docker=20=E5=86=85=E8=BF=90=E8=A1=8C=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E6=B1=A1=E6=9F=93=E6=9C=AC=E5=9C=B0=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/scripts/gen-proto.sh | 45 ++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/backend/scripts/gen-proto.sh b/backend/scripts/gen-proto.sh index 49389c2..eff20f5 100755 --- a/backend/scripts/gen-proto.sh +++ b/backend/scripts/gen-proto.sh @@ -1,6 +1,6 @@ #!/bin/bash -# 生成 proto 代码的脚本 +# 生成 proto 代码的脚本 - 纯 Docker 容器运行版本 # 定义颜色 GREEN="\033[0;32m" @@ -8,18 +8,43 @@ YELLOW="\033[1;33m" RED="\033[0;31m" NC="\033[0m" # No Color -echo -e "${GREEN}Starting proto compilation...${NC}" +echo -e "${GREEN}Starting proto compilation in Docker container...${NC}" -# 使用 Docker 运行 protoc 编译 -if docker run --rm -v "$(pwd):/app" -w /app golang:1.26.1-alpine3.23 sh -c "\ - apk add --no-cache protobuf-dev && \ - go install google.golang.org/protobuf/cmd/protoc-gen-go@latest && \ - go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest && \ - protoc --go_out=. --go-grpc_out=. services/user-svc/user.proto && \ - protoc --go_out=. --go-grpc_out=. shared/proto/common.proto\ -"; then +# 检查 Docker 是否可用 +if ! command -v docker &> /dev/null; then + echo -e "${RED}Docker is not installed or not running! Please install and start Docker first.${NC}" + exit 1 +fi + +# 使用 Docker 容器运行 protoc 编译 +DOCKER_IMAGE="golang:1.26.1-alpine3.23" + +# 执行编译命令 +echo -e "${YELLOW}Running protoc compilation in Docker container...${NC}" + +if docker run --rm \ + -v "$(pwd):/app" \ + -w "/app" \ + "${DOCKER_IMAGE}" \ + sh -c "\ + # 安装必要的依赖 + apk add --no-cache protobuf-dev git && \ + # 设置 GOPATH + export GOPATH=/go && \ + export PATH=\$PATH:\$GOPATH/bin && \ + # 安装 protoc-gen-go 和 protoc-gen-go-grpc + go install google.golang.org/protobuf/cmd/protoc-gen-go@latest && \ + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest && \ + # 编译 user-svc 的 proto 文件 + protoc --go_out=. --go-grpc_out=. services/user-svc/user.proto && \ + # 编译 shared/proto 的 proto 文件 + protoc --go_out=. --go-grpc_out=. shared/proto/common.proto && \ + echo 'Proto compilation completed successfully!'\ + "; then echo -e "${GREEN}Proto compilation completed successfully!${NC}" else echo -e "${RED}Proto compilation failed!${NC}" exit 1 fi + +echo -e "${GREEN}All proto files have been compiled successfully!${NC}"