Skip to content

Instantly share code, notes, and snippets.

@navono
Last active June 20, 2019 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navono/f91d275a780eb8d32182217260de54c2 to your computer and use it in GitHub Desktop.
Save navono/f91d275a780eb8d32182217260de54c2 to your computer and use it in GitHub Desktop.
使用 batch 编译 proto 文件
@ECHO OFF
REM 声明采用UTF-8编码
chcp 65001
echo "请先安装 Golang 和 protoc,然后安装 protoc 插件"
REM plugin binary path
set PROTOC_GEN_TS_PATH=%cd%\\bin\\node_modules\\.bin\\protoc-gen-ts.cmd
set PROTOC_GEN_GRPC_PATH=%cd%\\bin\\grpc_cpp_plugin.exe
set PROTOC_GEN_LINT_PATH=%cd%\\bin\\protoc-gen-lint.exe
set PROTOC_GEN_GO_PATH=%cd%\\bin\\protoc-gen-go.exe
set PROTOC_GEN_SWAGGER_PATH=%cd%\\bin\\protoc-gen-swagger.exe
REM Directory to write generated code to
set OUT_DIR="./generated"
set CPP_DIR=%OUT_DIR%/cpp
set JS_DIR=%OUT_DIR%/js
set GO_DIR=%OUT_DIR%/go
set SWAGGER_DIR=%OUT_DIR%/swagger
mkdir %CPP_DIR%
mkdir %JS_DIR%
mkdir %GO_DIR%
mkdir %SWAGGER_DIR%
REM 转换以下文件夹中的 proto 文件
set are=./are
set services=./services
set third_party=./third_party
call :iterateDir are
call :iterateDir services
call :iterateDir third_party
goto :eof
:iterateDir
SETLOCAL
REM echo %1
echo "Compliling %1..."
for /r "%1" %%f in (*.proto) do (
REM echo "%%f"
call :compileFile "%%f"
)
ENDLOCAL
goto :eof
:compileFile
SETLOCAL
REM echo %1
set filepath=%1
REM 删除 are\ 之前的所有字符
set tempfile=%filepath:*supconapis\=%
REM 将 \ 替换成 /
set file=%tempfile:\=/%
REM 删除最后一个 "
set file=%file:~0, -1%
protoc -I. -Ithird_party ^
--plugin="protoc-gen-ts=%PROTOC_GEN_TS_PATH%" ^
--plugin="protoc-gen-grpc=%PROTOC_GEN_GRPC_PATH%" ^
--plugin="protoc-gen-go=%PROTOC_GEN_GO_PATH%" ^
--plugin="protoc-gen-lint=%PROTOC_GEN_LINT_PATH%" ^
--plugin="protoc-gen-swagger=%PROTOC_GEN_SWAGGER_PATH%" ^
--cpp_out=%CPP_DIR% ^
--grpc_out=%CPP_DIR% ^
--go_out=plugins=grpc:%GO_DIR% ^
--grpc-gateway_out=logtostderr=true:%GO_DIR% ^
--js_out=import_style=commonjs,binary:%JS_DIR% ^
--ts_out=service=true:%JS_DIR% ^
--swagger_out=logtostderr=true:%SWAGGER_DIR% ^
--lint_out=. ^
%file%
ENDLOCAL
EXIT /B 0
@navono
Copy link
Author

navono commented Jun 12, 2019

需要注意的是:

set filepath=%1
REM 删除 vxsip\ 之前的所有字符
set tempfile=%filepath:*supconapis\=%
REM 将 \ 替换成 /
set file=%tempfile:\=/%
REM 删除最后一个 "
set file=%file:~0, -1%

上面的字符串需要根据实际情况修改。

@navono
Copy link
Author

navono commented Jun 12, 2019

包括 set protoDir=./vxsip

@navono
Copy link
Author

navono commented Jun 12, 2019

third_party 中包括了 :

google\api
google\protobuf
google\rpc
protoc-gen-swagger\options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment