@rem Redistribution and use in source and binary forms, with or without
@rem modification, are permitted provided that the following conditions are
@rem met:
@rem
@rem * Redistributions of source code must retain the above copyright
@rem notice, this list of conditions and the following disclaimer.
@rem * Redistributions in binary form must reproduce the above
@rem copyright notice, this list of conditions and the following disclaimer
@rem in the documentation and/or other materials provided with the
@rem distribution.
@rem * Neither the name of Google Inc. nor the names of its
@rem contributors may be used to endorse or promote products derived from
@rem this software without specific prior written permission.
@rem
@rem THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
@rem "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@rem LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@rem A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
@rem OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
@rem SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
@rem LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
@rem DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
@rem THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
@rem (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
@rem OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@rem Generate the C# code for .proto files
setlocal
@rem enter this directory
cd /d %~dp0
set TOOLS_PATH=tools
for %%a in (protos/*.proto) do %TOOLS_PATH%\protoc.exe -I./protos --csharp_out ./code --grpc_out ./code ./protos/%%a --plugin=protoc-gen-grpc=%TOOLS_PATH%\grpc_csharp_plugin.exe
pause
复制代码
@rem
全部 @rem 开头的行是注释,不会被实行。这些注释内容主要是版权声明和使用允许条款,告知用户 Google 的版权信息以及对源代码和二进制文件的使用限定。
cd /d %~dp0
这行下令将当前工作目录切换到批处理脚本地点的目录。%~dp0 代表当前批处理文件地点的驱动器和路径。/d 参数确保跨驱动器切换目录。
set TOOLS_PATH=tools
这行下令界说了一个名为 TOOLS_PATH 的情况变量,值为 tools,表示 protoc.exe 和 grpc_csharp_plugin.exe 工具的路径。这个变量用于简化下令中的路径引用。
for %%a in (protos/*.proto) do %TOOLS_PATH%\protoc.exe -I./protos --csharp_out ./code --grpc_out ./code ./protos/%%a --plugin=protoc-gen-grpc=%TOOLS_PATH%\grpc_csharp_plugin.exe
这段代码是脚本的核心,使用 for 循环批量处理 protos 目录中的全部 .proto 文件。以下是逐步阐明:
for %%a in (protos/*.proto): 这个 for 循环会遍历 protos 目录中的全部 .proto 文件,每个文件路径保存在 %%a 中。
do %TOOLS_PATH%\protoc.exe: 调用 protoc.exe 工具来编译 .proto 文件。
-I./protos: 指定 protoc.exe 的输入目录是 protos 文件夹。
--csharp_out ./code: 将天生的 C# 代码输出到 code 目录。
--grpc_out ./code: 将天生的 gRPC 服务代码输出到 code 目录。
./protos/%%a: 指定要编译的 .proto 文件,文件名由 for 循环提供,即当前迭代的 %%a 变量。