1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
1.生成單個Proto.bat內容
@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
@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 命令用於在腳本結束時暫停執行,等待用戶按下任意鍵。這是為了防止腳本執行完後視窗立即關閉,便於用戶查看執行結果。