[Cake] 2. dotnet 全域性工具 cake
1. 簡化cake的安裝
當時dotnet 2.1還未正式釋出,dotnet 還沒有工具的支援,使得安裝cake非常麻煩。不過隨著dotnet tool 的加入,這一問題得到了很好的解決。目前安裝cake(0.30.0版本之後)只需要一行命令即可:
1 dotnet tool install -g cake.tool
然後就可以使用cake了。用dotnet cake 或者 dotnet-cake 都可以,推薦使用前者。
1 $ dotnet cake --help 2 3 Usage: Cake.exe [script] [--verbosity=value] 4[--showdescription] [--dryrun] [..] 5 6 Example: Cake.exe 7 Example: Cake.exe build.cake --verbosity=quiet 8 Example: Cake.exe build.cake --showdescription 9 10 Options: 11--verbosity=valueSpecifies the amount of information to be displayed. 12(Quiet, Minimal, Normal, Verbose, Diagnostic) 13--debugPerforms a debug. 14--showdescriptionShows description about tasks. 15--showtreeShows the task dependency tree. 16--dryrunPerforms a dry run. 17--exclusiveExecute a single task without any dependencies. 18--bootstrapDownload/install modules defined by #module directives 19--versionDisplays version information. 20--infoDisplays additional information about Cake execution. 21--helpDisplays usage information.
2. 簡化cake的引導指令碼
上一篇部落格[Cake] 1. CI中的Cake中出現的cake的引導指令碼build.ps1 和 build.sh ,絕大部分程式碼都是在下載安裝cake用的,既然有了上面的 dotnet tool 命令可以安裝cake,那麼當然也就可以簡化一下了。
引導指令碼中包含安裝和執行命令的程式碼。nuget相關的環境變數是專案需要的,cake指令碼可以讀取這些資訊來使用。
2.1 cake.ps1
1 [string]$SCRIPT= '0-build/build.cake' 2 [string]$CAKE_VERSION = '0.33.0' 3 4 # nuget server config 5 $ENV:NUGET_REPOSITORY_API_URL = "http://nuget-server.test/nuget" 6 $ENV:NUGET_REPOSITORY_API_KEY = "123456" 7 8 # Install cake.tool 9 dotnet tool install --global cake.tool --version $CAKE_VERSION 10 11 # Start Cake 12 [string]$CAKE_ARGS = "-verbosity=diagnostic" 13 14 Write-Host "dotnet cake $SCRIPT $CAKE_ARGS $ARGS" -ForegroundColor GREEN 15 16 dotnet cake $SCRIPT $CAKE_ARGS $ARGS
檢視一下cake指令碼都有哪些task:
1 $ .\cake.ps1 --showtree 2 Tool 'cake.tool' is already installed. 3 dotnet cake 0-build/build.cake -verbosity=diagnostic --showtree 4 5 .... 6 7 default 8 └─test 9└─build 10├─clean 11└─restore 12 13 push 14 └─pack 15└─test 16└─build 17├─clean 18└─restore
2.2 cake.sh
1 #!/bin/sh 2 3 SCRIPT='0-build/build.cake' 4 CAKE_VERSION='0.33.0' 5 6 # nuget server config 7 export NUGET_REPOSITORY_API_URL='http://nuget-server.test/nuget' 8 export NUGET_REPOSITORY_API_KEY='123456' 9 10 # Installcake.tool 11 dotnet tool install --global cake.tool --version $CAKE_VERSION 12 export PATH="$PATH:$HOME/.dotnet/tools" 13 14 # Start Cake 15 CAKE_ARGS="$SCRIPT -verbosity=diagnostic" 16 17 echo "\033[32mdotnet cake $CAKE_ARGS $@" 18 19 dotnet cake $CAKE_ARGS "$@"
3. CI/CD
CI/CD的yaml配置檔案不用做調整,只需執行對 cake.sh 或者cake.ps1 的呼叫即可。這也是cake帶來的避免在CI/CD中程式設計的好處,所有的邏輯都位於cake指令碼中。
參考
dotnet toolhttps://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet-tool-install