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}"