泉缘泉 发表于 2024-9-13 17:41:29

第22篇 天生proto文件bat脚本

1.天生单个Proto.bat内容

https://img2024.cnblogs.com/blog/2212230/202409/2212230-20240913160345373-1336581968.png
@rem Copyright 2016, Google Inc.
@rem All rights reserved.
@rem
@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

set /p PROTO_FILE_NAME=input proto filename:

%TOOLS_PATH%\protoc.exe -I./protos --csharp_out ./code --grpc_out ./code ./protos/%PROTO_FILE_NAME%.proto --plugin=protoc-gen-grpc=%TOOLS_PATH%\grpc_csharp_plugin.exe

pause这个脚本是一个批处理文件(.bat 文件),其作用是使用 protoc.exe 工具天生 C# 代码和 gRPC 服务代码,基于指定的 .proto 文件。以下是对脚本的逐步解释:

[*]@rem 用于注释。全部以 @rem 开头的行是注释,批处理脚本不会实行它们。上面的注释内容是版权声明和允许条款。
[*]setlocal
这个下令用于将变量的作用域限定在脚本实行期间,防止脚本结束后污染外部情况中的变量
[*]cd /d %~dp0
这行下令是将当前目录切换到批处理文件地点的目录。%~dp0 表示当前批处理文件地点的驱动器和路径。
[*]set TOOLS_PATH=tools
这行下令界说了一个名为 TOOLS_PATH 的情况变量,它的值为 tools,用于后续引用 protoc.exe 和 grpc_csharp_plugin.exe 的路径。
[*]set /p PROTO_FILE_NAME=input proto filename:
set /p 下令用于在下令行提示用户输入内容。在这里,它提示用户输入 .proto 文件名,并将输入的文件名存储在 PROTO_FILE_NAME 变量中。
[*]%TOOLS_PATH%\protoc.exe -I./protos --csharp_out ./code --grpc_out ./code ./protos/%PROTO_FILE_NAME%.proto --plugin=protoc-gen-grpc=%TOOLS_PATH%\grpc_csharp_plugin.exe
这是批处理文件的核心下令,它调用 protoc.exe,用于根据用户输入的 .proto 文件天生相应的 C# 和 gRPC 代码。以下是参数的具体解释:


[*]%TOOLS_PATH%\protoc.exe: 调用 protoc.exe,用于编译 .proto 文件。
[*]-I./protos: 指定 protoc.exe 的输入目录,这里是 ./protos 目录,存放 .proto 文件。
[*]--csharp_out ./code: 天生 C# 代码,并将代码输出到 ./code 目录。
[*]--grpc_out ./code: 天生 gRPC 服务代码,并将代码输出到 ./code 目录。
[*]./protos/%PROTO_FILE_NAME%.proto: 指定要编译的 .proto 文件,文件名由用户输入,存储在 PROTO_FILE_NAME 变量中。
[*]--plugin=protoc-gen-grpc=%TOOLS_PATH%\grpc_csharp_plugin.exe: 指定用于 gRPC 代码天生的插件,路径为 tools 目录中的 grpc_csharp_plugin.exe。

[*]pause
pause 下令用于在脚本结束时停息,等候用户按下任意键。这是为了防止下令行窗口立即关闭,以便用户检察输出结果。
总结

这个批处理脚本的作用是:当用户输入 .proto 文件名后,使用 protoc.exe 和 gRPC 插件天生相应的 C# 和 gRPC 代码,并将它们保存到 ./code 目录下。
=================================================================================================

2.批量天生Protos.bat

https://img2024.cnblogs.com/blog/2212230/202409/2212230-20240913162033231-2004657832.png
@rem Copyright 2016, Google Inc.
@rem All rights reserved.
@rem
@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 的版权信息以及对源代码和二进制文件的使用限定。
[*]setlocal
setlocal 下令用于将变量的作用域限定在脚本实行期间,防止情况变量影响脚本实行后的系统情况。
[*]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 变量。
[*]--plugin=protoc-gen-grpc=%TOOLS_PATH%\grpc_csharp_plugin.exe: 使用 gRPC 插件天生 gRPC 代码,插件路径为 tools/grpc_csharp_plugin.exe。

[*]pause
pause 下令用于在脚本结束时停息实行,等候用户按下任意键。这是为了防止脚本实行完后窗口立即关闭,便于用户检察实行结果。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 第22篇 天生proto文件bat脚本