第22篇 生成proto文件bat脚本
  rtv1pxAayTPS 5天前 35 0

1.生成单个Proto.bat内容

image

@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 文件。以下是对脚本的逐步解释:

  1. @rem 用于注释。所有以 @rem 开头的行是注释,批处理脚本不会执行它们。上面的注释内容是版权声明和许可条款。

  2. setlocal
    这个命令用于将变量的作用域限制在脚本执行期间,防止脚本结束后污染外部环境中的变量

  3. cd /d %~dp0
    这行命令是将当前目录切换到批处理文件所在的目录。%~dp0 表示当前批处理文件所在的驱动器和路径。

  4. set TOOLS_PATH=tools
    这行命令定义了一个名为 TOOLS_PATH 的环境变量,它的值为 tools,用于后续引用 protoc.exe 和 grpc_csharp_plugin.exe 的路径。

  5. set /p PROTO_FILE_NAME=input proto filename:
    set /p 命令用于在命令行提示用户输入内容。在这里,它提示用户输入 .proto 文件名,并将输入的文件名存储在 PROTO_FILE_NAME 变量中。

  6. %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。
  1. pause
    pause 命令用于在脚本结束时暂停,等待用户按下任意键。这是为了防止命令行窗口立即关闭,以便用户查看输出结果。

总结

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

=================================================================================================

2.批量生成Protos.bat

image

@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
  1. @rem
    所有 @rem 开头的行是注释,不会被执行。这些注释内容主要是版权声明和使用许可条款,告知用户 Google 的版权信息以及对源代码和二进制文件的使用限制。

  2. setlocal
    setlocal 命令用于将变量的作用域限制在脚本执行期间,防止环境变量影响脚本执行后的系统环境。

  3. cd /d %~dp0
    这行命令将当前工作目录切换到批处理脚本所在的目录。%~dp0 代表当前批处理文件所在的驱动器和路径。/d 参数确保跨驱动器切换目录。

  4. set TOOLS_PATH=tools
    这行命令定义了一个名为 TOOLS_PATH 的环境变量,值为 tools,表示 protoc.exe 和 grpc_csharp_plugin.exe 工具的路径。这个变量用于简化命令中的路径引用。

  5. 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。
  1. pause
    pause 命令用于在脚本结束时暂停执行,等待用户按下任意键。这是为了防止脚本执行完后窗口立即关闭,便于用户查看执行结果。
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 5天前 0

暂无评论

推荐阅读
  2fqsT8Yu1xAt   7天前   37   0   0 .NET
rtv1pxAayTPS