第22篇 生成proto檔案bat指令碼

似梦亦非梦發表於2024-09-13

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 命令用於在指令碼結束時暫停執行,等待使用者按下任意鍵。這是為了防止指令碼執行完後視窗立即關閉,便於使用者檢視執行結果。

相關文章