.NET Core 3.0 本地工具


.NET Core從最早期的版本就開始支持全局工具了。如果僅僅需要在某個項目中或某個文件夾中使用特定的工具,那么.NET Core 3.0就允許您這樣做。 

使用.NET Core 3.0,您可以在特定的文件夾下安裝“本地”工具,它的作用范圍僅限於該文件夾及其子文件夾。 

 

.NET Core 3.0之前 

如果我想在一個項目中使用Cake Build,那么在.NET Core 3.0之前,我可以這樣做,把Cake.Tool安裝為全局的工具: 

dotnet tool install —global Cake. Tool

 

但是如果多個項目需要使用不同版本的 Cake.Tool,那么在.NET Core 3.0之前,我們只能這么做: 

dotnet tool install —tool-path . tools Cake. Tool

 

然后這樣用: 

./. tools/dotnet-cake 
—help

但是這樣也有幾個缺點: 

  • 這個命令比較難記,略長。 

  • 沒有這個項目中所安裝工具的清單。 

  • 如果同一個版本的工具安裝在n個地方,那么就有n個拷貝。 

  • 由於工具文件的后綴名可能不一樣,所以啟動工具的命令可能是和平台有關的。 

 

 

.NET Core 3.0 本地工具

打開Visual StudioPackage Manager Console(Powershell或者其它終端也可以),在安裝本地工具之前,首先要創建一個本地工具清單文件,使用命令dotnet new tool-manifest 

dotnet new tool-manifest 
Getting ready.. 
The template "Dotnet local tool manifest file" 
was created successfully.

 

然后進入項目所在目錄,在.config文件夾下面可以看到名為dotnet-tools.json這個文件: 

{J dotnet-tools.json x 
D: > Projects > dotnet > ConsoleApp1 > config > datnet-toalsjson 
1 
2 
3 
4 
5 
"version" 
"isRoot " : 
"tools" 
true,

 

然后我在這個項目里面安裝Cake.Tool這個工具,使用dotnet tool install xxx即可: 

dotnet tool install Cake . Tool 
You can invoke the tool fr 
Tool ' cake . tool' (version ' 
1 rectory using the following commands: dotnet 
was successfully installed. Entry is added to

 

然后再看dotnet-tools.json文件: 

ojects > dotnet > ConsoleApp1 > config > {b datnet-toalsjson > 
"version" 
"isRoot" : 
true, 
"tools": 
'I cake. tool " : 
" version " 
"0.35.0" 
" commands" : 
" dotnet-cake"

可以看到cake.tool顯示在里面了。 

 

運行本地工具 

如果Cake.Tool是全局安裝的話,那么我直接執行cake --help就應該有結果: 

但是,很顯然這樣做不行。 

 

由於Cake.Tool是本地安裝的,運行本地工具需要在前邊加上dotnet: 

這樣就可以了。 

 

其實上面那種寫法是下面寫法的簡寫: 

 

也可以這樣寫: 

 

此外,在工具安裝目錄的子目錄里也可以使用這個工具。 

 

共享本地工具 

我們可以把.config目錄提交到源碼管理,這樣的話,其它開發者也可以使用安裝的本地工具了。這些工具實際是安裝在全局nuget包緩存里,如果第一次運行的時候沒有安裝工具的包,那么你需要先執行 dotnet tool restore 進行還原,然后才可以使用這些本地工具。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM