首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.NET Core 3.0 本地工具

.NET Core 3.0 本地工具

作者头像
solenovex
发布2019-11-03 14:04:21
5560
发布2019-11-03 14:04:21
举报
文章被收录于专栏:草根专栏草根专栏草根专栏

.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
dotnet tool install —global Cake. Tool

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

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

然后这样用:

./. tools/dotnet-cake 
—help
./. tools/dotnet-cake —help

但是这样也有几个缺点:

  • 这个命令比较难记,略长。
  • 没有这个项目中所安装工具的清单。
  • 如果同一个版本的工具安装在n个地方,那么就有n个拷贝。
  • 由于工具文件的后缀名可能不一样,所以启动工具的命令可能是和平台有关的。

.NET Core 3.0 本地工具

打开Visual Studio的Package Manager Console(Powershell或者其它终端也可以),在安装本地工具之前,首先要创建一个本地工具清单文件,使用命令dotnet new tool-manifest:

dotnet new tool-manifest 
Getting ready.. 
The template "Dotnet local tool manifest file" 
was created successfully.
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,
{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 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"
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 进行还原,然后才可以使用这些本地工具。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在.NET Core 3.0之前
  • .NET Core 3.0 本地工具
  • 运行本地工具
  • 共享本地工具
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档